Skip to main content

How to Split a String in Go with strings.Split

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

strings.Split cuts a string into pieces wherever a separator appears and returns the pieces as a slice of strings. strings.Split("2026-11-28", "-") gives the year, the month and the day.

Syntax​

Syntax
<pieces> := strings.Split(<text>, <separator>)
<pieces> := strings.SplitN(<text>, <separator>, <count>)
PartWhat it does
<text>The string to cut.
<separator>The text to cut at. It doesn't appear in the pieces.
<count>For SplitN: the most pieces to return; the last one keeps the rest. -1 means all of them.
<pieces>A []string with the pieces, in order.

The Go documentation for strings.Split: "Split slices s into all substrings separated by sep and returns a slice of the substrings between those separators." It adds: "If s does not contain sep and sep is not empty, Split returns a slice of length 1 whose only element is s."

The Example​

The example splits a date at its dashes and counts the pieces, splits it again into at most two pieces with SplitN, splits a list with an empty spot in it, and splits the date at a separator it doesn't contain. %q prints each piece in quotes, so empty ones are easy to see.

Prerequisites​

  • Go. go run compiles and runs a single file. This guide uses Go 1.27.4.

Step 1: Write the Code​

Create a file named main.go:

main.go, which prints strings.Split of 2026-11-28 at the dash, the number of pieces, strings.SplitN with a count of 2, a,b,,c split at the comma with %q, and the date split at a slash with %q.

main.go
package main

import (
"fmt"
"strings"
)

func main() {
date := "2026-11-28"

fmt.Println(strings.Split(date, "-"))
fmt.Println(len(strings.Split(date, "-")))
fmt.Println(strings.SplitN(date, "-", 2))
fmt.Printf("%q\n", strings.Split("a,b,,c", ","))
fmt.Printf("%q\n", strings.Split(date, "/"))
}

Step 2: Run It​

Terminal
go run main.go
Output
[2026 11 28]
3
[2026 11-28]
["a" "b" "" "c"]
["2026-11-28"]

A zsh terminal where go run main.go prints [2026 11 28], 3, [2026 11-28], the quoted pieces a, b, an empty string and c, and the whole date as one quoted piece.

The command is the same in zsh, bash and PowerShell.

What Happened​

  • Split cut the date at both dashes into three pieces, and len counted them.
  • SplitN with a count of 2 made one cut and kept 11-28 together in the last piece.
  • The two commas side by side left an empty string, "", between them.
  • The date has no /, so Split returned one piece: the whole string.

Common Mistakes​

MistakeWhat happensDo this instead
Expecting empty pieces to disappear"a,b,,c" gives an empty "" between the commasSkip empty pieces in a loop, or use strings.Fields for whitespace
Reading parts[1] without checking the lengthWhen the separator is missing, there's only parts[0], so parts[1] panicsCheck len(parts) first
Passing an empty separator by accidentSplit("abc", "") splits after every character: ["a" "b" "c"]Pass the separator you mean

Troubleshooting​

SymptomLikely causeFix
panic: runtime error: index out of range [1] with length 1 after a splitThe separator wasn't foundCheck len(parts)
undefined: stringsThe strings package isn't importedAdd "strings" to the imports

FAQ​

How do I split on any amount of whitespace?​

Use strings.Fields(text), which splits around runs of spaces, tabs and line breaks and never returns empty pieces.

Does Split change the original string?​

No. Strings in Go can't change; Split returns a new slice.

Conclusion​

strings.Split(text, sep) returns the pieces between the separators as a []string. Use strings.SplitN to stop after a number of pieces, and check the length before you index the result.

Additional Resources​