1 回答

TA貢獻2019條經(jīng)驗 獲得超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 關(guān)注
- 94 瀏覽
添加回答
舉報