编码结构体:
package main
import (
"encoding/gob"
"fmt"
"os"
)
func main() {
info := map[string]string{
"name": "xichen",
"age": "24",
}
name := "test.gob"
File, _ := os.OpenFile(name, os.O_RDWR|os.O_CREATE, 0777)
defer File.Close()
enc := gob.NewEncoder(File)
if err := enc.Encode(info); err != nil {
fmt.Println(err)
}
}
解码结构体:
package main
import (
"encoding/gob"
"fmt"
"os"
)
func main() {
var M map[string]string
File, _ := os.Open("test.gob")
D := gob.NewDecoder(File)
D.Decode(&M)
fmt.Println(M)
}
Gob的使用方法和Go内置的json基本上是一样的,很方便