How to Use encoding/gob for Go-Specific Serialization

Serialize Go structs to binary streams using encoding/gob's Encoder and Decoder for efficient data persistence and transmission.

Use encoding/gob to serialize Go structs into a binary stream by registering types and encoding them to an io.Writer.

import "encoding/gob"

type User struct {
    ID   int
    Name string
}

func main() {
    var buf bytes.Buffer
    enc := gob.NewEncoder(&buf)
    dec := gob.NewDecoder(&buf)

    u := User{ID: 1, Name: "Alice"}
    enc.Encode(u)

    var u2 User
    dec.Decode(&u2)
}