Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(884)

Side by Side Diff: doc/go_spec.html

Issue 14419054: code review 14419054: spec: clarify re-use of underlying arrays in slice oper... (Closed)
Patch Set: diff -r 9d0d95344a6c https://code.google.com/p/go Created 10 years, 5 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 Oct 16, 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
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
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>
886 make([]T, length)
887 make([]T, length, capacity)
888 </pre>
889
890 <p>
891 A call to <code>make</code> allocates a new, hidden array to which the returned
892 slice value refers. That is, executing
893 </p> 885 </p>
894 886
895 <pre> 887 <pre>
896 make([]T, length, capacity) 888 make([]T, length, capacity)
897 </pre> 889 </pre>
898 890
899 <p> 891 <p>
900 produces the same slice as allocating an array and slicing it, so these two exam ples 892 produces the same slice as allocating an array and <a href="#Slice_expressions"> slicing</a>
901 result in the same slice: 893 it, so these two expressions are equivalent:
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 1775 matching lines...) Expand 10 before | Expand all | Expand 10 after
2700 <p> 2692 <p>
2701 Except for <a href="#Constants">untyped strings</a>, if the sliced operand is a string or slice, 2693 Except for <a href="#Constants">untyped strings</a>, if the sliced operand is a string or slice,
2702 the result of the slice operation is a non-constant value of the same type as th e operand. 2694 the result of the slice operation is a non-constant value of the same type as th e operand.
2703 For untyped string operands the result is a non-constant value of type <code>str ing</code>. 2695 For untyped string operands the result is a non-constant value of type <code>str ing</code>.
2704 If the sliced operand is an array, it must be <a href="#Address_operators">addre ssable</a> 2696 If the sliced operand is an array, it must be <a href="#Address_operators">addre ssable</a>
2705 and the result of the slice operation is a slice with the same element type as t he array. 2697 and the result of the slice operation is a slice with the same element type as t he array.
2706 </p> 2698 </p>
2707 2699
2708 <p> 2700 <p>
2709 If the sliced operand of a valid slice expression is a <code>nil</code> slice, t he result 2701 If the sliced operand of a valid slice expression is a <code>nil</code> slice, t he result
2710 is a <code>nil</code> slice. 2702 is a <code>nil</code> slice. Otherwise, the result shares its underlying array w ith the
2703 operand.
2711 </p> 2704 </p>
2712 2705
2713 <h4>Full slice expressions</h4> 2706 <h4>Full slice expressions</h4>
2714 2707
2715 <p> 2708 <p>
2716 For an array, pointer to array, or slice <code>a</code> (but not a string), the primary expression 2709 For an array, pointer to array, or slice <code>a</code> (but not a string), the primary expression
2717 </p> 2710 </p>
2718 2711
2719 <pre> 2712 <pre>
2720 a[low : high : max] 2713 a[low : high : max]
(...skipping 2633 matching lines...) Expand 10 before | Expand all | Expand 10 after
5354 string type followed by <code>...</code>. This form appends the 5347 string type followed by <code>...</code>. This form appends the
5355 bytes of the string. 5348 bytes of the string.
5356 </p> 5349 </p>
5357 5350
5358 <pre class="grammar"> 5351 <pre class="grammar">
5359 append(s S, x ...T) S // T is the element type of S 5352 append(s S, x ...T) S // T is the element type of S
5360 </pre> 5353 </pre>
5361 5354
5362 <p> 5355 <p>
5363 If the capacity of <code>s</code> is not large enough to fit the additional 5356 If the capacity of <code>s</code> is not large enough to fit the additional
5364 values, <code>append</code> allocates a new, sufficiently large slice that fits 5357 values, <code>append</code> allocates a new, sufficiently large underlying
5365 both the existing slice elements and the additional values. Thus, the returned 5358 array that fits both the existing slice elements and the additional values.
iant 2013/10/16 22:57:52 In the case where a new array is allocated, are we
gri 2013/10/16 23:09:16 I was trying to avoid that. If people rely on tha
5366 slice may refer to a different underlying array. 5359 Otherwise, <code>append</code> re-uses the underlying array.
5367 </p> 5360 </p>
5368 5361
5369 <pre> 5362 <pre>
5370 s0 := []int{0, 0} 5363 s0 := []int{0, 0}
5371 s1 := append(s0, 2) // append a single element s1 == []int{0, 0, 2} 5364 s1 := append(s0, 2) // append a single element s1 == []int{0, 0, 2}
5372 s2 := append(s1, 3, 5, 7) // append multiple elements s2 == []int{0, 0, 2, 3, 5, 7} 5365 s2 := append(s1, 3, 5, 7) // append multiple elements s2 == []int{0, 0, 2, 3, 5, 7}
5373 s3 := append(s2, s0...) // append a slice s3 == []int{0, 0, 2, 3, 5, 7, 0, 0} 5366 s3 := append(s2, s0...) // append a slice s3 == []int{0, 0, 2, 3, 5, 7, 0, 0}
5374 s4 := append(s3[3:6], s3[2:]...) // append overlapping slice s4 == []int{3, 5, 7, 2, 3, 5, 7, 0, 0} 5367 s4 := append(s3[3:6], s3[2:]...) // append overlapping slice s4 == []int{3, 5, 7, 2, 3, 5, 7, 0, 0}
5375 5368
5376 var t []interface{} 5369 var t []interface{}
(...skipping 655 matching lines...) Expand 10 before | Expand all | Expand 10 after
6032 </li> 6025 </li>
6033 6026
6034 <li>For a variable <code>x</code> of array type: <code>unsafe.Alignof(x)</code> is the same as 6027 <li>For a variable <code>x</code> of array type: <code>unsafe.Alignof(x)</code> is the same as
6035 <code>unsafe.Alignof(x[0])</code>, but at least 1. 6028 <code>unsafe.Alignof(x[0])</code>, but at least 1.
6036 </li> 6029 </li>
6037 </ol> 6030 </ol>
6038 6031
6039 <p> 6032 <p>
6040 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. 6033 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.
6041 </p> 6034 </p>
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b