0Pricing
Go Academy · Lesson

Runes vs Bytes

Understand Go string internals.

What Is a Go String?

In Go a string is an immutable sequence of bytes. The bytes usually hold UTF-8 encoded text, but Go never forces that.

  • You cannot change a string in place.
  • Its len counts bytes, not characters.
package main

import "fmt"

func main() {
    s := "Go"
    fmt.Println(s)
    fmt.Println(len(s))
}

Bytes Behind the Scenes

Indexing a string with s[i] returns a single byte (type byte, an alias for uint8).

For plain ASCII text each character is exactly one byte.

package main

import "fmt"

func main() {
    s := "ABC"
    fmt.Println(s[0])
    fmt.Println(s[1])
    fmt.Println(s[2])
}

All lessons in this course

  1. Runes vs Bytes
  2. strings Package Functions
  3. strings.Builder
  4. Unicode and utf8
← Back to Go Academy