Strings
Beginner · Fundamentals
A Go string is an immutable sequence of bytes, usually UTF-8. The strings
package has the utilities; unicode/utf8 and []rune handle characters.
- strings1 —
stringspackage helpers (ToLower,ReplaceAll) - strings2 — bytes vs runes (
lenvsutf8.RuneCountInString)
Resources
Section titled “Resources”- Go by Example: String Functions
- Go blog: Strings, bytes, runes and characters
- pkg.go.dev: strings · unicode/utf8
Exercises
Section titled “Exercises”strings1 test
Section titled “strings1 test”The strings package holds the everyday string utilities.
Show hint
Chain two strings helpers:
return strings.ReplaceAll(strings.ToLower(s), " ", "-")strings2 test
Section titled “strings2 test”A Go string is bytes, not characters. len(s) counts BYTES; one multibyte
Show hint
len counts bytes; count runes instead:
return utf8.RuneCountInString(s)