What is the difference between struct{int} and struct{int int}?

huangapple go评论86阅读模式
英文:

What is the difference between struct{int} and struct{int int}?

问题

这两个结构体的区别是什么,除了它们不被认为是等价的?

它们看起来是一样的:

package main
import "fmt"
func main() {
    a := struct{int}{1}
    b := struct{int int}{1}
    fmt.Println(a,b)
    a.int=2
    b.int=a.int
    fmt.Println(a,b)
    //a = b
}

输出结果为:

{1} {1}
{2} {2}

但是如果你取消注释a = b,会得到以下错误信息:

# command-line-arguments
./a.go:10: cannot use b (type struct { int int }) as type struct { int } in assignment

然而,struct{a,b int}struct{a int;b int}是等价的:

package main

func main() {
    a := struct{a,b int}{1,2}
    b := struct{a int;b int}{1,2}
    a = b
    b = a
}
英文:

What is the difference between these two structs other than that they aren't considered equivalent?

package main
import "fmt"
func main() {
    a := struct{int}{1}
    b := struct{int int}{1}
    fmt.Println(a,b)
    a.int=2
    b.int=a.int
    fmt.Println(a,b)
    //a = b
}

They look the same:

$ go run a.go 
{1} {1}
{2} {2}

But if you uncomment a = b, it says:

$ go run a.go 
# command-line-arguments
./a.go:10: cannot use b (type struct { int int }) as type struct { int } in assignment

Yet struct{a,b int} and struct{a int;b int} are equivalent:

package main

func main() {
    a := struct{a,b int}{1,2}
    b := struct{a int;b int}{1,2}
    a = b
    b = a
}

?

答案1

得分: 9

struct { int } 是一个具有匿名字段类型为 int 的结构体。

struct { int int } 是一个具有名为 int 类型为 int 的字段的结构体。

int 不是一个关键字;它可以用作标识符。

这两个结构体类型不相同;相应的字段没有相同的名称。

使用类型但没有显式字段名声明的字段是匿名字段,未限定的类型名称充当匿名字段名。因此,字段名 a.intb.int 是有效的。例如,

a := struct{ int }{1}
b := struct{ int int }{1}
a.int = 2
b.int = a.int

> Go编程语言规范
>
> 结构体类型
>
> 结构体是一系列具有名称和类型的命名元素,称为字段。字段名可以显式指定(IdentifierList)或隐式指定(AnonymousField)。在结构体内部,非空白字段名必须是唯一的。
>
> StructType = "struct" "{" { FieldDecl ";" } "}" .
> FieldDecl = (IdentifierList Type | AnonymousField) [ Tag ] .
> AnonymousField = [ "*" ] TypeName .
>
> 使用类型但没有显式字段名声明的字段是匿名字段,也称为嵌入字段或将类型嵌入结构体中。嵌入类型必须指定为类型名称 T 或非接口类型名称 *T 的指针,并且 T 本身不能是指针类型。未限定的类型名称充当字段名。
>
> 关键字
>
> 以下关键字是保留的,不能用作标识符。
>
> break default func interface select
> case defer go map struct
> chan else goto package switch
> const fallthrough if range type
> continue for import return var
>
> 类型标识
>
> 如果两个结构体类型具有相同的字段序列,并且相应的字段具有相同的名称、相同的类型和相同的标签,则它们是相同的。两个匿名字段被认为具有相同的名称。来自不同包的小写字段名始终是不同的。

英文:

struct { int } is a struct with an anonymous field of type int.

struct { int int } is a struct with a field named int of type int.

int is not a keyword; it can be used as an identifier.

The struct types are not identical; the corresponding fields do not have the same names.

A field declared with a type but no explicit field name is an anonymous field and the unqualified type name acts as the anonymous field name. Therefore, field names a.int and b.int are valid. For example,

a := struct{ int }{1}
b := struct{ int int }{1}
a.int = 2
b.int = a.int

> The Go Programming Language Specification
>
> Struct types
>
> A struct is a sequence of named elements, called fields, each of which
> has a name and a type. Field names may be specified explicitly
> (IdentifierList) or implicitly (AnonymousField). Within a struct,
> non-blank field names must be unique.
>
> StructType = "struct" "{" { FieldDecl ";" } "}" .
> FieldDecl = (IdentifierList Type | AnonymousField) [ Tag ] .
> AnonymousField = [ "*" ] TypeName .
>
> A field declared with a type but no explicit field name is an
> anonymous field, also called an embedded field or an embedding of the
> type in the struct. An embedded type must be specified as a type name
> T or as a pointer to a non-interface type name *T, and T itself may
> not be a pointer type. The unqualified type name acts as the field
> name.
>
> Keywords
>
> The following keywords are reserved and may not be used as
> identifiers.
>
> break default func interface select
> case defer go map struct
> chan else goto package switch
> const fallthrough if range type
> continue for import return var
>
> Type identity
>
> Two struct types are identical if they have the same sequence of
> fields, and if corresponding fields have the same names, and identical
> types, and identical tags. Two anonymous fields are considered to have
> the same name. Lower-case field names from different packages are
> always different.

huangapple
  • 本文由 发表于 2014年8月27日 05:44:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/25515653.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定