type T struct { X, Y, Z int }
var t = T{X: 7, Y: 0, Z: 8}
type U struct { X, Y *int8 }
var u U
package main
import (
    "encoding/gob"
    "fmt"
    "os"
)
func main() {
    info := map[string]string{
        "name":    "C语言中文网",
        "website": "http://c.biancheng.net/golang/",
    }
    name := "demo.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)
    }
}
运行上面的代码会在当前目录下生成 demo.gob 文件,文件的内容如下所示:
0eff 8104 0102 ff82 0001 0c01 0c00 0041
ff82 0002 046e 616d 6510 43e8 afad e8a8
80e4 b8ad e696 87e7 bd91 0777 6562 7369
7465 1e68 7474 703a 2f2f 632e 6269 616e
... ...
package main
import (
    "encoding/gob"
    "fmt"
    "os"
)
func main() {
    var M map[string]string
    File, _ := os.Open("demo.gob")
    D := gob.NewDecoder(File)
    D.Decode(&M)
    fmt.Println(M)
}
运行结果如下:
go run main.go
map[name:C语言中文网 website:http://c.biancheng.net/golang/]
 版权说明:
	  版权说明:Copyright © 广州松河信息科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州松河信息科技有限公司 版权所有 18520775521
18520775521



 QQ洽谈
QQ洽谈
 sales@itwy.com
sales@itwy.com
