Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Created January 7, 2019 05:14
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save miguelmota/2a0c0e96c22bccc8740819d5d64ff8d0 to your computer and use it in GitHub Desktop.
Save miguelmota/2a0c0e96c22bccc8740819d5d64ff8d0 to your computer and use it in GitHub Desktop.
Golang gob encoding and decoding example
package main
import (
"bytes"
"encoding/gob"
"fmt"
"log"
)
func main() {
input := []byte{14, 255, 129, 4, 1, 2, 255, 130, 0, 1, 12, 1, 12, 0, 0, 12, 255, 130, 0, 1, 3, 102, 111, 111, 3, 98, 97, 114}
buf := bytes.NewBuffer(input)
dec := gob.NewDecoder(buf)
m := make(map[string]string)
if err := dec.Decode(&m); err != nil {
log.Fatal(err)
}
fmt.Println(m["foo"]) // "bar"
}
package main
import (
"bytes"
"encoding/gob"
"fmt"
"log"
)
func main() {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
m := make(map[string]string)
m["foo"] = "bar"
if err := enc.Encode(m); err != nil {
log.Fatal(err)
}
fmt.Println(buf.Bytes()) // [14 255 129 4 1 2 255 130 0 1 12 1 12 0 0 12 255 130 0 1 3 102 111 111 3 98 97 114]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment