Day 033 — strings, strconv & bytes¶
Month 2 · Week 1 · ⬅ Day 032 · Day 034 ➡ · Journal index
🎯 Learning Objective¶
Wield the three text-wrangling workhorses: strings for search/split/replace, strconv for explicit string↔number conversion, and bytes as the mutable []byte mirror of strings.
📚 Topics¶
strings:Split,Fields,Cut,Contains,Replace*,Trim*,Indexstrconv:Atoi/Itoa,ParseInt/ParseFloat/ParseBool,Format*,Quotebytes:Buffer,Index, mirror ofstringsfor[]byte- The conversion-vs-cast distinction (
strconv≠string(int))
📖 Reading / Sources¶
📝 Notes¶
strings.Split(s, sep)splits on everysep;strings.Fields(s)splits on runs of whitespace (no empty tokens) — useFieldsfor "split on spaces" → [[strings-split]].strings.Cut(s, sep)(Go 1.18+) returnsbefore, after, foundaround the firstsep— the clean idiom forkey=valueparsing, replacingIndex+slicing.- Search/test helpers:
Contains,HasPrefix,HasSuffix,Index,Count. Transformers:ReplaceAll,ToLower/ToUpper,TrimSpace,Trim/TrimLeft/TrimRight,Repeat,Join. strconvis conversion, not casting.string(65)produces"A"(the rune), not"65"— a classic bug. To turn the number 65 into"65", usestrconv.Itoa(65)→ [[strconv-vs-cast]].strconv.Atoi/Itoaare the int↔string shortcuts. For control over base and bit size, useParseInt(s, base, bitSize)/FormatInt(i, base), plusParseFloat/ParseBool. They always return an error — never discard it; bad input is*strconv.NumError.strconv.Quote(s)adds quotes and escapes (the engine behind%q);Unquotereverses it.bytesmirrorsstringsbut operates on[]byte, avoidingstring([]byte)allocations when you already hold bytes (e.g. from a network read).bytes.IndexByte,bytes.Split,bytes.Contains, etc.bytes.Bufferis a growable, read+write byte container that satisfies bothio.Readerandio.Writer— ideal when an API needs both, or for assembling output you'll later consume.
💻 Code Examples¶
i, err := strconv.Atoi("42") // 42, nil — NOT string casting
_, err2 := strconv.Atoi("4x2") // 0, *strconv.NumError
hex, _ := strconv.ParseInt("ff", 16, 64) // 255 (base 16)
before, after, found := strings.Cut("key=value", "=") // "key","value",true
_ = i; _ = err; _ = err2; _ = hex; _ = before; _ = after; _ = found
Full code:
examples/month-02/strings-strconv/main.go· Run:go run ./examples/month-02/strings-strconv
🏋️ Exercises / Practice¶
| Exercise | Status | Link |
|---|---|---|
Sum CSV integer columns (Split + strconv + %w) |
✅ | exercises/month-02/week-1/csvsum |
🐛 Mistakes Made¶
- Wrote
string(42)expecting"42"and got"*"(rune U+002A). Switched tostrconv.Itoa. - Ignored the error from
strconv.Atoion user input and propagated a silent0.
❓ Open Questions¶
- When does converting
[]byte→stringactually allocate, and when does the compiler optimize it away (e.g. map lookups)?
🧠 Active Recall (answer without looking)¶
-
Q: What is the result and type of
string(65), and how do you get"65"?
A
`string(65)` is `"A"` — it interprets 65 as a rune (U+0041). To render the digits `"65"`, use `strconv.Itoa(65)`. -
Q: You want to split a sentence into words, ignoring extra spaces.
SplitorFields?
A
`strings.Fields` — it splits on runs of whitespace and drops empty tokens. `strings.Split(s, " ")` would emit empty strings for consecutive spaces.
🪶 Feynman Reflection¶
strings and bytes are twins: same toolbox, one for immutable text and one for mutable byte buffers. strconv is the careful translator between text and numbers — and it's careful on purpose, returning an error every time, because "abc" is not a number and Go won't pretend it is. The trap is thinking a type conversion string(n) does that translation; it doesn't — it picks a rune.
🕳️ Knowledge Gaps¶
strings.Replacerfor multi-pair replacement;unicodepackage case folding.
✅ Summary¶
I can search/split/replace text, convert between strings and numbers safely with strconv, and use bytes/bytes.Buffer to avoid needless allocations.
⏭️ Next Steps / Prep for Tomorrow¶
- Day 034:
strings.Builderand why+=in a loop is a performance trap.
| Time spent | Difficulty | Confidence |
|---|---|---|
| 90 min | 🟦🟦⬜⬜⬜ | 🟦🟦🟦⬜⬜ |
Suggested commit: feat(examples): strings, strconv, and bytes packages (day 033)