1
0
mirror of https://github.com/golang/go synced 2024-11-21 21:54:40 -07:00

- 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
This commit is contained in:
Robert Griesemer 2009-11-16 08:58:55 -08:00
parent 0660d243b1
commit d3b1565716

View File

@ -694,17 +694,19 @@ new([100]int)[0:50]
<h3 id="Struct_types">Struct types</h3> <h3 id="Struct_types">Struct types</h3>
<p> <p>
A struct is a sequence of named A struct is a sequence of named elements, called fields, each of which has a
elements, called fields, with various types. A struct type declares name and a type. Field names may be specified explicitly (IdentifierList) or
an identifier and type for each field. Within a struct, non-<a href="#Blank_identifier">blank</a> implicitly (AnonymousField).
field identifiers must be unique. Within a struct, non-<a href="#Blank_identifier">blank</a> field names must
be unique.
</p> </p>
<pre class="ebnf"> <pre class="ebnf">
StructType = "struct" "{" [ FieldDeclList ] "}" . StructType = "struct" "{" [ FieldDeclList ] "}" .
FieldDeclList = FieldDecl { ";" FieldDecl } [ ";" ] . FieldDeclList = FieldDecl { ";" FieldDecl } [ ";" ] .
FieldDecl = (IdentifierList Type | [ "*" ] TypeName) [ Tag ] . FieldDecl = (IdentifierList Type | AnonymousField) [ Tag ] .
Tag = StringLit . AnonymousField = [ "*" ] TypeName .
Tag = StringLit .
</pre> </pre>
<pre> <pre>
@ -722,28 +724,27 @@ struct {
</pre> </pre>
<p> <p>
A field declared with a type but no field identifier is an <i>anonymous field</i>. A field declared with a type but no explicit field name is an <i>anonymous field</i>.
Such a field type must be specified as Such a field type must be specified as
a type name <code>T</code> or as a pointer to a type name <code>*T</code>, a type name <code>T</code> or as a pointer to a type name <code>*T</code>,
and <code>T</code> itself may not be and <code>T</code> itself may not be
a pointer type. The unqualified type name acts as the field identifier. a pointer type. The unqualified type name acts as the field name.
</p> </p>
<pre> <pre>
// A struct with four anonymous fields of type T1, *T2, P.T3 and *P.T4 // A struct with four anonymous fields of type T1, *T2, P.T3 and *P.T4
struct { struct {
T1; // the field name is T1 T1; // field name is T1
*T2; // the field name is T2 *T2; // field name is T2
P.T3; // the field name is T3 P.T3; // field name is T3
*P.T4; // the field name is T4 *P.T4; // field name is T4
x, y int; x, y int; // field names are x and y
} }
</pre> </pre>
<p> <p>
The unqualified type name of an anonymous field must be distinct from the The following declaration is illegal because field names must be unique
field identifier (or unqualified type name for an anonymous field) of every in a struct type:
other field within the struct. The following declaration is illegal:
</p> </p>
<pre> <pre>
@ -778,7 +779,7 @@ a type named <code>T</code>:
</ul> </ul>
<p> <p>
A field declaration may be followed by an optional string literal <i>tag</i>, A field declaration may be followed by an optional string literal <i>tag</i>,
which becomes an attribute for all the identifiers in the corresponding which becomes an attribute for all the fields in the corresponding
field declaration. The tags are made field declaration. The tags are made
visible through a <a href="#Package_unsafe">reflection interface</a> visible through a <a href="#Package_unsafe">reflection interface</a>
but are otherwise ignored. but are otherwise ignored.
@ -1915,6 +1916,8 @@ constant key value.
For struct literals the following rules apply: For struct literals the following rules apply:
</p> </p>
<ul> <ul>
<li>A key must be a field name declared in the LiteralType.
</li>
<li>A literal that does not contain any keys must <li>A literal that does not contain any keys must
list an element for each struct field in the list an element for each struct field in the
order in which the fields are declared. order in which the fields are declared.