1 回答
TA贡献2019条经验 获得超9个赞
看起来您正在使用已弃用 github.com/golang/protobuf。使用 google.golang.org/protobuf/encoding/prototext
UPD:使用丢弃未知
(prototext.UnmarshalOptions{DiscardUnknown: true}).Unmarshal(b, msg)
pb.proto:
syntax = "proto3";
// protoc --go_out=. *.proto
package pb;
option go_package = "./pb";
message RequestOld {
uint64 account_id = 1;
message Foo{
uint64 foo_id = 1;
}
repeated Foo foos = 2;
}
message RequestNew {
uint64 account_id = 1;
message Foo{
uint64 foo_id = 1;
uint64 bar_id = 2;
}
repeated Foo foos = 2;
}
功能:
import "google.golang.org/protobuf/encoding/prototext"
// marshal old message
msgOld := &pb.RequestOld{
AccountId: 1,
Foos: []*pb.RequestOld_Foo{
{
FooId: 2,
},
},
}
log.Println("old:", msgOld.String())
bOld, err := prototext.Marshal(msgOld)
if err != nil {
panic(err)
}
// unmarshal to new message
msgNew := &pb.RequestNew{}
if err := prototext.Unmarshal(bOld, msgNew); err != nil {
panic(err)
}
log.Println("new:", msgNew.String())
输出:
2021/04/07 old: account_id:1 foos:{foo_id:2}
2021/04/07 new: account_id:1 foos:{foo_id:2}
- 1 回答
- 0 关注
- 64 浏览
添加回答
举报