Golang split string and assign to variables: strings.Split, Fields, and Cut

Golang split string and golang string split with strings.Split and strings.Fields; strings split golang and go split string; golang split string into two variables with strings.Cut or slice indexing; net.SplitHostPort for host and port.

Published

Updated

Read time 3 min read

Reviewed byDeepak Prasad

Golang split string and assign to variables: strings.Split, Fields, and Cut

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]:

go
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)
}
Output

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:

go
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)
	}
}
Output

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:

go
package main

import (
	"fmt"
	"strings"
)

func main() {
	key, val, ok := strings.Cut("key=value", "=")
	fmt.Println(key, val, ok)
}
Output

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.

go
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)
	}
}
Output
text
host: 127.0.0.1 port: 8080 err: <nil>
address ACKSEWW8080: missing port in address

strings.Fields: whitespace without choosing a separator

strings.Fields splits on runs of Unicode space characters and trims leading and trailing emptiness:

go
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)
}
Output

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


Frequently Asked Questions

1. How do I golang split string or use golang strings split?

Import strings and use strings.Split(s, sep) for a separator, or strings.Fields(s) to split on any run of Unicode whitespace. Both return []string you can assign to one variable then index.

2. How do I golang split string into two variables?

For a single delimiter, use before, after, ok := strings.Cut(s, sep) in Go 1.18+, or parts := strings.SplitN(s, sep, 2) and assign parts[0] and parts[1] after checking len(parts) == 2. For host:port addresses, use host, port, err := net.SplitHostPort(addr).

3. What is the difference between strings split golang with Split and Fields?

Split uses an exact separator string (comma, equals, or a space only if you pass a single space). Fields ignores leading and trailing space and splits on any whitespace run, similar to strings.FieldsFunc with unicode.IsSpace.

4. Where can I read more on string split golang patterns?

See the broader string splitting guide linked below and pkg.go.dev/strings for Split, SplitN, Fields, Cut, and related helpers.
Tuan Nguyen

Data Scientist

Proficient in Golang, Python, Java, MongoDB, Selenium, Spring Boot, Kubernetes, Scrapy, API development, Docker, Data Scraping, PrimeFaces, Linux, Data Structures, and Data Mining. With expertise …