encoding/gob

Guided tour · Encoding · pkg.go.dev →

Go-native binary serialization. Self-describing, concise, Go-to-Go only. Not a cross-language format.

Encode and decode over a stream

Round-trip a struct via bytes.Buffer

type P struct{ X, Y int }

var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
enc.Encode(P{3, 4})

var q P
dec := gob.NewDecoder(&buf)
dec.Decode(&q)
fmt.Println(q)
Output
{3 4}

Interface values — must Register concrete types

gob needs to know the concrete type behind an interface before it can encode it.

gob.Register(&Circle{})
gob.Register(&Square{})

enc.Encode([]Shape{&Circle{R: 1}, &Square{S: 2}})

When to use gob vs JSON vs protobuf

Use gob for Go-to-Go state snapshots (caches, disk persistence). Prefer JSON for humans and HTTP APIs. Use protobuf/flatbuffers when you cross languages or need a strict schema.