Chapter 08Data Structure InternalsPaid chapter

Strings, Bytes, and the Cost of Conversion

Understand when string ↔ []byte conversions allocate, when the compiler elides the copy, and how to minimize conversion overhead.

Launch paywall

This chapter is part of the paid launch. The preview stays visible so readers can evaluate the exact scope before buying.

Included in

Core, Complete, and Team. Complete also includes the practical exam and free Go-version updates.

Preview

What this chapter teaches

Common belief

string(b) and []byte(s) are basically free reinterpretations.

What actually happens

they copy — except in a few cases the compiler special-cases (map keys, comparisons, range, switch). Knowing which is which is the lesson.

Mechanisms covered
The compiler elides copies for m[string(b)] lookups and switch string(b) comparisons, but not for general assignments or function arguments.
strings.Builder avoids repeated allocation by growing a []byte buffer and converting once via unsafe at the end. unsafe.String/unsafe.SliceData (Go 1.20+) give zero-copy conversion if the immutability invariant is upheld manually.
benchexperiment
Email-first checkoutUnlock Chapter 08

Unlock Chapter 08 and the rest of the course.

Start with your email, then continue to Lemon Squeezy checkout. Core unlocks the paid chapters, Complete adds the exam and free Go-version updates, and Team gives you 5 seats.

Choose a plan

You'll head to Lemon Squeezy for secure checkout. Global tax is handled there. No subscription. Complete also includes the practical exam and free Go-version updates.

Free chapter
Escape Analysis — The Compiler's Fragile Decision
Understand how the compiler decides between stack and heap allocation, why the decision is fragile, and how to verify it.
Free chapter
Maps — Swiss Tables, Growth, and Permanent Memory
Understand the Go 1.24+ Swiss Table map layout, why maps don't shrink, and how to design for memory reclamation.