140x Faster String to Byte and Byte to String Conversions with Zero Allocation in Go

Jose Sitanggang
4 min readOct 21, 2023
Source: https://www.cnblogs.com/kkbill/p/13069596.html

Converting a string to bytes and bytes to a string requires allocating new memory. However, strings and bytes (which refer to a slice of bytes) have a similar memory representation. The only difference is that a slice has the capacity to grow as needed, whereas a string is immutable and doesn’t need capacity.

Strings and slices are built-in types in Go. Unfortunately, we cannot directly view their definitions. However, thanks to the Go documentation, we can learn that both strings and slices are defined using structs. Strings are defined using StringHeader, and slices are defined using SliceHeader. Let’s include those definitions here to make it easier to follow:

type SliceHeader struct {
Data uintptr
Len int
Cap int
}

type StringHeader struct {
Data uintptr
Len int
}

The Data field represents the memory address of the first item in the backing array, which is where the data is stored. The backing array has a fixed size since it is allocated, which is why a slice has a capacity ( Cap) to allow it to grow when more space is needed to store new data. If you are interested in learning how a slice grows, please refer to my article titled "Exploring Go Slice Internal Implementation in C++."

--

--