struct {int}和struct {int int}有什么区别? [英] What is the difference between struct{int} and struct{int int}?

查看:218
本文介绍了struct {int}和struct {int int}有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  package main 
导入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
}

它们看起来一样:

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

但如果您取消注释 a = b ,则表示:

  $ go运行a.go 
#命令行参数
./a.go:10:不能使用b(类型struct {int int})作为类型struct {int}赋值

然而 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
}


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


$ b struct {int int} 是一个结构, int



c $ c> int int 不是关键字;它可以用作标识符。



结构类型不相同;

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

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




Go编程语言规范



< a struct href =http://golang.org/ref/spec#Struct_types>结构类型



结构是一系列命名元素,称为字段,每个
都有一个名称和一个类型。字段名称可以显式指定
(IdentifierList)或隐式指定(AnonymousField)。在结构中,
非空白字段名必须是唯一的。

  StructType =struct{{ FieldDecl; }}。 
FieldDecl =(IdentifierList Type | AnonymousField)[Tag]。
AnonymousField = [*] TypeName。

使用类型但没有显式字段名称声明的字段是
anonymous字段,也是称为嵌入式字段,或者在结构中嵌入
类型。嵌入类型必须被指定为类型名称
T或者指向非接口类型名称* T,并且T本身可以
不是指针类型。非限定类型名称充当字段
名称。



关键字



以下关键字是保留字,不得用作
标识符。

  break默认func接口选择
大小写延迟转换映射struct b $ b chan else转换包转换
如果范围类型为
则转入通过导入返回var

类型标识符



如果两个结构类型具有相同的
字段序列,并且相同的字段具有相同的名称并且完全相同
类型和相同的标签。两个匿名字段被认为是同名的
。来自不同包裹的小写字段名称始终不同。



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
}

?

解决方案

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.

这篇关于struct {int}和struct {int int}有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆