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.
var b byte = 'A' // 65 var r rune = 'æ¥' // 26085
Try it
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.
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
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 withutf8.RuneCountInString()orlen([]rune(s)).s[i]is abyte, not a character. Slicings[a:b]cuts at byte offsets and can split a multi-byte character in the middle.rangeover a string yields runes and the byte index; invalid UTF-8 producesU+FFFDwith width 1.- Converting to
[]runeallocates 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
eplus an accent) are two runes. Normalization is ingolang.org/x/text, outside the standard library. - Printing:
%cthe character,%qquoted with escapes,%UasU+0041,%dthe number,%xhex. - Comparisons:
unicode.IsLetter,IsDigitandIsUppertake runes (seeunicode.IsLetter()); thebytesandstringspackages have matching functions for each side. - Arithmetic:
'a' + 1is an untyped rune constant98; add to a variable of typebyteorruneand it keeps that type.