Created
January 7, 2019 05:14
-
-
Save miguelmota/2a0c0e96c22bccc8740819d5d64ff8d0 to your computer and use it in GitHub Desktop.
Golang gob encoding and decoding example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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