These are the notes for Go episode 17. Almost every program works with text: names, messages, orders. Today you find out what a Go string really is inside, and why counting its characters is trickier than it looks. You will learn UTF-8 encoding, code points, byte vs rune, why len counts bytes, why indexing returns a byte, how range decodes runes, utf8.RuneCountInString, immutable strings, []byte and []rune conversions, escape sequences and raw string literals, the strings package (TrimSpace, Split, Join, Contains, Fields, Replace), strings.Builder, the bytes package and the unicode package. Every example runs as-is in the SyntaxPilot compiler (language: Go); outputs and errors are from real runs (Go 1.24).
The problem: Raju's SMS counter is wrong
Raju sends offers to his chai customers by SMS. His Go program shows how many characters are left out of 160, using len, the built-in function that gives a length. Many customers prefer Hindi, so a message starts with the Hindi word namaste (नमस्ते), which has 6 characters.
package main
import "fmt"
func main() {
sms := "Raju Chai"
sign := "Café"
hello := "नमस्ते"
fmt.Println(len(sms), len(sign), len(hello))
fmt.Println("characters left:", 160-len(hello))
}
9 5 18
characters left: 142
"Raju Chai"→ 9. Correct."Café"→ 5, not 4."नमस्ते"→ 18, not 6. Only 142 characters "left".
len is not counting characters. len(s) counts bytes.
Why: a string is UTF-8 bytes
| A train | In Go | Meaning |
|---|---|---|
| One train, a row of coaches | a string | a row of bytes |
| A group needs 1 to 4 coaches | UTF-8 | 1 to 4 bytes per character |
| Each group's ticket number | a code point | é is U+00E9 = 233 |
| Counting coaches, not passengers | len(s) |
bytes, not characters |
- A byte is a small number from 0 to 255 (8 bits, episode 4). Inside, a Go string is a read-only row of bytes.
- Computers store characters as numbers. Unicode gives every character in every script its own number, called its code point. It is usually written U+ followed by the number in hexadecimal (base 16):
Ais U+0041 (65),éis U+00E9 (233),नis U+0928 (2344),₹is U+20B9 (8377). - An encoding is the rule that turns code points into bytes. Go source code and Go string literals use UTF-8, the most common encoding on the web.
- In UTF-8, a character takes 1 to 4 bytes:
| Characters | Code points | Bytes in UTF-8 | Example |
|---|---|---|---|
| ASCII: English letters, digits, common symbols | 0 to 127 | 1 | A → 65 |
| Accented Latin letters, Greek, Arabic... | 128 to 2047 | 2 | é → 195 169 |
Most other scripts: Hindi, Chinese, ₹... |
2048 to 65535 | 3 | ₹ → 226 130 185 |
| Emoji and rare characters | above 65535 | 4 | 😀 |
- ASCII is the original set of 128 characters (English letters, digits, punctuation). UTF-8 was designed so every ASCII character is exactly its own single byte, which is why plain English strings have
len= number of characters. - So
"Café"= 3 one-byte letters + a 2-byteé= 5 bytes, and"नमस्ते"= 6 code points × 3 bytes = 18 bytes.
Indexing gives a byte, not a letter
An index is a position, counted from 0. s[i] gives the byte at byte position i:
package main
import "fmt"
func main() {
sign := "Café"
fmt.Println(sign[0], sign[3])
fmt.Printf("%c %c\n", sign[0], sign[3])
for i := 0; i < len(sign); i++ {
fmt.Print(sign[i], " ")
}
fmt.Println()
var b byte = sign[0]
var r rune = 'é'
fmt.Printf("%T %T %v %v\n", b, r, b, r)
}
67 195
C Ã
67 97 102 195 169
uint8 int32 67 233
sign[0]is67, the byte forC.sign[3]is195: only the first half ofé.%cis the Printf verb (a placeholder after%) that prints a number as its character. Byte 195 alone is printed asÃ, a wrong letter.- The loop up to
len(sign)visits 5 bytes:éis the two bytes195 169. %Tprints a value's type,%vits value in the default format.
byte vs rune
| Type | Is another name for | Holds | Example |
|---|---|---|---|
byte |
uint8 |
one byte, 0 to 255 | sign[0] → 67 |
rune |
int32 |
one Unicode code point: one whole character | 'é' → 233 |
- An alias is a second name for the same type:
byteisuint8, andruneisint32. - A rune literal is one character in single quotes:
'R','é','₹'. Double quotes make a string. - A rune can hold any code point; a byte cannot hold
'₹':
var b byte = '₹'
./main.go:6:18: cannot use '₹' (untyped rune constant 8377) as byte value in variable declaration (overflows)
- Comparing a byte with a one-letter string is a type error. Compare with a rune literal instead (
sign[0] == 'C'):
if sign[0] == "C" {
./main.go:7:19: invalid operation: sign[0] == "C" (mismatched types byte and untyped string)
- Indexing past the end panics:
"Raju"[10]→panic: runtime error: index out of range [10] with length 4.
range decodes runes
To walk through the characters, use range on the string. Each step gives the byte index where the character starts, and the character as a rune:
package main
import "fmt"
func main() {
for i, r := range "Café" {
fmt.Println(i, r, string(r))
}
for i, r := range "नमस्ते" {
fmt.Printf("%d:%U ", i, r)
}
fmt.Println()
}
0 67 C
1 97 a
2 102 f
3 233 é
0:U+0928 3:U+092E 6:U+0938 9:U+094D 12:U+0924 15:U+0947
rangedecodes UTF-8: it reads the 1 to 4 bytes of each character and hands you one whole rune.éarrives whole, as 233.- The index is a byte position, so for the Hindi word it jumps 0, 3, 6, 9, 12, 15: three bytes per character.
%Uprints a rune as a code point inU+form.string(r)converts one rune to a string of that character.- The Hindi word is 6 code points, but your eye may see fewer letters: some vowel signs and the halant (U+094D) are separate code points that join the letter before them. Code points are what SMS and most systems count.
- If the bytes are not valid UTF-8,
rangegivesutf8.RuneError(U+FFFD, 65533) for each bad byte and moves on by 1:
package main
import (
"fmt"
"unicode/utf8"
)
func main() {
bad := string([]byte{0xff, 0x41})
fmt.Println(utf8.ValidString(bad), utf8.ValidString("Café"))
for i, r := range bad {
fmt.Println(i, r, r == utf8.RuneError)
}
}
false true
0 65533 true
1 65 false
Counting characters: utf8.RuneCountInString
The unicode/utf8 package works with UTF-8 bytes directly:
package main
import (
"fmt"
"unicode/utf8"
)
func main() {
sign := "Café"
hello := "नमस्ते"
fmt.Println(len(sign), utf8.RuneCountInString(sign))
fmt.Println(len(hello), utf8.RuneCountInString(hello))
fmt.Println(utf8.RuneLen('a'), utf8.RuneLen('é'))
}
5 4
18 6
1 2
| Function | What it does |
|---|---|
utf8.RuneCountInString(s) |
number of runes (code points) in a string |
utf8.RuneLen(r) |
how many bytes rune r needs in UTF-8 |
utf8.ValidString(s) |
is s valid UTF-8? |
utf8.RuneError |
the rune U+FFFD, used for bad bytes |
len([]rune(s)) gives the same count as RuneCountInString, but it builds a whole new slice first, so RuneCountInString is the better choice.
Strings are immutable
The sign painter wrote Baju Chai instead of Raju Chai. Change the first byte?
package main
import "fmt"
func main() {
sign := "Baju Chai"
sign[0] = 'R'
fmt.Println(sign)
}
# command-line-arguments
./main.go:7:5: cannot assign to sign[0] (neither addressable nor a map index expression)
In plain words: sign[0] is not a place you are allowed to write to. Go strings are immutable: once a string is made, its bytes never change. Think of a painted signboard: you can't rub out one letter.
What looks like changing a string actually makes a new string:
package main
import "fmt"
func main() {
s := "Café"
fmt.Println(s[3:])
fmt.Println(s[:4])
fmt.Println(s[:3])
a := "chai"
b := a
a += "!"
fmt.Println(a, b)
}
é
Caf�
Caf
chai! chai
a += "!"builds a new string"chai!"and pointsaat it.bstill sees the old"chai".- Slicing a string with
s[i:j](byte positions, like slices) gives a substring without copying.s[3:]isé, buts[:4]cutséin half and prints a broken symbol (the terminal shows the replacement character�for the lone byte\xc3). Slice only at positions you got fromrangeor from functions likestrings.Index. - Because strings never change, Go can share their bytes safely between variables.
Editing text: []byte and []rune conversions
| Painted signboard | In Go |
|---|---|
| Copy the letters onto a whiteboard | b := []byte(sign) or r := []rune(sign): an editable copy |
| Fix one letter on the whiteboard | b[0] = 'R' |
| Paint a new board from the whiteboard | string(b): a new string |
package main
import "fmt"
func main() {
sign := "Baju Chai"
b := []byte(sign)
b[0] = 'R'
fixed := string(b)
fmt.Println(sign, "->", fixed)
name := "Cafe Raju"
r := []rune(name)
r[3] = 'é'
fmt.Println(string(r), len(r), len(string(r)))
}
Baju Chai -> Raju Chai
Café Raju 9 10
[]byte(s)copies the string's bytes into a new byte slice you can change. Fine when you change single-byte (ASCII) characters.[]rune(s)decodes the string into a slice of runes, one per character. Use it when characters may need more than one byte:r[3] = 'é'puts in one whole character.string(b)/string(r)converts back, making a new string. Above: 9 runes, but 10 bytes as a string.- Both conversions copy, so the original
signis untouched.
More conversions (real outputs):
string(rune(2344))→न, the character with that code point;string(rune(65))→A. Writerune(...)explicitly:string(65)on a plain int is flagged bygo vet(episode 4).[]byte("₹")→[226 130 185], its 3 UTF-8 bytes;[]rune("₹")→[8377], its single code point.
Reversing a string: runes, not bytes
package main
import (
"fmt"
"slices"
)
func main() {
s := "Café"
b := []byte(s)
slices.Reverse(b)
fmt.Println(string(b))
r := []rune(s)
slices.Reverse(r)
fmt.Println(string(r))
}
��faC
éfaC
slices.Reverse(Go 1.21) reverses a slice in place.- Reversing bytes swapped the two bytes of
éinto the order169 195, which is not valid UTF-8, so the terminal shows two broken symbols (the raw output bytes area9 c3 66 61 43). - Reversing runes keeps every character whole:
éfaC.
Escape sequences
A string in double quotes is an interpreted string literal: a backslash \ starts an escape sequence, a backslash plus a letter that stands for a special character.
| Escape | Means |
|---|---|
\n |
new line |
\t |
tab |
\" |
a double quote inside a string |
\\ |
one backslash |
\' |
a single quote (inside a rune literal) |
\u20b9 |
the character with code point U+20B9 (₹); \U takes 8 hex digits |
\x41 |
one byte given in hex (here A) |
package main
import "fmt"
func main() {
fmt.Println("Raju said \"2 chai\"\tRs 40")
fmt.Println("Line 1\nLine 2 \\ end")
fmt.Println("\u20b9 20")
path := "C:\new\table.txt"
fmt.Println(path)
}
Raju said "2 chai" Rs 40
Line 1
Line 2 \ end
₹ 20
C:
ew able.txt
The first three lines work. But the Windows path broke: \n in \new became a newline and \t in \table became a tab. No error, just wrong text.
An unknown escape is a compile error. \d (a digit, in regular expressions, the search patterns you will meet later) is not a Go escape:
package main
import "fmt"
func main() {
phone := "\d{10}"
fmt.Println(phone)
}
# command-line-arguments
./main.go:6:16: unknown escape
Raw string literals
A raw string literal is text between backticks ` instead of double quotes. Inside it, nothing is escaped: a backslash is just a backslash, and the text can span several lines.
package main
import "fmt"
func main() {
path := `C:\new\table.txt`
phone := `\d{10}`
menu := `Chai Rs 20
Samosa Rs 15
"Bun" Rs 35`
fmt.Println(path, phone)
fmt.Println(menu)
}
C:\new\table.txt \d{10}
Chai Rs 20
Samosa Rs 15
"Bun" Rs 35
- Perfect for Windows paths, regular expressions, multi-line text (menus, SQL, JSON, help messages) and text full of double quotes.
- Everything between the backticks is kept, including line breaks and any indentation, which is why the menu lines start at column 0.
- The only thing a raw string can't contain is a backtick.
The strings package
Customers type their orders, and they are messy. The strings package has the everyday text tools. %q prints a string inside double quotes (with escapes), so you can see every space.
TrimSpace, Split, Join
package main
import (
"fmt"
"strings"
)
func main() {
order := " chai, samosa,bun maska , chai "
clean := strings.TrimSpace(order)
fmt.Printf("%q\n", clean)
items := strings.Split(clean, ",")
fmt.Printf("%q\n", items)
for i, it := range items {
items[i] = strings.TrimSpace(it)
}
fmt.Println(strings.Join(items, " + "))
}
"chai, samosa,bun maska , chai"
["chai" " samosa" "bun maska " " chai"]
chai + samosa + bun maska + chai
strings.TrimSpace(s)removes spaces, tabs and newlines at both ends (not in the middle).strings.Split(s, sep)cutssat everysepand returns a[]string. The pieces keep their spaces, so we trimmed each one.strings.Join(parts, sep)glues a[]stringinto one string withsepbetween the parts.
Contains, Fields, Replace
package main
import (
"fmt"
"strings"
)
func main() {
note := "no sugar in chai, extra ginger"
fmt.Println(strings.Contains(note, "sugar"))
words := strings.Fields(note)
fmt.Printf("%q %d\n", words, len(words))
fmt.Println(strings.Replace("chai chai chai", "chai", "tea", 2))
fmt.Println(strings.ReplaceAll("98100-11111", "-", ""))
}
true
["no" "sugar" "in" "chai," "extra" "ginger"] 6
tea tea chai
9810011111
strings.Contains(s, sub): issubsomewhere ins?strings.Fields(s)splits at any run of whitespace and drops empty pieces, so messy spacing doesn't matter. (It doesn't remove punctuation:"chai,"keeps its comma.)strings.Replace(s, old, new, n)replaces the firstnmatches (-1means all).strings.ReplaceAll(s, old, new)replaces every match.- None of these change the original: strings are immutable, so they all return a new string.
More strings functions you will use
All results below are from a real run:
| Function | What it does | Example result |
|---|---|---|
==, < |
compare strings byte by byte (case matters) | "Chai" == "chai" → false |
strings.EqualFold(a, b) |
equal, ignoring upper/lower case | true |
strings.ToUpper / ToLower |
change case (works for é) |
CAFÉ |
strings.HasPrefix / HasSuffix |
starts with / ends with? | true |
strings.Index(s, sub) |
byte position of the first match, or -1 | 7 |
strings.Repeat(s, n) |
s repeated n times |
===== |
strings.ContainsRune(s, r) |
is rune r in s? |
true |
strings.Count(s, sub) |
how many times sub appears |
2 |
Note: strings.Split keeps empty pieces ("a,b,,c" → 4 parts including ""), while strings.Fields never returns empty pieces.
Building long strings: += is slow
Raju's printer needs one long string: "chai " written 20,000 times. The obvious way, += in a loop, timed with the time package (time.Now() records the moment, time.Since(start) gives the time passed):
package main
import (
"fmt"
"time"
)
func main() {
start := time.Now()
s := ""
for i := 0; i < 20000; i++ {
s += "chai "
}
fmt.Println(len(s), "bytes in", time.Since(start))
}
One real run (your time will differ):
100000 bytes in 217.076635ms
A big fraction of a second for a small job. Why: strings are immutable, so every += makes a brand-new string and copies all the old bytes into it. The copies grow: 5 bytes, 10, 15 ... 100,000. Over the loop that is about 5 × (1 + 2 + ... + 20,000) ≈ one billion bytes copied to build a 100,000-byte string.
Real-life picture: to add one line to a notebook, you first copy the whole notebook by hand into a new one.
strings.Builder
strings.Builder keeps a growing buffer (a []byte that gets bigger as needed) and appends to it. No copying of the whole text on every write; you just write the next line in the same notebook.
package main
import (
"fmt"
"strings"
"time"
)
func main() {
start := time.Now()
var sb strings.Builder
for i := 0; i < 20000; i++ {
sb.WriteString("chai ")
}
fmt.Println(sb.Len(), "bytes in", time.Since(start))
var bill strings.Builder
bill.WriteString("2 chai = ")
bill.WriteRune('₹')
bill.WriteString("40")
fmt.Println(bill.String())
}
One real run:
100000 bytes in 297.851µs
2 chai = ₹40
var sb strings.Builder: the zero value is ready to use, no setup.sb.WriteString(s)appends a string,sb.WriteRune(r)a rune (encoded as UTF-8),sb.WriteByte(b)one byte.sb.Len()is the number of bytes so far;sb.String()returns the finished string (without copying).- Same 100,000 bytes in microseconds (millionths of a second): hundreds of times faster than
+=. fmt.Fprintf(&sb, "...", ...)also writes formatted text into a Builder (episode 18 coversPrintfverbs).- Don't copy a Builder that has been written to. This compiles, but panics:
var a strings.Builder
a.WriteString("chai")
b := a
b.WriteString("!")
panic: strings: illegal use of non-zero Builder copied by value
Rule of thumb: a few + joins are fine; inside a loop, or for long text, use strings.Builder (or strings.Join when you already have a []string).
The bytes package
Data from files and the network usually arrives as a []byte, not a string. Converting it to a string just to search it would copy it. The bytes package has the same kind of functions as strings, working directly on byte slices:
package main
import (
"bytes"
"fmt"
)
func main() {
data := []byte("ORDER chai x2; samosa x1")
fmt.Println(bytes.Contains(data, []byte("chai")))
fmt.Println(bytes.Count(data, []byte("x")))
fmt.Println(string(bytes.ToLower(data)))
var buf bytes.Buffer
buf.WriteString("Bill: ")
buf.Write(data[6:13])
fmt.Println(buf.String(), buf.Len())
}
true
2
order chai x2; samosa x1
Bill: chai x2 13
| strings | bytes |
|---|---|
strings.Contains(s, "chai") |
bytes.Contains(b, []byte("chai")) |
strings.Count, ToLower, Split, Fields, TrimSpace... |
bytes.Count, ToLower, Split, Fields, TrimSpace... |
s1 == s2 |
bytes.Equal(b1, b2) (slices can't use ==) |
strings.Builder |
bytes.Buffer |
bytes.Bufferis a growable buffer of bytes you can write into and read back out of.buf.WriteString,buf.Write([]byte),buf.String(),buf.Len(). Its zero value is ready to use.data[6:13]is a slice of the byte slice: bytes 6 to 12, the textchai x2.- Use
strings.Builderto build a string; usebytes.Bufferwhen you need a buffer of bytes to read from as well (files, network).
The unicode package
The unicode package answers questions about one rune, for every script:
package main
import (
"fmt"
"unicode"
)
func main() {
text := "Raju Café: 2 chai, Rs 40!"
letters, digits := 0, 0
for _, r := range text {
if unicode.IsLetter(r) {
letters++
}
if unicode.IsDigit(r) {
digits++
}
}
fmt.Println(letters, digits)
fmt.Println(unicode.IsUpper('R'), unicode.IsUpper('a'))
fmt.Println(string(unicode.ToUpper('é')), unicode.IsLetter('₹'))
}
14 3
true false
É false
| Function | Question / action | Example |
|---|---|---|
unicode.IsLetter(r) |
a letter in any language? | 'é' → true, '₹' → false |
unicode.IsDigit(r) |
a decimal digit? | '2' → true |
unicode.IsUpper(r) / IsLower(r) |
capital / small letter? | 'R' → true |
unicode.IsSpace(r) |
space, tab, newline...? | ' ' → true |
unicode.IsPunct(r) |
punctuation? | '!' → true |
unicode.ToUpper(r) / ToLower(r) |
change case of one rune | 'é' → 'É' |
unicode.MaxASCII |
the last ASCII code point | 127 |
- Range over the string (not index it) so each check sees a whole rune.
ToUpper('é')→É: a simple "subtract 32 from the byte" trick only works for ASCII,unicodeworks everywhere.'₹'is a currency symbol, not a letter.
Mini project: an SMS character counter
A simplified version of the real SMS rule: if a message uses only ASCII characters, one SMS holds 160 characters. One character outside ASCII and the phone switches to Unicode mode, with 70 per SMS.
package main
import (
"fmt"
"unicode"
"unicode/utf8"
)
func main() {
msgs := []string{"Chai Rs 20 today!", "नमस्ते Raju", "Café ₹20"}
for _, m := range msgs {
limit := 160
for _, r := range m {
if r > unicode.MaxASCII {
limit = 70
}
}
n := utf8.RuneCountInString(m)
fmt.Printf("%s: %d/%d chars, %d bytes\n", m, n, limit, len(m))
}
}
Chai Rs 20 today!: 17/160 chars, 17 bytes
नमस्ते Raju: 11/70 chars, 23 bytes
Café ₹20: 8/70 chars, 11 bytes
r > unicode.MaxASCIIspots any non-ASCII character, so the limit drops to 70.utf8.RuneCountInString(m)counts characters, whilelen(m)still shows bytes: the namaste message is 11 characters but 23 bytes.- This is the counter your phone shows while you type.
The next problem: we keep using Printf verbs (%q, %c, %U, %T), and Raju's program still can't read what a customer types. Episode 18: formatted output and reading input.
Common mistakes (and the real error messages)
| You wrote | Real error / what happens | Fix |
|---|---|---|
sign[0] = 'R' |
cannot assign to sign[0] (neither addressable nor a map index expression) |
b := []byte(sign); b[0] = 'R'; sign = string(b) |
len("नमस्ते") as a character count |
no error: 18, not 6 |
utf8.RuneCountInString(s) |
s[3] expecting a letter |
no error: 195, half of é |
for i, r := range s |
"C:\new\table.txt" |
no error: a newline and a tab appear | `C:\new\table.txt` |
"\d{10}" |
unknown escape |
`\d{10}` |
s += "chai " in a big loop |
no error: very slow (copies every time) | strings.Builder |
reversing []byte(s) |
no error: broken symbols for é |
reverse []rune(s) |
sign[0] == "C" |
invalid operation: sign[0] == "C" (mismatched types byte and untyped string) |
sign[0] == 'C' |
var b byte = '₹' |
cannot use '₹' (untyped rune constant 8377) as byte value in variable declaration (overflows) |
var r rune = '₹' |
r := 'ab' |
more than one character in rune literal |
"ab" (a string) |
"Raju"[10] |
panic: runtime error: index out of range [10] with length 4 |
check len(s) first |
copying a used strings.Builder |
panic: strings: illegal use of non-zero Builder copied by value |
pass a pointer &sb |
Practice
- Count the vowels in
"Raju's Chai Is Open Early"(any case). - Reverse
"Chai ₹20"correctly. - Capitalise every word of a messy name:
" raju kumar yadav "→Raju Kumar Yadav. - Check whether a phrase is a palindrome (reads the same backwards), ignoring case, spaces and punctuation.
- Build
chai, samosa, bun maskafrom a[]stringwithstrings.Builder(no comma after the last item).
Solutions
strings.ToLowerfirst, then check each rune withstrings.ContainsRune:
package main
import (
"fmt"
"strings"
)
func main() {
text := "Raju's Chai Is Open Early"
vowels := 0
for _, r := range strings.ToLower(text) {
if strings.ContainsRune("aeiou", r) {
vowels++
}
}
fmt.Println("vowels:", vowels)
}
vowels: 9
- Runes, so
₹stays whole:
package main
import (
"fmt"
"slices"
)
func main() {
s := "Chai ₹20"
r := []rune(s)
slices.Reverse(r)
fmt.Println(string(r))
}
02₹ iahC
Fieldshandles the messy spaces;[]runelets us change the first character even if it isn't ASCII:
package main
import (
"fmt"
"strings"
"unicode"
)
func main() {
name := " raju kumar yadav "
words := strings.Fields(name)
for i, w := range words {
r := []rune(w)
r[0] = unicode.ToUpper(r[0])
words[i] = string(r)
}
fmt.Println(strings.Join(words, " "))
}
Raju Kumar Yadav
- Keep only letters and digits, lower-cased, as runes; then compare from both ends:
package main
import (
"fmt"
"unicode"
)
func isPalindrome(s string) bool {
var r []rune
for _, c := range s {
if unicode.IsLetter(c) || unicode.IsDigit(c) {
r = append(r, unicode.ToLower(c))
}
}
for i, j := 0, len(r)-1; i < j; i, j = i+1, j-1 {
if r[i] != r[j] {
return false
}
}
return true
}
func main() {
for _, s := range []string{"Nitin", "Never odd or even", "Raju"} {
fmt.Println(s, isPalindrome(s))
}
}
Nitin true
Never odd or even true
Raju false
- (In real code
strings.Join(items, ", ")does this in one call.)
package main
import (
"fmt"
"strings"
)
func main() {
items := []string{"chai", "samosa", "bun maska"}
var sb strings.Builder
for i, it := range items {
if i > 0 {
sb.WriteString(", ")
}
sb.WriteString(it)
}
fmt.Println(sb.String())
}
chai, samosa, bun maska
Quick quiz
What does this print?
func main() {
s := "₹50"
n := utf8.RuneCountInString(s)
fmt.Println(len(s), s[1], n)
}
- A.
3 50 3 - B.
5 130 3 - C.
5 53 3 - D.
3 130 5
Answer
B. 5 130 3. ₹ takes 3 bytes in UTF-8 (226 130 185), so len is 3 + 2 = 5. s[1] is the second byte of ₹, 130, not the character 5. There are 3 runes: ₹, 5, 0.
Interview questions
- What is a Go string internally? Is it mutable?
- What does
len(s)return for a string with non-English characters? Why? - What is the difference between a
byteand arune? What types are they aliases for? - What does
s[i]return? What doesfor i, r := range sgive you, and why canijump by more than 1? - How do you count the characters in a string?
- How do you change one character of a string?
- Why does reversing a string as bytes break non-ASCII text?
- What is the difference between an interpreted string literal and a raw string literal?
- Why is
+=in a loop slow, and what should you use instead? - When would you use the
bytespackage instead ofstrings? - What is UTF-8, and why is an ASCII character always one byte in it?
Glossary
| Term | Meaning | Example |
|---|---|---|
| String | a read-only row of bytes, usually UTF-8 text | "Café" |
| Immutable | can never change after it is made | sign[0] = 'R' is an error |
| Byte | a number from 0 to 255; byte is an alias for uint8 |
"Café"[0] → 67 |
| Rune | one Unicode code point; rune is an alias for int32 |
'é' → 233 |
| Alias | a second name for the same type | byte = uint8 |
| Rune literal | one character in single quotes | 'R', '₹' |
| Unicode | the standard that numbers every character of every script | ₹ = U+20B9 |
| Code point | a character's Unicode number | é = U+00E9 = 233 |
| Hexadecimal | base-16 numbers, digits 0-9 and A-F | E9 = 233 |
| Encoding | the rule that turns code points into bytes | UTF-8 |
| UTF-8 | Go's encoding: 1 to 4 bytes per character | é → 195 169 |
| ASCII | the first 128 characters; 1 byte each in UTF-8 | A = 65 |
len(s) |
number of bytes in a string | len("नमस्ते") → 18 |
| Index | a byte position, from 0 | s[0] |
| Indexing a string | gives the byte at that position | "Café"[3] → 195 |
| Slicing a string | a substring by byte positions | s[3:] → é |
range over a string |
decodes UTF-8: byte index + rune | 0:U+0928 3:U+092E |
| Decode | read the bytes of a character and produce its rune | 195 169 → é |
utf8.RuneCountInString |
number of runes (characters) | "Café" → 4 |
utf8.RuneLen |
bytes a rune needs | 'é' → 2 |
utf8.ValidString |
is the string valid UTF-8? | true |
utf8.RuneError |
U+FFFD, given for invalid bytes | 65533 |
| Conversion | making a value of another type | []byte(s), string(r) |
[]byte(s) |
an editable copy of a string's bytes | b[0] = 'R' |
[]rune(s) |
an editable slice of the string's runes | r[3] = 'é' |
string(b) / string(r) |
a new string from bytes / runes | "Raju Chai" |
slices.Reverse |
reverses a slice in place | éfaC |
| Interpreted string literal | text in double quotes; escapes work | "a\tb" |
| Escape sequence | backslash + letter for a special character | \n, \t, \", \\, \u20b9 |
| Backslash | the \ character, which starts an escape |
\n |
| Raw string literal | text in backticks; no escapes, can be multi-line | `C:\new` |
| Backtick | the ` character |
`\d{10}` |
| Regular expression | a search pattern for text (later episode) | \d{10} |
| Printf verb | a % placeholder in Printf |
%c, %q, %U, %T, %v, %d |
%c |
prints a number as its character | 67 → C |
%q |
prints a string in double quotes | "chai" |
%U |
prints a rune as U+ code point |
U+0928 |
%T |
prints a value's type | uint8 |
strings package |
everyday functions for strings | strings.Split |
strings.TrimSpace |
removes whitespace at both ends | " chai " → "chai" |
strings.Split |
cuts at a separator into []string |
"a,b" → [a b] |
strings.Join |
glues a []string with a separator |
chai + samosa |
strings.Contains |
is a substring present? | true |
strings.Fields |
splits at any whitespace | [no sugar in] |
strings.Replace / ReplaceAll |
replace the first n / every match | tea tea chai |
| Whitespace | spaces, tabs, newlines | " \t\n" |
| Substring | a part of a string | "chai" in "masala chai" |
time.Now / time.Since |
the current moment / time passed since | 219.4ms |
| Microsecond | a millionth of a second (µs) | 365µs |
| Buffer | a growable area of memory that collects bytes | inside a Builder |
strings.Builder |
builds a string efficiently by appending | sb.WriteString("chai ") |
WriteString / WriteRune / WriteByte |
append a string / rune / byte | bill.WriteRune('₹') |
bytes package |
strings-like functions for []byte |
bytes.Contains |
bytes.Buffer |
a byte buffer you can write to and read from | buf.Write(data[6:13]) |
bytes.Equal |
compares two byte slices | true |
unicode package |
classifies and converts single runes | unicode.IsLetter |
unicode.IsLetter / IsDigit / IsUpper |
letter? digit? capital? | 'é' → letter |
unicode.ToUpper |
the capital form of a rune | 'é' → 'É' |
unicode.MaxASCII |
127, the last ASCII code point | r > unicode.MaxASCII |
| Palindrome | reads the same backwards | Nitin |
Summary
- A Go string is an immutable row of UTF-8 bytes. UTF-8 uses 1 to 4 bytes per character; ASCII is 1 byte.
len(s)counts bytes, ands[i]is a byte. Abyteis auint8; aruneis anint32holding one code point.rangeover a string decodes runes (the index jumps by the character's byte length).utf8.RuneCountInStringcounts characters.- You can't change a string; convert to
[]byteor[]rune, edit, and convert back. Reverse and edit text as runes. - Escape sequences (
\n,\t,\",\\) work in double quotes; raw strings in backticks keep backslashes and line breaks as typed. - The
stringspackage:TrimSpace,Split,Join,Contains,Fields,Replace, and more. They all return new strings. +=in a loop copies everything every time; usestrings.Builder. Thebytespackage does the same jobs for[]byte, withbytes.Buffer. Theunicodepackage classifies runes in every script.
Next episode: formatted output and reading input: every Printf verb, width and precision, Sprintf, and reading what the user types with fmt.Scan and bufio.