Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 <!--{ | 1 <!--{ |
2 "Title": "The Go Programming Language Specification", | 2 "Title": "The Go Programming Language Specification", |
3 » "Subtitle": "Version of Oct 7, 2013", | 3 » "Subtitle": "Version of Nov 13, 2013", |
4 "Path": "/ref/spec" | 4 "Path": "/ref/spec" |
5 }--> | 5 }--> |
6 | 6 |
7 <!-- | 7 <!-- |
8 TODO | 8 TODO |
9 [ ] need language about function/method calls and parameter passing rules | 9 [ ] need language about function/method calls and parameter passing rules |
10 [ ] last paragraph of #Assignments (constant promotion) should be elsewhere | 10 [ ] last paragraph of #Assignments (constant promotion) should be elsewhere |
11 and mention assignment to empty interface. | 11 and mention assignment to empty interface. |
12 [ ] need to say something about "scope" of selectors? | 12 [ ] need to say something about "scope" of selectors? |
13 [ ] clarify what a field name is in struct declarations | 13 [ ] clarify what a field name is in struct declarations |
(...skipping 818 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
832 [32]byte | 832 [32]byte |
833 [2*N] struct { x, y int32 } | 833 [2*N] struct { x, y int32 } |
834 [1000]*float64 | 834 [1000]*float64 |
835 [3][5]int | 835 [3][5]int |
836 [2][2][2]float64 // same as [2]([2]([2]float64)) | 836 [2][2][2]float64 // same as [2]([2]([2]float64)) |
837 </pre> | 837 </pre> |
838 | 838 |
839 <h3 id="Slice_types">Slice types</h3> | 839 <h3 id="Slice_types">Slice types</h3> |
840 | 840 |
841 <p> | 841 <p> |
842 A slice is a descriptor for a contiguous segment of an array and | 842 A slice is a descriptor for a contiguous segment of an <i>underlying array</i> a nd |
843 provides access to a numbered sequence of elements from that array. | 843 provides access to a numbered sequence of elements from that array. |
844 A slice type denotes the set of all slices of arrays of its element type. | 844 A slice type denotes the set of all slices of arrays of its element type. |
845 The value of an uninitialized slice is <code>nil</code>. | 845 The value of an uninitialized slice is <code>nil</code>. |
846 </p> | 846 </p> |
847 | 847 |
848 <pre class="ebnf"> | 848 <pre class="ebnf"> |
849 SliceType = "[" "]" ElementType . | 849 SliceType = "[" "]" ElementType . |
850 </pre> | 850 </pre> |
851 | 851 |
852 <p> | 852 <p> |
(...skipping 19 matching lines...) Expand all Loading... | |
872 <a href="#Slice_expressions"><i>slicing</i></a> a new one from the original slic e. | 872 <a href="#Slice_expressions"><i>slicing</i></a> a new one from the original slic e. |
873 The capacity of a slice <code>a</code> can be discovered using the | 873 The capacity of a slice <code>a</code> can be discovered using the |
874 built-in function <a href="#Length_and_capacity"><code>cap(a)</code></a>. | 874 built-in function <a href="#Length_and_capacity"><code>cap(a)</code></a>. |
875 </p> | 875 </p> |
876 | 876 |
877 <p> | 877 <p> |
878 A new, initialized slice value for a given element type <code>T</code> is | 878 A new, initialized slice value for a given element type <code>T</code> is |
879 made using the built-in function | 879 made using the built-in function |
880 <a href="#Making_slices_maps_and_channels"><code>make</code></a>, | 880 <a href="#Making_slices_maps_and_channels"><code>make</code></a>, |
881 which takes a slice type | 881 which takes a slice type |
882 and parameters specifying the length and optionally the capacity: | 882 and parameters specifying the length and optionally the capacity. |
883 </p> | 883 A slice created with <code>make</code> always allocates a new, hidden array |
884 | 884 to which the returned slice value refers. That is, executing |
885 <pre> | 885 </p> |
886 make([]T, length) | 886 |
887 <pre> | |
887 make([]T, length, capacity) | 888 make([]T, length, capacity) |
888 </pre> | 889 </pre> |
889 | 890 |
890 <p> | 891 <p> |
891 A call to <code>make</code> allocates a new, hidden array to which the returned | 892 produces the same slice as allocating an array and <a href="#Slice_expressions"> slicing</a> |
892 slice value refers. That is, executing | 893 it, so these two expressions are equivalent: |
893 </p> | |
894 | |
895 <pre> | |
896 make([]T, length, capacity) | |
897 </pre> | |
898 | |
899 <p> | |
900 produces the same slice as allocating an array and slicing it, so these two exam ples | |
901 result in the same slice: | |
902 </p> | 894 </p> |
903 | 895 |
904 <pre> | 896 <pre> |
905 make([]int, 50, 100) | 897 make([]int, 50, 100) |
906 new([100]int)[0:50] | 898 new([100]int)[0:50] |
907 </pre> | 899 </pre> |
908 | 900 |
909 <p> | 901 <p> |
910 Like arrays, slices are always one-dimensional but may be composed to construct | 902 Like arrays, slices are always one-dimensional but may be composed to construct |
911 higher-dimensional objects. | 903 higher-dimensional objects. |
912 With arrays of arrays, the inner arrays are, by construction, always the same le ngth; | 904 With arrays of arrays, the inner arrays are, by construction, always the same le ngth; |
913 however with slices of slices (or arrays of slices), the lengths may vary dynami cally. | 905 however with slices of slices (or arrays of slices), the inner lengths may vary dynamically. |
914 Moreover, the inner slices must be allocated individually (with <code>make</code >). | 906 Moreover, the inner slices must be initialized individually. |
915 </p> | 907 </p> |
916 | 908 |
917 <h3 id="Struct_types">Struct types</h3> | 909 <h3 id="Struct_types">Struct types</h3> |
918 | 910 |
919 <p> | 911 <p> |
920 A struct is a sequence of named elements, called fields, each of which has a | 912 A struct is a sequence of named elements, called fields, each of which has a |
921 name and a type. Field names may be specified explicitly (IdentifierList) or | 913 name and a type. Field names may be specified explicitly (IdentifierList) or |
922 implicitly (AnonymousField). | 914 implicitly (AnonymousField). |
923 Within a struct, non-<a href="#Blank_identifier">blank</a> field names must | 915 Within a struct, non-<a href="#Blank_identifier">blank</a> field names must |
924 be <a href="#Uniqueness_of_identifiers">unique</a>. | 916 be <a href="#Uniqueness_of_identifiers">unique</a>. |
(...skipping 662 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1587 In contrast to other identifiers, labels are not block scoped and do | 1579 In contrast to other identifiers, labels are not block scoped and do |
1588 not conflict with identifiers that are not labels. The scope of a label | 1580 not conflict with identifiers that are not labels. The scope of a label |
1589 is the body of the function in which it is declared and excludes | 1581 is the body of the function in which it is declared and excludes |
1590 the body of any nested function. | 1582 the body of any nested function. |
1591 </p> | 1583 </p> |
1592 | 1584 |
1593 | 1585 |
1594 <h3 id="Blank_identifier">Blank identifier</h3> | 1586 <h3 id="Blank_identifier">Blank identifier</h3> |
1595 | 1587 |
1596 <p> | 1588 <p> |
1597 The <i>blank identifier</i> is the underscore character <code>_</code>. | 1589 The <i>blank identifier</i> is represented by the underscore character <code>_</ code>. |
r
2013/10/07 22:08:07
s/is/is represented by/
gri
2013/10/07 22:26:14
Done.
| |
1598 It serves as an anonymous placeholder instead of a regular (non-blank) | 1590 It serves as an anonymous placeholder instead of a regular (non-blank) |
1599 identifier and has special meaning in <a href="#Declarations_and_scope">declarat ions</a>, | 1591 identifier and has special meaning in <a href="#Declarations_and_scope">declarat ions</a>, |
1600 as an <a href="#Operands">operand</a>, and in <a href="#Assignments">assignments </a>. | 1592 as an <a href="#Operands">operand</a>, and in <a href="#Assignments">assignments </a>. |
1601 </p> | 1593 </p> |
1602 | 1594 |
1603 | 1595 |
1604 <h3 id="Predeclared_identifiers">Predeclared identifiers</h3> | 1596 <h3 id="Predeclared_identifiers">Predeclared identifiers</h3> |
1605 | 1597 |
1606 <p> | 1598 <p> |
1607 The following identifiers are implicitly declared in the | 1599 The following identifiers are implicitly declared in the |
(...skipping 1100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2708 <p> | 2700 <p> |
2709 Except for <a href="#Constants">untyped strings</a>, if the sliced operand is a string or slice, | 2701 Except for <a href="#Constants">untyped strings</a>, if the sliced operand is a string or slice, |
2710 the result of the slice operation is a non-constant value of the same type as th e operand. | 2702 the result of the slice operation is a non-constant value of the same type as th e operand. |
2711 For untyped string operands the result is a non-constant value of type <code>str ing</code>. | 2703 For untyped string operands the result is a non-constant value of type <code>str ing</code>. |
2712 If the sliced operand is an array, it must be <a href="#Address_operators">addre ssable</a> | 2704 If the sliced operand is an array, it must be <a href="#Address_operators">addre ssable</a> |
2713 and the result of the slice operation is a slice with the same element type as t he array. | 2705 and the result of the slice operation is a slice with the same element type as t he array. |
2714 </p> | 2706 </p> |
2715 | 2707 |
2716 <p> | 2708 <p> |
2717 If the sliced operand of a valid slice expression is a <code>nil</code> slice, t he result | 2709 If the sliced operand of a valid slice expression is a <code>nil</code> slice, t he result |
2718 is a <code>nil</code> slice. | 2710 is a <code>nil</code> slice. Otherwise, the result shares its underlying array w ith the |
2711 operand. | |
2719 </p> | 2712 </p> |
2720 | 2713 |
2721 <h4>Full slice expressions</h4> | 2714 <h4>Full slice expressions</h4> |
2722 | 2715 |
2723 <p> | 2716 <p> |
2724 For an array, pointer to array, or slice <code>a</code> (but not a string), the primary expression | 2717 For an array, pointer to array, or slice <code>a</code> (but not a string), the primary expression |
2725 </p> | 2718 </p> |
2726 | 2719 |
2727 <pre> | 2720 <pre> |
2728 a[low : high : max] | 2721 a[low : high : max] |
(...skipping 1548 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
4277 | 4270 |
4278 <pre> | 4271 <pre> |
4279 x = 1 | 4272 x = 1 |
4280 *p = f() | 4273 *p = f() |
4281 a[i] = 23 | 4274 a[i] = 23 |
4282 (k) = <-ch // same as: k = <-ch | 4275 (k) = <-ch // same as: k = <-ch |
4283 </pre> | 4276 </pre> |
4284 | 4277 |
4285 <p> | 4278 <p> |
4286 An <i>assignment operation</i> <code>x</code> <i>op</i><code>=</code> | 4279 An <i>assignment operation</i> <code>x</code> <i>op</i><code>=</code> |
4287 <code>y</code> where <i>op</i> is a binary arithmetic operation is equivalent | 4280 <code>y</code> where <i>op</i> is a binary arithmetic operation equivalent |
r
2013/10/07 22:08:07
s2/is//
gri
2013/10/07 22:26:14
Done.
| |
4288 to <code>x</code> <code>=</code> <code>x</code> <i>op</i> | 4281 to <code>x</code> <code>=</code> <code>x</code> <i>op</i> |
4289 <code>y</code> but evaluates <code>x</code> | 4282 <code>y</code> but evaluates <code>x</code> |
4290 only once. The <i>op</i><code>=</code> construct is a single token. | 4283 only once. The <i>op</i><code>=</code> construct is a single token. |
4291 In assignment operations, both the left- and right-hand expression lists | 4284 In assignment operations, both the left- and right-hand expression lists |
4292 must contain exactly one single-valued expression, and the left-hand | 4285 must contain exactly one single-valued expression, and the left-hand |
4293 expression must not be the blank identifier. | 4286 expression must not be the blank identifier. |
4294 </p> | 4287 </p> |
4295 | 4288 |
4296 <pre> | 4289 <pre> |
4297 a[i] <<= 2 | 4290 a[i] <<= 2 |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
4373 | 4366 |
4374 <p> | 4367 <p> |
4375 In assignments, each value must be <a href="#Assignability">assignable</a> | 4368 In assignments, each value must be <a href="#Assignability">assignable</a> |
4376 to the type of the operand to which it is assigned, with the following special c ases: | 4369 to the type of the operand to which it is assigned, with the following special c ases: |
4377 </p> | 4370 </p> |
4378 | 4371 |
4379 <ol> | 4372 <ol> |
4380 <li><p> | 4373 <li><p> |
4381 If an untyped <a href="#Constants">constant</a> | 4374 If an untyped <a href="#Constants">constant</a> |
4382 is assigned to a variable of interface type or the blank identifier, | 4375 is assigned to a variable of interface type or the blank identifier, |
4383 » the constant is <a href="#Conversions">converted</a> to type | 4376 » the constant is first <a href="#Conversions">converted</a> to type |
4384 <code>bool</code>, <code>rune</code>, <code>int</code>, <code>float64</c ode>, | 4377 <code>bool</code>, <code>rune</code>, <code>int</code>, <code>float64</c ode>, |
4385 <code>complex128</code> or <code>string</code> respectively, depending o n | 4378 <code>complex128</code> or <code>string</code> respectively, depending o n |
4386 whether the value is a boolean, rune, integer, floating-point, complex, or | 4379 whether the value is a boolean, rune, integer, floating-point, complex, or |
4387 string constant. | 4380 string constant. |
4388 </p></li> | 4381 </p></li> |
4389 | 4382 |
4390 <li><p> | 4383 <li><p> |
4391 <!-- Note that the result of a comparison is an untyped bool that may no t be constant. --> | 4384 <!-- Note that the result of a comparison is an untyped bool that may no t be constant. --> |
4392 » If the left-hand side is the blank identifier, any typed or non-constant | 4385 » If a left-hand side is the blank identifier, any typed or non-constant |
4393 value except for the predeclared identifier | 4386 value except for the predeclared identifier |
4394 <a href="#Predeclared_identifiers"><code>nil</code></a> | 4387 <a href="#Predeclared_identifiers"><code>nil</code></a> |
4395 may be assigned to it. | 4388 may be assigned to it. |
4396 </p></li> | 4389 </p></li> |
4397 </ol> | 4390 </ol> |
4398 | 4391 |
4399 <h3 id="If_statements">If statements</h3> | 4392 <h3 id="If_statements">If statements</h3> |
4400 | 4393 |
4401 <p> | 4394 <p> |
4402 "If" statements specify the conditional execution of two branches | 4395 "If" statements specify the conditional execution of two branches |
(...skipping 979 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
5382 string type followed by <code>...</code>. This form appends the | 5375 string type followed by <code>...</code>. This form appends the |
5383 bytes of the string. | 5376 bytes of the string. |
5384 </p> | 5377 </p> |
5385 | 5378 |
5386 <pre class="grammar"> | 5379 <pre class="grammar"> |
5387 append(s S, x ...T) S // T is the element type of S | 5380 append(s S, x ...T) S // T is the element type of S |
5388 </pre> | 5381 </pre> |
5389 | 5382 |
5390 <p> | 5383 <p> |
5391 If the capacity of <code>s</code> is not large enough to fit the additional | 5384 If the capacity of <code>s</code> is not large enough to fit the additional |
5392 values, <code>append</code> allocates a new, sufficiently large slice that fits | 5385 values, <code>append</code> allocates a new, sufficiently large underlying |
5393 both the existing slice elements and the additional values. Thus, the returned | 5386 array that fits both the existing slice elements and the additional values. |
5394 slice may refer to a different underlying array. | 5387 Otherwise, <code>append</code> re-uses the underlying array. |
5395 </p> | 5388 </p> |
5396 | 5389 |
5397 <pre> | 5390 <pre> |
5398 s0 := []int{0, 0} | 5391 s0 := []int{0, 0} |
5399 s1 := append(s0, 2) // append a single element s1 == []int{0, 0, 2} | 5392 s1 := append(s0, 2) // append a single element s1 == []int{0, 0, 2} |
5400 s2 := append(s1, 3, 5, 7) // append multiple elements s2 == []int{0, 0, 2, 3, 5, 7} | 5393 s2 := append(s1, 3, 5, 7) // append multiple elements s2 == []int{0, 0, 2, 3, 5, 7} |
5401 s3 := append(s2, s0...) // append a slice s3 == []int{0, 0, 2, 3, 5, 7, 0, 0} | 5394 s3 := append(s2, s0...) // append a slice s3 == []int{0, 0, 2, 3, 5, 7, 0, 0} |
5402 s4 := append(s3[3:6], s3[2:]...) // append overlapping slice s4 == []int{3, 5, 7, 2, 3, 5, 7, 0, 0} | 5395 s4 := append(s3[3:6], s3[2:]...) // append overlapping slice s4 == []int{3, 5, 7, 2, 3, 5, 7, 0, 0} |
5403 | 5396 |
5404 var t []interface{} | 5397 var t []interface{} |
(...skipping 655 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
6060 </li> | 6053 </li> |
6061 | 6054 |
6062 <li>For a variable <code>x</code> of array type: <code>unsafe.Alignof(x)</code> is the same as | 6055 <li>For a variable <code>x</code> of array type: <code>unsafe.Alignof(x)</code> is the same as |
6063 <code>unsafe.Alignof(x[0])</code>, but at least 1. | 6056 <code>unsafe.Alignof(x[0])</code>, but at least 1. |
6064 </li> | 6057 </li> |
6065 </ol> | 6058 </ol> |
6066 | 6059 |
6067 <p> | 6060 <p> |
6068 A struct or array type has size zero if it contains no fields (or elements, resp ectively) that have a size greater than zero. Two distinct zero-size variables m ay have the same address in memory. | 6061 A struct or array type has size zero if it contains no fields (or elements, resp ectively) that have a size greater than zero. Two distinct zero-size variables m ay have the same address in memory. |
6069 </p> | 6062 </p> |
LEFT | RIGHT |