Skip to content

Instantly share code, notes, and snippets.

@cristianoliveira
Created February 27, 2018 15:00
Show Gist options
  • Save cristianoliveira/e42587577c9fbb191506fea29306e847 to your computer and use it in GitHub Desktop.
Save cristianoliveira/e42587577c9fbb191506fea29306e847 to your computer and use it in GitHub Desktop.
Go map copy
package main
import (
"bytes"
"encoding/gob"
"fmt"
"log"
)
func main() {
ori := map[string]int{
"key": 3,
"clef": 5,
}
var mod bytes.Buffer
enc := gob.NewEncoder(&mod)
dec := gob.NewDecoder(&mod)
fmt.Println("ori:", ori) // key:3 clef:5
err := enc.Encode(ori)
if err != nil {
log.Fatal("encode error:", err)
}
var cpy map[string]int
err = dec.Decode(&cpy)
if err != nil {
log.Fatal("decode error:", err)
}
fmt.Println("cpy:", cpy) // key:3 clef:5
cpy["key"] = 2
fmt.Println("cpy:", cpy) // key:2 clef:5
fmt.Println("ori:", ori) // key:3 clef:5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment