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

Delta Between Two Patch Sets: doc/go_spec.html

Issue 11884043: code review 11884043: spec: clarify index and selector expressions (Closed)
Left Patch Set: Created 11 years, 8 months ago
Right Patch Set: diff -r 38097c493d8a https://code.google.com/p/go Created 11 years, 8 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:
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
(no file at all)
1 <!--{ 1 <!--{
2 "Title": "The Go Programming Language Specification", 2 "Title": "The Go Programming Language Specification",
3 » "Subtitle": "Version of July 25, 2013", 3 » "Subtitle": "Version of July 31, 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 1888 matching lines...) Expand 10 before | Expand all | Expand 10 after
1902 1902
1903 <p> 1903 <p>
1904 A <i>short variable declaration</i> uses the syntax: 1904 A <i>short variable declaration</i> uses the syntax:
1905 </p> 1905 </p>
1906 1906
1907 <pre class="ebnf"> 1907 <pre class="ebnf">
1908 ShortVarDecl = IdentifierList ":=" ExpressionList . 1908 ShortVarDecl = IdentifierList ":=" ExpressionList .
1909 </pre> 1909 </pre>
1910 1910
1911 <p> 1911 <p>
1912 It is a shorthand for a regular <a href="#Variable_declarations">variable declar ation</a> 1912 It is shorthand for a regular <a href="#Variable_declarations">variable declarat ion</a>
1913 with initializer expressions but no types: 1913 with initializer expressions but no types:
1914 </p> 1914 </p>
1915 1915
1916 <pre class="grammar"> 1916 <pre class="grammar">
1917 "var" IdentifierList = ExpressionList . 1917 "var" IdentifierList = ExpressionList .
1918 </pre> 1918 </pre>
1919 1919
1920 <pre> 1920 <pre>
1921 i, j := 0, 10 1921 i, j := 0, 10
1922 f := func() int { return 7 } 1922 f := func() int { return 7 }
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after
2238 A slice literal describes the entire underlying array literal. 2238 A slice literal describes the entire underlying array literal.
2239 Thus, the length and capacity of a slice literal are the maximum 2239 Thus, the length and capacity of a slice literal are the maximum
2240 element index plus one. A slice literal has the form 2240 element index plus one. A slice literal has the form
2241 </p> 2241 </p>
2242 2242
2243 <pre> 2243 <pre>
2244 []T{x1, x2, … xn} 2244 []T{x1, x2, … xn}
2245 </pre> 2245 </pre>
2246 2246
2247 <p> 2247 <p>
2248 and is a shortcut for a slice operation applied to an array: 2248 and is shorthand for a slice operation applied to an array:
2249 </p> 2249 </p>
2250 2250
2251 <pre> 2251 <pre>
2252 tmp := [n]T{x1, x2, … xn} 2252 tmp := [n]T{x1, x2, … xn}
2253 tmp[0 : n] 2253 tmp[0 : n]
2254 </pre> 2254 </pre>
2255 2255
2256 <p> 2256 <p>
2257 Within a composite literal of array, slice, or map type <code>T</code>, 2257 Within a composite literal of array, slice, or map type <code>T</code>,
2258 elements that are themselves composite literals may elide the respective 2258 elements that are themselves composite literals may elide the respective
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
2455 2455
2456 <p> 2456 <p>
2457 Selectors automatically <a href="#Address_operators">dereference</a> 2457 Selectors automatically <a href="#Address_operators">dereference</a>
2458 pointers to structs. 2458 pointers to structs.
2459 If <code>x</code> is a pointer to a struct, <code>x.y</code> 2459 If <code>x</code> is a pointer to a struct, <code>x.y</code>
2460 is shorthand for <code>(*x).y</code>; if the field <code>y</code> 2460 is shorthand for <code>(*x).y</code>; if the field <code>y</code>
2461 is also a pointer to a struct, <code>x.y.z</code> is shorthand 2461 is also a pointer to a struct, <code>x.y.z</code> is shorthand
2462 for <code>(*(*x).y).z</code>, and so on. 2462 for <code>(*(*x).y).z</code>, and so on.
2463 If <code>x</code> contains an anonymous field of type <code>*A</code>, 2463 If <code>x</code> contains an anonymous field of type <code>*A</code>,
2464 where <code>A</code> is also a struct type, 2464 where <code>A</code> is also a struct type,
2465 <code>x.f</code> is a shortcut for <code>(*x.A).f</code>. 2465 <code>x.f</code> is shorthand for <code>(*x.A).f</code>.
2466 </p> 2466 </p>
2467 2467
2468 <p> 2468 <p>
2469 For example, given the declarations: 2469 For example, given the declarations:
2470 </p> 2470 </p>
2471 2471
2472 <pre> 2472 <pre>
2473 type T0 struct { 2473 type T0 struct {
2474 x int 2474 x int
2475 } 2475 }
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
2512 2512
2513 <p> 2513 <p>
2514 A primary expression of the form 2514 A primary expression of the form
2515 </p> 2515 </p>
2516 2516
2517 <pre> 2517 <pre>
2518 a[x] 2518 a[x]
2519 </pre> 2519 </pre>
2520 2520
2521 <p> 2521 <p>
2522 denotes the element of the array, slice, string or map <code>a</code> indexed by <code>x</code>. 2522 denotes the element of the array, pointer to array, slice, string or map <code>a </code> indexed by <code>x</code>.
2523 The value <code>x</code> is called the 2523 The value <code>x</code> is called the <i>index</i> or <i>map key</i>, respectiv ely.
2524 <i>index</i> or <i>map key</i>, respectively. The following 2524 The following rules apply:
2525 rules apply:
2526 </p> 2525 </p>
2527 2526
2528 <p> 2527 <p>
2529 If <code>a</code> is not a map: 2528 If <code>a</code> is not a map:
2530 </p> 2529 </p>
2531 <ul> 2530 <ul>
2532 <li>the index <code>x</code> must be of integer type or untyped; 2531 <li>the index <code>x</code> must be of integer type or untyped;
2533 it is <i>in range</i> if <code>0 &lt;= x &lt; len(a)</code>, 2532 it is <i>in range</i> if <code>0 &lt;= x &lt; len(a)</code>,
2534 otherwise it is <i>out of range</i></li> 2533 otherwise it is <i>out of range</i></li>
2535 <li>a <a href="#Constants">constant</a> index must be non-negative 2534 <li>a <a href="#Constants">constant</a> index must be non-negative
2536 and representable by a value of type <code>int</code> 2535 and representable by a value of type <code>int</code>
2537 </ul> 2536 </ul>
2538 2537
2539 <p> 2538 <p>
2540 For <code>a</code> of type <code>A</code> or <code>*A</code> 2539 For <code>a</code> of <a href="#Array_types">array type</a> <code>A</code>:
2541 where <code>A</code> is an <a href="#Array_types">array type</a>:
2542 </p> 2540 </p>
2543 <ul> 2541 <ul>
2544 <li>a <a href="#Constants">constant</a> index must be in range</li> 2542 <li>a <a href="#Constants">constant</a> index must be in range</li>
2545 » <li>if <code>a</code> is <code>nil</code> or if <code>x</code> is out of range at run time, 2543 » <li>if <code>x</code> is out of range at run time,
2546 a <a href="#Run_time_panics">run-time panic</a> occurs</li> 2544 a <a href="#Run_time_panics">run-time panic</a> occurs</li>
2547 <li><code>a[x]</code> is the array element at index <code>x</code> and t he type of 2545 <li><code>a[x]</code> is the array element at index <code>x</code> and t he type of
2548 <code>a[x]</code> is the element type of <code>A</code></li> 2546 <code>a[x]</code> is the element type of <code>A</code></li>
2549 </ul> 2547 </ul>
2550 2548
2551 <p> 2549 <p>
2552 For <code>a</code> of type <code>S</code> where <code>S</code> is a <a href="#Sl ice_types">slice type</a>: 2550 For <code>a</code> of <a href="#Pointer_types">pointer</a> to array type:
2553 </p> 2551 </p>
2554 <ul> 2552 <ul>
2555 » <li>if the slice is <code>nil</code> or if <code>x</code> is out of rang e at run time, 2553 » <li><code>a[x]</code> is shorthand for <code>(*a)[x]</code></li>
2554 </ul>
2555
2556 <p>
2557 For <code>a</code> of <a href="#Slice_types">slice type</a> <code>S</code>:
2558 </p>
2559 <ul>
2560 » <li>if <code>x</code> is out of range at run time,
2556 a <a href="#Run_time_panics">run-time panic</a> occurs</li> 2561 a <a href="#Run_time_panics">run-time panic</a> occurs</li>
2557 <li><code>a[x]</code> is the slice element at index <code>x</code> and t he type of 2562 <li><code>a[x]</code> is the slice element at index <code>x</code> and t he type of
2558 <code>a[x]</code> is the element type of <code>S</code></li> 2563 <code>a[x]</code> is the element type of <code>S</code></li>
2559 </ul> 2564 </ul>
2560 2565
2561 <p> 2566 <p>
2562 For <code>a</code> of type <code>T</code> 2567 For <code>a</code> of <a href="#String_types">string type</a>:
2563 where <code>T</code> is a <a href="#String_types">string type</a>:
2564 </p> 2568 </p>
2565 <ul> 2569 <ul>
2566 <li>a <a href="#Constants">constant</a> index must be in range 2570 <li>a <a href="#Constants">constant</a> index must be in range
2567 if the string <code>a</code> is also constant</li> 2571 if the string <code>a</code> is also constant</li>
2568 <li>if <code>x</code> is out of range at run time, 2572 <li>if <code>x</code> is out of range at run time,
2569 a <a href="#Run_time_panics">run-time panic</a> occurs</li> 2573 a <a href="#Run_time_panics">run-time panic</a> occurs</li>
2570 » <li><code>a[x]</code> is the byte at index <code>x</code> and the type o f 2574 » <li><code>a[x]</code> is the non-constant byte value at index <code>x</c ode> and the type of
2571 <code>a[x]</code> is <code>byte</code></li> 2575 <code>a[x]</code> is <code>byte</code></li>
2572 <li><code>a[x]</code> may not be assigned to</li> 2576 <li><code>a[x]</code> may not be assigned to</li>
2573 </ul> 2577 </ul>
2574 2578
2575 <p> 2579 <p>
2576 For <code>a</code> of type <code>M</code> 2580 For <code>a</code> of <a href="#Map_types">map type</a> <code>M</code>:
2577 where <code>M</code> is a <a href="#Map_types">map type</a>:
2578 </p> 2581 </p>
2579 <ul> 2582 <ul>
2580 <li><code>x</code>'s type must be 2583 <li><code>x</code>'s type must be
2581 <a href="#Assignability">assignable</a> 2584 <a href="#Assignability">assignable</a>
2582 to the key type of <code>M</code></li> 2585 to the key type of <code>M</code></li>
2583 <li>if the map contains an entry with key <code>x</code>, 2586 <li>if the map contains an entry with key <code>x</code>,
2584 <code>a[x]</code> is the map value with key <code>x</code> 2587 <code>a[x]</code> is the map value with key <code>x</code>
2585 and the type of <code>a[x]</code> is the value type of <code>M</code ></li> 2588 and the type of <code>a[x]</code> is the value type of <code>M</code ></li>
2586 <li>if the map is <code>nil</code> or does not contain such an entry, 2589 <li>if the map is <code>nil</code> or does not contain such an entry,
2587 <code>a[x]</code> is the <a href="#The_zero_value">zero value</a> 2590 <code>a[x]</code> is the <a href="#The_zero_value">zero value</a>
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
2621 2624
2622 <p> 2625 <p>
2623 For a string, array, pointer to array, or slice <code>a</code>, the primary expr ession 2626 For a string, array, pointer to array, or slice <code>a</code>, the primary expr ession
2624 </p> 2627 </p>
2625 2628
2626 <pre> 2629 <pre>
2627 a[low : high] 2630 a[low : high]
2628 </pre> 2631 </pre>
2629 2632
2630 <p> 2633 <p>
2631 constructs a substring or slice. The indices <code>low</code> and 2634 constructs a substring or slice. The <i>indices</i> <code>low</code> and
2632 <code>high</code> select which elements appear in the result. The result has 2635 <code>high</code> select which elements of operand <code>a</code> appear
2633 indices starting at 0 and length equal to 2636 in the result. The result has indices starting at 0 and length equal to
2634 <code>high</code>&nbsp;-&nbsp;<code>low</code>. 2637 <code>high</code>&nbsp;-&nbsp;<code>low</code>.
2635 After slicing the array <code>a</code> 2638 After slicing the array <code>a</code>
2636 </p> 2639 </p>
2637 2640
2638 <pre> 2641 <pre>
2639 a := [5]int{1, 2, 3, 4, 5} 2642 a := [5]int{1, 2, 3, 4, 5}
2640 s := a[1:4] 2643 s := a[1:4]
2641 </pre> 2644 </pre>
2642 2645
2643 <p> 2646 <p>
(...skipping 12 matching lines...) Expand all
2656 sliced operand: 2659 sliced operand:
2657 </p> 2660 </p>
2658 2661
2659 <pre> 2662 <pre>
2660 a[2:] // same a[2 : len(a)] 2663 a[2:] // same a[2 : len(a)]
2661 a[:3] // same as a[0 : 3] 2664 a[:3] // same as a[0 : 3]
2662 a[:] // same as a[0 : len(a)] 2665 a[:] // same as a[0 : len(a)]
2663 </pre> 2666 </pre>
2664 2667
2665 <p> 2668 <p>
2666 For arrays or strings, the indices <code>low</code> and <code>high</code> are 2669 If <code>a</code> is a pointer to an array, <code>a[low : high]</code> is shorth and for
2667 <i>in range</i> if <code>0</code> &lt;= <code>low</code> &lt;= <code>high</code> &lt;= <code>len(a)</code>, 2670 <code>(*a)[low : high]</code>.
2671 </p>
2672
2673 <p>
2674 For arrays or strings, the indices are <i>in range</i> if
2675 <code>0</code> &lt;= <code>low</code> &lt;= <code>high</code> &lt;= <code>len(a) </code>,
2668 otherwise they are <i>out of range</i>. 2676 otherwise they are <i>out of range</i>.
r 2013/07/31 21:09:16 look at line 2532.
gri 2013/07/31 21:23:14 Line 2532 defines these terms for index expression
2669 For slices, the upper index bound is the slice capacity <code>cap(a)</code> rath er than the length. 2677 For slices, the upper index bound is the slice capacity <code>cap(a)</code> rath er than the length.
2670 A <a href="#Constants">constant</a> index must be non-negative and representable by a value of type 2678 A <a href="#Constants">constant</a> index must be non-negative and representable by a value of type
2671 <code>int</code>. 2679 <code>int</code>.
2672 If both indices 2680 If both indices are constant, they must satisfy <code>low &lt;= high</code>.
2673 are constant, they must satisfy <code>low &lt;= high</code>. If <code>a</code> i s <code>nil</code> 2681 If the indices are out of range at run time, a <a href="#Run_time_panics">run-ti me panic</a> occurs.
2674 or if the indices are out of range at run time, a <a href="#Run_time_panics">run -time panic</a> occurs. 2682 </p>
2675 </p> 2683
2676 2684 <p>
2677 <p> 2685 Except for <a href="#Constants">untyped strings</a>, if the sliced operand is a string or slice,
2678 If the sliced operand is a string or slice, the result of the slice operation 2686 the result of the slice operation is a non-constant value of the same type as th e operand.
2679 is a string or slice of the same type. 2687 For untyped string operands the result is a non-constant value of type <code>str ing</code>.
2680 If the sliced operand is an array, it must be <a href="#Address_operators">addre ssable</a> 2688 If the sliced operand is an array, it must be <a href="#Address_operators">addre ssable</a>
2681 and the result of the slice operation is a slice with the same element type as t he array. 2689 and the result of the slice operation is a slice with the same element type as t he array.
2682 </p> 2690 </p>
2683 2691
2692 <!-- TODO: should this be an implementation restriction? -->
2693 <p>
2694 If the sliced operand of a valid slice expression is a <code>nil</code> slice, t he result
2695 is a <code>nil</code> slice.
2696 <p>
2684 2697
2685 <h3 id="Type_assertions">Type assertions</h3> 2698 <h3 id="Type_assertions">Type assertions</h3>
2686 2699
2687 <p> 2700 <p>
2688 For an expression <code>x</code> of <a href="#Interface_types">interface type</a > 2701 For an expression <code>x</code> of <a href="#Interface_types">interface type</a >
2689 and a type <code>T</code>, the primary expression 2702 and a type <code>T</code>, the primary expression
2690 </p> 2703 </p>
2691 2704
2692 <pre> 2705 <pre>
2693 x.(T) 2706 x.(T)
(...skipping 3220 matching lines...) Expand 10 before | Expand all | Expand 10 after
5914 </li> 5927 </li>
5915 5928
5916 <li>For a variable <code>x</code> of array type: <code>unsafe.Alignof(x)</code> is the same as 5929 <li>For a variable <code>x</code> of array type: <code>unsafe.Alignof(x)</code> is the same as
5917 <code>unsafe.Alignof(x[0])</code>, but at least 1. 5930 <code>unsafe.Alignof(x[0])</code>, but at least 1.
5918 </li> 5931 </li>
5919 </ol> 5932 </ol>
5920 5933
5921 <p> 5934 <p>
5922 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. 5935 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.
5923 </p> 5936 </p>
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

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