package main import ( "encoding/gob" "bytes" "fmt" ) type Person struct { Name string Age int } func main() { var p Person data := []byte{0x0d, 0x00, 0x00, 0x00, 0x0d, 0x42, 0x6f, 0x62, 0x20, 0x53, 0x6d, 0x69, 0x74, 0x68, 0x00, 0x00, 0x00, 0x00, 0x02} decoder := gob.NewDecoder(bytes.NewReader(data)) err := decoder.Decode(&p) if err != nil { fmt.Println("error:", err) } else { fmt.Printf("Name: %s, Age: %d\n", p.Name, p.Age) // Output: Name: Bob Smith, Age: 13 } }
package main import ( "encoding/gob" "bytes" "fmt" ) func main() { var m map[string]int data := []byte{0x0d, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00} decoder := gob.NewDecoder(bytes.NewReader(data)) err := decoder.Decode(&m) if err != nil { fmt.Println("error:", err) } else { fmt.Println(m) // Output: map[a:1 b:2] } }This example decodes a map[string]int from a binary stream that was encoded using gob Encoder. Based on the package name, we can determine that the encoding/gob package is part of the standard library in Go.