ProgramGuru

Bytes and Runes

Start with Bytes and Runes

A byte is an alias for uint8, while a rune is an alias for int32 and conventionally represents a Unicode code point. Both are important when handling strings.

What you’ll learn

  • Explain the main purpose of bytes and runes.
  • Read and run the complete Go example in this lesson.
  • Apply the concept and recognize common mistakes.

Understand the idea

Converting a string to []byte exposes its encoded bytes. Converting it to []rune decodes the string into Unicode code points. For non-ASCII text, these slices generally contain different numbers of elements.

A character written in single quotes is a rune literal, such as 'A' or 'é'. A rune can hold one code point, but not every user-perceived character is a single rune. Conversion to a slice lets you modify or inspect elements before building a new string.

Visualize how it works

Follow this diagram before executing the example. Check where a value or decision changes what runs next.

Visual explanation for bytes runesUTF-8 uses variable-width bytes to encode runes.Text "café"Step 1Encode: 5 bytesStep 2Decode: 4 runesStep 3Change rune, new textStep 4UTF-8 uses variable-width bytes to encode runes.
UTF-8 uses variable-width bytes to encode runes.

Run a complete Go example

This example is a complete program. Save it as main.go or paste it into the Go Playground.

Go code

package main

import "fmt"

func main() {
	word := "café"
	bytes := []byte(word)
	runes := []rune(word)
	fmt.Println("Byte count:", len(bytes))
	fmt.Println("Rune count:", len(runes))
	fmt.Printf("Last rune: %c\n", runes[len(runes)-1])
	runes[0] = 'C'
	fmt.Println("Changed:", string(runes))
}

Output

Byte count: 5
Rune count: 4
Last rune: é
Changed: Café

Trace what the program does

The word café uses five encoded bytes but four runes. The last rune is é, not one of its individual UTF-8 bytes. Replacing the first rune with C produces a new string Café, leaving the original word unchanged.

Avoid common mistakes

  • A byte indexes encoded data; a rune represents a decoded code point.
  • A rune is not guaranteed to match a complete visual grapheme cluster.

Try it yourself

Convert "Go😀" into []byte and []rune. Print their lengths and identify the emoji rune.

Tip: Edit the example above, predict the output, and run the modified code to test your reasoning.

Check your understanding

Which conversion decodes UTF-8 text into code points?
Choose one answer. You can try again.

Answer and explanation

[]rune(text)

Correct. The word café uses five encoded bytes but four runes.

What you should remember

You have practiced Bytes and Runes in a small, self-contained program. The important habit is to predict what runs, inspect the actual output and distinguish the language rule from the particular example.

Before continuing, make sure you can explain the program to someone who has not seen it, including the line that changes its result.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Unicode and UTF-8 in Go →