gob and Binary Encoding
Encode Go values.
gob and Binary Encoding is a free Go Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is gob
encoding/gob is a Go-specific binary format for serializing Go values. It is compact and fast, ideal for Go-to-Go communication and on-disk storage.
gob vs JSON
JSON is text, human-readable, and cross-language. gob is binary, Go-only, but more compact and faster to encode. Use gob when both ends are Go programs.
Encoding to a Buffer
A gob.Encoder writes to any io.Writer. Here we use a bytes.Buffer to hold the binary output.
package main
import (
"bytes"
"encoding/gob"
"fmt"
)
func main() {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
enc.Encode(42)
fmt.Println("bytes written:", buf.Len())
}Decoding from a Buffer
A gob.Decoder reads from an io.Reader and fills a pointer, just like Unmarshal.
package main
import (
"bytes"
"encoding/gob"
"fmt"
)
func main() {
var buf bytes.Buffer
gob.NewEncoder(&buf).Encode(42)
var n int
gob.NewDecoder(&buf).Decode(&n)
fmt.Println(n)
}Encoding Structs
gob handles structs naturally. Only exported fields are encoded.
package main
import (
"bytes"
"encoding/gob"
"fmt"
)
type User struct {
Name string
Age int
}
func main() {
var buf bytes.Buffer
gob.NewEncoder(&buf).Encode(User{"Ann", 30})
var u User
gob.NewDecoder(&buf).Decode(&u)
fmt.Println(u)
}Encoding Maps and Slices
Composite types like maps and slices encode and decode transparently.
package main
import (
"bytes"
"encoding/gob"
"fmt"
)
func main() {
var buf bytes.Buffer
gob.NewEncoder(&buf).Encode([]int{1, 2, 3})
var s []int
gob.NewDecoder(&buf).Decode(&s)
fmt.Println(s)
}Self-Describing Streams
gob streams include type information, so the decoder can rebuild values even mapping to a slightly different but compatible struct. Extra or missing fields are tolerated.
Exported Fields Only
Like JSON, gob only encodes exported fields. Unexported (lowercase) fields are skipped silently.
package main
import (
"bytes"
"encoding/gob"
"fmt"
)
type T struct {
Public int
private int
}
func main() {
var buf bytes.Buffer
gob.NewEncoder(&buf).Encode(T{Public: 1, private: 2})
var out T
gob.NewDecoder(&buf).Decode(&out)
fmt.Println(out.Public, out.private)
}Other Binary Options
For raw fixed-size binary, encoding/binary writes integers and floats in big or little endian. It is lower level than gob and not self-describing.
package main
import (
"bytes"
"encoding/binary"
"fmt"
)
func main() {
var buf bytes.Buffer
binary.Write(&buf, binary.BigEndian, uint32(258))
fmt.Println(buf.Bytes())
}When to Use Each
Choose:
json- APIs, config, cross-languagegob- Go-to-Go, compact, easy with structsbinary- exact byte layouts, protocols
Not Human Readable
gob output is binary; printing it shows gibberish. That is the tradeoff for compactness. Use it for machines, not logs you read.
Quick Check
When is gob a better choice than JSON?
Recap
gob and binary encoding:
gob.NewEncoder/NewDecoderfor Go-to-Go binary- Compact, self-describing, exported fields only
encoding/binaryfor exact byte layouts- JSON remains best for cross-language and readability
Frequently asked questions
Is the “gob and Binary Encoding” lesson free?
Yes — the full text of “gob and Binary Encoding” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.
What will I learn in “gob and Binary Encoding”?
Encode Go values. You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Go Academy?
No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “gob and Binary Encoding” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Go Academy lesson?
Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- encoding/json Recap
- encoding/xml
- encoding/csv
- gob and Binary Encoding