多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
> ### protobuf序列化 * 体积小: 对于未传值的字段, 不需要像json那样传个字段名和默认值, 可以节省流量 * protobuf是用二进制存储, 速度比json快, 也因为是二进制, 可读性差, 调试麻烦些 > ### protoc ~~~ go //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 ~~~ go 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)