如何迭代google.protobuf.ListValue
我的协议缓冲区规范如下所示:如何迭代google.protobuf.ListValue,go,iteration,protocol-buffers,grpc,Go,Iteration,Protocol Buffers,Grpc,我的协议缓冲区规范如下所示: message CreateContextRequest { map<string, google.protobuf.ListValue> my_mapping = 2; } 1: fmt.Println("protocBuff = ", protocBuff); 2: fmt.Println("protocBuff.MyMapping = ", protocBuff.MyMapping); 3:
message CreateContextRequest {
map<string, google.protobuf.ListValue> my_mapping = 2;
}
1: fmt.Println("protocBuff = ", protocBuff);
2: fmt.Println("protocBuff.MyMapping = ", protocBuff.MyMapping);
3: for myKey, myListValue := range protocBuff.MyMapping {
4: fmt.Println("myKey:", myKey, "=>", "myListValue:", myListValue)
5: for _, element := range myListValue {
6: fmt.Printf("element = ", element)
7: }
8: }
第1-4行工作正常。但是第5行给出了编译时错误:不能超过myListValue(type*structpb.ListValue)
那么如何迭代myListValue呢?的定义(删除了私有字段)是:
因此,要迭代此过程,您可以使用:
for,元素:=范围myListValue.Values
(检查对于元素:=范围myListValue.GetValues()
nil
)myListValue
(可能更好,取决于您对这些值所做的操作)对于u,element:=range myListValue.AsSlice()
type ListValue struct {
// Repeated field of dynamically typed values.
Values []*Value `protobuf:"bytes,1,rep,name=values,proto3" json:"values,omitempty"`
}