diff options
Diffstat (limited to 'doc/go_spec.html')
-rw-r--r-- | doc/go_spec.html | 404 |
1 files changed, 260 insertions, 144 deletions
diff --git a/doc/go_spec.html b/doc/go_spec.html index 3938ba3e6..bc9ec682a 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -1,6 +1,6 @@ <!--{ "Title": "The Go Programming Language Specification", - "Subtitle": "Version of May 8, 2013", + "Subtitle": "Version of Nov 13, 2013", "Path": "/ref/spec" }--> @@ -15,6 +15,7 @@ TODO [ ] need explicit language about the result type of operations [ ] should probably write something about evaluation order of statements even though obvious +[ ] in Selectors section, clarify what receiver value is passed in method invocations --> @@ -538,7 +539,7 @@ literal. <i>rune constants</i>, <i>integer constants</i>, <i>floating-point constants</i>, <i>complex constants</i>, -and <i>string constants</i>. Character, integer, floating-point, +and <i>string constants</i>. Rune, integer, floating-point, and complex constants are collectively called <i>numeric constants</i>. </p> @@ -639,10 +640,10 @@ expressions</a>. <p> A type determines the set of values and operations specific to values of that -type. A type may be specified by a -(possibly <a href="#Qualified_identifiers">qualified</a>) -<a href="#Type_declarations"><i>type name</i></a> or a <i>type literal</i>, -which composes a new type from previously declared types. +type. Types may be <i>named</i> or <i>unnamed</i>. Named types are specified +by a (possibly <a href="#Qualified_identifiers">qualified</a>) +<a href="#Type_declarations"><i>type name</i></a>; unnamed types are specified +using a <i>type literal</i>, which composes a new type from existing types. </p> <pre class="ebnf"> @@ -838,7 +839,7 @@ multi-dimensional types. <h3 id="Slice_types">Slice types</h3> <p> -A slice is a descriptor for a contiguous segment of an array and +A slice is a descriptor for a contiguous segment of an <i>underlying array</i> and provides access to a numbered sequence of elements from that array. A slice type denotes the set of all slices of arrays of its element type. The value of an uninitialized slice is <code>nil</code>. @@ -868,7 +869,7 @@ The array underlying a slice may extend past the end of the slice. The <i>capacity</i> is a measure of that extent: it is the sum of the length of the slice and the length of the array beyond the slice; a slice of length up to that capacity can be created by -<a href="#Slices"><i>slicing</i></a> a new one from the original slice. +<a href="#Slice_expressions"><i>slicing</i></a> a new one from the original slice. The capacity of a slice <code>a</code> can be discovered using the built-in function <a href="#Length_and_capacity"><code>cap(a)</code></a>. </p> @@ -878,26 +879,18 @@ A new, initialized slice value for a given element type <code>T</code> is made using the built-in function <a href="#Making_slices_maps_and_channels"><code>make</code></a>, which takes a slice type -and parameters specifying the length and optionally the capacity: +and parameters specifying the length and optionally the capacity. +A slice created with <code>make</code> always allocates a new, hidden array +to which the returned slice value refers. That is, executing </p> <pre> -make([]T, length) make([]T, length, capacity) </pre> <p> -A call to <code>make</code> allocates a new, hidden array to which the returned -slice value refers. That is, executing -</p> - -<pre> -make([]T, length, capacity) -</pre> - -<p> -produces the same slice as allocating an array and slicing it, so these two examples -result in the same slice: +produces the same slice as allocating an array and <a href="#Slice_expressions">slicing</a> +it, so these two expressions are equivalent: </p> <pre> @@ -909,8 +902,8 @@ new([100]int)[0:50] Like arrays, slices are always one-dimensional but may be composed to construct higher-dimensional objects. With arrays of arrays, the inner arrays are, by construction, always the same length; -however with slices of slices (or arrays of slices), the lengths may vary dynamically. -Moreover, the inner slices must be allocated individually (with <code>make</code>). +however with slices of slices (or arrays of slices), the inner lengths may vary dynamically. +Moreover, the inner slices must be initialized individually. </p> <h3 id="Struct_types">Struct types</h3> @@ -1016,6 +1009,7 @@ A field declaration may be followed by an optional string literal <i>tag</i>, which becomes an attribute for all the fields in the corresponding field declaration. The tags are made visible through a <a href="/pkg/reflect/#StructTag">reflection interface</a> +and take part in <a href="Type_identity">type identity</a> for structs but are otherwise ignored. </p> @@ -1357,9 +1351,9 @@ Two types are either <i>identical</i> or <i>different</i>. </p> <p> -Two named types are identical if their type names originate in the same +Two <a href="#Types">named types</a> are identical if their type names originate in the same <a href="#Type_declarations">TypeSpec</a>. -A named and an unnamed type are always different. Two unnamed types are identical +A named and an <a href="#Types">unnamed type</a> are always different. Two unnamed types are identical if the corresponding type literals are identical, that is, if they have the same literal structure and corresponding components have identical types. In detail: </p> @@ -1441,7 +1435,7 @@ A value <code>x</code> is <i>assignable</i> to a variable of type <code>T</code> <li> <code>x</code>'s type <code>V</code> and <code>T</code> have identical <a href="#Types">underlying types</a> and at least one of <code>V</code> -or <code>T</code> is not a named type. +or <code>T</code> is not a <a href="#Types">named type</a>. </li> <li> <code>T</code> is an interface type and @@ -1462,10 +1456,6 @@ by a value of type <code>T</code>. </li> </ul> -<p> -Any value may be assigned to the <a href="#Blank_identifier">blank identifier</a>. -</p> - <h2 id="Blocks">Blocks</h2> @@ -1510,13 +1500,23 @@ Blocks nest and influence <a href="#Declarations_and_scope">scoping</a>. <h2 id="Declarations_and_scope">Declarations and scope</h2> <p> -A declaration binds a non-<a href="#Blank_identifier">blank</a> -identifier to a constant, type, variable, function, or package. +A <i>declaration</i> binds a non-<a href="#Blank_identifier">blank</a> identifier to a +<a href="#Constant_declarations">constant</a>, +<a href="#Type_declarations">type</a>, +<a href="#Variable_declarations">variable</a>, +<a href="#Function_declarations">function</a>, +<a href="#Labeled_statements">label</a>, or +<a href="#Import_declarations">package</a>. Every identifier in a program must be declared. No identifier may be declared twice in the same block, and no identifier may be declared in both the file and package block. </p> +<p> +The <a href="#Blank_identifier">blank identifier</a> may be used like any other identifier +in a declaration, but it does not introduce a binding and thus is not declared. +</p> + <pre class="ebnf"> Declaration = ConstDecl | TypeDecl | VarDecl . TopLevelDecl = Declaration | FunctionDecl | MethodDecl . @@ -1524,15 +1524,15 @@ TopLevelDecl = Declaration | FunctionDecl | MethodDecl . <p> The <i>scope</i> of a declared identifier is the extent of source text in which -the identifier denotes the specified constant, type, variable, function, or package. +the identifier denotes the specified constant, type, variable, function, label, or package. </p> <p> -Go is lexically scoped using blocks: +Go is lexically scoped using <a href="#Blocks">blocks</a>: </p> <ol> - <li>The scope of a predeclared identifier is the universe block.</li> + <li>The scope of a <a href="#Predeclared_identifiers">predeclared identifier</a> is the universe block.</li> <li>The scope of an identifier denoting a constant, type, variable, or function (but not method) declared at top level (outside any @@ -1586,8 +1586,10 @@ the body of any nested function. <h3 id="Blank_identifier">Blank identifier</h3> <p> -The <i>blank identifier</i>, represented by the underscore character <code>_</code>, may be used in a declaration like -any other identifier but the declaration does not introduce a new binding. +The <i>blank identifier</i> is represented by the underscore character <code>_</code>. +It serves as an anonymous placeholder instead of a regular (non-blank) +identifier and has special meaning in <a href="#Declarations_and_scope">declarations</a>, +as an <a href="#Operands">operand</a>, and in <a href="#Assignments">assignments</a>. </p> @@ -1908,7 +1910,7 @@ ShortVarDecl = IdentifierList ":=" ExpressionList . </pre> <p> -It is a shorthand for a regular <a href="#Variable_declarations">variable declaration</a> +It is shorthand for a regular <a href="#Variable_declarations">variable declaration</a> with initializer expressions but no types: </p> @@ -1969,9 +1971,9 @@ a <a href="#Terminating_statements">terminating statement</a>. </p> <pre> -func findMarker(c <-chan int) int { +func findMarker(c <-chan int) int { for i := range c { - if x := <-c; isMarker(x) { + if x := <-c; isMarker(x) { return x } } @@ -2078,8 +2080,8 @@ operators and functions to operands. <p> Operands denote the elementary values in an expression. An operand may be a -literal, a (possibly <a href="#Qualified_identifiers">qualified</a>) identifier -denoting a +literal, a (possibly <a href="#Qualified_identifiers">qualified</a>) +non-<a href="#Blank_identifier">blank</a> identifier denoting a <a href="#Constant_declarations">constant</a>, <a href="#Variable_declarations">variable</a>, or <a href="#Function_declarations">function</a>, @@ -2087,6 +2089,11 @@ a <a href="#Method_expressions">method expression</a> yielding a function, or a parenthesized expression. </p> +<p> +The <a href="#Blank_identifier">blank identifier</a> may appear as an +operand only on the left-hand side of an <a href="#Assignments">assignment</a>. +</p> + <pre class="ebnf"> Operand = Literal | OperandName | MethodExpr | "(" Expression ")" . Literal = BasicLit | CompositeLit | FunctionLit . @@ -2244,7 +2251,7 @@ element index plus one. A slice literal has the form </pre> <p> -and is a shortcut for a slice operation applied to an array: +and is shorthand for a slice operation applied to an array: </p> <pre> @@ -2357,7 +2364,9 @@ PrimaryExpr = Selector = "." identifier . Index = "[" Expression "]" . -Slice = "[" [ Expression ] ":" [ Expression ] "]" . +Slice = "[" ( [ Expression ] ":" [ Expression ] ) | + ( [ Expression ] ":" Expression ":" Expression ) + "]" . TypeAssertion = "." "(" Type ")" . Call = "(" [ ArgumentList [ "," ] ] ")" . ArgumentList = ExpressionList [ "..." ] . @@ -2461,7 +2470,7 @@ is also a pointer to a struct, <code>x.y.z</code> is shorthand for <code>(*(*x).y).z</code>, and so on. If <code>x</code> contains an anonymous field of type <code>*A</code>, where <code>A</code> is also a struct type, -<code>x.f</code> is a shortcut for <code>(*x.A).f</code>. +<code>x.f</code> is shorthand for <code>(*x.A).f</code>. </p> <p> @@ -2507,13 +2516,6 @@ p.M0() // ((*p).T0).M0() </pre> -<!-- -<span class="alert"> -TODO: Specify what happens to receivers. -</span> ---> - - <h3 id="Index_expressions">Index expressions</h3> <p> @@ -2525,10 +2527,9 @@ a[x] </pre> <p> -denotes the element of the array, slice, string or map <code>a</code> indexed by <code>x</code>. -The value <code>x</code> is called the -<i>index</i> or <i>map key</i>, respectively. The following -rules apply: +denotes the element of the array, pointer to array, slice, string or map <code>a</code> indexed by <code>x</code>. +The value <code>x</code> is called the <i>index</i> or <i>map key</i>, respectively. +The following rules apply: </p> <p> @@ -2543,44 +2544,48 @@ If <code>a</code> is not a map: </ul> <p> -For <code>a</code> of type <code>A</code> or <code>*A</code> -where <code>A</code> is an <a href="#Array_types">array type</a>: +For <code>a</code> of <a href="#Array_types">array type</a> <code>A</code>: </p> <ul> <li>a <a href="#Constants">constant</a> index must be in range</li> - <li>if <code>a</code> is <code>nil</code> or if <code>x</code> is out of range at run time, + <li>if <code>x</code> is out of range at run time, a <a href="#Run_time_panics">run-time panic</a> occurs</li> <li><code>a[x]</code> is the array element at index <code>x</code> and the type of <code>a[x]</code> is the element type of <code>A</code></li> </ul> <p> -For <code>a</code> of type <code>S</code> where <code>S</code> is a <a href="#Slice_types">slice type</a>: +For <code>a</code> of <a href="#Pointer_types">pointer</a> to array type: +</p> +<ul> + <li><code>a[x]</code> is shorthand for <code>(*a)[x]</code></li> +</ul> + +<p> +For <code>a</code> of <a href="#Slice_types">slice type</a> <code>S</code>: </p> <ul> - <li>if the slice is <code>nil</code> or if <code>x</code> is out of range at run time, + <li>if <code>x</code> is out of range at run time, a <a href="#Run_time_panics">run-time panic</a> occurs</li> <li><code>a[x]</code> is the slice element at index <code>x</code> and the type of <code>a[x]</code> is the element type of <code>S</code></li> </ul> <p> -For <code>a</code> of type <code>T</code> -where <code>T</code> is a <a href="#String_types">string type</a>: +For <code>a</code> of <a href="#String_types">string type</a>: </p> <ul> <li>a <a href="#Constants">constant</a> index must be in range if the string <code>a</code> is also constant</li> <li>if <code>x</code> is out of range at run time, a <a href="#Run_time_panics">run-time panic</a> occurs</li> - <li><code>a[x]</code> is the byte at index <code>x</code> and the type of + <li><code>a[x]</code> is the non-constant byte value at index <code>x</code> and the type of <code>a[x]</code> is <code>byte</code></li> <li><code>a[x]</code> may not be assigned to</li> </ul> <p> -For <code>a</code> of type <code>M</code> -where <code>M</code> is a <a href="#Map_types">map type</a>: +For <code>a</code> of <a href="#Map_types">map type</a> <code>M</code>: </p> <ul> <li><code>x</code>'s type must be @@ -2623,7 +2628,15 @@ Assigning to an element of a <code>nil</code> map causes a </p> -<h3 id="Slices">Slices</h3> +<h3 id="Slice_expressions">Slice expressions</h3> + +<p> +Slice expressions construct a substring or slice from a string, array, pointer +to array, or slice. There are two variants: a simple form that specifies a low +and high bound, and a full form that also specifies a bound on the capacity. +</p> + +<h4>Simple slice expressions</h4> <p> For a string, array, pointer to array, or slice <code>a</code>, the primary expression @@ -2634,9 +2647,9 @@ a[low : high] </pre> <p> -constructs a substring or slice. The indices <code>low</code> and -<code>high</code> select which elements appear in the result. The result has -indices starting at 0 and length equal to +constructs a substring or slice. The <i>indices</i> <code>low</code> and +<code>high</code> select which elements of operand <code>a</code> appear +in the result. The result has indices starting at 0 and length equal to <code>high</code> - <code>low</code>. After slicing the array <code>a</code> </p> @@ -2663,30 +2676,87 @@ sliced operand: </p> <pre> -a[2:] // same a[2 : len(a)] +a[2:] // same as a[2 : len(a)] a[:3] // same as a[0 : 3] a[:] // same as a[0 : len(a)] </pre> <p> -For arrays or strings, the indices <code>low</code> and <code>high</code> are -<i>in range</i> if <code>0</code> <= <code>low</code> <= <code>high</code> <= <code>len(a)</code>, +If <code>a</code> is a pointer to an array, <code>a[low : high]</code> is shorthand for +<code>(*a)[low : high]</code>. +</p> + +<p> +For arrays or strings, the indices are <i>in range</i> if +<code>0</code> <= <code>low</code> <= <code>high</code> <= <code>len(a)</code>, otherwise they are <i>out of range</i>. For slices, the upper index bound is the slice capacity <code>cap(a)</code> rather than the length. A <a href="#Constants">constant</a> index must be non-negative and representable by a value of type <code>int</code>. -If both indices -are constant, they must satisfy <code>low <= high</code>. If <code>a</code> is <code>nil</code> -or if the indices are out of range at run time, a <a href="#Run_time_panics">run-time panic</a> occurs. +If both indices are constant, they must satisfy <code>low <= high</code>. +If the indices are out of range at run time, a <a href="#Run_time_panics">run-time panic</a> occurs. </p> <p> -If the sliced operand is a string or slice, the result of the slice operation -is a string or slice of the same type. +Except for <a href="#Constants">untyped strings</a>, if the sliced operand is a string or slice, +the result of the slice operation is a non-constant value of the same type as the operand. +For untyped string operands the result is a non-constant value of type <code>string</code>. If the sliced operand is an array, it must be <a href="#Address_operators">addressable</a> and the result of the slice operation is a slice with the same element type as the array. </p> +<p> +If the sliced operand of a valid slice expression is a <code>nil</code> slice, the result +is a <code>nil</code> slice. Otherwise, the result shares its underlying array with the +operand. +</p> + +<h4>Full slice expressions</h4> + +<p> +For an array, pointer to array, or slice <code>a</code> (but not a string), the primary expression +</p> + +<pre> +a[low : high : max] +</pre> + +<p> +constructs a slice of the same type, and with the same length and elements as the simple slice +expression <code>a[low : high]</code>. Additionally, it controls the resulting slice's capacity +by setting it to <code>max - low</code>. Only the first index may be omitted; it defaults to 0. +After slicing the array <code>a</code> +</p> + +<pre> +a := [5]int{1, 2, 3, 4, 5} +t := a[1:3:5] +</pre> + +<p> +the slice <code>t</code> has type <code>[]int</code>, length 2, capacity 4, and elements +</p> + +<pre> +t[0] == 2 +t[1] == 3 +</pre> + +<p> +As for simple slice expressions, if <code>a</code> is a pointer to an array, +<code>a[low : high : max]</code> is shorthand for <code>(*a)[low : high : max]</code>. +If the sliced operand is an array, it must be <a href="#Address_operators">addressable</a>. +</p> + +<p> +The indices are <i>in range</i> if <code>0 <= low <= high <= max <= cap(a)</code>, +otherwise they are <i>out of range</i>. +A <a href="#Constants">constant</a> index must be non-negative and representable by a value of type +<code>int</code>. +If multiple indices are constant, the constants that are present must be in range relative to each +other. +If the indices are out of range at run time, a <a href="#Run_time_panics">run-time panic</a> occurs. +</p> <h3 id="Type_assertions">Type assertions</h3> @@ -3240,7 +3310,7 @@ is also allowed and follows from the general rules above. </p> <pre> -const c = 3 < 4 // c is the untyped bool constant true +const c = 3 < 4 // c is the untyped bool constant true type MyBool bool var x, y int @@ -3280,7 +3350,10 @@ or an array indexing operation of an addressable array. As an exception to the addressability requirement, <code>x</code> may also be a (possibly parenthesized) <a href="#Composite_literals">composite literal</a>. +If the evaluation of <code>x</code> would cause a <a href="#Run_time_panics">run-time panic</a>, +then the evaluation of <code>&x</code> does too. </p> + <p> For an operand <code>x</code> of pointer type <code>*T</code>, the pointer indirection <code>*x</code> denotes the value of type <code>T</code> pointed @@ -3295,6 +3368,10 @@ will cause a <a href="#Run_time_panics">run-time panic</a>. &Point{2, 3} *p *pf(x) + +var x *int = nil +*x // causes a run-time panic +&*x // causes a run-time panic </pre> @@ -3307,9 +3384,8 @@ from the channel <code>ch</code>. The channel direction must permit receive oper and the type of the receive operation is the element type of the channel. The expression blocks until a value is available. Receiving from a <code>nil</code> channel blocks forever. -Receiving from a <a href="#Close">closed</a> channel always succeeds, -immediately returning the element type's <a href="#The_zero_value">zero -value</a>. +A receive operation on a <a href="#Close">closed</a> channel can always proceed +immediately, yielding the element type's <a href="#The_zero_value">zero value</a>. </p> <pre> @@ -3337,13 +3413,6 @@ channel, or <code>false</code> if it is a zero value generated because the channel is closed and empty. </p> -<!-- -<p> -<span class="alert">TODO: Probably in a separate section, communication semantics -need to be presented regarding send, receive, select, and goroutines.</span> -</p> ---> - <h3 id="Method_expressions">Method expressions</h3> @@ -3550,7 +3619,7 @@ using a pointer will automatically dereference that pointer: <code>pt.Mv</code> <p> As with <a href="#Calls">method calls</a>, a reference to a non-interface method with a pointer receiver -using an addressable value will automatically take the address of that value: <code>t.Mv</code> is equivalent to <code>(&t).Mv</code>. +using an addressable value will automatically take the address of that value: <code>t.Mp</code> is equivalent to <code>(&t).Mp</code>. </p> <pre> @@ -3749,11 +3818,12 @@ MyString(0x65e5) // "\u65e5" == "日" == "\xe6\x97\xa5" <li> Converting a slice of bytes to a string type yields -a string whose successive bytes are the elements of the slice. If -the slice value is <code>nil</code>, the result is the empty string. +a string whose successive bytes are the elements of the slice. <pre> -string([]byte{'h', 'e', 'l', 'l', '\xc3', '\xb8'}) // "hellø" +string([]byte{'h', 'e', 'l', 'l', '\xc3', '\xb8'}) // "hellø" +string([]byte{}) // "" +string([]byte(nil)) // "" type MyBytes []byte string(MyBytes{'h', 'e', 'l', 'l', '\xc3', '\xb8'}) // "hellø" @@ -3763,11 +3833,12 @@ string(MyBytes{'h', 'e', 'l', 'l', '\xc3', '\xb8'}) // "hellø" <li> Converting a slice of runes to a string type yields a string that is the concatenation of the individual rune values -converted to strings. If the slice value is <code>nil</code>, the -result is the empty string. +converted to strings. <pre> -string([]rune{0x767d, 0x9d6c, 0x7fd4}) // "\u767d\u9d6c\u7fd4" == "白鵬翔" +string([]rune{0x767d, 0x9d6c, 0x7fd4}) // "\u767d\u9d6c\u7fd4" == "白鵬翔" +string([]rune{}) // "" +string([]rune(nil)) // "" type MyRunes []rune string(MyRunes{0x767d, 0x9d6c, 0x7fd4}) // "\u767d\u9d6c\u7fd4" == "白鵬翔" @@ -3777,10 +3848,11 @@ string(MyRunes{0x767d, 0x9d6c, 0x7fd4}) // "\u767d\u9d6c\u7fd4" == "白鵬翔" <li> Converting a value of a string type to a slice of bytes type yields a slice whose successive elements are the bytes of the string. -If the string is empty, the result is <code>[]byte(nil)</code>. <pre> []byte("hellø") // []byte{'h', 'e', 'l', 'l', '\xc3', '\xb8'} +[]byte("") // []byte{} + MyBytes("hellø") // []byte{'h', 'e', 'l', 'l', '\xc3', '\xb8'} </pre> </li> @@ -3788,9 +3860,11 @@ MyBytes("hellø") // []byte{'h', 'e', 'l', 'l', '\xc3', '\xb8'} <li> Converting a value of a string type to a slice of runes type yields a slice containing the individual Unicode code points of the string. -If the string is empty, the result is <code>[]rune(nil)</code>. + <pre> []rune(MyString("白鵬翔")) // []rune{0x767d, 0x9d6c, 0x7fd4} +[]rune("") // []rune{} + MyRunes("白鵬翔") // []rune{0x767d, 0x9d6c, 0x7fd4} </pre> </li> @@ -3914,15 +3988,6 @@ context, even if it would be integral when calculated using infinite precision. </p> -<!-- -<p> -<span class="alert"> -TODO: perhaps ^ should be disallowed on non-uints instead of assuming twos complement. -Also it may be possible to make typed constants more like variables, at the cost of fewer -overflow etc. errors being caught. -</span> -</p> ---> <h3 id="Order_of_evaluation">Order of evaluation</h3> @@ -3952,8 +4017,10 @@ of <code>y</code> is not specified. <pre> a := 1 -f := func() int { a = 2; return 3 } -x := []int{a, f()} // x may be [1, 3] or [2, 3]: evaluation order between a and f() is not specified +f := func() int { a++; return a } +x := []int{a, f()} // x may be [1, 2] or [2, 2]: evaluation order between a and f() is not specified +m := map[int]int{a: 1, a: 2} // m may be {2: 1} or {2: 2}: evaluation order between the two map assignments is not specified +m2 := map[int]int{a: f()} // m2 may be {2: 3} or {3: 3}: evaluation order between the key and the value is not specified </pre> <p> @@ -4142,6 +4209,20 @@ A send on a closed channel proceeds by causing a <a href="#Run_time_panics">run- A send on a <code>nil</code> channel blocks forever. </p> +<p> +Channels act as first-in-first-out queues. +For example, if a single goroutine sends on a channel values +that are received by a single goroutine, the values are received in the order sent. +</p> + +<p> +A single channel may be used for send and receive +operations and calls to the built-in functions +<a href="#Length_and_capacity"><code>cap</code></a> and +<a href="#Length_and_capacity"><code>len</code></a> +by any number of goroutines without further synchronization. +</p> + <pre> ch <- 3 </pre> @@ -4182,7 +4263,8 @@ assign_op = [ add_op | mul_op ] "=" . <p> Each left-hand side operand must be <a href="#Address_operators">addressable</a>, -a map index expression, or the <a href="#Blank_identifier">blank identifier</a>. +a map index expression, or (for <code>=</code> assignments only) the +<a href="#Blank_identifier">blank identifier</a>. Operands may be parenthesized. </p> @@ -4195,12 +4277,13 @@ a[i] = 23 <p> An <i>assignment operation</i> <code>x</code> <i>op</i><code>=</code> -<code>y</code> where <i>op</i> is a binary arithmetic operation is equivalent +<code>y</code> where <i>op</i> is a binary arithmetic operation equivalent to <code>x</code> <code>=</code> <code>x</code> <i>op</i> <code>y</code> but evaluates <code>x</code> only once. The <i>op</i><code>=</code> construct is a single token. In assignment operations, both the left- and right-hand expression lists -must contain exactly one single-valued expression. +must contain exactly one single-valued expression, and the left-hand +expression must not be the blank identifier. </p> <pre> @@ -4225,21 +4308,26 @@ x, y = f() <p> assigns the first value to <code>x</code> and the second to <code>y</code>. -The <a href="#Blank_identifier">blank identifier</a> provides a -way to ignore values returned by a multi-valued expression: +In the second form, the number of operands on the left must equal the number +of expressions on the right, each of which must be single-valued, and the +<i>n</i>th expression on the right is assigned to the <i>n</i>th +operand on the left: </p> <pre> -x, _ = f() // ignore second value returned by f() +one, two, three = '一', '二', '三' </pre> <p> -In the second form, the number of operands on the left must equal the number -of expressions on the right, each of which must be single-valued, and the -<i>n</i>th expression on the right is assigned to the <i>n</i>th -operand on the left. +The <a href="#Blank_identifier">blank identifier</a> provides a way to +ignore right-hand side values in an assignment: </p> +<pre> +_ = x // evaluate x but ignore it +x, _ = f() // evaluate f() but ignore second result value +</pre> + <p> The assignment proceeds in two phases. First, the operands of <a href="#Index_expressions">index expressions</a> @@ -4277,16 +4365,29 @@ for i, x[i] = range x { // set i, x[2] = 0, x[0] </pre> <p> -In assignments, each value must be -<a href="#Assignability">assignable</a> to the type of the -operand to which it is assigned. If an untyped <a href="#Constants">constant</a> -is assigned to a variable of interface type, the constant is <a href="#Conversions">converted</a> -to type <code>bool</code>, <code>rune</code>, <code>int</code>, <code>float64</code>, -<code>complex128</code> or <code>string</code> -respectively, depending on whether the value is a -boolean, rune, integer, floating-point, complex, or string constant. +In assignments, each value must be <a href="#Assignability">assignable</a> +to the type of the operand to which it is assigned, with the following special cases: </p> +<ol> +<li><p> + If an untyped <a href="#Constants">constant</a> + is assigned to a variable of interface type or the blank identifier, + the constant is first <a href="#Conversions">converted</a> to type + <code>bool</code>, <code>rune</code>, <code>int</code>, <code>float64</code>, + <code>complex128</code> or <code>string</code> respectively, depending on + whether the value is a boolean, rune, integer, floating-point, complex, or + string constant. +</p></li> + +<li><p> + <!-- Note that the result of a comparison is an untyped bool that may not be constant. --> + If a left-hand side is the blank identifier, any typed or non-constant + value except for the predeclared identifier + <a href="#Predeclared_identifiers"><code>nil</code></a> + may be assigned to it. +</p></li> +</ol> <h3 id="If_statements">If statements</h3> @@ -4620,7 +4721,7 @@ channel c chan E, <-chan E element e E For an array, pointer to array, or slice value <code>a</code>, the index iteration values are produced in increasing order, starting at element index 0. If only the first iteration variable is present, the range loop produces -iteration values from 0 up to <code>len(a)</code> and does not index into the array +iteration values from 0 up to <code>len(a)-1</code> and does not index into the array or slice itself. For a <code>nil</code> slice, the number of iterations is 0. </li> @@ -4680,7 +4781,6 @@ for i, _ := range testdata.a { } var a [10]string -m := map[string]int{"mon":0, "tue":1, "wed":2, "thu":3, "fri":4, "sat":5, "sun":6} for i, s := range a { // type of i is int // type of s is string @@ -4690,6 +4790,7 @@ for i, s := range a { var key string var val interface {} // value type of m is assignable to val +m := map[string]int{"mon":0, "tue":1, "wed":2, "thu":3, "fri":4, "sat":5, "sun":6} for key, val = range m { h(key, val) } @@ -4901,13 +5002,6 @@ function. A "return" statement that specifies results sets the result parameters any deferred functions are executed. </p> -<!-- -<p> -<span class="alert"> -TODO: Define when return is required.<br /> -</span> -</p> ---> <h3 id="Break_statements">Break statements</h3> @@ -4929,11 +5023,17 @@ and that is the one whose execution terminates. </p> <pre> -L: - for i < n { - switch i { - case 5: - break L +OuterLoop: + for i = 0; i < n; i++ { + for j = 0; j < m; j++ { + switch a[i][j] { + case nil: + state = Error + break OuterLoop + case item: + state = Found + break OuterLoop + } } } </pre> @@ -4955,6 +5055,18 @@ If there is a label, it must be that of an enclosing advances. </p> +<pre> +RowLoop: + for y, row := range rows { + for x, data := range row { + if data == endOfRow { + continue RowLoop + } + row[x] = data + bias(x, y) + } + } +</pre> + <h3 id="Goto_statements">Goto statements</h3> <p> @@ -5270,9 +5382,9 @@ append(s S, x ...T) S // T is the element type of S <p> If the capacity of <code>s</code> is not large enough to fit the additional -values, <code>append</code> allocates a new, sufficiently large slice that fits -both the existing slice elements and the additional values. Thus, the returned -slice may refer to a different underlying array. +values, <code>append</code> allocates a new, sufficiently large underlying +array that fits both the existing slice elements and the additional values. +Otherwise, <code>append</code> re-uses the underlying array. </p> <pre> @@ -5863,6 +5975,8 @@ func Sizeof(variable ArbitraryType) uintptr <p> Any pointer or value of <a href="#Types">underlying type</a> <code>uintptr</code> can be converted to a <code>Pointer</code> type and vice versa. +A <code>Pointer</code> is a <a href="#Pointer_types">pointer type</a> but a <code>Pointer</code> +value may not be <a href="#Address_operators">dereferenced</a>. </p> <pre> @@ -5871,6 +5985,8 @@ bits = *(*uint64)(unsafe.Pointer(&f)) type ptr unsafe.Pointer bits = *(*uint64)(ptr(&f)) + +var p ptr = nil </pre> <p> |