Created
February 27, 2018 15:00
-
-
Save cristianoliveira/e42587577c9fbb191506fea29306e847 to your computer and use it in GitHub Desktop.
Go map copy
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() { | |
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