Standard library / encoding/gob

encoding/gob.NewEncoder()

Create a stateful encoder for an output stream. Reuse it for successive values so type descriptors can be shared. Expected stdout: '7 8\n'.

Formal Syntax & Structure

go syntaxgo
func NewEncoder(w io.Writer) *Encoder

Parameters & Attributes

NameTypeDescription
wio.WriterWriter receiving encoded data.

Return Value

*Encoder

New Gob encoder.

Working Example

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