3 回答

TA貢獻1802條經驗 獲得超4個贊
即使未設置字段,Proto3 也會返回零值。目前無法區分某個字段是否已設置。
可能的解決方案:
使用proto2而不是proto3。
使用 gogoproto 的可為空擴展。
使用
google.protobuf.FieldMask
擴展,請參閱 Google API 設計指南中的常見設計模式:部分響應和輸出字段。

TA貢獻1911條經驗 獲得超7個贊
就我而言,我用幾個包解決了這個問題:
我的原型文件如下所示:
syntax = "proto3";
import "google/protobuf/wrappers.proto";
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
message Message {
google.protobuf.StringValue path = 1 [(gogoproto.wktpointer) = true];
}
我使用的生成 go 代碼的命令如下所示:
protoc -I. -I%GOPATH%/src --gogofaster_out=plugins=grpc:. proto/*.proto
生成的go文件如下所示:
type Message struct {
Path *string `protobuf:"bytes,1,opt,name=path,json=path,proto3,wktptr" json:"path,omitempty"`
}
- 3 回答
- 0 關注
- 366 瀏覽
添加回答
舉報