Learn Go← Dashboard

Numbers, booleans, strings, bytes, runes, and type conversion.

Strings, Bytes, and Runes

Go strings are immutable sequences of bytes, almost always interpreted as UTF-8. That last word is important: a Go string isn't a sequence of "characters", it's a sequence of bytes that, by convention, encode Unicode using UTF-8.

Literals

s1 := "hello"           // interpreted string — supports \n, \t, etc.
s2 := `raw string
line two`              // raw string — no escapes, multi-line OK

Raw strings (back-ticks) are perfect for regexes, JSON snippets, or anything where you'd otherwise be escaping backslashes:

re := `\d{3}-\d{4}`     // no need to double-escape

Concatenate with +:

greeting := "hello, " + "world"

For many concatenations in a loop, prefer strings.Builder — repeated + allocates a new string each time (we'll see this in the strings chapter).

Length is in bytes, not characters

len(s) returns the number of bytes, not user-visible characters:

go playground
Loading...

Bytes vs runes

| Term | Meaning | | ---------- | ------------------------------------------------------------------- | | byte | An 8-bit value. Alias for uint8. Strings index as bytes. | | rune | A 32-bit Unicode code point. Alias for int32. |

Single-quote literals like 'A' are runes, not strings.

var b byte = 'A'        // 65
var r rune = '日'       // 26085

Ranging over a string

for ... range s decodes UTF-8 one rune at a time and gives you the byte offset of where each rune starts.

go playground
Loading...

We come back to strings, runes and the strings package in chapter 8.

What does `len("日本語")` return?