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.
a.int
和b.int
都可以工作呢? - Dogint
可以用来访问字段,但它不是字段的名称,就像struct{ a int }
中的a
是字段的名称一样(因此不是类型相等的目的)。 - joshlfstruct{int}=struct{int int}
。但我认为这里没有足够的信息来确定。无论哪种情况,我想知道这两种类型实际上有什么不同,除了编译器认为它们不同之外。 - Dogstruct { int }
与struct { int int }
并不相同,它们并不完全相同。匿名字段(明确无名称)不能与显式命名字段具有完全相同的名称。按设计,Go编程语言简单、直观和正交。该规范仅约50页长。在Go中,类型标识仅意味着在所有方面都相同。没有理由使它变得更加复杂。 - peterSO