Strings, Bytes, and the Cost of Conversion
Separate string-conversion copying, heap allocation, and aliasing constraints.
This chapter is part of the paid launch. The preview stays visible so readers can evaluate the exact scope before buying.
Core and Complete. Complete also includes the practical exam and free Go-version updates.
What this chapter teaches
string(b) and []byte(s) are basically free — they just reinterpret the same bytes with a different type." (And its inverse, from performance folklore: "every conversion copies unless it's literally m[string(b)].")
the conversions copy by default, because a string is immutable and a []byte is mutable — otherwise a write through the slice would mutate a string, which the language forbids. But the compiler elides the copy wherever it can prove the copy is unobservable, and that proof now covers more than the old folklore says: string(b) used directly in a map index, comparison, switch, or range; a string(b) assigned to a local that provably stays transient (the two-step map lookup is now free); []byte(s) that is never written (zero-copy since Go 1.22); and any small non-escaping conversion, which lands in a 32-byte stack buffer. What always copies: a conversion whose result is retained (returned, stored in a map or struct) or whose bytes may be mutated.
Unlock Chapter 08 and the rest of the course.
Core unlocks every paid chapter and lab. Complete also includes the practical exam and future Go-version updates.
Full course access with the complete mechanics curriculum.
Best-value launch option with the exam and free Go-version course updates.
No subscription or account required. Access is tied to the email entered at Stripe Checkout and can be restored by email.