@@ -694,17 +694,19 @@ <h3 id="Slice_types">Slice types</h3>
694
694
< h3 id ="Struct_types "> Struct types</ h3 >
695
695
696
696
< p >
697
- A struct is a sequence of named
698
- elements, called fields, with various types. A struct type declares
699
- an identifier and type for each field. Within a struct, non-< a href ="#Blank_identifier "> blank</ a >
700
- field identifiers must be unique.
697
+ A struct is a sequence of named elements, called fields, each of which has a
698
+ name and a type. Field names may be specified explicitly (IdentifierList) or
699
+ implicitly (AnonymousField).
700
+ Within a struct, non-< a href ="#Blank_identifier "> blank</ a > field names must
701
+ be unique.
701
702
</ p >
702
703
703
704
< pre class ="ebnf ">
704
- StructType = "struct" "{" [ FieldDeclList ] "}" .
705
- FieldDeclList = FieldDecl { ";" FieldDecl } [ ";" ] .
706
- FieldDecl = (IdentifierList Type | [ "*" ] TypeName) [ Tag ] .
707
- Tag = StringLit .
705
+ StructType = "struct" "{" [ FieldDeclList ] "}" .
706
+ FieldDeclList = FieldDecl { ";" FieldDecl } [ ";" ] .
707
+ FieldDecl = (IdentifierList Type | AnonymousField) [ Tag ] .
708
+ AnonymousField = [ "*" ] TypeName .
709
+ Tag = StringLit .
708
710
</ pre >
709
711
710
712
< pre >
@@ -722,28 +724,27 @@ <h3 id="Struct_types">Struct types</h3>
722
724
</ pre >
723
725
724
726
< p >
725
- A field declared with a type but no field identifier is an < i > anonymous field</ i > .
727
+ A field declared with a type but no explicit field name is an < i > anonymous field</ i > .
726
728
Such a field type must be specified as
727
729
a type name < code > T</ code > or as a pointer to a type name < code > *T</ code > ,
728
730
and < code > T</ code > itself may not be
729
- a pointer type. The unqualified type name acts as the field identifier .
731
+ a pointer type. The unqualified type name acts as the field name .
730
732
</ p >
731
733
732
734
< pre >
733
735
// A struct with four anonymous fields of type T1, *T2, P.T3 and *P.T4
734
736
struct {
735
- T1; // the field name is T1
736
- *T2; // the field name is T2
737
- P.T3; // the field name is T3
738
- *P.T4; // the field name is T4
739
- x, y int;
737
+ T1; // field name is T1
738
+ *T2; // field name is T2
739
+ P.T3; // field name is T3
740
+ *P.T4; // field name is T4
741
+ x, y int; // field names are x and y
740
742
}
741
743
</ pre >
742
744
743
745
< p >
744
- The unqualified type name of an anonymous field must be distinct from the
745
- field identifier (or unqualified type name for an anonymous field) of every
746
- other field within the struct. The following declaration is illegal:
746
+ The following declaration is illegal because field names must be unique
747
+ in a struct type:
747
748
</ p >
748
749
749
750
< pre >
@@ -778,7 +779,7 @@ <h3 id="Struct_types">Struct types</h3>
778
779
</ ul >
779
780
< p >
780
781
A field declaration may be followed by an optional string literal < i > tag</ i > ,
781
- which becomes an attribute for all the identifiers in the corresponding
782
+ which becomes an attribute for all the fields in the corresponding
782
783
field declaration. The tags are made
783
784
visible through a < a href ="#Package_unsafe "> reflection interface</ a >
784
785
but are otherwise ignored.
@@ -1915,6 +1916,8 @@ <h3 id="Composite_literals">Composite literals</h3>
1915
1916
For struct literals the following rules apply:
1916
1917
</ p >
1917
1918
< ul >
1919
+ < li > A key must be a field name declared in the LiteralType.
1920
+ </ li >
1918
1921
< li > A literal that does not contain any keys must
1919
1922
list an element for each struct field in the
1920
1923
order in which the fields are declared.
0 commit comments