我是 grpc 的新手,我已经使用名为 Accounts 的服务创建了 proto 文件,方法名称为 GetValidators,我想在使用 (gogoproto.customtype) 的消息中创建指向 BondedValidators 和 UnbondingValidators 变量的指针 []*。
syntax = 'proto3';
package grpc;
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "google/protobuf/struct.proto";
option (gogoproto.marshaler_all) = false;
option (gogoproto.unmarshaler_all) = false;
option (gogoproto.sizer_all) = false;
option (gogoproto.goproto_registration) = true;
option (gogoproto.messagename_all) = true;
option (gogoproto.protosizer_all) =false;
service Accounts {
rpc GetValidators(Empty) returns (ValidatorOutput);
}
message ValidatorOutput {
uint64 BlockHeight = 1 ;
repeated google.protobuf.ListValue BondedValidators = 2 [(gogoproto.customtype) = "github.com/gallactic/gallactic/core/validator.Validator", (gogoproto.nullable) = false];
repeated google.protobuf.ListValue UnbondingValidators = 3 [(gogoproto.customtype) = "github.com/gallactic/gallactic/core/validator.Validator", (gogoproto.nullable) = false];
}
每当使用以下代码时:
protoc -I/usr/local/include -I. -I$GOPATH/src-I$GOPATH/src/github.com/grpcecosystem/grpc-gateway/third_party/googleapis --gofast_out=plugins=grpc:./ ./protobuf/account.proto
它在 .pb.go 文件中生成输出,带有三个额外的变量
type ValidatorOutput struct {
BlockHeight uint64 `protobuf:"varint,1,opt,name=BlockHeight,proto3" json:"BlockHeight,omitempty"`
BondedValidators []github_com_gallactic_gallactic_core_validator.Validator `protobuf:"bytes,2,rep,name=BondedValidators,customtype=github.com/gallactic/gallactic/core/validator.Validator" json:"BondedValidators"`
UnbondingValidators []github_com_gallactic_gallactic_core_validator.Validator `protobuf:"bytes,3,rep,name=UnbondingValidators,customtype=github.com/gallactic/gallactic/core/validator.Validator" json:"UnbondingValidators"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
输出应该是
type ValidatorOutput struct {
BlockHeight uint64 `protobuf:"varint,1,opt,name=BlockHeight,proto3" json:"BlockHeight,omitempty"`
BondedValidators []*github_com_gallactic_gallactic_core_validator.Validator `protobuf:"bytes,2,rep,name=BondedValidators,customtype=github.com/gallactic/gallactic/core/validator.Validator" json:"BondedValidators"`
UnbondingValidators []*github_com_gallactic_gallactic_core_validator.Validator `protobuf:"bytes,3,rep,name=UnbondingValidators,customtype=github.com/gallactic/gallactic/core/validator.Validator" json:"UnbondingValidators"`
}
最佳答案
gogo proto 将在您使用 nullable=false
选项时删除指针,因此您需要删除 (gogoproto.nullable) = false
选项proto 定义让它生成 []*foo
关于go - 使用 go lang 在 protobuf 中创建数组指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52724781/