I was solving the Anagram exercise in Go when a single word stopped me: rune.
I had written a loop to count characters, and Go handed me a rune instead of a byte or a char. I knew it had something to do with Unicode, but I couldn't have told you what a rune really was, or why len() sometimes disagrees with the number of characters I can see.
One small type led to a lot of questions. What is Unicode, and does it assign bits to characters? How does UTF-8 fit in? And why do some screenshots show text as a row of empty boxes?
This post follows how I worked through those questions. Every Go snippet below compiles and runs, and the outputs are real.
The Beginning: A Simple Go Problem
The exercise gives you a subject word and a list of candidates, and asks which candidates are anagrams of the subject:
subject := "listen" candidates := []string{"silent", "enlist", "google"} The approach is to count how often each character appears in the subject, then compare that against each candidate. That means walking through a string one character at a time:
package main import "fmt" func main() { subject := "listen" for _, r := range subject { fmt.Printf("%c\n", r) } } This prints l, i, s, t, e, n, one per line. The variable r is a rune.
Here's a counting function built on it:
func runeCounts(s string) map[rune]int { counts := make(map[rune]int) for _, r := range s { counts[r]++ } return counts } Running it on "listen" and "silent" prints the same map for both:
map[101:1 105:1 108:1 110:1 115:1 116:1] That output surprised me. The keys are numbers, not letters. 101 is e, 105 is i, 108 is l. A rune is just an integer, and it holds the number Unicode assigns to a character. That's where the rabbit hole starts.
What Exactly Is Unicode?
Computers only store numbers. To store text, everyone has to agree on which number means which character. Before Unicode, there were many competing agreements (different encodings for different languages), and text often came out garbled when it moved between systems.
Unicode is the shared agreement. It gives every character a unique number called a code point, written as U+ followed by hex digits:
| Character | Unicode code point |
|---|---|
| A | U+0041 |
| ₹ | U+20B9 |
| 你 | U+4F60 |
The same table covers Latin letters, Tamil, Chinese, mathematical symbols and emoji. The Unicode Consortium maintains it, and new characters are added through a proposal and review process, with a new version released about once a year.
The point I got wrong at first: a code point is just a number. It says nothing about bits or bytes. Unicode doesn't give every character a fixed number of bits. How that number is stored in memory or in a file is a separate decision, made by an encoding.
Unicode vs UTF-8: What's the Difference?
This was the part that made it click for me.
- Unicode defines the code points: which number means which character.
- UTF-8 is one way to turn those numbers into bytes.
There are other encodings. UTF-32 uses 4 bytes for every code point, and UTF-16 uses 2 or 4. UTF-8 uses a variable number of bytes, from 1 to 4:
| Code point range | Bytes | Byte pattern |
|---|---|---|
| U+0000 to U+007F | 1 | 0xxxxxxx |
| U+0080 to U+07FF | 2 | 110xxxxx 10xxxxxx |
| U+0800 to U+FFFF | 3 | 1110xxxx 10xxxxxx 10xxxxxx |
| U+10000 to U+10FFFF | 4 | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx |
The x bits are filled with the binary digits of the code point. Here's ₹ (U+20B9) going through that:
U+20B9 -> 0010 0000 1011 1001 (the code point in binary) 3-byte pattern: 1110xxxx 10xxxxxx 10xxxxxx filled in: 11100010 10000010 10111001 in hex: E2 82 B9 So ₹ becomes the three bytes E2 82 B9. I confirmed this in Go:
for _, r := range "A₹你" { fmt.Printf("%c %U bytes: % x\n", r, r, []byte(string(r))) } A U+0041 bytes: 41 ₹ U+20B9 bytes: e2 82 b9 你 U+4F60 bytes: e4 bd a0 Notice that A is the single byte 41. UTF-8 was designed so that the first 128 code points encode exactly like ASCII. Any file that's pure ASCII is already valid UTF-8, which is a big reason UTF-8 took over.
Strings, Bytes and Runes in Go
Now back to Go. A Go string is an immutable sequence of bytes. By convention, and because Go source files are UTF-8, those bytes usually hold UTF-8 text. Two aliases matter here:
-
byteis an alias foruint8 -
runeis an alias forint32, and it represents a Unicode code point
Here's the example that cleared up len() for me:
package main import ( "fmt" "unicode/utf8" ) func main() { s := "A₹你" fmt.Println("String:", s) fmt.Println("Bytes:", len(s)) fmt.Println("Runes:", utf8.RuneCountInString(s)) for i, r := range s { fmt.Printf("Index: %d, Character: %c, Code point: %U\n", i, r, r) } } String: A₹你 Bytes: 7 Runes: 3 Index: 0, Character: A, Code point: U+0041 Index: 1, Character: ₹, Code point: U+20B9 Index: 4, Character: 你, Code point: U+4F60 Three visible characters, but len(s) is 7. That's 1 byte for A, 3 for ₹ and 3 for 你. len counts bytes. utf8.RuneCountInString decodes the bytes and counts code points, which gives 3.
The range loop decodes as it goes, so r is a rune. And look at the index: 0, 1, 4. It's the byte offset where each rune starts, not the rune number. ₹ starts at byte 1, and 你 starts at byte 4 because ₹ took three bytes.
Indexing with s[1] doesn't give you a character either:
fmt.Println(s[0], s[1], s[2]) // 65 226 130 s[1] is 226 (0xE2), the first byte of ₹, which isn't a character on its own. When your text may contain more than ASCII, that's the reason to use range or []rune(s), not byte indexing.
A rune is still not a "character"
Even a rune isn't always what a person sees as one character. Take two ways of writing é:
a := "\u00e9" // é as one code point b := "e\u0301" // e followed by a combining acute accent fmt.Println(a, b) // é é fmt.Println(a == b) // false fmt.Println(len(a), utf8.RuneCountInString(a)) // 2 1 fmt.Println(len(b), utf8.RuneCountInString(b)) // 3 2 They look identical on screen, but Go says they're different strings, with different byte and rune counts. What the reader perceives as one character is called a grapheme cluster, and it can be made of several code points.
Emoji make this obvious. The family emoji 👨👩👧 is 5 code points (three people joined by two invisible U+200D joiners) and takes 18 bytes. The flag 🇮🇳 is 2 code points and 8 bytes. Counting runes won't give you the number of things a user sees. Go's standard library doesn't segment grapheme clusters, so you'd reach for a third-party package if you need that.
For the anagram exercise, counting runes is the right granularity. For something like truncating a username to 10 characters, it might not be.
5. What Happens When Unicode Doesn't Have a Code Point?
My next question was what happens to a symbol that Unicode doesn't know about. Say someone invents a new symbol today.
Code points fall into a few groups:
- Assigned: the number has a character, like
U+0041forA. - Unassigned: the number is within the valid range, but no character has been given to it yet. UTF-8 can still encode it as bytes. It just doesn't mean anything yet.
- Private use: ranges (such as
U+E000toU+F8FF) that Unicode promises never to assign. Two parties can agree on their own meaning for them, usually with a custom font. - Surrogates (
U+D800toU+DFFF): reserved for UTF-16, and not allowed in UTF-8.
So a brand new symbol has no identity in Unicode. Nothing can store it as standard text, because there's no number to store, and another application has no way to know what it should look like. The ways forward are to get it added to Unicode (a proposal process that takes time and is reviewed before a new version), to use a private-use code point with a matching font, or to send it as an image.
This also explains why Unicode versions matter. A character added in a recent version is perfectly valid, but a device that hasn't updated yet may not know it.
The replacement character
There's one special code point to know: U+FFFD (�), the replacement character. Decoders use it when they hit bytes that can't be decoded as valid UTF-8. It's different from an unassigned code point: unassigned means the bytes are valid but the character doesn't exist yet, while U+FFFD shows up because the bytes themselves were invalid.
Why Do Characters Sometimes Appear as Empty Boxes?
I came across a screenshot where some text appeared as a row of empty boxes. Nothing was wrong with the text itself. This is a rendering problem, and it's often called tofu, after the blocky look of the empty boxes. (Google's Noto font family is named for "no tofu".)
The key distinction is between a code point and a glyph. A code point is the identity of a character. A glyph is the drawing of it, and glyphs come from fonts. A font is a collection of drawings for the characters it supports, and no font supports all of Unicode.
When a program needs to draw a character and the current font has no glyph for it:
- Most systems try fallback fonts, looking for another installed font that does have it.
- If none do, the program draws a placeholder, usually an empty box.
So a perfectly valid UTF-8 string can show up as boxes. That means the font has no drawing for it. It does not mean the character has no code point. The two cases to keep apart:
| Situation | What happens |
|---|---|
| Missing font glyph | Empty box (tofu) |
| Invalid UTF-8 | May decode to U+FFFD |
| Valid character with supported font | Character displays normally |
This is the difference between encoding (turning numbers into bytes and back) and rendering (turning code points into shapes on a screen). Boxes are almost always a rendering problem, and � is an encoding problem.
Experimenting With Invalid UTF-8 in Go
Go strings can hold any bytes, so I wanted to see what happens with bytes that aren't valid UTF-8:
package main import ( "fmt" "unicode/utf8" ) func main() { s := string([]byte{0xff, 0xfe}) fmt.Println("Valid UTF-8:", utf8.ValidString(s)) for _, r := range s { fmt.Printf("%U\n", r) } } Valid UTF-8: false U+FFFD U+FFFD The bytes 0xff and 0xfe never appear in valid UTF-8. utf8.ValidString reports that, and when range runs into each invalid byte, it yields U+FFFD and moves forward by a single byte, so two bad bytes give two replacement characters.
One thing worth being clear about: Go doesn't fix the string. The original two bytes are still in s. Only the decoding step shows U+FFFD. If you need to know whether input is valid, check it with utf8.ValidString.
Putting Everything Together
| Concept | Responsibility |
|---|---|
| Unicode | Defines code points |
| UTF-8 | Encodes code points into bytes |
| Go string | Stores a sequence of bytes |
| Go rune | Represents a Unicode code point |
| Font | Provides glyphs |
| Rendering engine | Displays glyphs on screen |
And the full journey of a character looks like this:
character identity -> Unicode code point -> UTF-8 bytes -> string in memory -> decoding -> glyph selection (font) -> rendering Each arrow is a different layer with its own failure mode. Wrong bytes give you �. A missing glyph gives you a box. Counting bytes when you meant code points gives you a wrong len. Knowing which layer you're in tells you where to look.
Final Thoughts
I started with a loop over "listen" and ended up learning about code points, variable-length encoding, fonts and fallback. None of it was hidden, but I'd been using strings for years without needing to know any of it.
The best way to make it stick is to try it yourself. Take a string with a few non-ASCII characters in your language of choice, print its bytes, its length and its code points, and then break it on purpose with invalid bytes. The surprises are where you learn the most.

