Every Go backend engineer eventually discovers struct{}{}: the canonical zero-sized value used for sets (map[string]struct{}), signal channels (chan struct{}), and context keys. In isolation, unsafe.Sizeof(struct{}{}) is 0, and escaping pointers share a single runtime sentinel address called runtime.zerobase.
package main
import (
"fmt"
"unsafe"
)
func main() {
a := new(struct{})
b := new(struct{})
fmt.Println(unsafe.Sizeof(*a)) // 0
fmt.Printf("%p == %p\n", a, b) // both point to runtime.zerobase
}
The Trailing Zero-Sized Field Trap
When you embed or place a struct{} as the final field of a non-empty struct on a 64-bit target, the Go compiler cannot leave its size at zero. If code took &item.marker at the end of a struct, that pointer would alias the start of the next adjacent object in memory and confuse the garbage collector.
To prevent interior-pointer aliasing across heap objects, the compiler pads a trailing zero-sized field by 1 byte—which 8-byte struct alignment immediately rounds up to 8 extra bytes:
package layout
import "unsafe"
// Trailing zero-sized field forces 1 byte + 7 bytes alignment padding.
type TrailingEmpty struct {
ID uint64
Marker struct{}
}
// Leading zero-sized field sits at offset 0 with zero padding.
type LeadingEmpty struct {
Marker struct{}
ID uint64
}
var (
_ = unsafe.Sizeof(TrailingEmpty{}) // 16 bytes (+8 bytes!)
_ = unsafe.Sizeof(LeadingEmpty{}) // 8 bytes
)
Across a ring buffer or cache slice of 50 million entries, placing struct{} last turns a 400 MB allocation into 800 MB—paying 400 MB to store literally nothing.
Size Classes and Boxing Still Count in Words
Even when your type layout stays tight, the allocator and type system operate on machine words. Boxing struct{}{} into any constructs a two-word interface header (type pointer + data pointer), and heap-allocating small composite wrappers lands in the 8-byte tiny allocator slot:
func BenchmarkEmptyAllocation(b *testing.B) {
var sink []TrailingEmpty
b.ReportAllocs()
for b.Loop() {
// 1,024 * 16B = 16 KiB instead of 8 KiB
sink = make([]TrailingEmpty, 1024)
}
_ = sink
}
Rule of thumb for high-throughput Go services: put zero-sized fields first (or omit them via distinct named types), verify hot struct layouts with unsafe.Sizeof and unsafe.Offsetof in unit tests, and never assume zero-sized in the spec means zero bytes on the heap.