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 4, 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 589 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1514 <a href="#Variable_declarations">variable</a>, | 1506 <a href="#Variable_declarations">variable</a>, |
1515 <a href="#Function_declarations">function</a>, | 1507 <a href="#Function_declarations">function</a>, |
1516 <a href="#Labeled_statements">label</a>, or | 1508 <a href="#Labeled_statements">label</a>, or |
1517 <a href="#Import_declarations">package</a>. | 1509 <a href="#Import_declarations">package</a>. |
1518 Every identifier in a program must be declared. | 1510 Every identifier in a program must be declared. |
1519 No identifier may be declared twice in the same block, and | 1511 No identifier may be declared twice in the same block, and |
1520 no identifier may be declared in both the file and package block. | 1512 no identifier may be declared in both the file and package block. |
1521 </p> | 1513 </p> |
1522 | 1514 |
1523 <p> | 1515 <p> |
1524 The <a href="#Blank_identifier">blank identifier</a> may be used in a declaratio n | 1516 The <a href="#Blank_identifier">blank identifier</a> may be used like any other identifier |
1525 like any other identifier, but it does not introduce a binding and thus is not | 1517 in a declaration, but it does not introduce a binding and thus is not declared. |
r
2013/10/05 16:02:36
grammar depends on order.
may be used like any ot
gri
2013/10/07 19:58:30
Done.
| |
1526 declared. | |
1527 </p> | 1518 </p> |
1528 | 1519 |
1529 <pre class="ebnf"> | 1520 <pre class="ebnf"> |
1530 Declaration = ConstDecl | TypeDecl | VarDecl . | 1521 Declaration = ConstDecl | TypeDecl | VarDecl . |
1531 TopLevelDecl = Declaration | FunctionDecl | MethodDecl . | 1522 TopLevelDecl = Declaration | FunctionDecl | MethodDecl . |
1532 </pre> | 1523 </pre> |
1533 | 1524 |
1534 <p> | 1525 <p> |
1535 The <i>scope</i> of a declared identifier is the extent of source text in which | 1526 The <i>scope</i> of a declared identifier is the extent of source text in which |
1536 the identifier denotes the specified constant, type, variable, function, label, or package. | 1527 the identifier denotes the specified constant, type, variable, function, label, or package. |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1588 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 |
1589 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 |
1590 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 |
1591 the body of any nested function. | 1582 the body of any nested function. |
1592 </p> | 1583 </p> |
1593 | 1584 |
1594 | 1585 |
1595 <h3 id="Blank_identifier">Blank identifier</h3> | 1586 <h3 id="Blank_identifier">Blank identifier</h3> |
1596 | 1587 |
1597 <p> | 1588 <p> |
1598 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>. |
1599 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) |
1600 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>, |
1601 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>. |
1602 </p> | 1593 </p> |
1603 | 1594 |
1604 | 1595 |
1605 <h3 id="Predeclared_identifiers">Predeclared identifiers</h3> | 1596 <h3 id="Predeclared_identifiers">Predeclared identifiers</h3> |
1606 | 1597 |
1607 <p> | 1598 <p> |
1608 The following identifiers are implicitly declared in the | 1599 The following identifiers are implicitly declared in the |
(...skipping 483 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2092 literal, a (possibly <a href="#Qualified_identifiers">qualified</a>) | 2083 literal, a (possibly <a href="#Qualified_identifiers">qualified</a>) |
2093 non-<a href="#Blank_identifier">blank</a> identifier denoting a | 2084 non-<a href="#Blank_identifier">blank</a> identifier denoting a |
2094 <a href="#Constant_declarations">constant</a>, | 2085 <a href="#Constant_declarations">constant</a>, |
2095 <a href="#Variable_declarations">variable</a>, or | 2086 <a href="#Variable_declarations">variable</a>, or |
2096 <a href="#Function_declarations">function</a>, | 2087 <a href="#Function_declarations">function</a>, |
2097 a <a href="#Method_expressions">method expression</a> yielding a function, | 2088 a <a href="#Method_expressions">method expression</a> yielding a function, |
2098 or a parenthesized expression. | 2089 or a parenthesized expression. |
2099 </p> | 2090 </p> |
2100 | 2091 |
2101 <p> | 2092 <p> |
2102 The <a href="#Blank_identifier">blank identifier</a> is not permitted | 2093 The <a href="#Blank_identifier">blank identifier</a> may appear as an |
2103 as an operand except as an expression on the left-hand side of an | 2094 operand only on the left-hand side of an <a href="#Assignments">assignment</a>. |
2104 <a href="#Assignments">assignment</a>. | |
2105 </p> | 2095 </p> |
r
2013/10/05 16:02:36
or short declaration? i know they're not operands
gri
2013/10/07 19:58:30
Done.
| |
2106 | 2096 |
2107 <pre class="ebnf"> | 2097 <pre class="ebnf"> |
2108 Operand = Literal | OperandName | MethodExpr | "(" Expression ")" . | 2098 Operand = Literal | OperandName | MethodExpr | "(" Expression ")" . |
2109 Literal = BasicLit | CompositeLit | FunctionLit . | 2099 Literal = BasicLit | CompositeLit | FunctionLit . |
2110 BasicLit = int_lit | float_lit | imaginary_lit | rune_lit | string_lit . | 2100 BasicLit = int_lit | float_lit | imaginary_lit | rune_lit | string_lit . |
2111 OperandName = identifier | QualifiedIdent. | 2101 OperandName = identifier | QualifiedIdent. |
2112 </pre> | 2102 </pre> |
2113 | 2103 |
2114 <h3 id="Qualified_identifiers">Qualified identifiers</h3> | 2104 <h3 id="Qualified_identifiers">Qualified identifiers</h3> |
2115 | 2105 |
(...skipping 594 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2710 <p> | 2700 <p> |
2711 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, |
2712 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. |
2713 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>. |
2714 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> |
2715 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. |
2716 </p> | 2706 </p> |
2717 | 2707 |
2718 <p> | 2708 <p> |
2719 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 |
2720 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. | |
2721 </p> | 2712 </p> |
2722 | 2713 |
2723 <h4>Full slice expressions</h4> | 2714 <h4>Full slice expressions</h4> |
2724 | 2715 |
2725 <p> | 2716 <p> |
2726 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 |
2727 </p> | 2718 </p> |
2728 | 2719 |
2729 <pre> | 2720 <pre> |
2730 a[low : high : max] | 2721 a[low : high : max] |
(...skipping 1548 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
4279 | 4270 |
4280 <pre> | 4271 <pre> |
4281 x = 1 | 4272 x = 1 |
4282 *p = f() | 4273 *p = f() |
4283 a[i] = 23 | 4274 a[i] = 23 |
4284 (k) = <-ch // same as: k = <-ch | 4275 (k) = <-ch // same as: k = <-ch |
4285 </pre> | 4276 </pre> |
4286 | 4277 |
4287 <p> | 4278 <p> |
4288 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> |
4289 <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 |
4290 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> |
4291 <code>y</code> but evaluates <code>x</code> | 4282 <code>y</code> but evaluates <code>x</code> |
4292 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. |
4293 In assignment operations, both the left- and right-hand expression lists | 4284 In assignment operations, both the left- and right-hand expression lists |
4294 must contain exactly one single-valued expression, and the left-hand | 4285 must contain exactly one single-valued expression, and the left-hand |
4295 expression must not be the blank identifier. | 4286 expression must not be the blank identifier. |
4296 </p> | 4287 </p> |
4297 | 4288 |
4298 <pre> | 4289 <pre> |
4299 a[i] <<= 2 | 4290 a[i] <<= 2 |
(...skipping 27 matching lines...) Expand all Loading... | |
4327 one, two, three = '一', '二', '三' | 4318 one, two, three = '一', '二', '三' |
4328 </pre> | 4319 </pre> |
4329 | 4320 |
4330 <p> | 4321 <p> |
4331 The <a href="#Blank_identifier">blank identifier</a> provides a way to | 4322 The <a href="#Blank_identifier">blank identifier</a> provides a way to |
4332 ignore right-hand side values in an assignment: | 4323 ignore right-hand side values in an assignment: |
4333 </p> | 4324 </p> |
4334 | 4325 |
4335 <pre> | 4326 <pre> |
4336 _ = x // evaluate x but ignore it | 4327 _ = x // evaluate x but ignore it |
4337 x, _ = f() // evaluate f() but ignore second result value | 4328 x, _ = f() // evaluate f() but ignore second result value |
r
2013/10/05 16:02:36
a, _, c = f(), g(), h() // evaluate all three func
| |
4338 </pre> | 4329 </pre> |
4339 | 4330 |
4340 <p> | 4331 <p> |
4341 The assignment proceeds in two phases. | 4332 The assignment proceeds in two phases. |
4342 First, the operands of <a href="#Index_expressions">index expressions</a> | 4333 First, the operands of <a href="#Index_expressions">index expressions</a> |
4343 and <a href="#Address_operators">pointer indirections</a> | 4334 and <a href="#Address_operators">pointer indirections</a> |
4344 (including implicit pointer indirections in <a href="#Selectors">selectors</a>) | 4335 (including implicit pointer indirections in <a href="#Selectors">selectors</a>) |
4345 on the left and the expressions on the right are all | 4336 on the left and the expressions on the right are all |
4346 <a href="#Order_of_evaluation">evaluated in the usual order</a>. | 4337 <a href="#Order_of_evaluation">evaluated in the usual order</a>. |
4347 Second, the assignments are carried out in left-to-right order. | 4338 Second, the assignments are carried out in left-to-right order. |
(...skipping 19 matching lines...) Expand all Loading... | |
4367 | 4358 |
4368 i = 2 | 4359 i = 2 |
4369 x = []int{3, 5, 7} | 4360 x = []int{3, 5, 7} |
4370 for i, x[i] = range x { // set i, x[2] = 0, x[0] | 4361 for i, x[i] = range x { // set i, x[2] = 0, x[0] |
4371 break | 4362 break |
4372 } | 4363 } |
4373 // after this loop, i == 0 and x == []int{3, 5, 3} | 4364 // after this loop, i == 0 and x == []int{3, 5, 3} |
4374 </pre> | 4365 </pre> |
4375 | 4366 |
4376 <p> | 4367 <p> |
4377 In assignments, each value must be | 4368 In assignments, each value must be <a href="#Assignability">assignable</a> |
4378 <a href="#Assignability">assignable</a> to the type of the | 4369 to the type of the operand to which it is assigned, with the following special c ases: |
4379 operand to which it is assigned. If an untyped <a href="#Constants">constant</a> | 4370 </p> |
4380 is assigned to a variable of interface type, the constant is <a href="#Conversio ns">converted</a> | 4371 |
4381 to type <code>bool</code>, <code>rune</code>, <code>int</code>, <code>float64</c ode>, | 4372 <ol> |
4382 <code>complex128</code> or <code>string</code> | 4373 <li><p> |
4383 respectively, depending on whether the value is a | 4374 » If an untyped <a href="#Constants">constant</a> |
4384 boolean, rune, integer, floating-point, complex, or string constant. | 4375 » is assigned to a variable of interface type or the blank identifier, |
4385 </p> | 4376 » the constant is first <a href="#Conversions">converted</a> to type |
4386 | 4377 » <code>bool</code>, <code>rune</code>, <code>int</code>, <code>float64</c ode>, |
4387 <!-- TODO(gri) specify rules for assignment to blank identifiers --> | 4378 » <code>complex128</code> or <code>string</code> respectively, depending o n |
4379 » whether the value is a boolean, rune, integer, floating-point, complex, or | |
4380 » string constant. | |
4381 </p></li> | |
4382 | |
4383 <li><p> | |
4384 » <!-- Note that the result of a comparison is an untyped bool that may no t be constant. --> | |
4385 » If a left-hand side is the blank identifier, any typed or non-constant | |
4386 » value except for the predeclared identifier | |
4387 » <a href="#Predeclared_identifiers"><code>nil</code></a> | |
4388 » may be assigned to it. | |
4389 </p></li> | |
4390 </ol> | |
4388 | 4391 |
4389 <h3 id="If_statements">If statements</h3> | 4392 <h3 id="If_statements">If statements</h3> |
4390 | 4393 |
4391 <p> | 4394 <p> |
4392 "If" statements specify the conditional execution of two branches | 4395 "If" statements specify the conditional execution of two branches |
4393 according to the value of a boolean expression. If the expression | 4396 according to the value of a boolean expression. If the expression |
4394 evaluates to true, the "if" branch is executed, otherwise, if | 4397 evaluates to true, the "if" branch is executed, otherwise, if |
4395 present, the "else" branch is executed. | 4398 present, the "else" branch is executed. |
4396 </p> | 4399 </p> |
4397 | 4400 |
(...skipping 974 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
5372 string type followed by <code>...</code>. This form appends the | 5375 string type followed by <code>...</code>. This form appends the |
5373 bytes of the string. | 5376 bytes of the string. |
5374 </p> | 5377 </p> |
5375 | 5378 |
5376 <pre class="grammar"> | 5379 <pre class="grammar"> |
5377 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 |
5378 </pre> | 5381 </pre> |
5379 | 5382 |
5380 <p> | 5383 <p> |
5381 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 |
5382 values, <code>append</code> allocates a new, sufficiently large slice that fits | 5385 values, <code>append</code> allocates a new, sufficiently large underlying |
5383 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. |
5384 slice may refer to a different underlying array. | 5387 Otherwise, <code>append</code> re-uses the underlying array. |
5385 </p> | 5388 </p> |
5386 | 5389 |
5387 <pre> | 5390 <pre> |
5388 s0 := []int{0, 0} | 5391 s0 := []int{0, 0} |
5389 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} |
5390 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} |
5391 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} |
5392 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} |
5393 | 5396 |
5394 var t []interface{} | 5397 var t []interface{} |
(...skipping 570 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
5965 type Pointer *ArbitraryType | 5968 type Pointer *ArbitraryType |
5966 | 5969 |
5967 func Alignof(variable ArbitraryType) uintptr | 5970 func Alignof(variable ArbitraryType) uintptr |
5968 func Offsetof(selector ArbitraryType) uintptr | 5971 func Offsetof(selector ArbitraryType) uintptr |
5969 func Sizeof(variable ArbitraryType) uintptr | 5972 func Sizeof(variable ArbitraryType) uintptr |
5970 </pre> | 5973 </pre> |
5971 | 5974 |
5972 <p> | 5975 <p> |
5973 Any pointer or value of <a href="#Types">underlying type</a> <code>uintptr</code > can be converted to | 5976 Any pointer or value of <a href="#Types">underlying type</a> <code>uintptr</code > can be converted to |
5974 a <code>Pointer</code> type and vice versa. | 5977 a <code>Pointer</code> type and vice versa. |
5978 A <code>Pointer</code> is a <a href="#Pointer_types">pointer type</a> but a <cod e>Pointer</code> | |
5979 value may not be <a href="#Address_operators">dereferenced</a>. | |
5975 </p> | 5980 </p> |
5976 | 5981 |
5977 <pre> | 5982 <pre> |
5978 var f float64 | 5983 var f float64 |
5979 bits = *(*uint64)(unsafe.Pointer(&f)) | 5984 bits = *(*uint64)(unsafe.Pointer(&f)) |
5980 | 5985 |
5981 type ptr unsafe.Pointer | 5986 type ptr unsafe.Pointer |
5982 bits = *(*uint64)(ptr(&f)) | 5987 bits = *(*uint64)(ptr(&f)) |
5988 | |
5989 var p ptr = nil | |
5983 </pre> | 5990 </pre> |
5984 | 5991 |
5985 <p> | 5992 <p> |
5986 The functions <code>Alignof</code> and <code>Sizeof</code> take an expression <c ode>x</code> | 5993 The functions <code>Alignof</code> and <code>Sizeof</code> take an expression <c ode>x</code> |
5987 of any type and return the alignment or size, respectively, of a hypothetical va riable <code>v</code> | 5994 of any type and return the alignment or size, respectively, of a hypothetical va riable <code>v</code> |
5988 as if <code>v</code> was declared via <code>var v = x</code>. | 5995 as if <code>v</code> was declared via <code>var v = x</code>. |
5989 </p> | 5996 </p> |
5990 <p> | 5997 <p> |
5991 The function <code>Offsetof</code> takes a (possibly parenthesized) <a href="#Se lectors">selector</a> | 5998 The function <code>Offsetof</code> takes a (possibly parenthesized) <a href="#Se lectors">selector</a> |
5992 <code>s.f</code>, denoting a field <code>f</code> of the struct denoted by <code >s</code> | 5999 <code>s.f</code>, denoting a field <code>f</code> of the struct denoted by <code >s</code> |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
6046 </li> | 6053 </li> |
6047 | 6054 |
6048 <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 |
6049 <code>unsafe.Alignof(x[0])</code>, but at least 1. | 6056 <code>unsafe.Alignof(x[0])</code>, but at least 1. |
6050 </li> | 6057 </li> |
6051 </ol> | 6058 </ol> |
6052 | 6059 |
6053 <p> | 6060 <p> |
6054 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. |
6055 </p> | 6062 </p> |
LEFT | RIGHT |