A byte is an alias for uint8 and holds one byte of data, while a rune is an alias for int32 and holds one Unicode code point. A Go string is an immutable sequence of bytes, usually UTF-8 encoded, so len(s) counts bytes, not characters. Convert to []byte when you work with raw data and to []rune when you need to index or modify text by code point.
The difference only shows up with non-ASCII text. For plain ASCII, one byte equals one rune. For text such as héllo, 世界 or emoji, one character can take two, three or four bytes.
A string value is a read-only slice of bytes. Go source files are UTF-8, so a string literal holds the UTF-8 encoding of its text. The language does not guarantee that a string is valid UTF-8, but literals and most text from the standard library are.
The letter é takes two bytes (c3 a9) and each of 世 and 界 takes three. That is why the length is 15 bytes for 9 characters.
| Type | Alias for | Size | Represents |
|---|---|---|---|
byte | uint8 | 8 bits | One byte of a string or binary buffer |
rune | int32 | 32 bits | One Unicode code point |
A rune literal such as '世' is an untyped rune constant. UTF-8 encodes a code point in one to four bytes, so a []rune is always four bytes per element, while a []byte copy of the same text is usually smaller.
A rune is a code point, not always what a user sees as one character. A letter with a combining accent, a flag emoji or a family emoji can be several runes. For user-perceived characters (grapheme clusters) you need a library such as golang.org/x/text, or a Unicode segmentation package.
Indexing a string with s[i] returns a byte. A for ... range loop over a string decodes UTF-8 and yields the byte offset and the rune at that offset.
The index jumps from 2 to 5 because 世 uses three bytes. If the string contains invalid UTF-8, range yields utf8.RuneError (U+FFFD) for each bad byte and advances by one byte.
Converting a string to []byte copies the raw bytes. Converting to []rune decodes the string and allocates four bytes per code point. Both conversions copy, because strings are immutable.
Use []byte for I/O, hashing, network protocols and changes that only touch ASCII. Use []rune when you must replace, reverse or index characters that may be multi-byte. Reversing a string byte by byte corrupts any multi-byte character, so reverse a []rune instead (and remember that combining marks still need grapheme-aware handling).
Do not convert in hot loops just to count characters. The unicode/utf8 package avoids the allocation:
| Task | Use |
|---|---|
| Reading files, sockets, HTTP bodies | []byte or io.Reader |
| Hashing, encoding, compression | []byte |
| Building large strings | strings.Builder or bytes.Buffer |
| Counting characters | utf8.RuneCountInString |
| Per-character logic such as case checks | range over the string, with unicode functions |
| Modifying characters by position | []rune |
Building strings with strings.Builder has WriteByte for single bytes and WriteRune for code points, and WriteRune encodes the rune as UTF-8 for you.
len(s) as a character count. It is a byte count.s[:n] for truncation. This can cut a multi-byte character in half and produce invalid UTF-8. Truncate a rune slice or find a boundary with utf8.DecodeLastRuneInString.s[i] with a rune literal outside ASCII. s[i] == '世' does not compile because the constant overflows byte.Not exactly. A rune is one Unicode code point. Many visible characters, such as an e plus a combining accent, are made of several runes.
len returns the number of bytes in the UTF-8 encoding. Characters outside ASCII use two to four bytes each.
It decodes the whole string and allocates four bytes per code point, so it is O(n) in time and memory. For counting only, use utf8.RuneCountInString.
No, strings are immutable. Convert to []byte or []rune, change the slice, then convert back to a string.
A byte is one uint8 of data and a rune is one Unicode code point stored as an int32. Strings are UTF-8 bytes, so indexing gives bytes while range gives runes. Use bytes for raw data and I/O, use runes (or range and unicode/utf8) for character-level logic, and avoid treating len or slicing as character operations.