💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
> ### protobuf序列化和反序列化 * 体积小: 对于未传值的字段, 不需要像json那样传个字段名和默认值, 可以节省流量 * protobuf是用二进制存储, 速度比json快, 也因为是二进制, 可读性差, 调试麻烦些 > ### protoc ~~~ //protoc --go_out-. person.protoc syntax = "proto3"; package person; message Information { string name = 1; int32 age = 2; } message Human { string otherName = 1; repeated Information info = 2; } ~~~ > ### main ~~~ package main import ( "gitee.com/winnie_gss/gin/protoc" "github.com/golang/protobuf/proto" "fmt" ) func main() { var i []*person.Information i1 := person.Information{ Name: "winnie", Age: 18, } i = append(i, &i1) i2 := person.Information{ Name: "winnie2", } i = append(i, &i2) m := person.Human{ OtherName: "别名", Info: i, } //序列化 data, err := proto.Marshal(&m) if err != nil { fmt.Println(err) } fmt.Println(data) //反序列化 var m2 person.Human err = proto.Unmarshal(data, &m2) fmt.Println(m2) } ~~~ > ### 相关阅读 * [Golang 序列化之 ProtoBuf](https://studygolang.com/articles/9548)