阅读这里: https://github.com/protocolbuffers/protobuf/blob/master/docs/field_presence.md#go-example 高浪的例子:
m := GetProto()
if (m.HasFoo()) {
// Clear the field:
m.Foo = nil
} else {
// Field is not present, so set it.
m.Foo = proto.Int32(1);
}
如果我使用:
protoc pkg/user.proto --go_out=. --go_opt=module=my_app --go-grpc_out=. --go-grpc_opt=module=my_app
跟:
syntax = "proto3";
package example;
message MyPlayer {
uint64 id = 1;
optional string description = 2;
uint32 qty = 3;
optional uint64 age = 4;
}
它不会生成任何方法。has<T>()
为什么?
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.26.0
// protoc v3.17.3
如果我使用生成的原型字段而不是方法,我错了吗?MyPlayer
例:
if MyPlayer.description != nil {
description = *MyPlayer.description
}
而不是
description = MyPlayer.GetDescription()
这不是我想要的(我想检测零值)。
Qyouu
相关分类