Standard library / encoding/gob

encoding/gob.Register()

Associate a concrete type with its default wire name before sending it through an interface. Both processes must know the mapping; conflicting registrations panic. Expected stdout: '7\n'.

Formal Syntax & Structure

go syntaxgo
func Register(value any)

Parameters & Attributes

NameTypeDescription
valueanyConcrete value identifying a type for registration, or reflect.Value for EncodeValue.

Working Example

Example Snippetgo
package main
import (  "bytes"  "encoding/gob"  "fmt")
func must(e error) {  if e != nil {    panic(e)  }}
type Item struct{ N int }
func main() {  gob.Register(Item{})  var buf bytes.Buffer  var v any = Item{7}  must(gob.NewEncoder(&buf).Encode(&v))  var got any  must(gob.NewDecoder(&buf).Decode(&got))  fmt.Println(got.(Item).N)}
Go Reference