简体   繁体   English

结构初始化:pU 文字中的值太少或 pU 文字中未导出字段“c”的隐式赋值?

[英]struct initialization: too few values in p.U literal or implicit assignment of unexported field 'c' in p.U literal?

I am sorry for that simple question I have the following code对于这个简单的问题,我很抱歉我有以下代码

// package p
package p
// U type 
type U struct {
    A, B int
    c    int
} // A and B are exported only
// main
package main

import (
    "fmt"
    "./p"
)

func main() {
    pp := p.U{A: 3, B: 4}
    uu := p.U{3, 5}    // the error resulted from this line
    fmt.Println(pp)
    fmt.Println(uu)
}

when I try to compile I get an error: too few values in pU literal .当我尝试编译时出现错误: pU literal 中的值太少 I'm expecting that there no need to add c value.我期望不需要添加c值。 when I try to add the c value I get another expected error: implicit assignment of unexported field 'c' in pU literal当我尝试添加c值时,我得到另一个预期错误: pU 文字中未导出字段“c”的隐式分配

You must keep the following rules in mind while creating a struct literal:创建结构文字时必须牢记以下规则:

  1. A key must be a field name declared in the struct type.键必须是在结构类型中声明的字段名称。
  2. An element list that does not contain any keys must list an element for each struct field in the order in which the fields are declared.不包含任何键的元素列表必须按照字段声明的顺序为每个结构字段列出一个元素。
  3. An element list that contains keys does not need to have an element for each struct field.包含键的元素列表不需要每个结构字段都有一个元素。 Omitted fields get the zero value for that field.省略的字段获得该字段的零值。
  4. A literal may omit the element list;文字可以省略元素列表; such a literal evaluates to the zero value for its type.这样的文字对其类型求值为零值。
  5. It is an error to specify an element for a non-exported field of a struct belonging to a different package.为属于不同 package 的结构的非导出字段指定元素是错误的。

In golang, there's to ways to instantiate structs: with keys and without keys.在 golang 中,有多种方法来实例化结构:有键和无键。

When instantiating with keys, you would write the value of each field name next to the field name like such:使用键实例化时,您可以在字段名称旁边写下每个字段名称的值,如下所示:


type Employee struct {

name string
age int
boss bool

}

employee := Employee{name: "John", age: 30}

When instantiating without keys, you just write the value of each field without writing the field names like such:在没有键的情况下实例化时,您只需编写每个字段的值而不用像这样编写字段名称:

type Employee struct{

name string
age int
boss bool

}

employee := Employee{"John", 30, false}

As you might've noticed, when instantiating with keys, you don't have to specify a value for each field (you don't have to include a value for boss).您可能已经注意到,在使用键进行实例化时,您不必为每个字段指定一个值(您不必为 boss 包含一个值)。 This is because, since you're only giving values to specific fields, golang can assume the values for the other fields.这是因为,由于您只为特定字段赋值,golang 可以假定其他字段的值。

On the other hand, when instantiating without keys, you do have to specify a value for each field since, otherwise, if you didn't specify a value for each field, golang would not be able to assume which value goes to which field.另一方面,在没有键的情况下实例化时,您必须为每个字段指定一个值,否则,如果您没有为每个字段指定一个值,golang 将无法假定哪个值进入哪个字段。

So, long story short, you can only instantiate structs without keys if you specify values for each field.因此,长话短说,如果为每个字段指定值,则只能实例化没有键的结构。 Otherwise, you have to use keys and allow golang to assume the default values for the other fields.否则,您必须使用键并允许 golang 假定其他字段的默认值。

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

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