Strings, bytes and runes

A Go string is a read-only slice of bytes: a pointer and a length, with no encoding attached. The language does not promise UTF-8, and a string can hold any bytes at all; the standard library and most of the world treat those bytes as UTF-8. Everything confusing about strings follows from that one design choice, and the slices lesson explains the header that is underneath.

A string is a byte sequence

len(s) counts bytes, s[i] is a byte, and a byte is a uint8. Indexing never decodes anything, so it is O(1) and safe for ASCII. A loop that walks the indexes visits every byte, which is what a byte counter, a checksum or a parser of a binary format wants.

package main import "fmt" func main() { s := "hello" fmt.Println("len:", len(s)) fmt.Println("s[1]:", s[1]) fmt.Printf("s[1] as a character: %c\n", s[1]) total := 0 for i := 0; i < len(s); i++ { total += int(s[i]) } fmt.Println("sum of the bytes:", total) }

go run bytes.go

len: 5
s[1]: 101
s[1] as a character: e
sum of the bytes: 532

The character e printed as 101, its ASCII code, because %c was told to read the byte as a character. On a string with non-ASCII text the same loop would print the first byte of a two-byte letter and then the second byte on its own, which is never what a reader wants.

range decodes UTF-8

for i, r := range s decodes the string as UTF-8: the index is the byte offset of the rune and r is a rune, which is an int32. The index jumps by the width of each rune, so it is not a counter. An invalid byte becomes utf8.RuneError and takes one byte, which keeps the loop moving instead of failing.

package main import "fmt" func main() { s := "héllo" fmt.Println("len in bytes:", len(s)) count := 0 for i, r := range s { fmt.Printf("index %d rune %c\n", i, r) count++ } fmt.Println("runes:", count) }

go run runes.go

len in bytes: 6
index 0 rune h
index 1 rune é
index 3 rune l
index 4 rune l
index 5 rune o
runes: 5

Five runes in six bytes: é is two bytes, so the index after it is 2. The length of the string and the number of runes are two different questions, and the next lesson answers the second one with the unicode/utf8 package.

Slicing shares, converting copies

A substring is another header over the same bytes: no allocation and no copy, so it keeps the whole original alive as long as the substring is used. The conversions []byte(s) and string(b) allocate a copy, because a string must stay immutable and a byte slice must stay writable.

package main import "fmt" func main() { s := "hello" t := s[1:4] fmt.Println("slice:", t) b := []byte(s) b[0] = 'J' fmt.Println("original:", s) fmt.Println("changed copy:", string(b)) }

go run slice.go

slice: ell
original: hello
changed copy: Jello

Changing the copy left the original at hello. The compiler refuses the shorter route, a write through the string index, because a string is immutable:

package main import "fmt" func main() { s := "hello" s[0] = 'H' fmt.Println(s) }

go build -o assign assign.go

# command-line-arguments
./assign.go:7:2: cannot assign to s[0] (neither addressable nor a map index expression)

Both halves of the message matter: the element is not addressable and not a map entry. The usual answer is a byte slice when the text is edited and a string when it is read, with one conversion at the boundary between them.

Building a string

s += part in a loop copies the whole string each time, which turns a linear job into a quadratic one. strings.Builder keeps a byte slice and appends into it, and String() hands out the bytes without a copy. It implements io.Writer, so fmt.Fprintf and the encoders write into it directly; Grow reserves room when the size is known in advance.

package main import ( "fmt" "strings" ) func main() { var b strings.Builder b.Grow(16) for i := 0; i < 4; i++ { fmt.Fprintf(&b, "part%d-", i) } out := b.String() fmt.Println(out) fmt.Println("len:", len(out), "builder len:", b.Len()) }

go run builder.go

part0-part1-part2-part3-
len: 24 builder len: 24

The builder is not safe for concurrent use and cannot be copied after the first write, so it belongs inside one function, not in a struct that is passed around. A builder that is read with String() and written again keeps working, but the bytes must not be modified after they are handed out.

Exercises

  1. Write a function that reports the number of bytes, the number of runes and the number of words of a sentence. Which one needs the standard library?
  2. Build a comma-separated line from a slice of strings with strings.Builder and then with strings.Join. What does Join do that a naive loop does not?
  3. Take a ten-byte string, slice one byte out of the middle and print the capacity of []byte(sub). Why is it one and not ten?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does len(s) return for a Go string?

    Answer

    The number of bytes

    A string is a byte sequence, so len counts bytes; the rune count needs utf8.RuneCountInString.

  2. What does this program print? package main import "fmt" func main() { s := "hello" fmt.Println("len:", len(s)) fmt.Println("s[1]:", s[1]) fmt.Printf("s[1] as a character: %c\n", s[1]) total := 0 for i := 0; i < len(s); i++ { total += int(s[i]) } fmt.Println("sum of the bytes:", total) }

    Answer

    len: 5 / s[1]: 101 / s[1] as a character: e / sum of the bytes: 532

    Indexing gives the byte 101, %c prints it as e, and the sum of the five ASCII codes is 532.

  3. What does a range over a string give?

    Answer

    Byte offsets and decoded runes

    range decodes UTF-8: the index is the byte offset and the value is a rune.

  4. What does this program print? package main import "fmt" func main() { s := "héllo" fmt.Println("len in bytes:", len(s)) count := 0 for i, r := range s { fmt.Printf("index %d rune %c\n", i, r) count++ } fmt.Println("runes:", count) }

    Answer

    len in bytes: 6 / index 0 rune h / index 1 rune é / index 3 rune l / index 4 rune l / index 5 rune o / runes: 5

    é takes two bytes at offset 1, so the following letters start at 2, and there are five runes in six bytes.

  5. What does []byte(s) do?

    Answer

    Allocates a copy of the bytes

    A string is immutable and a byte slice is writable, so the conversion copies.

  6. What does this program print? package main import "fmt" func main() { s := "hello" t := s[1:4] fmt.Println("slice:", t) b := []byte(s) b[0] = 'J' fmt.Println("original:", s) fmt.Println("changed copy:", string(b)) }

    Answer

    slice: ell / original: hello / changed copy: Jello

    The substring shares and the byte conversion copies, so only the copy changes.

  7. What does the compiler say about this program? package main import "fmt" func main() { s := "hello" s[0] = 'H' fmt.Println(s) }

    Answer

    cannot assign to s[0] (neither addressable nor a map index expression)

    A string element is not addressable, so it cannot be assigned even though it can be read.

  8. Why is s += part in a loop a bad idea for a long string?

    Answer

    Every step copies the string built so far, so the total work is quadratic

    Strings are immutable: each += builds a new one, and strings.Builder exists to avoid that.

  9. Which type does a rune hold in Go?

    Answer

    int32, one Unicode code point

    A rune is an alias for int32; a byte is uint8, and a string of length one is another thing entirely.

  10. What does strings.Builder implement that lets fmt.Fprintf write into it?

    Answer

    io.Writer

    Builder has Write([]byte) (int, error), so every io.Writer consumer can target it.

The code on this page was run with go 1.26.10 on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev