0
0
GoComparisonBeginner · 4 min read

Byte vs Rune in Go: Key Differences and When to Use Each

In Go, byte is an alias for uint8 and represents a single 8-bit value, usually for raw data or ASCII characters. rune is an alias for int32 and represents a Unicode code point, allowing you to work with any character from the Unicode set.
⚖️

Quick Comparison

Here is a quick side-by-side comparison of byte and rune in Go.

Aspectbyterune
Type Aliasuint8int32
Size8 bits (1 byte)32 bits (4 bytes)
RepresentsRaw data or ASCII charactersUnicode code points (characters)
Use CaseBinary data, ASCII textUnicode text, characters beyond ASCII
Range0 to 255All Unicode code points (0 to 0x10FFFF)
Example'a' as byte'世' as rune
⚖️

Key Differences

byte is simply an alias for uint8, meaning it holds an 8-bit unsigned integer. It is mainly used to represent raw data or ASCII characters, which fit within one byte. For example, when working with files or network data, byte slices are common.

On the other hand, rune is an alias for int32 and is designed to hold Unicode code points. Unicode characters can require more than one byte, so rune uses 32 bits to cover all possible characters, including emojis and symbols beyond ASCII.

When you index a string in Go, you get a byte because strings are byte slices. To properly handle Unicode characters, you convert the string to a slice of rune. This distinction is important for correct text processing in multiple languages.

⚖️

Code Comparison

This example shows how to print each byte of a string in Go.

go
package main

import "fmt"

func main() {
    s := "Hello"
    for i := 0; i < len(s); i++ {
        fmt.Printf("byte at index %d: %d ('%c')\n", i, s[i], s[i])
    }
}
Output
byte at index 0: 72 ('H') byte at index 1: 101 ('e') byte at index 2: 108 ('l') byte at index 3: 108 ('l') byte at index 4: 111 ('o')
↔️

Rune Equivalent

This example shows how to print each rune (Unicode character) of a string in Go.

go
package main

import "fmt"

func main() {
    s := "Hello 世"
    for i, r := range s {
        fmt.Printf("rune at index %d: %d ('%c')\n", i, r, r)
    }
}
Output
rune at index 0: 72 ('H') rune at index 1: 101 ('e') rune at index 2: 108 ('l') rune at index 3: 108 ('l') rune at index 4: 111 ('o') rune at index 6: 19990 ('世')
🎯

When to Use Which

Choose byte when you work with raw data, binary files, or ASCII text where each character fits in one byte. It is efficient for low-level data handling and simple English text.

Choose rune when you need to handle Unicode characters, such as international text, emojis, or symbols beyond the ASCII range. Use rune to correctly process and manipulate characters in strings.

Key Takeaways

byte is an 8-bit alias for raw data or ASCII characters.
rune is a 32-bit alias for Unicode code points.
Use byte for binary or ASCII data, rune for Unicode text.
Indexing a string gives byte, use rune to handle full characters.
Choosing the right type ensures correct and efficient text processing.