Skip to content

Commit d3b1565

Browse files
committed
- Clarify that struct composite literal keys are field names not selectors.
- Slight re-phrasing of struct type section since "field name" was not properly introduced. Fixes #164. R=r, rsc, iant https://golang.org/cl/155061
1 parent 0660d24 commit d3b1565

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

doc/go_spec.html

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -694,17 +694,19 @@ <h3 id="Slice_types">Slice types</h3>
694694
<h3 id="Struct_types">Struct types</h3>
695695

696696
<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.
701702
</p>
702703

703704
<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 .
708710
</pre>
709711

710712
<pre>
@@ -722,28 +724,27 @@ <h3 id="Struct_types">Struct types</h3>
722724
</pre>
723725

724726
<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>.
726728
Such a field type must be specified as
727729
a type name <code>T</code> or as a pointer to a type name <code>*T</code>,
728730
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.
730732
</p>
731733

732734
<pre>
733735
// A struct with four anonymous fields of type T1, *T2, P.T3 and *P.T4
734736
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
740742
}
741743
</pre>
742744

743745
<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:
747748
</p>
748749

749750
<pre>
@@ -778,7 +779,7 @@ <h3 id="Struct_types">Struct types</h3>
778779
</ul>
779780
<p>
780781
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
782783
field declaration. The tags are made
783784
visible through a <a href="#Package_unsafe">reflection interface</a>
784785
but are otherwise ignored.
@@ -1915,6 +1916,8 @@ <h3 id="Composite_literals">Composite literals</h3>
19151916
For struct literals the following rules apply:
19161917
</p>
19171918
<ul>
1919+
<li>A key must be a field name declared in the LiteralType.
1920+
</li>
19181921
<li>A literal that does not contain any keys must
19191922
list an element for each struct field in the
19201923
order in which the fields are declared.

0 commit comments

Comments
 (0)