1 回答

TA贡献1786条经验 获得超11个赞
该文档是错误的,如此处所述:https://github.com/golang/protobuf/issues/1336:
有关 https://github.com/protocolbuffers/protobuf/blob/master/docs/field_presence.md#go-example 的文档不正确。在 proto3 中使用“可选”可以生成字段,就像在 proto2 中一样。
该文档是错误的。生成的代码中没有方法。通过将字段与 进行比较来测试是否存在。
Has
nil
正确重写这些示例:
// Field foo does not have presence.
// If field foo is not 0, set it to 0.
// If field foo is 0, set it to 1.
m := GetProto()
if m.Foo != 0 {
// "Clear" the field:
m.Foo = 0
} else {
// Default value: field may not have been present.
m.Foo = 1
}
// Field foo has presence.
// If foo is set, clear it.
// If foo is not set, set it to 1.
m := GetProto()
if m.Foo != nil {
// Clear the field:
m.Foo = nil
} else {
// Field is not present, so set it.
m.Foo = proto.Int32(1)
}
PR来修复该文档:协议缓冲器/原型buff#8788
- 1 回答
- 0 关注
- 114 浏览
添加回答
举报