Skip to content
Tutorials Center
My learning

Topics

Go Reference · Built-in Types

Go byte and rune types

Reference for Go's byte (uint8) and rune (int32) aliases: character literals, iterating strings, converting between strings, bytes and runes, and UTF-8 pitfalls.

byte is an alias for uint8, and rune is an alias for int32. They are the same types, not new ones: the different names only document intent. Use byte for raw data (file contents, network packets, one element of a string's encoding) and rune for a Unicode code point, the number that identifies a character such as a, é or 日.

Go strings are sequences of bytes that normally hold UTF-8 text. A single character can take one to four bytes, so indexing a string gives bytes, while range over a string decodes runes.

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

Try it

Go Bytes, runes and string indexing

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.

Go Converting and modifying text

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.

Syntax

Go
type byte = uint8
type rune = int32
Form Meaning
'a' Rune literal: an untyped rune constant holding the code point (97).
'\n', '\x41', 'é', '\U0001F600' Escape forms.
[]byte(s) Copy of the string's bytes.
[]rune(s) Copy of the string decoded into code points.
string(bs), string(rs) Build a string from bytes or runes.
string(r) Converts a single rune to its UTF-8 text (for example string(rune(65)) is "A").

Zero value: 0.

Errors / edge cases: converting an integer that is not a valid code point (negative, surrogate halves, above U+10FFFF) with string(r) yields "ᅵ" (the replacement character). go vet warns about string(i) where i is an int that is not a rune or byte, because it gives a character, not decimal digits.

Usage notes

  • len(s) counts bytes. Count runes with utf8.RuneCountInString() or len([]rune(s)).
  • s[i] is a byte, not a character. Slicing s[a:b] cuts at byte offsets and can split a multi-byte character in the middle.
  • range over a string yields runes and the byte index; invalid UTF-8 produces U+FFFD with width 1.
  • Converting to []rune allocates a copy and costs time proportional to the string length. For ASCII-only data, working with bytes is faster.
  • A rune is a code point, not a letter. Characters built from combining marks (for example e plus an accent) are two runes. Normalization is in golang.org/x/text, outside the standard library.
  • Printing: %c the character, %q quoted with escapes, %U as U+0041, %d the number, %x hex.
  • Comparisons: unicode.IsLetter, IsDigit and IsUpper take runes (see unicode.IsLetter()); the bytes and strings packages have matching functions for each side.
  • Arithmetic: 'a' + 1 is an untyped rune constant 98; add to a variable of type byte or rune and it keeps that type.