Strings & the strings Package
A Go string is an immutable sequence of bytes, and the standard strings package gives you everything you need to search, split, join, replace, and efficiently build text.
Learn Strings & the strings Package in our free Go course — an interactive lesson with runnable examples, a practice exercise and a quick reference.
Part of the free Go course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
What You'll Learn in This Lesson
1️⃣ Strings Are Immutable Byte Sequences
Under the hood a Go string is just a read-only run of bytes . You can index it ( s[0] gives the byte value) and re-slice it ( s[1:] ), but you can never assign to a single position — that's a compile error. To "change" a string you create a brand-new one, often by concatenating slices. And len reports the number of bytes , which matters once you move beyond plain ASCII.
2️⃣ The strings Package Toolkit
The strings package is where the real work happens. Split turns a string into a []string on a separator and Join glues one back together. Add searching ( Contains , HasPrefix , Count ), case conversion ( ToUpper ), and replacement ( Replace , ReplaceAll ), and you can handle almost any text task without a third-party library.
3️⃣ Concatenation Cost & strings.Builder
Because strings are immutable, every s = s + x allocates a whole new string and copies the old bytes. Inside a loop that's quadratic work and a pile of garbage. strings.Builder fixes this by growing a single internal buffer: you WriteString / WriteByte / Fprintf into it, then call String() once at the end. It's the idiomatic way to assemble text piece by piece.
🎯 Your Turn
Fill in the two blanks: join a slice of tags with ", " , then print the result in uppercase.
Use a strings.Builder to assemble a numbered list — one line per word in the form "1. apple" — then print it. Range over the slice to get both the index and the word.
Practice quiz
What is a Go string under the hood?
A Go string is an immutable sequence of bytes.
What happens if you write s[0] = 'H' on a string?
- Compile error — strings are immutable
- It works
- It silently does nothing
- It panics at runtime
Answer: Compile error — strings are immutable. You cannot assign to a string index; it is a compile error because strings are immutable.
What does fmt.Println(s[0]) print for s := "hello"?
- h
- "h"
- 0
- 104
Answer: 104. Indexing yields a byte; the byte value of 'h' is 104. Use %c to see the letter.
What does len("héllo") return, where é is 2 bytes?
- 5
- 6
- 4
- 7
Answer: 6. len counts bytes, and é takes 2, so the total is 6.
What does strings.Split("a,b,c", ",") return?
Split breaks the string on the separator into a slice [a b c].
What does strings.Join(parts, sep) do?
- Splits a string
- Counts separators
- Glues a slice of strings together with sep between them
- Removes whitespace
Answer: Glues a slice of strings together with sep between them. Join is the inverse of Split: it concatenates the slice elements with sep.
How does strings.Replace differ from strings.ReplaceAll?
- They are identical
- Replace takes a count n (use -1 for all); ReplaceAll replaces every match
- ReplaceAll only replaces the first match
- Replace returns a slice
Answer: Replace takes a count n (use -1 for all); ReplaceAll replaces every match. Replace(s, old, new, n) replaces at most n; ReplaceAll is Replace with n = -1.
Why is repeated s = s + x in a loop slow?
- It uses too many goroutines
- + is not allowed on strings
- It never terminates
- Strings are immutable, so each + allocates a new string and copies everything (O(n^2))
Answer: Strings are immutable, so each + allocates a new string and copies everything (O(n^2)). Immutability means each concatenation reallocates and copies, giving quadratic work.
What is the idiomatic way to assemble a string from many pieces in a loop?
- Repeated +
- strings.Builder
- fmt.Sprint each time
Answer: strings.Builder. strings.Builder grows a single buffer, making the work linear.
After building "Hello, Gopher!" in a strings.Builder, what does b.Len() return?
- 13
- 12
- 14
- 15
Answer: 14. "Hello, Gopher!" is 14 bytes, which is what Len() reports.