Go build with protocol buffer error: too few values in struct initializer
Go build with protocol buffer error: too few values in struct initializer
我有一个原型文件:
syntax = "proto3";
package main;
message Client {
int32 Id = 1;
string Name = 2;
string Email = 3;
}
编译后的 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:"-"`
}
当我尝试初始化这个 Client
结构时,如下所示:
client := &Client{123, "John", "john@aol.com"}
我遇到构建错误:too few values in struct initializer
。我找到了一种通过添加 XXX_NoUnkeyedLiteral
、XXX_unrecognized
、XXX_sizecache
来修复它的方法。我不知道这些是什么,想知道这样做是否正确:
client := &Client{123, "John", "john@aol.com", struct{}{}, []byte{}, int32(0)}
在结构 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. 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).
您使用了无键复合文字,在这种情况下,您必须列出所有字段的值,而您没有这样做。这是错误消息告诉您的内容:"too few values in struct initializer".
字段名称(由 protobuf 生成)本身应该给你提示:XXX_NoUnkeyedLiteral
。它建议你不应该使用没有键的复合文字。
所以使用复合文字和键,像这样::
client := &Client{
Id: 123,
Name: "John",
Email: "john@aol.com",
}
这种形式更具可读性,并且不受结构变化的影响。例如。如果 Client
结构将获得新字段,或者字段将重新排列,此代码仍然有效并可以编译。
在值前加上字段名可以解决构建错误,如
client := &Client{Id: 123, Name: "John", Email: "john@aol.com"}
我通过检查 grpc golang example 发现了这一点,但也许有人可以解释为什么? ;)
我有一个原型文件:
syntax = "proto3";
package main;
message Client {
int32 Id = 1;
string Name = 2;
string Email = 3;
}
编译后的 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:"-"`
}
当我尝试初始化这个 Client
结构时,如下所示:
client := &Client{123, "John", "john@aol.com"}
我遇到构建错误:too few values in struct initializer
。我找到了一种通过添加 XXX_NoUnkeyedLiteral
、XXX_unrecognized
、XXX_sizecache
来修复它的方法。我不知道这些是什么,想知道这样做是否正确:
client := &Client{123, "John", "john@aol.com", struct{}{}, []byte{}, int32(0)}
在结构 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. 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).
您使用了无键复合文字,在这种情况下,您必须列出所有字段的值,而您没有这样做。这是错误消息告诉您的内容:"too few values in struct initializer".
字段名称(由 protobuf 生成)本身应该给你提示:XXX_NoUnkeyedLiteral
。它建议你不应该使用没有键的复合文字。
所以使用复合文字和键,像这样::
client := &Client{
Id: 123,
Name: "John",
Email: "john@aol.com",
}
这种形式更具可读性,并且不受结构变化的影响。例如。如果 Client
结构将获得新字段,或者字段将重新排列,此代码仍然有效并可以编译。
在值前加上字段名可以解决构建错误,如
client := &Client{Id: 123, Name: "John", Email: "john@aol.com"}
我通过检查 grpc golang example 发现了这一点,但也许有人可以解释为什么? ;)