ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
### 4.6 利用protobuf生成的类来编码 ```go package main import ( "fmt" "github.com/golang/protobuf/proto" "protocolbuffer_excise/pb" ) func main() { person := &pb.Person{ Name: "Aceld", Age: 16, Emails: []string{"https://legacy.gitbook.com/@aceld", "https://github.com/aceld"}, Phones: []*pb.PhoneNumber{ &pb.PhoneNumber{ Number: "13113111311", Type: pb.PhoneType_MOBILE, }, &pb.PhoneNumber{ Number: "14141444144", Type: pb.PhoneType_HOME, }, &pb.PhoneNumber{ Number: "19191919191", Type: pb.PhoneType_WORK, }, }, } data, err := proto.Marshal(person) if err != nil { fmt.Println("marshal err:", err) } newdata := &pb.Person{} err = proto.Unmarshal(data, newdata) if err != nil { fmt.Println("unmarshal err:", err) } fmt.Println(newdata) } ```