简体   繁体   English

使用协议缓冲区错误构建:struct initializer中的值太少

[英]Go build with protocol buffer error: too few values in struct initializer

I have a proto file: 我有一个原型文件:

syntax = "proto3";

package main;

message Client {
    int32 Id = 1;
    string Name = 2;
    string Email = 3;
}

The compiled Client struct like below: 编译后的Client结构如下:

type Client struct {
    Id                   int32    `protobuf:"varint,1,opt,name=Id,proto3" json:"Id,omitempty"`
    Name                 string   `protobuf:"bytes,2,opt,name=Name,proto3" json:"Name,omitempty"`
    Email                string   `protobuf:"bytes,3,opt,name=Email,proto3" json:"Email,omitempty"`
    XXX_NoUnkeyedLiteral struct{} `json:"-"`
    XXX_unrecognized     []byte   `json:"-"`
    XXX_sizecache        int32    `json:"-"`
}

When I am trying to init this Client struct like below: 当我尝试初始化此Client结构时,如下所示:

client := &Client{123, "John", "john@aol.com"}

I am getting building error: too few values in struct initializer . 我正在构建错误: too few values in struct initializer I found a way to fix it by adding XXX_NoUnkeyedLiteral , XXX_unrecognized , XXX_sizecache . 我找到了一种方法来修复它,添加XXX_NoUnkeyedLiteralXXX_unrecognizedXXX_sizecache I don't know what these are, and wondering if this is the right way to do it: 我不知道这些是什么,并想知道这是否是正确的方法:

client := &Client{123, "John", "john@aol.com", struct{}{}, []byte{}, int32(0)}

In struct composite literals you may omit the field names to which you list values for (this is called unkeyed literal ), but then you have to list initial values for all fields and in their declaration order. 在struct composite literals中,您可以省略列出值的字段名称(这称为unkeyed literal ),但是您必须列出所有字段的初始值并按其声明顺序列出。 Or you may use a keyed literal where you explicitly state which fields you specify initial values for. 或者,您可以使用键控文字,其中您明确说明了为其指定初始值的字段。 In the latter, you are allowed to omit any of the fields, you may just list the ones you want to give an initial value different from the field's zero value ). 在后者中,您可以省略任何字段,您可以只列出要提供与字段的零值不同的初始值的字段。

You used unkeyed composite literal, in which case you must list values for all fields, which you didn't. 您使用了未加密的复合文字,在这种情况下,您必须列出所有字段的值,而您没有。 This is what the error message tells you: "too few values in struct initializer" . 这是错误消息告诉您的: “struct initializer中的值太少”

The field name (generated by protobuf) itself should give you the hint: XXX_NoUnkeyedLiteral . 字段名称(由protobuf生成)本身应该为您提供提示: XXX_NoUnkeyedLiteral It suggests you should not use a composite literal without keys. 它建议你不要使用没有键的复合文字。

So use a composite literal with keys , like this:: 所以使用带有键的复合文字,比如::

client := &Client{
    Id:    123,
    Name:  "John",
    Email: "john@aol.com",
}

This form is more readable, and it is immune to struct changes. 这种形式更具可读性,并且不受结构变化的影响。 Eg if the Client struct would get new fields, or fields would get rearranged, this code would still be valid and compile. 例如,如果Client结构将获得新字段,或者字段将重新排列,则此代码仍然有效并编译。

Add field name before the value can solve the building error, as 在值可以解决构建错误之前添加字段名称,如

client := &Client{Id: 123, Name: "John", Email: "john@aol.com"}

I find this out by checking the grpc golang example , but maybe somebody can explain why? 我通过检查grpc golang示例找到了这个,但也许有人可以解释为什么? ;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM