Complete Guide to the encoding/binary Package in Go

The encoding/binary package converts Go primitive types to and from byte slices using configurable byte orders.

The encoding/binary package in Go handles the conversion between primitive types and byte slices, supporting both big-endian and little-endian byte orders. Use binary.LittleEndian.PutUint64 to write a number to a byte slice or binary.BigEndian.Uint32 to read one back.

import "encoding/binary"

// Write uint64 to byte slice (little-endian)
buf := make([]byte, 8)
binary.LittleEndian.PutUint64(buf, 12345)

// Read uint32 from byte slice (big-endian)
val := binary.BigEndian.Uint32(buf[:4])