Go: String handling cheat sheet

String literals

Expression Result Note
"" Empty string, zero value for string
"Japan 日本" Japan 日本 Go code is Unicode text encoded in UTF‑8
"\xe6\x97\xa5" \xNN specifies a byte
"\u65E5" \uNNNN specifies a Unicode value
`\xe6` \xe6 Raw string literal*

In `` string literals, characters are interpreted literally: backslashes have no special meaning and the string may contain newlines.

Basic string handling

Expression Result Note
"Ja" + "pan" Japan Concatenation
"Japan" == "Japan" true Equality
strings.EqualFold("Japan", "JAPAN") true Unicode case-folding
"japan" < "Japan" true Lexical order
len("日") 3 Length in bytes
utf8.RuneCountInString("日") 1 in runes unicode/utf8
utf8.ValidString("日") true UTF-8? unicode/utf8
"Japan"[2] 'p' Byte at postion 2
"Japan"[1:3] ap Byte indexing
"Japan"[:2] Ja
"Japan"[2:] pan

A Go range loop iterates over UTF-8 encoded characters (runes):

for i, ch := range "Japan 日本" {
        fmt.Printf("%d:%q ", i, ch)
// Output: 0:'J' 1:'a' 2:'p' 3:'a' 4:'n' 5:' ' 6:'日' 9:'本'

Iterating over bytes produces nonsense characters for non-ASCII text:

s := "Japan 日本"
for i := 0; i < len(s); i++ {
        fmt.Printf("%q ", s[i])
// Output: 'J' 'a' 'p' 'a' 'n' ' ' 'æ' '\u0097' '¥' 'æ' '\u009c' '¬'


Expression Result Note
strings.Contains("Japan", "abc") false Is abc in Japan?
strings.ContainsAny("Japan", "abc") true Is a, b or c in Japan?
strings.Count("Banana", "ana") 1 Non-overlapping instances of ana
strings.HasPrefix("Japan", "Ja") true Does Japan start with Ja?
strings.HasSuffix("Japan", "pan") true Does Japan end with pan?
strings.Index("Japan", "abc") -1 Index of first abc
strings.IndexAny("Japan", "abc") 1 a, b or c
strings.LastIndex("Japan", "abc") -1 Index of last abc
strings.LastIndexAny("Japan", "abc") 3 a, b or c


Expression Result Note
strings.Replace("foo", "o", ".") f.. Replace “o” with “.”
f := func(r rune) rune {
    return r + 1
strings.Map(f, "ab")
bc Apply function to each character
strings.ToUpper("Japan") JAPAN Uppercase
strings.ToLower("Japan") japan Lowercase
strings.Title("ja pan") Ja Pan Initial letters to uppercase
strings.TrimSpace(" foo\n") foo Strip leading and trailing white space
strings.Trim("foo", "fo") Strip leading and trailing f:s and o:s
strings.TrimLeft("foo", "fo") oo only leading
strings.TrimRight("foo", "fo") f only trailing
strings.TrimPrefix("foo", "fo") o
strings.TrimSuffix("foo", "o") fo

Split and join

Expression Result Note
strings.Fields(" a\t b\n") ["a" "b"] Into substrings, remove white space
strings.Split("a:b", ":") ["a" "b"] remove separator
strings.SplitAfter("a:b", ":") ["a:" "b"] keep separator
arr := []string{"a", "b"}
strings.Join(arr, ":")
a:b Join strings, add separator
strings.Repeat("da", 2) dada 2 copies of “da”


Expression Result Note
strconv.Itoa(-42) "-42" Int to string
strconv.FormatInt(255, 16) "ff" Base 16


The fmt.Sprintf function is often your best friend when converting data to string:

s := fmt.Sprintf("%.4f", math.Pi) // s == "3.1416"

The Print with fmt cheat sheet explains the most common formatting flags.


Be the first to comment!