Standard library / encoding/gob

encoding/gob.Decoder.Decode()

Read one value into a compatible pointer; nil discards the next value. io.EOF reports the end of a complete stream. Expected stdout: '2 true\n'.

Formal Syntax & Structure

go syntaxgo
func (dec *Decoder) Decode(e any) error

Parameters & Attributes

NameTypeDescription
eanyDestination pointer or nil to discard the next value.

Return Value

error

Nil on success; otherwise the operation error.

Working Example

Example Snippetgo
package main
import (  "bytes"  "encoding/gob"  "fmt"  "io")
func must(e error) {  if e != nil {    panic(e)  }}
func main() {  var buf bytes.Buffer  enc := gob.NewEncoder(&buf)  must(enc.Encode(1))  must(enc.Encode(2))  dec := gob.NewDecoder(&buf)  must(dec.Decode(new(int)))  var n int  must(dec.Decode(&n))  fmt.Println(n, dec.Decode(&n) == io.EOF)}
Go Reference