Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 147 · Variables and Types

Strings, Bytes and Runes

Understand Go strings as immutable UTF-8 bytes - len versus rune count, indexing, ranging over runes, []byte and []rune conversion, raw strings and escapes.

Text is where many languages hide complexity, and Go makes the model refreshingly explicit. A Go string is an immutable sequence of bytes. By convention, and in all string literals you write in source code, those bytes hold text encoded as UTF-8, the encoding that covers every writing system on Earth and is used by the web, by Linux and by modern Windows tools.

That one sentence explains most of what surprises newcomers: why len("héllo") is 6, why indexing a string gives a number and not a letter, why you can't assign to a character, and why Go has a separate type, rune, for a character. This lesson walks through all of it: bytes versus runes, indexing and slicing, range over a string, conversion to []byte and []rune, string literals and escape sequences, comparison and concatenation, and the unicode/utf8 package. The strings package lesson covers the helper functions, and Unicode and text goes deeper later.

A string is a sequence of bytes

In memory, a string is a small header holding a pointer to the bytes and the number of bytes. The built-in len returns that byte count, not the number of characters:

Go len counts bytes

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

For plain English letters, one character is one byte. The accented é takes two bytes in UTF-8, each of the Japanese characters takes three, and the emoji takes four. UTF-8 uses 1 to 4 bytes per character, which keeps ASCII text compact and still handles everything else.

Bytes versus runes

The two views of a string are:

  • bytes: the raw data. A single byte has type byte (an alias of uint8).
  • runes: Unicode code points, roughly "characters". A rune has type rune (an alias of int32) and is written in single quotes.

You can look at the same string both ways with %x (hex bytes) and %U (code points):

Go The same text as bytes and as runes

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

utf8.RuneCountInString counts code points. It walks the string, so it's O(n), whereas len is O(1). Index s[i] returns the i-th byte, never a character: in "Gö!", s[1] and s[2] are the two halves of ö. Slicing a string in the middle of a character produces invalid UTF-8, so only cut at positions you know are boundaries (for example those returned by strings.Index).

A rune is a code point, not necessarily what a reader sees as one character. A letter plus a combining accent, or many emoji (a family, a flag), are several runes shown as one symbol. Most programs never need to care; when yours does, a third-party text segmentation package or golang.org/x/text is the answer.

Ranging over a string

A for ... range loop over a string decodes UTF-8 for you. It gives the byte index where each rune starts and the rune itself:

Go range decodes runes

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Note that the indexes jump: 0, 1, 3, 4. After ñ (two bytes) the next rune starts at 3. If the string contains invalid UTF-8, range yields the replacement character U+FFFD for each bad byte and moves on by one, so it never fails. The range lesson covers loops over other collections.

Strings are immutable

You can read bytes from a string but never change them:

Go Assigning to a string byte (fails on purpose)

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

The compiler says:

TEXT
./main.go:7:2: cannot assign to s[0] (neither addressable nor a map index expression)

To "modify" a string you build a new one. You can reassign the variable (s = "Hello"), concatenate, slice, or convert to a mutable []byte or []rune, change that, and convert back.

Immutability has an upside: slicing a string is cheap, because the new string just points into the same bytes, and a string can be shared freely between goroutines without locking.

Converting to []byte and []rune

Converting copies the data:

Go Strings, []byte and []rune

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Use []byte for binary data and for ASCII-only edits; use []rune when you need to treat each character as a unit, for instance to reverse a Unicode string or to take the first three characters:

Go Reversing and truncating by characters

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Notice i, j = i+1, j-1, the multiple assignment from the variables lesson, used to move both indexes at once.

A rune is not a string

string(r) converts a rune to a string containing that character. It doesn't convert an integer to its digits. That is a classic mistake:

Go string(int) is not strconv.Itoa

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

The go vet tool warns about string(n) when n is an integer that isn't a rune, because it almost never does what the author meant. Write string(rune(n)) when you really want a character, and strconv.Itoa(n) for digits. The type conversion lesson has more.

String literals and escape sequences

There are two kinds of literals:

  • Interpreted strings use double quotes and understand escape sequences.
  • Raw strings use backticks, can span many lines, and contain every character exactly as written (no escapes at all).
Go Escapes and raw strings

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

The common escapes are \n (newline), \t (tab), \\ (backslash), \" (quote), \r (carriage return), \uXXXX and \UXXXXXXXX (Unicode code points) and \xHH (a single byte). Raw strings are ideal for Windows paths, regular expressions, JSON, SQL and HTML snippets. They can't contain a backtick (end the raw string, add an interpreted string holding the backtick with +, and continue) and they drop carriage returns.

Concatenation, comparison and building strings

+ joins strings, == and < compare them byte by byte (lexicographic order, which for UTF-8 matches the order of code points):

Go Concatenating and comparing

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

For a handful of pieces, + is fine. When you assemble thousands of pieces in a loop, strings.Builder avoids copying the growing string each time. "Zebra" < "apple" is true because uppercase letters have smaller byte values than lowercase ones.

The unicode/utf8 package

unicode/utf8 gives you the tools to inspect and decode strings by hand:

Go Working with utf8

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Useful functions are RuneCountInString, ValidString, DecodeRuneInString, DecodeLastRuneInString, RuneLen and AppendRune. The constant utf8.RuneError (U+FFFD) is what decoding returns for invalid data.

Rule of thumb: use len(s) when you mean bytes (sizes, byte offsets, network protocols), use utf8.RuneCountInString(s) when you mean characters, and use range to walk characters. Reach for []rune only when you need random access to characters.

Check your understanding

Quick check · 5 questions

of 5 answered You scored / 5 · perfect!

  1. 1. What does len("naïve") return?

    len counts bytes. The letter ï takes two bytes in UTF-8, so the five characters use six bytes.
  2. 2. What is the type of s[0] when s is a string?

    Indexing a string gives a single byte, whose type is byte, an alias of uint8. It is not a character.
  3. 3. What does for i, r := range "aé" give for the second iteration?

    range decodes runes and reports the byte index where each starts. a is at index 0 and takes one byte, so é starts at index 1 and r is the code point 233.
  4. 4. Which line compiles?

    Strings are immutable, so s[0] = 'b' is a compile error. Converting to a []byte makes a mutable copy. Strings have no methods, and single quotes are for one rune only.
  5. 5. What is the output of fmt.Println(string(rune(66)), strconv.Itoa(66))?

    string(rune(66)) is the character with code point 66, which is B. strconv.Itoa(66) is the text of the number, 66.

Summary

  • A Go string is an immutable sequence of bytes, normally UTF-8. len counts bytes; utf8.RuneCountInString counts runes.
  • Indexing gives a byte; slicing cuts at byte positions; range decodes runes and gives byte indexes.
  • rune (= int32) is a Unicode code point written in single quotes; byte (= uint8) is one byte.
  • Convert with []byte(s) and []rune(s) (both copy) and back with string(...). Use []rune to reverse or cut by characters.
  • string(65) is not "65"; use strconv.Itoa for digits and string(rune(n)) for a character.
  • Interpreted literals use "..." with escapes; raw literals use backticks and keep everything as written. Use strings.Builder to assemble many pieces.

Next: Type Conversion and Inference