Standard library / encoding/gob

encoding/gob.Encoder.Encode()

Transmit a value and any required type information. Struct fields must be exported to participate in ordinary encoding; nil pointers cannot be encoded directly. Expected stdout: '7 0\n'.

Formal Syntax & Structure

go syntaxgo
func (enc *Encoder) Encode(e any) error

Parameters & Attributes

NameTypeDescription
eanyValue to transmit; nil pointers are not supported.

Return Value

error

Nil on success; otherwise the operation error.

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  hidden int}
func main() {  var buf bytes.Buffer  must(gob.NewEncoder(&buf).Encode(Item{7, 8}))  var got Item  must(gob.NewDecoder(&buf).Decode(&got))  fmt.Println(got.N, got.hidden)}
Go Reference