Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.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
Pointers Can';由于自动生成指针,无法在消息中嵌入消息_Pointers_Go_Struct_Protocol Buffers - Fatal编程技术网

Pointers Can';由于自动生成指针,无法在消息中嵌入消息

Pointers Can';由于自动生成指针,无法在消息中嵌入消息,pointers,go,struct,protocol-buffers,Pointers,Go,Struct,Protocol Buffers,我有以下3条protobuf消息 message Item { uint32 ID = 1; string Name = 2; ... } message ItemIdsRequest{ string batchUUID = 1; repeated uint32 itemIds = 2; } message ItemsResponse{ string batchUUID = 1; repeated Item items = 2; }

我有以下3条protobuf消息

message Item {
    uint32 ID = 1;
    string Name = 2;
    ...
}

message ItemIdsRequest{
    string batchUUID = 1;
    repeated uint32 itemIds = 2;
}

message ItemsResponse{
    string batchUUID = 1;
    repeated Item items = 2;
}
函数检索项ID的列表,以便稍后获取其所有详细信息。这些ID与用于通过事件源聚合的
batchUUID一起存储在mssage
ItemIdsRequest

然后,一个函数从
messages.itemidRequest
中的int切片中检索所有详细信息,作为
[]messages.Item
。我将
batchUUID
从消息
itemdsrequest
复制到
消息中。ItemsResponse

但是,当我尝试将返回的
[]消息.Item
复制到belows消息中时,出现错误
无法使用items(type[]messages.Item作为type[]*Item)

我无法将函数更改为以下内容,因为该项是指针,而不是函数返回的项片段。我不能让函数返回“message.ItemsResponse”

TLDR:我有两个独立的protobuf结构。我正在尝试在
messages.ItemsResponse
items
属性中设置
[]messages.items
,但不允许设置,因为生成的protobuf代码使
messages.ItemsResponse
items
属性成为指针。当我编辑自动生成的代码并删除指针时。。。一切按计划进行

type ItemsResponse struct {
    BatchUUID            string   `protobuf:"bytes,1,opt,name=batchUUID,proto3" json:"batchUUID,omitempty"`
    Items                []*Item  `protobuf:"bytes,2,rep,name=items,proto3" json:"items,omitempty"`
    XXX_NoUnkeyedLiteral struct{} `json:"-"`
    XXX_unrecognized     []byte   `json:"-"`
    XXX_sizecache        int32    `json:"-"`
}
我不熟悉protobuf,也不擅长指针(希望成为),因此我还可以借助一些帮助来理解为什么它会自动生成为指针。

protobuf对于重复的消息字段,生成的Go代码将使用一段指针。这允许使用可选值,因为in-Go结构不能是
nil
,但指针可以

如果您有一个结构片,并且希望将其分配给作为指针片的变量或字段,则必须“手动”生成该值

使用一个简单的循环来执行此操作:

// returns []messages.Item
itemsPB, _ := api.getItems("", items.ItemIds...)

itemPtrs := make([]*messages.Item, len(itemsPB))
for i := range itemsPB {
    itemPtrs[i] = &itemsPB[i]
}

itemsResponse := &messages.ItemsResponse{
    BatchUUID: uuid.NewV4().String(),
    Items:     itemPtrs,
}
请注意,我们在上面组装的指针片段指向原始
itemsPB
片段的元素

如果修改
api.getItems()
以返回指针片(
[]*messages.Item
),则无需创建指针片即可分配该指针片

// returns []messages.Item
itemsPB, _ := api.getItems("", items.ItemIds...)

itemPtrs := make([]*messages.Item, len(itemsPB))
for i := range itemsPB {
    itemPtrs[i] = &itemsPB[i]
}

itemsResponse := &messages.ItemsResponse{
    BatchUUID: uuid.NewV4().String(),
    Items:     itemPtrs,
}