People search for golang split string, golang string split, strings split golang, golang split, split string golang, go split string, string split golang, golang strings split, go string split, and golang split string into two variables when they need pieces of text in variables. In Go the usual answer is the strings package: assign the []string return value to a slice, index elements, or use helpers like strings.Cut (Go 1.18+) and net.SplitHostPort for host:port. For more patterns (limits, separators, SplitAfter), see split a string in Go.
Tested with Go 1.24 on Linux.
strings.Split: assign the whole slice
strings.Split returns a slice of substrings around each non-overlapping instance of sep. Assign that slice to a variable, then read result[i]:
package main
import (
"fmt"
"strings"
)
func main() {
result := strings.Split("this is a split string example", " ")
fmt.Println(len(result))
fmt.Println("first element:", result[0])
fmt.Printf("type of result: %T\n", result)
}Running it prints 6 (six words), first element: this, and type of result: []string.
To take the first two fields only when you know there are at least two, check length before indexing:
package main
import (
"fmt"
"strings"
)
func main() {
parts := strings.Split("a,b,c", ",")
if len(parts) >= 2 {
first, second := parts[0], parts[1]
fmt.Println(first, second)
}
}strings.SplitN limits how many segments you get, which is handy when the remainder should stay intact.
golang split string into two variables
strings.Cut (Go 1.18 and later)
For exactly one separator and two sides, strings.Cut returns before, after, and found without building a slice:
package main
import (
"fmt"
"strings"
)
func main() {
key, val, ok := strings.Cut("key=value", "=")
fmt.Println(key, val, ok)
}That prints key value true.
net.SplitHostPort for host:port
net.SplitHostPort returns host, port, and err; IPv6 literals must use brackets, for example [::1]:80.
package main
import (
"fmt"
"net"
)
func main() {
host, port, err := net.SplitHostPort("127.0.0.1:8080")
if err == nil {
fmt.Println("host:", host, "port:", port, "err:", err)
}
if _, _, err2 := net.SplitHostPort("ACKSEWW8080"); err2 != nil {
fmt.Println(err2)
}
}host: 127.0.0.1 port: 8080 err: <nil>
address ACKSEWW8080: missing port in addressstrings.Fields: whitespace without choosing a separator
strings.Fields splits on runs of Unicode space characters and trims leading and trailing emptiness:
package main
import (
"fmt"
"strings"
)
func main() {
result := strings.Fields(" \t \n this is \t split example \n \t ")
fmt.Println(len(result))
fmt.Println("first element:", result[0])
fmt.Printf("type of result: %T\n", result)
}Running it prints 4, first element: this, and []string for the type line.
Summary
Golang split workflows almost always store []string from strings.Split or strings.Fields, then assign result[0], result[1], or loop. For golang split string into two variables with a single delimiter, prefer strings.Cut when you are on Go 1.18+, or SplitN / length-checked Split. net.SplitHostPort covers host and port as separate named return values with an error to handle.
References
strings.Splitdocumentationstrings.Cutdocumentationstrings.Fieldsdocumentationnet.SplitHostPortdocumentation- Stack Overflow: split a string and assign to variables

