Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何迭代google.protobuf.ListValue_Go_Iteration_Protocol Buffers_Grpc - Fatal编程技术网

如何迭代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"`
}