~ubuntu-branches/ubuntu/trusty/golang/trusty

« back to all changes in this revision

Viewing changes to doc/go_spec.html

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-20 14:06:23 UTC
  • mfrom: (14.1.23 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130820140623-b414jfxi3m0qkmrq
Tags: 2:1.1.2-2ubuntu1
* Merge from Debian unstable (LP: #1211749, #1202027). Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - d/control,control.cross: Update Breaks/Replaces for Ubuntu
    versions to ensure smooth upgrades, regenerate control file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
<!--{
2
2
        "Title": "The Go Programming Language Specification",
3
 
        "Subtitle": "Version of June 4, 2012",
 
3
        "Subtitle": "Version of May 8, 2013",
4
4
        "Path": "/ref/spec"
5
5
}-->
6
6
 
15
15
[ ] need explicit language about the result type of operations
16
16
[ ] should probably write something about evaluation order of statements even
17
17
        though obvious
18
 
[ ] review language on implicit dereferencing
19
18
-->
20
19
 
21
20
 
89
88
canonicalized, so a single accented code point is distinct from the
90
89
same character constructed from combining an accent and a letter;
91
90
those are treated as two code points.  For simplicity, this document
92
 
will use the term <i>character</i> to refer to a Unicode code point.
 
91
will use the unqualified term <i>character</i> to refer to a Unicode code point
 
92
in the source text.
93
93
</p>
94
94
<p>
95
95
Each code point is distinct; for instance, upper and lower case letters
99
99
Implementation restriction: For compatibility with other tools, a
100
100
compiler may disallow the NUL character (U+0000) in the source text.
101
101
</p>
 
102
<p>
 
103
Implementation restriction: For compatibility with other tools, a
 
104
compiler may ignore a UTF-8-encoded byte order mark
 
105
(U+FEFF) if it is the first Unicode code point in the source text.
 
106
A byte order mark may be disallowed anywhere else in the source.
 
107
</p>
102
108
 
103
109
<h3 id="Characters">Characters</h3>
104
110
 
113
119
</pre>
114
120
 
115
121
<p>
116
 
In <a href="http://www.unicode.org/versions/Unicode6.0.0/">The Unicode Standard 6.0</a>,
 
122
In <a href="http://www.unicode.org/versions/Unicode6.2.0/">The Unicode Standard 6.2</a>,
117
123
Section 4.5 "General Category"
118
124
defines a set of character categories.  Go treats
119
125
those characters in category Lu, Ll, Lt, Lm, or Lo as Unicode letters,
198
204
            <a href="#Integer_literals">integer</a>,
199
205
            <a href="#Floating-point_literals">floating-point</a>,
200
206
            <a href="#Imaginary_literals">imaginary</a>,
201
 
            <a href="#Character_literals">character</a>, or
 
207
            <a href="#Rune_literals">rune</a>, or
202
208
            <a href="#String_literals">string</a> literal
203
209
        </li>
204
210
 
360
366
</pre>
361
367
 
362
368
 
363
 
<h3 id="Character_literals">Character literals</h3>
 
369
<h3 id="Rune_literals">Rune literals</h3>
364
370
 
365
371
<p>
366
 
A character literal represents a <a href="#Constants">character constant</a>,
367
 
typically a Unicode code point, as one or more characters enclosed in single
368
 
quotes.  Within the quotes, any character may appear except single
369
 
quote and newline. A single quoted character represents itself,
 
372
A rune literal represents a <a href="#Constants">rune constant</a>,
 
373
an integer value identifying a Unicode code point.
 
374
A rune literal is expressed as one or more characters enclosed in single quotes.
 
375
Within the quotes, any character may appear except single
 
376
quote and newline. A single quoted character represents the Unicode value
 
377
of the character itself,
370
378
while multi-character sequences beginning with a backslash encode
371
379
values in various formats.
372
380
</p>
380
388
a literal <code>a</code>-dieresis, U+00E4, value <code>0xe4</code>.
381
389
</p>
382
390
<p>
383
 
Several backslash escapes allow arbitrary values to be represented
384
 
as ASCII text.  There are four ways to represent the integer value
 
391
Several backslash escapes allow arbitrary values to be encoded as
 
392
ASCII text.  There are four ways to represent the integer value
385
393
as a numeric constant: <code>\x</code> followed by exactly two hexadecimal
386
394
digits; <code>\u</code> followed by exactly four hexadecimal digits;
387
395
<code>\U</code> followed by exactly eight hexadecimal digits, and a
409
417
\t   U+0009 horizontal tab
410
418
\v   U+000b vertical tab
411
419
\\   U+005c backslash
412
 
\'   U+0027 single quote  (valid escape only within character literals)
 
420
\'   U+0027 single quote  (valid escape only within rune literals)
413
421
\"   U+0022 double quote  (valid escape only within string literals)
414
422
</pre>
415
423
<p>
416
 
All other sequences starting with a backslash are illegal inside character literals.
 
424
All other sequences starting with a backslash are illegal inside rune literals.
417
425
</p>
418
426
<pre class="ebnf">
419
 
char_lit         = "'" ( unicode_value | byte_value ) "'" .
 
427
rune_lit         = "'" ( unicode_value | byte_value ) "'" .
420
428
unicode_value    = unicode_char | little_u_value | big_u_value | escaped_char .
421
429
byte_value       = octal_byte_value | hex_byte_value .
422
430
octal_byte_value = `\` octal_digit octal_digit octal_digit .
439
447
'\xff'
440
448
'\u12e4'
441
449
'\U00101234'
 
450
'aa'         // illegal: too many characters
 
451
'\xa'        // illegal: too few hexadecimal digits
 
452
'\0'         // illegal: too few octal digits
 
453
'\uDFFF'     // illegal: surrogate half
 
454
'\U00110000' // illegal: invalid Unicode code point
442
455
</pre>
443
456
 
444
457
 
453
466
Raw string literals are character sequences between back quotes
454
467
<code>``</code>.  Within the quotes, any character is legal except
455
468
back quote. The value of a raw string literal is the
456
 
string composed of the uninterpreted characters between the quotes;
 
469
string composed of the uninterpreted (implicitly UTF-8-encoded) characters
 
470
between the quotes;
457
471
in particular, backslashes have no special meaning and the string may
458
472
contain newlines.
459
473
Carriage returns inside raw string literals
464
478
quotes <code>&quot;&quot;</code>. The text between the quotes,
465
479
which may not contain newlines, forms the
466
480
value of the literal, with backslash escapes interpreted as they
467
 
are in character literals (except that <code>\'</code> is illegal and
468
 
<code>\"</code> is legal).  The three-digit octal (<code>\</code><i>nnn</i>)
 
481
are in rune literals (except that <code>\'</code> is illegal and
 
482
<code>\"</code> is legal), with the same restrictions.
 
483
The three-digit octal (<code>\</code><i>nnn</i>)
469
484
and two-digit hexadecimal (<code>\x</code><i>nn</i>) escapes represent individual
470
485
<i>bytes</i> of the resulting string; all other escapes represent
471
486
the (possibly multi-byte) UTF-8 encoding of individual <i>characters</i>.
492
507
"日本語"
493
508
"\u65e5本\U00008a9e"
494
509
"\xff\u00FF"
 
510
"\uD800"       // illegal: surrogate half
 
511
"\U00110000"   // illegal: invalid Unicode code point
495
512
</pre>
496
513
 
497
514
<p>
501
518
<pre>
502
519
"日本語"                                 // UTF-8 input text
503
520
`日本語`                                 // UTF-8 input text as a raw literal
504
 
"\u65e5\u672c\u8a9e"                    // The explicit Unicode code points
505
 
"\U000065e5\U0000672c\U00008a9e"        // The explicit Unicode code points
506
 
"\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e"  // The explicit UTF-8 bytes
 
521
"\u65e5\u672c\u8a9e"                    // the explicit Unicode code points
 
522
"\U000065e5\U0000672c\U00008a9e"        // the explicit Unicode code points
 
523
"\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e"  // the explicit UTF-8 bytes
507
524
</pre>
508
525
 
509
526
<p>
510
527
If the source code represents a character as two code points, such as
511
528
a combining form involving an accent and a letter, the result will be
512
 
an error if placed in a character literal (it is not a single code
 
529
an error if placed in a rune literal (it is not a single code
513
530
point), and will appear as two code points if placed in a string
514
531
literal.
515
532
</p>
518
535
<h2 id="Constants">Constants</h2>
519
536
 
520
537
<p>There are <i>boolean constants</i>,
521
 
<i>character constants</i>,
 
538
<i>rune constants</i>,
522
539
<i>integer constants</i>,
523
540
<i>floating-point constants</i>, <i>complex constants</i>,
524
541
and <i>string constants</i>. Character, integer, floating-point,
528
545
 
529
546
<p>
530
547
A constant value is represented by a
531
 
<a href="#Character_literals">character</a>,
 
548
<a href="#Rune_literals">rune</a>,
532
549
<a href="#Integer_literals">integer</a>,
533
550
<a href="#Floating-point_literals">floating-point</a>,
534
551
<a href="#Imaginary_literals">imaginary</a>,
622
639
 
623
640
<p>
624
641
A type determines the set of values and operations specific to values of that
625
 
type.  A type may be specified by a (possibly qualified) <i>type name</i>
626
 
(§<a href="#Qualified_identifiers">Qualified identifier</a>, §<a href="#Type_declarations">Type declarations</a>) or a <i>type literal</i>,
 
642
type.  A type may be specified by a
 
643
(possibly <a href="#Qualified_identifiers">qualified</a>)
 
644
<a href="#Type_declarations"><i>type name</i></a> or a <i>type literal</i>,
627
645
which composes a new type from previously declared types.
628
646
</p>
629
647
 
630
648
<pre class="ebnf">
631
649
Type      = TypeName | TypeLit | "(" Type ")" .
632
 
TypeName  = QualifiedIdent .
 
650
TypeName  = identifier | QualifiedIdent .
633
651
TypeLit   = ArrayType | StructType | PointerType | FunctionType | InterfaceType |
634
652
            SliceType | MapType | ChannelType .
635
653
</pre>
646
664
The <i>static type</i> (or just <i>type</i>) of a variable is the
647
665
type defined by its declaration.  Variables of interface type
648
666
also have a distinct <i>dynamic type</i>, which
649
 
is the actual type of the value stored in the variable at run-time.
 
667
is the actual type of the value stored in the variable at run time.
650
668
The dynamic type may vary during execution but is always
651
669
<a href="#Assignability">assignable</a>
652
670
to the static type of the interface variable.  For non-interface
764
782
 
765
783
<p>
766
784
A <i>string type</i> represents the set of string values.
767
 
Strings behave like slices of bytes but are immutable: once created,
 
785
A string value is a (possibly empty) sequence of bytes.
 
786
Strings are immutable: once created,
768
787
it is impossible to change the contents of a string.
769
788
The predeclared string type is <code>string</code>.
 
789
</p>
770
790
 
771
791
<p>
772
 
The elements of strings have type <code>byte</code> and may be
773
 
accessed using the usual <a href="#Indexes">indexing operations</a>.  It is
774
 
illegal to take the address of such an element; if
775
 
<code>s[i]</code> is the <i>i</i>th byte of a
776
 
string, <code>&amp;s[i]</code> is invalid.  The length of string
777
 
<code>s</code> can be discovered using the built-in function
778
 
<code>len</code>. The length is a compile-time constant if <code>s</code>
779
 
is a string literal.
 
792
The length of a string <code>s</code> (its size in bytes) can be discovered using
 
793
the built-in function <a href="#Length_and_capacity"><code>len</code></a>.
 
794
The length is a compile-time constant if the string is a constant.
 
795
A string's bytes can be accessed by integer <a href="#Index_expressions">indices</a>
 
796
0 through <code>len(s)-1</code>.
 
797
It is illegal to take the address of such an element; if
 
798
<code>s[i]</code> is the <code>i</code>'th byte of a
 
799
string, <code>&amp;s[i]</code> is invalid.
780
800
</p>
781
801
 
782
802
 
796
816
</pre>
797
817
 
798
818
<p>
799
 
The length is part of the array's type and must be a
800
 
<a href="#Constant_expressions">constant expression</a> that evaluates to a non-negative
801
 
integer value.  The length of array <code>a</code> can be discovered
802
 
using the built-in function <a href="#Length_and_capacity"><code>len(a)</code></a>.
803
 
The elements can be indexed by integer
804
 
indices 0 through <code>len(a)-1</code> (§<a href="#Indexes">Indexes</a>).
 
819
The length is part of the array's type; it must evaluate to a non-
 
820
negative <a href="#Constants">constant</a> representable by a value
 
821
of type <code>int</code>.
 
822
The length of array <code>a</code> can be discovered
 
823
using the built-in function <a href="#Length_and_capacity"><code>len</code></a>.
 
824
The elements can be addressed by integer <a href="#Index_expressions">indices</a>
 
825
0 through <code>len(a)-1</code>.
805
826
Array types are always one-dimensional but may be composed to form
806
827
multi-dimensional types.
807
828
</p>
817
838
<h3 id="Slice_types">Slice types</h3>
818
839
 
819
840
<p>
820
 
A slice is a reference to a contiguous segment of an array and
821
 
contains a numbered sequence of elements from that array.  A slice
822
 
type denotes the set of all slices of arrays of its element type.
 
841
A slice is a descriptor for a contiguous segment of an array and
 
842
provides access to a numbered sequence of elements from that array.
 
843
A slice type denotes the set of all slices of arrays of its element type.
823
844
The value of an uninitialized slice is <code>nil</code>.
824
845
</p>
825
846
 
830
851
<p>
831
852
Like arrays, slices are indexable and have a length.  The length of a
832
853
slice <code>s</code> can be discovered by the built-in function
833
 
<a href="#Length_and_capacity"><code>len(s)</code></a>; unlike with arrays it may change during
834
 
execution.  The elements can be addressed by integer indices 0
835
 
through <code>len(s)-1</code> (§<a href="#Indexes">Indexes</a>).  The slice index of a
 
854
<a href="#Length_and_capacity"><code>len</code></a>; unlike with arrays it may change during
 
855
execution.  The elements can be addressed by integer <a href="#Index_expressions">indices</a>
 
856
0 through <code>len(s)-1</code>.  The slice index of a
836
857
given element may be less than the index of the same element in the
837
858
underlying array.
838
859
</p>
846
867
The array underlying a slice may extend past the end of the slice.
847
868
The <i>capacity</i> is a measure of that extent: it is the sum of
848
869
the length of the slice and the length of the array beyond the slice;
849
 
a slice of length up to that capacity can be created by `slicing' a new
850
 
one from the original slice (§<a href="#Slices">Slices</a>).
 
870
a slice of length up to that capacity can be created by
 
871
<a href="#Slices"><i>slicing</i></a> a new one from the original slice.
851
872
The capacity of a slice <code>a</code> can be discovered using the
852
873
built-in function <a href="#Length_and_capacity"><code>cap(a)</code></a>.
853
874
</p>
981
1002
        <code>T</code>. The method set of <code>*S</code> also
982
1003
        includes promoted methods with receiver <code>*T</code>.
983
1004
        </li>
984
 
        
 
1005
 
985
1006
        <li>
986
1007
        If <code>S</code> contains an anonymous field <code>*T</code>,
987
1008
        the method sets of <code>S</code> and <code>*S</code> both
994
1015
A field declaration may be followed by an optional string literal <i>tag</i>,
995
1016
which becomes an attribute for all the fields in the corresponding
996
1017
field declaration. The tags are made
997
 
visible through a <a href="#Package_unsafe">reflection interface</a>
 
1018
visible through a <a href="/pkg/reflect/#StructTag">reflection interface</a>
998
1019
but are otherwise ignored.
999
1020
</p>
1000
1021
 
1046
1067
<p>
1047
1068
Within a list of parameters or results, the names (IdentifierList)
1048
1069
must either all be present or all be absent. If present, each name
1049
 
stands for one item (parameter or result) of the specified type; if absent, each
1050
 
type stands for one item of that type.  Parameter and result
 
1070
stands for one item (parameter or result) of the specified type and
 
1071
all non-<a href="#Blank_identifier">blank</a> names in the signature
 
1072
must be <a href="#Uniqueness_of_identifiers">unique</a>.
 
1073
If absent, each type stands for one item of that type.
 
1074
Parameter and result
1051
1075
lists are always parenthesized except that if there is exactly
1052
1076
one unnamed result it may be written as an unparenthesized type.
1053
1077
</p>
1213
1237
</pre>
1214
1238
 
1215
1239
<p>
1216
 
The comparison operators <code>==</code> and <code>!=</code>
1217
 
(§<a href="#Comparison_operators">Comparison operators</a>) must be fully defined
 
1240
The <a href="#Comparison_operators">comparison operators</a>
 
1241
<code>==</code> and <code>!=</code> must be fully defined
1218
1242
for operands of the key type; thus the key type must not be a function, map, or
1219
1243
slice.
1220
1244
If the key type is an interface type, these
1232
1256
<p>
1233
1257
The number of map elements is called its length.
1234
1258
For a map <code>m</code>, it can be discovered using the
1235
 
built-in function <a href="#Length_and_capacity"><code>len(m)</code></a>
 
1259
built-in function <a href="#Length_and_capacity"><code>len</code></a>
1236
1260
and may change during execution. Elements may be added during execution
1237
1261
using <a href="#Assignments">assignments</a> and retrieved with
1238
 
<a href="#Indexes">index</a> expressions; they may be removed with the
 
1262
<a href="#Index_expressions">index expressions</a>; they may be removed with the
1239
1263
<a href="#Deletion_of_map_elements"><code>delete</code></a> built-in function.
1240
1264
</p>
1241
1265
<p>
1446
1470
<h2 id="Blocks">Blocks</h2>
1447
1471
 
1448
1472
<p>
1449
 
A <i>block</i> is a sequence of declarations and statements within matching
1450
 
brace brackets.
 
1473
A <i>block</i> is a possibly empty sequence of declarations and statements
 
1474
within matching brace brackets.
1451
1475
</p>
1452
1476
 
1453
1477
<pre class="ebnf">
1454
 
Block = "{" { Statement ";" } "}" .
 
1478
Block = "{" StatementList "}" .
 
1479
StatementList = { Statement ";" } .
1455
1480
</pre>
1456
1481
 
1457
1482
<p>
1467
1492
        <li>Each file has a <i>file block</i> containing all Go source text
1468
1493
            in that file.</li>
1469
1494
 
1470
 
        <li>Each <code>if</code>, <code>for</code>, and <code>switch</code>
 
1495
        <li>Each <a href="#If_statements">"if"</a>,
 
1496
            <a href="#For_statements">"for"</a>, and
 
1497
            <a href="#Switch_statements">"switch"</a>
1471
1498
            statement is considered to be in its own implicit block.</li>
1472
1499
 
1473
 
        <li>Each clause in a <code>switch</code> or <code>select</code> statement
 
1500
        <li>Each clause in a <a href="#Switch_statements">"switch"</a>
 
1501
            or <a href="#Select_statements">"select"</a> statement
1474
1502
            acts as an implicit block.</li>
1475
1503
</ol>
1476
1504
 
1510
1538
            or function (but not method) declared at top level (outside any
1511
1539
            function) is the package block.</li>
1512
1540
 
1513
 
        <li>The scope of an imported package identifier is the file block
 
1541
        <li>The scope of the package name of an imported package is the file block
1514
1542
            of the file containing the import declaration.</li>
1515
1543
 
1516
 
        <li>The scope of an identifier denoting a function parameter or
1517
 
            result variable is the function body.</li>
 
1544
        <li>The scope of an identifier denoting a method receiver, function parameter,
 
1545
            or result variable is the function body.</li>
1518
1546
 
1519
1547
        <li>The scope of a constant or variable identifier declared
1520
1548
            inside a function begins at the end of the ConstSpec or VarSpec
1544
1572
 
1545
1573
<p>
1546
1574
Labels are declared by <a href="#Labeled_statements">labeled statements</a> and are
1547
 
used in the <code>break</code>, <code>continue</code>, and <code>goto</code>
1548
 
statements (§<a href="#Break_statements">Break statements</a>, §<a href="#Continue_statements">Continue statements</a>, §<a href="#Goto_statements">Goto statements</a>).
 
1575
used in the <a href="#Break_statements">"break"</a>,
 
1576
<a href="#Continue_statements">"continue"</a>, and
 
1577
<a href="#Goto_statements">"goto"</a> statements.
1549
1578
It is illegal to define a label that is never used.
1550
1579
In contrast to other identifiers, labels are not block scoped and do
1551
1580
not conflict with identifiers that are not labels. The scope of a label
1845
1874
 
1846
1875
<p>
1847
1876
If a list of expressions is given, the variables are initialized
1848
 
by assigning the expressions to the variables (§<a href="#Assignments">Assignments</a>)
 
1877
by <a href="#Assignments">assigning</a> the expressions to the variables
1849
1878
in order; all expressions must be consumed and all variables initialized from them.
1850
1879
Otherwise, each variable is initialized to its <a href="#The_zero_value">zero value</a>.
1851
1880
</p>
1897
1926
 
1898
1927
<p>
1899
1928
Unlike regular variable declarations, a short variable declaration may redeclare variables provided they
1900
 
were originally declared in the same block with the same type, and at
 
1929
were originally declared earlier in the same block with the same type, and at
1901
1930
least one of the non-<a href="#Blank_identifier">blank</a> variables is new.  As a consequence, redeclaration
1902
1931
can only appear in a multi-variable short declaration.
1903
1932
Redeclaration does not introduce a new
1907
1936
<pre>
1908
1937
field1, offset := nextField(str, 0)
1909
1938
field2, offset := nextField(str, offset)  // redeclares offset
 
1939
a, a := 1, 2                              // illegal: double declaration of a or no new variable if a was declared elsewhere
1910
1940
</pre>
1911
1941
 
1912
1942
<p>
1913
1943
Short variable declarations may appear only inside functions.
1914
 
In some contexts such as the initializers for <code>if</code>,
1915
 
<code>for</code>, or <code>switch</code> statements,
1916
 
they can be used to declare local temporary variables (§<a href="#Statements">Statements</a>).
 
1944
In some contexts such as the initializers for
 
1945
<a href="#If_statements">"if"</a>,
 
1946
<a href="#For_statements">"for"</a>, or
 
1947
<a href="#Switch_statements">"switch"</a> statements,
 
1948
they can be used to declare local temporary variables.
1917
1949
</p>
1918
1950
 
1919
1951
<h3 id="Function_declarations">Function declarations</h3>
1924
1956
</p>
1925
1957
 
1926
1958
<pre class="ebnf">
1927
 
FunctionDecl = "func" FunctionName Signature [ Body ] .
 
1959
FunctionDecl = "func" FunctionName ( Function | Signature ) .
1928
1960
FunctionName = identifier .
1929
 
Body         = Block .
 
1961
Function     = Signature FunctionBody .
 
1962
FunctionBody = Block .
 
1963
</pre>
 
1964
 
 
1965
<p>
 
1966
If the function's <a href="#Function_types">signature</a> declares
 
1967
result parameters, the function body's statement list must end in
 
1968
a <a href="#Terminating_statements">terminating statement</a>.
 
1969
</p>
 
1970
 
 
1971
<pre>
 
1972
func findMarker(c <-chan int) int {
 
1973
        for i := range c {
 
1974
                if x := <-c; isMarker(x) {
 
1975
                        return x
 
1976
                }
 
1977
        }
 
1978
        // invalid: missing return statement.
 
1979
}
1930
1980
</pre>
1931
1981
 
1932
1982
<p>
1948
1998
<h3 id="Method_declarations">Method declarations</h3>
1949
1999
 
1950
2000
<p>
1951
 
A method is a function with a <i>receiver</i>.
1952
 
A method declaration binds an identifier, the <i>method name</i>, to a method.
1953
 
It also associates the method with the receiver's <i>base type</i>.
 
2001
A method is a <a href="#Function_declarations">function</a> with a <i>receiver</i>.
 
2002
A method declaration binds an identifier, the <i>method name</i>, to a method,
 
2003
and associates the method with the receiver's <i>base type</i>.
1954
2004
</p>
1955
2005
 
1956
2006
<pre class="ebnf">
1957
 
MethodDecl   = "func" Receiver MethodName Signature [ Body ] .
 
2007
MethodDecl   = "func" Receiver MethodName ( Function | Signature ) .
1958
2008
Receiver     = "(" [ identifier ] [ "*" ] BaseTypeName ")" .
1959
2009
BaseTypeName = identifier .
1960
2010
</pre>
1969
2019
</p>
1970
2020
 
1971
2021
<p>
1972
 
For a base type, the non-<a href="#Blank_identifier">blank</a> names of
1973
 
methods bound to it must be <a href="#Uniqueness_of_identifiers">unique</a>.
 
2022
A non-<a href="#Blank_identifier">blank</a> receiver identifier must be
 
2023
<a href="#Uniqueness_of_identifiers">unique</a> in the method signature.
 
2024
If the receiver's value is not referenced inside the body of the method,
 
2025
its identifier may be omitted in the declaration. The same applies in
 
2026
general to parameters of functions and methods.
 
2027
</p>
 
2028
 
 
2029
<p>
 
2030
For a base type, the non-blank names of methods bound to it must be unique.
1974
2031
If the base type is a <a href="#Struct_types">struct type</a>,
1975
2032
the non-blank method and field names must be distinct.
1976
2033
</p>
1997
2054
</p>
1998
2055
 
1999
2056
<p>
2000
 
If the receiver's value is not referenced inside the body of the method,
2001
 
its identifier may be omitted in the declaration. The same applies in
2002
 
general to parameters of functions and methods.
2003
 
</p>
2004
 
 
2005
 
<p>
2006
2057
The type of a method is the type of a function with the receiver as first
2007
2058
argument.  For instance, the method <code>Scale</code> has type
2008
2059
</p>
2026
2077
<h3 id="Operands">Operands</h3>
2027
2078
 
2028
2079
<p>
2029
 
Operands denote the elementary values in an expression.
 
2080
Operands denote the elementary values in an expression. An operand may be a
 
2081
literal, a (possibly <a href="#Qualified_identifiers">qualified</a>) identifier
 
2082
denoting a
 
2083
<a href="#Constant_declarations">constant</a>,
 
2084
<a href="#Variable_declarations">variable</a>, or
 
2085
<a href="#Function_declarations">function</a>,
 
2086
a <a href="#Method_expressions">method expression</a> yielding a function,
 
2087
or a parenthesized expression.
2030
2088
</p>
2031
2089
 
2032
2090
<pre class="ebnf">
2033
 
Operand    = Literal | QualifiedIdent | MethodExpr | "(" Expression ")" .
 
2091
Operand    = Literal | OperandName | MethodExpr | "(" Expression ")" .
2034
2092
Literal    = BasicLit | CompositeLit | FunctionLit .
2035
 
BasicLit   = int_lit | float_lit | imaginary_lit | char_lit | string_lit .
 
2093
BasicLit   = int_lit | float_lit | imaginary_lit | rune_lit | string_lit .
 
2094
OperandName = identifier | QualifiedIdent.
2036
2095
</pre>
2037
2096
 
2038
 
 
2039
2097
<h3 id="Qualified_identifiers">Qualified identifiers</h3>
2040
2098
 
2041
2099
<p>
2042
 
A qualified identifier is a non-<a href="#Blank_identifier">blank</a> identifier
2043
 
qualified by a package name prefix.
 
2100
A qualified identifier is an identifier qualified with a package name prefix.
 
2101
Both the package name and the identifier must not be
 
2102
<a href="#Blank_identifier">blank</a>.
2044
2103
</p>
2045
2104
 
2046
2105
<pre class="ebnf">
2047
 
QualifiedIdent = [ PackageName "." ] identifier .
 
2106
QualifiedIdent = PackageName "." identifier .
2048
2107
</pre>
2049
2108
 
2050
2109
<p>
2089
2148
to the respective field, element, and key types of the LiteralType;
2090
2149
there is no additional conversion.
2091
2150
The key is interpreted as a field name for struct literals,
2092
 
an index expression for array and slice literals, and a key for map literals.
 
2151
an index for array and slice literals, and a key for map literals.
2093
2152
For map literals, all elements must have a key. It is an error
2094
2153
to specify multiple elements with the same field name or
2095
2154
constant key value.
2101
2160
<ul>
2102
2161
        <li>A key must be a field name declared in the LiteralType.
2103
2162
        </li>
2104
 
        <li>A literal that does not contain any keys must
 
2163
        <li>An element list that does not contain any keys must
2105
2164
            list an element for each struct field in the
2106
2165
            order in which the fields are declared.
2107
2166
        </li>
2108
2167
        <li>If any element has a key, every element must have a key.
2109
2168
        </li>
2110
 
        <li>A literal that contains keys does not need to
 
2169
        <li>An element list that contains keys does not need to
2111
2170
            have an element for each struct field. Omitted fields
2112
2171
            get the zero value for that field.
2113
2172
        </li>
2114
2173
        <li>A literal may omit the element list; such a literal evaluates
2115
 
                to the zero value for its type.
 
2174
            to the zero value for its type.
2116
2175
        </li>
2117
2176
        <li>It is an error to specify an element for a non-exported
2118
2177
            field of a struct belonging to a different package.
2152
2211
</ul>
2153
2212
 
2154
2213
<p>
2155
 
Taking the address of a composite literal (§<a href="#Address_operators">Address operators</a>)
 
2214
<a href="#Address_operators">Taking the address</a> of a composite literal
2156
2215
generates a pointer to a unique instance of the literal's value.
2157
2216
</p>
2158
2217
<pre>
2198
2257
elements that are themselves composite literals may elide the respective
2199
2258
literal type if it is identical to the element type of <code>T</code>.
2200
2259
Similarly, elements that are addresses of composite literals may elide
2201
 
the <code>&amp;T</code> when the the element type is <code>*T</code>.
 
2260
the <code>&amp;T</code> when the element type is <code>*T</code>.
2202
2261
</p>
2203
2262
 
2204
2263
 
2251
2310
<h3 id="Function_literals">Function literals</h3>
2252
2311
 
2253
2312
<p>
2254
 
A function literal represents an anonymous function.
2255
 
It consists of a specification of the function type and a function body.
 
2313
A function literal represents an anonymous <a href="#Function_declarations">function</a>.
2256
2314
</p>
2257
2315
 
2258
2316
<pre class="ebnf">
2259
 
FunctionLit = FunctionType Body .
 
2317
FunctionLit = "func" Function .
2260
2318
</pre>
2261
2319
 
2262
2320
<pre>
2315
2373
m["foo"]
2316
2374
s[i : j + 1]
2317
2375
obj.color
2318
 
math.Sin
2319
2376
f.p[i].x()
2320
2377
</pre>
2321
2378
 
2323
2380
<h3 id="Selectors">Selectors</h3>
2324
2381
 
2325
2382
<p>
2326
 
A primary expression of the form
 
2383
For a <a href="#Primary_expressions">primary expression</a> <code>x</code>
 
2384
that is not a <a href="#Package_clause">package name</a>, the
 
2385
<i>selector expression</i>
2327
2386
</p>
2328
2387
 
2329
2388
<pre>
2331
2390
</pre>
2332
2391
 
2333
2392
<p>
2334
 
denotes the field or method <code>f</code> of the value denoted by <code>x</code>
2335
 
(or sometimes <code>*x</code>; see below). The identifier <code>f</code>
2336
 
is called the (field or method)
2337
 
<i>selector</i>; it must not be the <a href="#Blank_identifier">blank identifier</a>.
2338
 
The type of the expression is the type of <code>f</code>.
 
2393
denotes the field or method <code>f</code> of the value <code>x</code>
 
2394
(or sometimes <code>*x</code>; see below).
 
2395
The identifier <code>f</code> is called the (field or method) <i>selector</i>;
 
2396
it must not be the <a href="#Blank_identifier">blank identifier</a>.
 
2397
The type of the selector expression is the type of <code>f</code>.
 
2398
If <code>x</code> is a package name, see the section on
 
2399
<a href="#Qualified_identifiers">qualified identifiers</a>.
2339
2400
</p>
 
2401
 
2340
2402
<p>
2341
2403
A selector <code>f</code> may denote a field or method <code>f</code> of
2342
2404
a type <code>T</code>, or it may refer
2343
 
to a field or method <code>f</code> of a nested anonymous field of
2344
 
<code>T</code>.
 
2405
to a field or method <code>f</code> of a nested
 
2406
<a href="#Struct_types">anonymous field</a> of <code>T</code>.
2345
2407
The number of anonymous fields traversed
2346
2408
to reach <code>f</code> is called its <i>depth</i> in <code>T</code>.
2347
2409
The depth of a field or method <code>f</code>
2350
2412
an anonymous field <code>A</code> in <code>T</code> is the
2351
2413
depth of <code>f</code> in <code>A</code> plus one.
2352
2414
</p>
 
2415
 
2353
2416
<p>
2354
2417
The following rules apply to selectors:
2355
2418
</p>
 
2419
 
2356
2420
<ol>
2357
2421
<li>
2358
2422
For a value <code>x</code> of type <code>T</code> or <code>*T</code>
2364
2428
with shallowest depth, the selector expression is illegal.
2365
2429
</li>
2366
2430
<li>
2367
 
For a variable <code>x</code> of type <code>I</code>
2368
 
where <code>I</code> is an interface type,
2369
 
<code>x.f</code> denotes the actual method with name <code>f</code> of the value assigned
2370
 
to <code>x</code> if there is such a method.
2371
 
If no value or <code>nil</code> was assigned to <code>x</code>, <code>x.f</code> is illegal.
 
2431
For a variable <code>x</code> of type <code>I</code> where <code>I</code>
 
2432
is an interface type, <code>x.f</code> denotes the actual method with name
 
2433
<code>f</code> of the value assigned to <code>x</code>.
 
2434
If there is no method with name <code>f</code> in the
 
2435
<a href="#Method_sets">method set</a> of <code>I</code>, the selector
 
2436
expression is illegal.
2372
2437
</li>
2373
2438
<li>
2374
2439
In all other cases, <code>x.f</code> is illegal.
2375
2440
</li>
 
2441
<li>
 
2442
If <code>x</code> is of pointer type and has the value
 
2443
<code>nil</code> and <code>x.f</code> denotes a struct field,
 
2444
assigning to or evaluating <code>x.f</code>
 
2445
causes a <a href="#Run_time_panics">run-time panic</a>.
 
2446
</li>
 
2447
<li>
 
2448
If <code>x</code> is of interface type and has the value
 
2449
<code>nil</code>, <a href="#Calls">calling</a> or
 
2450
<a href="#Method_values">evaluating</a> the method <code>x.f</code>
 
2451
causes a <a href="#Run_time_panics">run-time panic</a>.
 
2452
</li>
2376
2453
</ol>
 
2454
 
2377
2455
<p>
2378
 
Selectors automatically dereference pointers to structs.
 
2456
Selectors automatically <a href="#Address_operators">dereference</a>
 
2457
pointers to structs.
2379
2458
If <code>x</code> is a pointer to a struct, <code>x.y</code>
2380
2459
is shorthand for <code>(*x).y</code>; if the field <code>y</code>
2381
2460
is also a pointer to a struct, <code>x.y.z</code> is shorthand
2384
2463
where <code>A</code> is also a struct type,
2385
2464
<code>x.f</code> is a shortcut for <code>(*x.A).f</code>.
2386
2465
</p>
 
2466
 
2387
2467
<p>
2388
2468
For example, given the declarations:
2389
2469
</p>
2421
2501
p.y   // ((*p).T1).y
2422
2502
p.x   // (*(*p).T0).x
2423
2503
 
2424
 
p.M2  // (*p).M2
2425
 
p.M1  // ((*p).T1).M1
2426
 
p.M0  // ((*p).T0).M0
 
2504
p.M2()  // (*p).M2()
 
2505
p.M1()  // ((*p).T1).M1()
 
2506
p.M0()  // ((*p).T0).M0()
2427
2507
</pre>
2428
2508
 
2429
2509
 
2434
2514
-->
2435
2515
 
2436
2516
 
2437
 
<h3 id="Indexes">Indexes</h3>
 
2517
<h3 id="Index_expressions">Index expressions</h3>
2438
2518
 
2439
2519
<p>
2440
2520
A primary expression of the form
2452
2532
</p>
2453
2533
 
2454
2534
<p>
 
2535
If <code>a</code> is not a map:
 
2536
</p>
 
2537
<ul>
 
2538
        <li>the index <code>x</code> must be of integer type or untyped;
 
2539
            it is <i>in range</i> if <code>0 &lt;= x &lt; len(a)</code>,
 
2540
            otherwise it is <i>out of range</i></li>
 
2541
        <li>a <a href="#Constants">constant</a> index must be non-negative
 
2542
            and representable by a value of type <code>int</code>
 
2543
</ul>
 
2544
 
 
2545
<p>
2455
2546
For <code>a</code> of type <code>A</code> or <code>*A</code>
2456
 
where <code>A</code> is an <a href="#Array_types">array type</a>,
2457
 
or for <code>a</code> of type <code>S</code> where <code>S</code> is a <a href="#Slice_types">slice type</a>:
 
2547
where <code>A</code> is an <a href="#Array_types">array type</a>:
2458
2548
</p>
2459
2549
<ul>
2460
 
        <li><code>x</code> must be an integer value and <code>0 &lt;= x &lt; len(a)</code></li>
 
2550
        <li>a <a href="#Constants">constant</a> index must be in range</li>
 
2551
        <li>if <code>a</code> is <code>nil</code> or if <code>x</code> is out of range at run time,
 
2552
            a <a href="#Run_time_panics">run-time panic</a> occurs</li>
2461
2553
        <li><code>a[x]</code> is the array element at index <code>x</code> and the type of
2462
 
          <code>a[x]</code> is the element type of <code>A</code></li>
2463
 
        <li>if <code>a</code> is <code>nil</code> or if the index <code>x</code> is out of range,
2464
 
        a <a href="#Run_time_panics">run-time panic</a> occurs</li>
 
2554
            <code>a[x]</code> is the element type of <code>A</code></li>
 
2555
</ul>
 
2556
 
 
2557
<p>
 
2558
For <code>a</code> of type <code>S</code> where <code>S</code> is a <a href="#Slice_types">slice type</a>:
 
2559
</p>
 
2560
<ul>
 
2561
        <li>if the slice is <code>nil</code> or if <code>x</code> is out of range at run time,
 
2562
            a <a href="#Run_time_panics">run-time panic</a> occurs</li>
 
2563
        <li><code>a[x]</code> is the slice element at index <code>x</code> and the type of
 
2564
            <code>a[x]</code> is the element type of <code>S</code></li>
2465
2565
</ul>
2466
2566
 
2467
2567
<p>
2469
2569
where <code>T</code> is a <a href="#String_types">string type</a>:
2470
2570
</p>
2471
2571
<ul>
2472
 
        <li><code>x</code> must be an integer value and <code>0 &lt;= x &lt; len(a)</code></li>
 
2572
        <li>a <a href="#Constants">constant</a> index must be in range
 
2573
            if the string <code>a</code> is also constant</li>
 
2574
        <li>if <code>x</code> is out of range at run time,
 
2575
            a <a href="#Run_time_panics">run-time panic</a> occurs</li>
2473
2576
        <li><code>a[x]</code> is the byte at index <code>x</code> and the type of
2474
 
          <code>a[x]</code> is <code>byte</code></li>
 
2577
            <code>a[x]</code> is <code>byte</code></li>
2475
2578
        <li><code>a[x]</code> may not be assigned to</li>
2476
 
        <li>if the index <code>x</code> is out of range,
2477
 
        a <a href="#Run_time_panics">run-time panic</a> occurs</li>
2478
2579
</ul>
2479
2580
 
2480
2581
<p>
2483
2584
</p>
2484
2585
<ul>
2485
2586
        <li><code>x</code>'s type must be
2486
 
        <a href="#Assignability">assignable</a>
2487
 
        to the key type of <code>M</code></li>
 
2587
            <a href="#Assignability">assignable</a>
 
2588
            to the key type of <code>M</code></li>
2488
2589
        <li>if the map contains an entry with key <code>x</code>,
2489
 
          <code>a[x]</code> is the map value with key <code>x</code>
2490
 
          and the type of <code>a[x]</code> is the value type of <code>M</code></li>
 
2590
            <code>a[x]</code> is the map value with key <code>x</code>
 
2591
            and the type of <code>a[x]</code> is the value type of <code>M</code></li>
2491
2592
        <li>if the map is <code>nil</code> or does not contain such an entry,
2492
 
          <code>a[x]</code> is the <a href="#The_zero_value">zero value</a>
2493
 
          for the value type of <code>M</code></li>
 
2593
            <code>a[x]</code> is the <a href="#The_zero_value">zero value</a>
 
2594
            for the value type of <code>M</code></li>
2494
2595
</ul>
2495
2596
 
2496
2597
<p>
2533
2634
</pre>
2534
2635
 
2535
2636
<p>
2536
 
constructs a substring or slice. The index expressions <code>low</code> and
 
2637
constructs a substring or slice. The indices <code>low</code> and
2537
2638
<code>high</code> select which elements appear in the result. The result has
2538
 
indexes starting at 0 and length equal to
 
2639
indices starting at 0 and length equal to
2539
2640
<code>high</code>&nbsp;-&nbsp;<code>low</code>.
2540
2641
After slicing the array <code>a</code>
2541
2642
</p>
2556
2657
</pre>
2557
2658
 
2558
2659
<p>
2559
 
For convenience, any of the index expressions may be omitted. A missing <code>low</code>
 
2660
For convenience, any of the indices may be omitted. A missing <code>low</code>
2560
2661
index defaults to zero; a missing <code>high</code> index defaults to the length of the
2561
2662
sliced operand:
2562
2663
</p>
2568
2669
</pre>
2569
2670
 
2570
2671
<p>
2571
 
For arrays or strings, the indexes <code>low</code> and <code>high</code> must
2572
 
satisfy 0 &lt;= <code>low</code> &lt;= <code>high</code> &lt;= length; for
2573
 
slices, the upper bound is the capacity rather than the length.
 
2672
For arrays or strings, the indices <code>low</code> and <code>high</code> are
 
2673
<i>in range</i> if <code>0</code> &lt;= <code>low</code> &lt;= <code>high</code> &lt;= <code>len(a)</code>,
 
2674
otherwise they are <i>out of range</i>.
 
2675
For slices, the upper index bound is the slice capacity <code>cap(a)</code> rather than the length.
 
2676
A <a href="#Constants">constant</a> index must be non-negative and representable by a value of type
 
2677
<code>int</code>.
 
2678
If both indices
 
2679
are constant, they must satisfy <code>low &lt;= high</code>. If <code>a</code> is <code>nil</code>
 
2680
or if the indices are out of range at run time, a <a href="#Run_time_panics">run-time panic</a> occurs.
2574
2681
</p>
2575
2682
 
2576
2683
<p>
2601
2708
More precisely, if <code>T</code> is not an interface type, <code>x.(T)</code> asserts
2602
2709
that the dynamic type of <code>x</code> is <a href="#Type_identity">identical</a>
2603
2710
to the type <code>T</code>.
 
2711
In this case, <code>T</code> must <a href="#Method_sets">implement</a> the (interface) type of <code>x</code>;
 
2712
otherwise the type assertion is invalid since it is not possible for <code>x</code>
 
2713
to store a value of type <code>T</code>.
2604
2714
If <code>T</code> is an interface type, <code>x.(T)</code> asserts that the dynamic type
2605
 
of <code>x</code> implements the interface <code>T</code> (§<a href="#Interface_types">Interface types</a>).
 
2715
of <code>x</code> implements the interface <code>T</code>.
2606
2716
</p>
2607
2717
<p>
2608
2718
If the type assertion holds, the value of the expression is the value
2609
2719
stored in <code>x</code> and its type is <code>T</code>. If the type assertion is false,
2610
2720
a <a href="#Run_time_panics">run-time panic</a> occurs.
2611
2721
In other words, even though the dynamic type of <code>x</code>
2612
 
is known only at run-time, the type of <code>x.(T)</code> is
 
2722
is known only at run time, the type of <code>x.(T)</code> is
2613
2723
known to be <code>T</code> in a correct program.
2614
2724
</p>
 
2725
 
 
2726
<pre>
 
2727
var x interface{} = 7  // x has dynamic type int and value 7
 
2728
i := x.(int)           // i has type int and value 7
 
2729
 
 
2730
type I interface { m() }
 
2731
var y I
 
2732
s := y.(string)        // illegal: string does not implement I (missing method m)
 
2733
r := y.(io.Reader)     // r has type io.Reader and y must implement both I and io.Reader
 
2734
</pre>
 
2735
 
2615
2736
<p>
2616
 
If a type assertion is used in an assignment or initialization of the form
 
2737
If a type assertion is used in an <a href="#Assignments">assignment</a> or initialization of the form
2617
2738
</p>
2618
2739
 
2619
2740
<pre>
2629
2750
is the <a href="#The_zero_value">zero value</a> for type <code>T</code>.
2630
2751
No run-time panic occurs in this case.
2631
2752
The type assertion in this construct thus acts like a function call
2632
 
returning a value and a boolean indicating success.  (§<a href="#Assignments">Assignments</a>)
 
2753
returning a value and a boolean indicating success.
2633
2754
</p>
2634
2755
 
2635
2756
 
2677
2798
</p>
2678
2799
 
2679
2800
<p>
2680
 
As a special case, if the return parameters of a function or method
 
2801
As a special case, if the return values of a function or method
2681
2802
<code>g</code> are equal in number and individually
2682
2803
assignable to the parameters of another function or method
2683
2804
<code>f</code>, then the call <code>f(g(<i>parameters_of_g</i>))</code>
2684
2805
will invoke <code>f</code> after binding the return values of
2685
2806
<code>g</code> to the parameters of <code>f</code> in order.  The call
2686
 
of <code>f</code> must contain no parameters other than the call of <code>g</code>.
 
2807
of <code>f</code> must contain no parameters other than the call of <code>g</code>,
 
2808
and <code>g</code> must have at least one return value.
2687
2809
If <code>f</code> has a final <code>...</code> parameter, it is
2688
2810
assigned the return values of <code>g</code> that remain after
2689
2811
assignment of regular parameters.
2805
2927
or be an untyped constant that can be converted to unsigned integer type.
2806
2928
If the left operand of a non-constant shift expression is an untyped constant,
2807
2929
the type of the constant is what it would be if the shift expression were
2808
 
replaced by its left operand alone; the type is <code>int</code> if it cannot
2809
 
be determined from the context (for instance, if the shift expression is an
2810
 
operand in a comparison against an untyped constant).
 
2930
replaced by its left operand alone.
2811
2931
</p>
2812
2932
 
2813
2933
<pre>
2816
2936
var j int32 = 1&lt;&lt;s     // 1 has type int32; j == 0
2817
2937
var k = uint64(1&lt;&lt;s)   // 1 has type uint64; k == 1&lt;&lt;33
2818
2938
var m int = 1.0&lt;&lt;s     // 1.0 has type int
2819
 
var n = 1.0&lt;&lt;s != 0    // 1.0 has type int; n == false if ints are 32bits in size
 
2939
var n = 1.0&lt;&lt;s != i    // 1.0 has type int; n == false if ints are 32bits in size
2820
2940
var o = 1&lt;&lt;s == 2&lt;&lt;s   // 1 and 2 have type int; o == true if ints are 32bits in size
2821
2941
var p = 1&lt;&lt;s == 1&lt;&lt;33  // illegal if ints are 32bits in size: 1 has type int, but 1&lt;&lt;33 overflows int
2822
2942
var u = 1.0&lt;&lt;s         // illegal: 1.0 has type float64, cannot shift
 
2943
var u1 = 1.0&lt;&lt;s != 0   // illegal: 1.0 has type float64, cannot shift
 
2944
var u2 = 1&lt;&lt;s != 1.0   // illegal: 1 has type float64, cannot shift
2823
2945
var v float32 = 1&lt;&lt;s   // illegal: 1 has type float32, cannot shift
2824
2946
var w int64 = 1.0&lt;&lt;33  // 1.0&lt;&lt;33 is a constant shift expression
2825
2947
</pre>
2834
2956
<p>
2835
2957
There are five precedence levels for binary operators.
2836
2958
Multiplication operators bind strongest, followed by addition
2837
 
operators, comparison operators, <code>&amp;&amp;</code> (logical and),
2838
 
and finally <code>||</code> (logical or):
 
2959
operators, comparison operators, <code>&amp;&amp;</code> (logical AND),
 
2960
and finally <code>||</code> (logical OR):
2839
2961
</p>
2840
2962
 
2841
2963
<pre class="grammar">
2878
3000
/    quotient               integers, floats, complex values
2879
3001
%    remainder              integers
2880
3002
 
2881
 
&amp;    bitwise and            integers
2882
 
|    bitwise or             integers
2883
 
^    bitwise xor            integers
2884
 
&amp;^   bit clear (and not)    integers
 
3003
&amp;    bitwise AND            integers
 
3004
|    bitwise OR             integers
 
3005
^    bitwise XOR            integers
 
3006
&amp;^   bit clear (AND NOT)    integers
2885
3007
 
2886
3008
&lt;&lt;   left shift             integer &lt;&lt; unsigned integer
2887
3009
&gt;&gt;   right shift            integer &gt;&gt; unsigned integer
2938
3060
</pre>
2939
3061
 
2940
3062
<p>
2941
 
If the divisor is zero, a <a href="#Run_time_panics">run-time panic</a> occurs.
2942
 
If the dividend is positive and the divisor is a constant power of 2,
 
3063
If the divisor is a <a href="#Constants">constant</a>, it must not be zero.
 
3064
If the divisor is zero at run time, a <a href="#Run_time_panics">run-time panic</a> occurs.
 
3065
If the dividend is non-negative and the divisor is a constant power of 2,
2943
3066
the division may be replaced by a right shift, and computing the remainder may
2944
 
be replaced by a bitwise "and" operation:
 
3067
be replaced by a bitwise AND operation:
2945
3068
</p>
2946
3069
 
2947
3070
<pre>
2976
3099
</pre>
2977
3100
 
2978
3101
<p>
2979
 
For floating-point numbers,
 
3102
For floating-point and complex numbers,
2980
3103
<code>+x</code> is the same as <code>x</code>,
2981
3104
while <code>-x</code> is the negation of <code>x</code>.
2982
 
The result of a floating-point division by zero is not specified beyond the
 
3105
The result of a floating-point or complex division by zero is not specified beyond the
2983
3106
IEEE-754 standard; whether a <a href="#Run_time_panics">run-time panic</a>
2984
3107
occurs is implementation-specific.
2985
3108
</p>
2990
3113
For unsigned integer values, the operations <code>+</code>,
2991
3114
<code>-</code>, <code>*</code>, and <code>&lt;&lt;</code> are
2992
3115
computed modulo 2<sup><i>n</i></sup>, where <i>n</i> is the bit width of
2993
 
the unsigned integer's type
2994
 
(§<a href="#Numeric_types">Numeric types</a>). Loosely speaking, these unsigned integer operations
 
3116
the <a href="#Numeric_types">unsigned integer</a>'s type.
 
3117
Loosely speaking, these unsigned integer operations
2995
3118
discard high bits upon overflow, and programs may rely on ``wrap around''.
2996
3119
</p>
2997
3120
<p>
3008
3131
<h3 id="Comparison_operators">Comparison operators</h3>
3009
3132
 
3010
3133
<p>
3011
 
Comparison operators compare two operands and yield a boolean value.
 
3134
Comparison operators compare two operands and yield an untyped boolean value.
3012
3135
</p>
3013
3136
 
3014
3137
<pre class="grammar">
3068
3191
 
3069
3192
        <li>
3070
3193
        Channel values are comparable.
3071
 
        Two channel values are equal if they were created by the same call to <code>make</code>
3072
 
        (§<a href="#Making_slices_maps_and_channels">Making slices, maps, and channels</a>)
 
3194
        Two channel values are equal if they were created by the same call to
 
3195
        <a href="#Making_slices_maps_and_channels"><code>make</code></a>
3073
3196
        or if both have value <code>nil</code>.
3074
3197
        </li>
3075
3198
 
3116
3239
is also allowed and follows from the general rules above.
3117
3240
</p>
3118
3241
 
3119
 
<p>
3120
 
The result of a comparison can be assigned to any boolean type.
3121
 
If the context does not demand a specific boolean type,
3122
 
the result has type <code>bool</code>.
3123
 
</p>
3124
 
 
3125
3242
<pre>
 
3243
const c = 3 < 4            // c is the untyped bool constant true
 
3244
 
3126
3245
type MyBool bool
3127
 
 
3128
3246
var x, y int
3129
3247
var (
3130
 
        b1 MyBool = x == y // result of comparison has type MyBool
3131
 
        b2 bool   = x == y // result of comparison has type bool
3132
 
        b3        = x == y // result of comparison has type bool
 
3248
        // The result of a comparison is an untyped bool.
 
3249
        // The usual assignment rules apply.
 
3250
        b3        = x == y // b3 has type bool
 
3251
        b4 bool   = x == y // b4 has type bool
 
3252
        b5 MyBool = x == y // b5 has type MyBool
3133
3253
)
3134
3254
</pre>
3135
3255
 
3142
3262
</p>
3143
3263
 
3144
3264
<pre class="grammar">
3145
 
&amp;&amp;    conditional and    p &amp;&amp; q  is  "if p then q else false"
3146
 
||    conditional or     p || q  is  "if p then true else q"
3147
 
!     not                !p      is  "not p"
 
3265
&amp;&amp;    conditional AND    p &amp;&amp; q  is  "if p then q else false"
 
3266
||    conditional OR     p || q  is  "if p then true else q"
 
3267
!     NOT                !p      is  "not p"
3148
3268
</pre>
3149
3269
 
3150
3270
 
3158
3278
operation; or a field selector of an addressable struct operand;
3159
3279
or an array indexing operation of an addressable array.
3160
3280
As an exception to the addressability requirement, <code>x</code> may also be a
 
3281
(possibly parenthesized)
3161
3282
<a href="#Composite_literals">composite literal</a>.
3162
3283
</p>
3163
3284
<p>
3171
3292
<pre>
3172
3293
&amp;x
3173
3294
&amp;a[f(2)]
 
3295
&amp;Point{2, 3}
3174
3296
*p
3175
3297
*pf(x)
3176
3298
</pre>
3181
3303
<p>
3182
3304
For an operand <code>ch</code> of <a href="#Channel_types">channel type</a>,
3183
3305
the value of the receive operation <code>&lt;-ch</code> is the value received
3184
 
from the channel <code>ch</code>. The type of the value is the element type of
3185
 
the channel. The expression blocks until a value is available.
 
3306
from the channel <code>ch</code>. The channel direction must permit receive operations,
 
3307
and the type of the receive operation is the element type of the channel.
 
3308
The expression blocks until a value is available.
3186
3309
Receiving from a <code>nil</code> channel blocks forever.
 
3310
Receiving from a <a href="#Close">closed</a> channel always succeeds,
 
3311
immediately returning the element type's <a href="#The_zero_value">zero
 
3312
value</a>.
3187
3313
</p>
3188
3314
 
3189
3315
<pre>
3204
3330
</pre>
3205
3331
 
3206
3332
<p>
3207
 
yields an additional result.
3208
 
The boolean variable <code>ok</code> indicates whether
3209
 
the received value was sent on the channel (<code>true</code>)
3210
 
or is a <a href="#The_zero_value">zero value</a> returned
3211
 
because the channel is closed and empty (<code>false</code>).
 
3333
yields an additional result of type <code>bool</code> reporting whether the
 
3334
communication succeeded. The value of <code>ok</code> is <code>true</code>
 
3335
if the value received was delivered by a successful send operation to the
 
3336
channel, or <code>false</code> if it is a zero value generated because the
 
3337
channel is closed and empty.
3212
3338
</p>
3213
3339
 
3214
3340
<!--
3230
3356
 
3231
3357
<pre class="ebnf">
3232
3358
MethodExpr    = ReceiverType "." MethodName .
3233
 
ReceiverType  = TypeName | "(" "*" TypeName ")" .
 
3359
ReceiverType  = TypeName | "(" "*" TypeName ")" | "(" ReceiverType ")" .
3234
3360
</pre>
3235
3361
 
3236
3362
<p>
3245
3371
}
3246
3372
func (tv  T) Mv(a int) int         { return 0 }  // value receiver
3247
3373
func (tp *T) Mp(f float32) float32 { return 1 }  // pointer receiver
 
3374
 
3248
3375
var t T
3249
3376
</pre>
3250
3377
 
3267
3394
 
3268
3395
<p>
3269
3396
That function may be called normally with an explicit receiver, so
3270
 
these three invocations are equivalent:
 
3397
these five invocations are equivalent:
3271
3398
</p>
3272
3399
 
3273
3400
<pre>
3274
3401
t.Mv(7)
3275
3402
T.Mv(t, 7)
3276
 
f := T.Mv; f(t, 7)
 
3403
(T).Mv(t, 7)
 
3404
f1 := T.Mv; f1(t, 7)
 
3405
f2 := (T).Mv; f2(t, 7)
3277
3406
</pre>
3278
3407
 
3279
3408
<p>
3328
3457
That is, given <code>f := T.Mv</code>, <code>f</code> is invoked
3329
3458
as <code>f(t, 7)</code> not <code>t.f(7)</code>.
3330
3459
To construct a function that binds the receiver, use a
3331
 
<a href="#Function_literals">closure</a>.
 
3460
<a href="#Function_literals">function literal</a> or
 
3461
<a href="#Method_values">method value</a>.
3332
3462
</p>
3333
3463
 
3334
3464
<p>
3336
3466
The resulting function takes an explicit receiver of that interface type.
3337
3467
</p>
3338
3468
 
 
3469
<h3 id="Method_values">Method values</h3>
 
3470
 
 
3471
<p>
 
3472
If the expression <code>x</code> has static type <code>T</code> and
 
3473
<code>M</code> is in the <a href="#Method_sets">method set</a> of type <code>T</code>,
 
3474
<code>x.M</code> is called a <i>method value</i>.
 
3475
The method value <code>x.M</code> is a function value that is callable
 
3476
with the same arguments as a method call of <code>x.M</code>.
 
3477
The expression <code>x</code> is evaluated and saved during the evaluation of the
 
3478
method value; the saved copy is then used as the receiver in any calls,
 
3479
which may be executed later.
 
3480
</p>
 
3481
 
 
3482
<p>
 
3483
The type <code>T</code> may be an interface or non-interface type.
 
3484
</p>
 
3485
 
 
3486
<p>
 
3487
As in the discussion of <a href="#Method_expressions">method expressions</a> above,
 
3488
consider a struct type <code>T</code> with two methods,
 
3489
<code>Mv</code>, whose receiver is of type <code>T</code>, and
 
3490
<code>Mp</code>, whose receiver is of type <code>*T</code>.
 
3491
</p>
 
3492
 
 
3493
<pre>
 
3494
type T struct {
 
3495
        a int
 
3496
}
 
3497
func (tv  T) Mv(a int) int         { return 0 }  // value receiver
 
3498
func (tp *T) Mp(f float32) float32 { return 1 }  // pointer receiver
 
3499
 
 
3500
var t T
 
3501
var pt *T
 
3502
func makeT() T
 
3503
</pre>
 
3504
 
 
3505
<p>
 
3506
The expression
 
3507
</p>
 
3508
 
 
3509
<pre>
 
3510
t.Mv
 
3511
</pre>
 
3512
 
 
3513
<p>
 
3514
yields a function value of type
 
3515
</p>
 
3516
 
 
3517
<pre>
 
3518
func(int) int
 
3519
</pre>
 
3520
 
 
3521
<p>
 
3522
These two invocations are equivalent:
 
3523
</p>
 
3524
 
 
3525
<pre>
 
3526
t.Mv(7)
 
3527
f := t.Mv; f(7)
 
3528
</pre>
 
3529
 
 
3530
<p>
 
3531
Similarly, the expression
 
3532
</p>
 
3533
 
 
3534
<pre>
 
3535
pt.Mp
 
3536
</pre>
 
3537
 
 
3538
<p>
 
3539
yields a function value of type
 
3540
</p>
 
3541
 
 
3542
<pre>
 
3543
func(float32) float32
 
3544
</pre>
 
3545
 
 
3546
<p>
 
3547
As with <a href="#Selectors">selectors</a>, a reference to a non-interface method with a value receiver
 
3548
using a pointer will automatically dereference that pointer: <code>pt.Mv</code> is equivalent to <code>(*pt).Mv</code>.
 
3549
</p>
 
3550
 
 
3551
<p>
 
3552
As with <a href="#Calls">method calls</a>, a reference to a non-interface method with a pointer receiver
 
3553
using an addressable value will automatically take the address of that value: <code>t.Mv</code> is equivalent to <code>(&amp;t).Mv</code>.
 
3554
</p>
 
3555
 
 
3556
<pre>
 
3557
f := t.Mv; f(7)   // like t.Mv(7)
 
3558
f := pt.Mp; f(7)  // like pt.Mp(7)
 
3559
f := pt.Mv; f(7)  // like (*pt).Mv(7)
 
3560
f := t.Mp; f(7)   // like (&amp;t).Mp(7)
 
3561
f := makeT().Mp   // invalid: result of makeT() is not addressable
 
3562
</pre>
 
3563
 
 
3564
<p>
 
3565
Although the examples above use non-interface types, it is also legal to create a method value
 
3566
from a value of interface type.
 
3567
</p>
 
3568
 
 
3569
<pre>
 
3570
var i interface { M(int) } = myVal
 
3571
f := i.M; f(7)  // like i.M(7)
 
3572
</pre>
 
3573
 
3339
3574
<h3 id="Conversions">Conversions</h3>
3340
3575
 
3341
3576
<p>
3345
3580
</p>
3346
3581
 
3347
3582
<pre class="ebnf">
3348
 
Conversion = Type "(" Expression ")" .
 
3583
Conversion = Type "(" Expression [ "," ] ")" .
3349
3584
</pre>
3350
3585
 
3351
3586
<p>
3352
 
If the type starts with an operator it must be parenthesized:
 
3587
If the type starts with the operator <code>*</code> or <code>&lt;-</code>,
 
3588
or if the type starts with the keyword <code>func</code>
 
3589
and has no result list, it must be parenthesized when
 
3590
necessary to avoid ambiguity:
3353
3591
</p>
3354
3592
 
3355
3593
<pre>
3356
3594
*Point(p)        // same as *(Point(p))
3357
 
(*Point)(p)      // p is converted to (*Point)
 
3595
(*Point)(p)      // p is converted to *Point
3358
3596
&lt;-chan int(c)    // same as &lt;-(chan int(c))
3359
 
(&lt;-chan int)(c)  // c is converted to (&lt;-chan int)
 
3597
(&lt;-chan int)(c)  // c is converted to &lt;-chan int
 
3598
func()(x)        // function signature func() x
 
3599
(func())(x)      // x is converted to func()
 
3600
(func() int)(x)  // x is converted to func() int
 
3601
func() int(x)    // x is converted to func() int (unambiguous)
3360
3602
</pre>
3361
3603
 
3362
3604
<p>
3369
3611
        <code>x</code> is representable by a value of type <code>T</code>.
3370
3612
        </li>
3371
3613
        <li>
 
3614
        <code>x</code> is a floating-point constant,
 
3615
        <code>T</code> is a floating-point type,
 
3616
        and <code>x</code> is representable by a value
 
3617
        of type <code>T</code> after rounding using
 
3618
        IEEE 754 round-to-even rules.
 
3619
        The constant <code>T(x)</code> is the rounded value.
 
3620
        </li>
 
3621
        <li>
3372
3622
        <code>x</code> is an integer constant and <code>T</code> is a
3373
3623
        <a href="#String_types">string type</a>.
3374
 
        The same rule as for non-constant <code>x</code> applies in this case
3375
 
        (§<a href="#Conversions_to_and_from_a_string_type">Conversions to and from a string type</a>).
 
3624
        The <a href="#Conversions_to_and_from_a_string_type">same rule</a>
 
3625
        as for non-constant <code>x</code> applies in this case.
3376
3626
        </li>
3377
3627
</ul>
3378
3628
 
3384
3634
uint(iota)               // iota value of type uint
3385
3635
float32(2.718281828)     // 2.718281828 of type float32
3386
3636
complex128(1)            // 1.0 + 0.0i of type complex128
 
3637
float32(0.49999999)      // 0.5 of type float32
3387
3638
string('x')              // "x" of type string
3388
3639
string(0x266c)           // "♬" of type string
3389
3640
MyString("foo" + "bar")  // "foobar" of type MyString
3419
3670
        <code>x</code>'s type and <code>T</code> are both complex types.
3420
3671
        </li>
3421
3672
        <li>
3422
 
        <code>x</code> is an integer or has type <code>[]byte</code> or
3423
 
        <code>[]rune</code> and <code>T</code> is a string type.
 
3673
        <code>x</code> is an integer or a slice of bytes or runes
 
3674
        and <code>T</code> is a string type.
3424
3675
        </li>
3425
3676
        <li>
3426
 
        <code>x</code> is a string and <code>T</code> is <code>[]byte</code> or
3427
 
        <code>[]rune</code>.
 
3677
        <code>x</code> is a string and <code>T</code> is a slice of bytes or runes.
3428
3678
        </li>
3429
3679
</ul>
3430
3680
 
3490
3740
 
3491
3741
<pre>
3492
3742
string('a')       // "a"
3493
 
string(-1)        // "\ufffd" == "\xef\xbf\xbd "
 
3743
string(-1)        // "\ufffd" == "\xef\xbf\xbd"
3494
3744
string(0xf8)      // "\u00f8" == "ø" == "\xc3\xb8"
3495
3745
type MyString string
3496
3746
MyString(0x65e5)  // "\u65e5" == "日" == "\xe6\x97\xa5"
3551
3801
 
3552
3802
<p>
3553
3803
Constant expressions may contain only <a href="#Constants">constant</a>
3554
 
operands and are evaluated at compile-time.
 
3804
operands and are evaluated at compile time.
3555
3805
</p>
3556
3806
 
3557
3807
<p>
3560
3810
respectively.
3561
3811
Except for shift operations, if the operands of a binary operation are
3562
3812
different kinds of untyped constants, the operation and, for non-boolean operations, the result use
3563
 
the kind that appears later in this list: integer, character, floating-point, complex.
 
3813
the kind that appears later in this list: integer, rune, floating-point, complex.
3564
3814
For example, an untyped integer constant divided by an
3565
3815
untyped complex constant yields an untyped complex constant.
3566
3816
</p>
3570
3820
an untyped boolean constant.  If the left operand of a constant
3571
3821
<a href="#Operators">shift expression</a> is an untyped constant, the
3572
3822
result is an integer constant; otherwise it is a constant of the same
3573
 
type as the left operand, which must be of integer type
3574
 
(§<a href="#Arithmetic_operators">Arithmetic operators</a>).
 
3823
type as the left operand, which must be of
 
3824
<a href="#Numeric_types">integer type</a>.
3575
3825
Applying all other operators to untyped constants results in an untyped
3576
3826
constant of the same kind (that is, a boolean, integer, floating-point,
3577
3827
complex, or string constant).
3581
3831
const a = 2 + 3.0          // a == 5.0   (untyped floating-point constant)
3582
3832
const b = 15 / 4           // b == 3     (untyped integer constant)
3583
3833
const c = 15 / 4.0         // c == 3.75  (untyped floating-point constant)
3584
 
const Θ float64 = 3/2      // Θ == 1.5   (type float64)
 
3834
const Θ float64 = 3/2      // Θ == 1.0   (type float64, 3/2 is integer division)
 
3835
const Π float64 = 3/2.     // Π == 1.5   (type float64, 3/2. is float division)
3585
3836
const d = 1 &lt;&lt; 3.0         // d == 8     (untyped integer constant)
3586
3837
const e = 1.0 &lt;&lt; 3         // e == 8     (untyped integer constant)
3587
 
const f = int32(1) &lt;&lt; 33   // f == 0     (type int32)
 
3838
const f = int32(1) &lt;&lt; 33   // illegal    (constant 8589934592 overflows int32)
3588
3839
const g = float64(2) &gt;&gt; 1  // illegal    (float64(2) is a typed floating-point constant)
3589
3840
const h = "foo" &gt; "bar"    // h == true  (untyped boolean constant)
3590
3841
const j = true             // j == true  (untyped boolean constant)
3591
 
const k = 'w' + 1          // k == 'x'   (untyped character constant)
 
3842
const k = 'w' + 1          // k == 'x'   (untyped rune constant)
3592
3843
const l = "hi"             // l == "hi"  (untyped string constant)
3593
3844
const m = string(k)        // m == "x"   (type string)
3594
3845
const Σ = 1 - 0.707i       //            (untyped complex constant)
3598
3849
 
3599
3850
<p>
3600
3851
Applying the built-in function <code>complex</code> to untyped
3601
 
integer, character, or floating-point constants yields
 
3852
integer, rune, or floating-point constants yields
3602
3853
an untyped complex constant.
3603
3854
</p>
3604
3855
 
3605
3856
<pre>
3606
 
const ic = complex(0, c)   // ic == 3.75i (untyped complex constant)
3607
 
const iΘ = complex(0, Θ)   // iΘ == 1.5i  (type complex128)
 
3857
const ic = complex(0, c)   // ic == 3.75i  (untyped complex constant)
 
3858
const iΘ = complex(0, Θ)   // iΘ == 1.5i   (type complex128)
3608
3859
</pre>
3609
3860
 
3610
3861
<p>
3614
3865
</p>
3615
3866
 
3616
3867
<pre>
3617
 
const Huge = 1 &lt;&lt; 100
3618
 
const Four int8 = Huge &gt;&gt; 98
 
3868
const Huge = 1 &lt;&lt; 100         // Huge == 1267650600228229401496703205376  (untyped integer constant)
 
3869
const Four int8 = Huge &gt;&gt; 98  // Four == 4                                (type int8)
 
3870
</pre>
 
3871
 
 
3872
<p>
 
3873
The divisor of a constant division or remainder operation must not be zero:
 
3874
</p>
 
3875
 
 
3876
<pre>
 
3877
3.14 / 0.0   // illegal: division by zero
3619
3878
</pre>
3620
3879
 
3621
3880
<p>
3626
3885
<pre>
3627
3886
uint(-1)     // -1 cannot be represented as a uint
3628
3887
int(3.14)    // 3.14 cannot be represented as an int
3629
 
int64(Huge)  // 1&lt;&lt;100 cannot be represented as an int64
3630
 
Four * 300   // 300 cannot be represented as an int8
3631
 
Four * 100   // 400 cannot be represented as an int8
 
3888
int64(Huge)  // 1267650600228229401496703205376 cannot be represented as an int64
 
3889
Four * 300   // operand 300 cannot be represented as an int8 (type of Four)
 
3890
Four * 100   // product 400 cannot be represented as an int8 (type of Four)
3632
3891
</pre>
3633
3892
 
3634
3893
<p>
3639
3898
 
3640
3899
<pre>
3641
3900
^1         // untyped integer constant, equal to -2
3642
 
uint8(^1)  // error, same as uint8(-2), out of range
 
3901
uint8(^1)  // illegal: same as uint8(-2), -2 cannot be represented as a uint8
3643
3902
^uint8(1)  // typed uint8 constant, same as 0xFF ^ uint8(1) = uint8(0xFE)
3644
3903
int8(^1)   // same as int8(-2)
3645
3904
^int8(1)   // same as -1 ^ int8(1) = -2
3668
3927
<h3 id="Order_of_evaluation">Order of evaluation</h3>
3669
3928
 
3670
3929
<p>
3671
 
When evaluating the elements of an assignment or expression,
3672
 
all function calls, method calls and
 
3930
When evaluating the <a href="#Operands">operands</a> of an expression,
 
3931
<a href="#Assignments">assignment</a>, or
 
3932
<a href="#Return_statements">return statement</a>,
 
3933
all function calls, method calls, and
3673
3934
communication operations are evaluated in lexical left-to-right
3674
3935
order.
3675
3936
</p>
3689
3950
of <code>y</code> is not specified.
3690
3951
</p>
3691
3952
 
 
3953
<pre>
 
3954
a := 1
 
3955
f := func() int { a = 2; return 3 }
 
3956
x := []int{a, f()}  // x may be [1, 3] or [2, 3]: evaluation order between a and f() is not specified
 
3957
</pre>
 
3958
 
3692
3959
<p>
3693
3960
Floating-point operations within a single expression are evaluated according to
3694
3961
the associativity of the operators.  Explicit parentheses affect the evaluation
3713
3980
SimpleStmt = EmptyStmt | ExpressionStmt | SendStmt | IncDecStmt | Assignment | ShortVarDecl .
3714
3981
</pre>
3715
3982
 
 
3983
<h3 id="Terminating_statements">Terminating statements</h3>
 
3984
 
 
3985
<p>
 
3986
A terminating statement is one of the following:
 
3987
</p>
 
3988
 
 
3989
<ol>
 
3990
<li>
 
3991
        A <a href="#Return_statements">"return"</a> or
 
3992
        <a href="#Goto_statements">"goto"</a> statement.
 
3993
        <!-- ul below only for regular layout -->
 
3994
        <ul> </ul>
 
3995
</li>
 
3996
 
 
3997
<li>
 
3998
        A call to the built-in function
 
3999
        <a href="#Handling_panics"><code>panic</code></a>.
 
4000
        <!-- ul below only for regular layout -->
 
4001
        <ul> </ul>
 
4002
</li>
 
4003
 
 
4004
<li>
 
4005
        A <a href="#Blocks">block</a> in which the statement list ends in a terminating statement.
 
4006
        <!-- ul below only for regular layout -->
 
4007
        <ul> </ul>
 
4008
</li>
 
4009
 
 
4010
<li>
 
4011
        An <a href="#If_statements">"if" statement</a> in which:
 
4012
        <ul>
 
4013
        <li>the "else" branch is present, and</li>
 
4014
        <li>both branches are terminating statements.</li>
 
4015
        </ul>
 
4016
</li>
 
4017
 
 
4018
<li>
 
4019
        A <a href="#For_statements">"for" statement</a> in which:
 
4020
        <ul>
 
4021
        <li>there are no "break" statements referring to the "for" statement, and</li>
 
4022
        <li>the loop condition is absent.</li>
 
4023
        </ul>
 
4024
</li>
 
4025
 
 
4026
<li>
 
4027
        A <a href="#Switch_statements">"switch" statement</a> in which:
 
4028
        <ul>
 
4029
        <li>there are no "break" statements referring to the "switch" statement,</li>
 
4030
        <li>there is a default case, and</li>
 
4031
        <li>the statement lists in each case, including the default, end in a terminating
 
4032
            statement, or a possibly labeled <a href="#Fallthrough_statements">"fallthrough"
 
4033
            statement</a>.</li>
 
4034
        </ul>
 
4035
</li>
 
4036
 
 
4037
<li>
 
4038
        A <a href="#Select_statements">"select" statement</a> in which:
 
4039
        <ul>
 
4040
        <li>there are no "break" statements referring to the "select" statement, and</li>
 
4041
        <li>the statement lists in each case, including the default if present,
 
4042
            end in a terminating statement.</li>
 
4043
        </ul>
 
4044
</li>
 
4045
 
 
4046
<li>
 
4047
        A <a href="#Labeled_statements">labeled statement</a> labeling
 
4048
        a terminating statement.
 
4049
</li>
 
4050
</ol>
 
4051
 
 
4052
<p>
 
4053
All other statements are not terminating.
 
4054
</p>
 
4055
 
 
4056
<p>
 
4057
A <a href="#Blocks">statement list</a> ends in a terminating statement if the list
 
4058
is not empty and its final statement is terminating.
 
4059
</p>
 
4060
 
3716
4061
 
3717
4062
<h3 id="Empty_statements">Empty statements</h3>
3718
4063
 
3745
4090
<h3 id="Expression_statements">Expression statements</h3>
3746
4091
 
3747
4092
<p>
3748
 
Function calls, method calls, and receive operations
 
4093
With the exception of specific built-in functions,
 
4094
function and method <a href="#Calls">calls</a> and
 
4095
<a href="#Receive_operator">receive operations</a>
3749
4096
can appear in statement context. Such statements may be parenthesized.
3750
4097
</p>
3751
4098
 
3753
4100
ExpressionStmt = Expression .
3754
4101
</pre>
3755
4102
 
 
4103
<p>
 
4104
The following built-in functions are not permitted in statement context:
 
4105
</p>
 
4106
 
 
4107
<pre>
 
4108
append cap complex imag len make new real
 
4109
unsafe.Alignof unsafe.Offsetof unsafe.Sizeof
 
4110
</pre>
 
4111
 
3756
4112
<pre>
3757
4113
h(x+y)
3758
4114
f.Close()
3759
4115
&lt;-ch
3760
4116
(&lt;-ch)
 
4117
len("foo")  // illegal if len is the built-in function
3761
4118
</pre>
3762
4119
 
3763
4120
 
3765
4122
 
3766
4123
<p>
3767
4124
A send statement sends a value on a channel.
3768
 
The channel expression must be of <a href="#Channel_types">channel type</a>
3769
 
and the type of the value must be <a href="#Assignability">assignable</a>
 
4125
The channel expression must be of <a href="#Channel_types">channel type</a>,
 
4126
the channel direction must permit send operations,
 
4127
and the type of the value to be sent must be <a href="#Assignability">assignable</a>
3770
4128
to the channel's element type.
3771
4129
</p>
3772
4130
 
3884
4242
 
3885
4243
<p>
3886
4244
The assignment proceeds in two phases.
3887
 
First, the operands of <a href="#Indexes">index expressions</a>
 
4245
First, the operands of <a href="#Index_expressions">index expressions</a>
3888
4246
and <a href="#Address_operators">pointer indirections</a>
3889
4247
(including implicit pointer indirections in <a href="#Selectors">selectors</a>)
3890
4248
on the left and the expressions on the right are all
3926
4284
to type <code>bool</code>, <code>rune</code>, <code>int</code>, <code>float64</code>,
3927
4285
<code>complex128</code> or <code>string</code>
3928
4286
respectively, depending on whether the value is a
3929
 
boolean, character, integer, floating-point, complex, or string constant.
 
4287
boolean, rune, integer, floating-point, complex, or string constant.
3930
4288
</p>
3931
4289
 
3932
4290
 
4006
4364
 
4007
4365
<pre class="ebnf">
4008
4366
ExprSwitchStmt = "switch" [ SimpleStmt ";" ] [ Expression ] "{" { ExprCaseClause } "}" .
4009
 
ExprCaseClause = ExprSwitchCase ":" { Statement ";" } .
 
4367
ExprCaseClause = ExprSwitchCase ":" StatementList .
4010
4368
ExprSwitchCase = "case" ExpressionList | "default" .
4011
4369
</pre>
4012
4370
 
4013
4371
<p>
4014
 
In a case or default clause,
4015
 
the last statement only may be a "fallthrough" statement
4016
 
(§<a href="#Fallthrough_statements">Fallthrough statement</a>) to
 
4372
In a case or default clause, the last non-empty statement
 
4373
may be a (possibly <a href="#Labeled_statements">labeled</a>)
 
4374
<a href="#Fallthrough_statements">"fallthrough" statement</a> to
4017
4375
indicate that control should flow from the end of this clause to
4018
4376
the first statement of the next clause.
4019
4377
Otherwise control flows to the end of the "switch" statement.
 
4378
A "fallthrough" statement may appear as the last statement of all
 
4379
but the last clause of an expression switch.
4020
4380
</p>
4021
4381
 
4022
4382
<p>
4049
4409
A type switch compares types rather than values. It is otherwise similar
4050
4410
to an expression switch. It is marked by a special switch expression that
4051
4411
has the form of a <a href="#Type_assertions">type assertion</a>
4052
 
using the reserved word <code>type</code> rather than an actual type.
4053
 
Cases then match literal types against the dynamic type of the expression
4054
 
in the type assertion.
 
4412
using the reserved word <code>type</code> rather than an actual type:
 
4413
</p>
 
4414
 
 
4415
<pre>
 
4416
switch x.(type) {
 
4417
// cases
 
4418
}
 
4419
</pre>
 
4420
 
 
4421
<p>
 
4422
Cases then match actual types <code>T</code> against the dynamic type of the
 
4423
expression <code>x</code>. As with type assertions, <code>x</code> must be of
 
4424
<a href="#Interface_types">interface type</a>, and each non-interface type
 
4425
<code>T</code> listed in a case must implement the type of <code>x</code>.
4055
4426
</p>
4056
4427
 
4057
4428
<pre class="ebnf">
4058
4429
TypeSwitchStmt  = "switch" [ SimpleStmt ";" ] TypeSwitchGuard "{" { TypeCaseClause } "}" .
4059
4430
TypeSwitchGuard = [ identifier ":=" ] PrimaryExpr "." "(" "type" ")" .
4060
 
TypeCaseClause  = TypeSwitchCase ":" { Statement ";" } .
 
4431
TypeCaseClause  = TypeSwitchCase ":" StatementList .
4061
4432
TypeSwitchCase  = "case" TypeList | "default" .
4062
4433
TypeList        = Type { "," Type } .
4063
4434
</pre>
4073
4444
</p>
4074
4445
 
4075
4446
<p>
4076
 
The type in a case may be <code>nil</code>
4077
 
(§<a href="#Predeclared_identifiers">Predeclared identifiers</a>);
 
4447
The type in a case may be <a href="#Predeclared_identifiers"><code>nil</code></a>;
4078
4448
that case is used when the expression in the TypeSwitchGuard
4079
4449
is a <code>nil</code> interface value.
4080
4450
</p>
4087
4457
<pre>
4088
4458
switch i := x.(type) {
4089
4459
case nil:
4090
 
        printString("x is nil")
 
4460
        printString("x is nil")                // type of i is type of x (interface{})
4091
4461
case int:
4092
 
        printInt(i)  // i is an int
 
4462
        printInt(i)                            // type of i is int
4093
4463
case float64:
4094
 
        printFloat64(i)  // i is a float64
 
4464
        printFloat64(i)                        // type of i is float64
4095
4465
case func(int) float64:
4096
 
        printFunction(i)  // i is a function
 
4466
        printFunction(i)                       // type of i is func(int) float64
4097
4467
case bool, string:
4098
 
        printString("type is bool or string")  // i is an interface{}
 
4468
        printString("type is bool or string")  // type of i is type of x (interface{})
4099
4469
default:
4100
 
        printString("don't know the type")
 
4470
        printString("don't know the type")     // type of i is type of x (interface{})
4101
4471
}
4102
4472
</pre>
4103
4473
 
4108
4478
<pre>
4109
4479
v := x  // x is evaluated exactly once
4110
4480
if v == nil {
 
4481
        i := v                                 // type of i is type of x (interface{})
4111
4482
        printString("x is nil")
4112
4483
} else if i, isInt := v.(int); isInt {
4113
 
        printInt(i)  // i is an int
 
4484
        printInt(i)                            // type of i is int
4114
4485
} else if i, isFloat64 := v.(float64); isFloat64 {
4115
 
        printFloat64(i)  // i is a float64
 
4486
        printFloat64(i)                        // type of i is float64
4116
4487
} else if i, isFunc := v.(func(int) float64); isFunc {
4117
 
        printFunction(i)  // i is a function
 
4488
        printFunction(i)                       // type of i is func(int) float64
4118
4489
} else {
4119
 
        i1, isBool := v.(bool)
4120
 
        i2, isString := v.(string)
 
4490
        _, isBool := v.(bool)
 
4491
        _, isString := v.(string)
4121
4492
        if isBool || isString {
4122
 
                i := v
4123
 
                printString("type is bool or string")  // i is an interface{}
 
4493
                i := v                         // type of i is type of x (interface{})
 
4494
                printString("type is bool or string")
4124
4495
        } else {
4125
 
                i := v
4126
 
                printString("don't know the type")  // i is an interface{}
 
4496
                i := v                         // type of i is type of x (interface{})
 
4497
                printString("don't know the type")
4127
4498
        }
4128
4499
}
4129
4500
</pre>
4206
4577
</p>
4207
4578
 
4208
4579
<pre class="ebnf">
4209
 
RangeClause = Expression [ "," Expression ] ( "=" | ":=" ) "range" Expression .
 
4580
RangeClause = ( ExpressionList "=" | IdentifierList ":=" ) "range" Expression .
4210
4581
</pre>
4211
4582
 
4212
4583
<p>
4213
4584
The expression on the right in the "range" clause is called the <i>range expression</i>,
4214
 
which may be an array, pointer to an array, slice, string, map, or channel.
 
4585
which may be an array, pointer to an array, slice, string, map, or channel permitting
 
4586
<a href="#Receive_operator">receive operations</a>.
4215
4587
As with an assignment, the operands on the left must be
4216
4588
<a href="#Address_operators">addressable</a> or map index expressions; they
4217
4589
denote the iteration variables. If the range expression is a channel, only
4218
 
one iteration variable is permitted, otherwise there may be one or two.
4219
 
If the second iteration variable is the <a href="#Blank_identifier">blank identifier</a>,
 
4590
one iteration variable is permitted, otherwise there may be one or two. In the latter case,
 
4591
if the second iteration variable is the <a href="#Blank_identifier">blank identifier</a>,
4220
4592
the range clause is equivalent to the same clause with only the first variable present.
4221
4593
</p>
4222
4594
 
4223
4595
<p>
4224
 
The range expression is evaluated once before beginning the loop
4225
 
except if the expression is an array, in which case, depending on
4226
 
the expression, it might not be evaluated (see below).
 
4596
The range expression is evaluated once before beginning the loop,
 
4597
with one exception. If the range expression is an array or a pointer to an array
 
4598
and only the first iteration value is present, only the range expression's
 
4599
length is evaluated; if that length is constant
 
4600
<a href="#Length_and_capacity">by definition</a>,
 
4601
the range expression itself will not be evaluated.
 
4602
</p>
 
4603
 
 
4604
<p>
4227
4605
Function calls on the left are evaluated once per iteration.
4228
4606
For each iteration, iteration values are produced as follows:
4229
4607
</p>
4234
4612
array or slice  a  [n]E, *[n]E, or []E    index    i  int    a[i]       E
4235
4613
string          s  string type            index    i  int    see below  rune
4236
4614
map             m  map[K]V                key      k  K      m[k]       V
4237
 
channel         c  chan E                 element  e  E
 
4615
channel         c  chan E, &lt;-chan E       element  e  E
4238
4616
</pre>
4239
4617
 
4240
4618
<ol>
4241
4619
<li>
4242
4620
For an array, pointer to array, or slice value <code>a</code>, the index iteration
4243
 
values are produced in increasing order, starting at element index 0. As a special
4244
 
case, if only the first iteration variable is present, the range loop produces
 
4621
values are produced in increasing order, starting at element index 0.
 
4622
If only the first iteration variable is present, the range loop produces
4245
4623
iteration values from 0 up to <code>len(a)</code> and does not index into the array
4246
4624
or slice itself. For a <code>nil</code> slice, the number of iterations is 0.
4247
4625
</li>
4260
4638
<li>
4261
4639
The iteration order over maps is not specified
4262
4640
and is not guaranteed to be the same from one iteration to the next.
4263
 
If map entries that have not yet been reached are deleted during iteration,
 
4641
If map entries that have not yet been reached are removed during iteration,
4264
4642
the corresponding iteration values will not be produced. If map entries are
4265
 
inserted during iteration, the behavior is implementation-dependent, but the
4266
 
iteration values for each entry will be produced at most once. If the map
4267
 
is <code>nil</code>, the number of iterations is 0.
 
4643
created during iteration, that entry may be produced during the iteration or
 
4644
may be skipped. The choice may vary for each entry created and from one
 
4645
iteration to the next.
 
4646
If the map is <code>nil</code>, the number of iterations is 0.
4268
4647
</li>
4269
4648
 
4270
4649
<li>
4327
4706
<h3 id="Go_statements">Go statements</h3>
4328
4707
 
4329
4708
<p>
4330
 
A "go" statement starts the execution of a function or method call
 
4709
A "go" statement starts the execution of a function call
4331
4710
as an independent concurrent thread of control, or <i>goroutine</i>,
4332
4711
within the same address space.
4333
4712
</p>
4337
4716
</pre>
4338
4717
 
4339
4718
<p>
4340
 
The expression must be a call.
 
4719
The expression must be a function or method call; it cannot be parenthesized.
 
4720
Calls of built-in functions are restricted as for
 
4721
<a href="#Expression_statements">expression statements</a>.
 
4722
</p>
 
4723
 
 
4724
<p>
4341
4725
The function value and parameters are
4342
4726
<a href="#Calls">evaluated as usual</a>
4343
4727
in the calling goroutine, but
4366
4750
 
4367
4751
<pre class="ebnf">
4368
4752
SelectStmt = "select" "{" { CommClause } "}" .
4369
 
CommClause = CommCase ":" { Statement ";" } .
 
4753
CommClause = CommCase ":" StatementList .
4370
4754
CommCase   = "case" ( SendStmt | RecvStmt ) | "default" .
4371
 
RecvStmt   = [ Expression [ "," Expression ] ( "=" | ":=" ) ] RecvExpr .
 
4755
RecvStmt   = [ ExpressionList "=" | IdentifierList ":=" ] RecvExpr .
4372
4756
RecvExpr   = Expression .
4373
4757
</pre>
4374
4758
 
4385
4769
chosen and the corresponding communication and statements are
4386
4770
evaluated.  Otherwise, if there is a default case, that executes;
4387
4771
if there is no default case, the statement blocks until one of the communications can
4388
 
complete.
 
4772
complete. There can be at most one default case and it may appear anywhere in the
 
4773
"select" statement.
4389
4774
If there are no cases with non-<code>nil</code> channels,
4390
4775
the statement blocks forever.
4391
4776
Even if the statement blocks,
4437
4822
<h3 id="Return_statements">Return statements</h3>
4438
4823
 
4439
4824
<p>
4440
 
A "return" statement terminates execution of the containing function
4441
 
and optionally provides a result value or values to the caller.
 
4825
A "return" statement in a function <code>F</code> terminates the execution
 
4826
of <code>F</code>, and optionally provides one or more result values.
 
4827
Any functions <a href="#Defer_statements">deferred</a> by <code>F</code>
 
4828
are executed before <code>F</code> returns to its caller.
4442
4829
</p>
4443
4830
 
4444
4831
<pre class="ebnf">
4488
4875
</pre>
4489
4876
        </li>
4490
4877
        <li>The expression list may be empty if the function's result
4491
 
                type specifies names for its result parameters (§<a href="#Function_types">Function Types</a>).
 
4878
                type specifies names for its <a href="#Function_types">result parameters</a>.
4492
4879
                The result parameters act as ordinary local variables
4493
4880
                and the function may assign values to them as necessary.
4494
4881
                The "return" statement returns the values of these variables.
4508
4895
</ol>
4509
4896
 
4510
4897
<p>
4511
 
Regardless of how they are declared, all the result values are initialized to the zero values for their type (§<a href="#The_zero_value">The zero value</a>) upon entry to the function.
 
4898
Regardless of how they are declared, all the result values are initialized to
 
4899
the <a href="#The_zero_value">zero values</a> for their type upon entry to the
 
4900
function. A "return" statement that specifies results sets the result parameters before
 
4901
any deferred functions are executed.
4512
4902
</p>
4513
4903
 
4514
4904
<!--
4523
4913
 
4524
4914
<p>
4525
4915
A "break" statement terminates execution of the innermost
4526
 
"for", "switch" or "select" statement.
 
4916
<a href="#For_statements">"for"</a>,
 
4917
<a href="#Switch_statements">"switch"</a>, or
 
4918
<a href="#Select_statements">"select"</a> statement.
4527
4919
</p>
4528
4920
 
4529
4921
<pre class="ebnf">
4532
4924
 
4533
4925
<p>
4534
4926
If there is a label, it must be that of an enclosing
4535
 
"for", "switch" or "select" statement, and that is the one whose execution
4536
 
terminates
4537
 
(§<a href="#For_statements">For statements</a>, §<a href="#Switch_statements">Switch statements</a>, §<a href="#Select_statements">Select statements</a>).
 
4927
"for", "switch", or "select" statement,
 
4928
and that is the one whose execution terminates.
4538
4929
</p>
4539
4930
 
4540
4931
<pre>
4551
4942
 
4552
4943
<p>
4553
4944
A "continue" statement begins the next iteration of the
4554
 
innermost "for" loop at its post statement (§<a href="#For_statements">For statements</a>).
 
4945
innermost <a href="#For_statements">"for" loop</a> at its post statement.
4555
4946
</p>
4556
4947
 
4557
4948
<pre class="ebnf">
4561
4952
<p>
4562
4953
If there is a label, it must be that of an enclosing
4563
4954
"for" statement, and that is the one whose execution
4564
 
advances
4565
 
(§<a href="#For_statements">For statements</a>).
 
4955
advances.
4566
4956
</p>
4567
4957
 
4568
4958
<h3 id="Goto_statements">Goto statements</h3>
4623
5013
 
4624
5014
<p>
4625
5015
A "fallthrough" statement transfers control to the first statement of the
4626
 
next case clause in a expression "switch" statement (§<a href="#Expression_switches">Expression switches</a>). It may
4627
 
be used only as the final non-empty statement in a case or default clause in an
4628
 
expression "switch" statement.
 
5016
next case clause in a <a href="#Expression_switches">expression "switch" statement</a>.
 
5017
It may be used only as the final non-empty statement in such a clause.
4629
5018
</p>
4630
5019
 
4631
5020
<pre class="ebnf">
4636
5025
<h3 id="Defer_statements">Defer statements</h3>
4637
5026
 
4638
5027
<p>
4639
 
A "defer" statement invokes a function whose execution is deferred to the moment
4640
 
the surrounding function returns.
 
5028
A "defer" statement invokes a function whose execution is deferred
 
5029
to the moment the surrounding function returns, either because the
 
5030
surrounding function executed a <a href="#Return_statements">return statement</a>,
 
5031
reached the end of its <a href="#Function_declarations">function body</a>,
 
5032
or because the corresponding goroutine is <a href="#Handling_panics">panicking</a>.
4641
5033
</p>
4642
5034
 
4643
5035
<pre class="ebnf">
4645
5037
</pre>
4646
5038
 
4647
5039
<p>
4648
 
The expression must be a function or method call.
 
5040
The expression must be a function or method call; it cannot be parenthesized.
 
5041
Calls of built-in functions are restricted as for
 
5042
<a href="#Expression_statements">expression statements</a>.
 
5043
</p>
 
5044
 
 
5045
<p>
4649
5046
Each time the "defer" statement
4650
5047
executes, the function value and parameters to the call are
4651
5048
<a href="#Calls">evaluated as usual</a>
4652
 
and saved anew but the
4653
 
actual function is not invoked.
4654
 
Instead, deferred calls are executed in LIFO order
4655
 
immediately before the surrounding function returns,
4656
 
after the return values, if any, have been evaluated, but before they
4657
 
are returned to the caller. For instance, if the deferred function is
 
5049
and saved anew but the actual function body is not executed.
 
5050
Instead, deferred functions are executed immediately before
 
5051
the surrounding function returns, in the reverse order
 
5052
they were deferred.
 
5053
</p>
 
5054
 
 
5055
<p>
 
5056
For instance, if the deferred function is
4658
5057
a <a href="#Function_literals">function literal</a> and the surrounding
4659
5058
function has <a href="#Function_types">named result parameters</a> that
4660
5059
are in scope within the literal, the deferred function may access and modify
4661
5060
the result parameters before they are returned.
4662
5061
If the deferred function has any return values, they are discarded when
4663
5062
the function completes.
 
5063
(See also the section on <a href="#Handling_panics">handling panics</a>.)
4664
5064
</p>
4665
5065
 
4666
5066
<pre>
4698
5098
 
4699
5099
<pre class="ebnf">
4700
5100
BuiltinCall = identifier "(" [ BuiltinArgs [ "," ] ] ")" .
4701
 
BuiltinArgs = Type [ "," ExpressionList ] | ExpressionList .
 
5101
BuiltinArgs = Type [ "," ArgumentList ] | ArgumentList .
4702
5102
</pre>
4703
5103
 
4704
5104
<h3 id="Close">Close</h3>
4750
5150
</pre>
4751
5151
 
4752
5152
<p>
4753
 
The length and capacity of a <code>nil</code> slice, map, or channel are 0.
 
5153
The length of a <code>nil</code> slice, map or channel is 0.
 
5154
The capacity of a <code>nil</code> slice and channel is 0.
4754
5155
</p>
4755
5156
 
4756
5157
<p>
4770
5171
<p>
4771
5172
The built-in function <code>new</code> takes a type <code>T</code> and
4772
5173
returns a value of type <code>*T</code>.
4773
 
The memory is initialized as described in the section on initial values
4774
 
(§<a href="#The_zero_value">The zero value</a>).
 
5174
The memory is initialized as described in the section on
 
5175
<a href="#The_zero_value">initial values</a>.
4775
5176
</p>
4776
5177
 
4777
5178
<pre class="grammar">
4797
5198
<h3 id="Making_slices_maps_and_channels">Making slices, maps and channels</h3>
4798
5199
 
4799
5200
<p>
4800
 
Slices, maps and channels are reference types that do not require the
4801
 
extra indirection of an allocation with <code>new</code>.
4802
5201
The built-in function <code>make</code> takes a type <code>T</code>,
4803
5202
which must be a slice, map or channel type,
4804
5203
optionally followed by a type-specific list of expressions.
4805
5204
It returns a value of type <code>T</code> (not <code>*T</code>).
4806
 
The memory is initialized as described in the section on initial values
4807
 
(§<a href="#The_zero_value">The zero value</a>).
 
5205
The memory is initialized as described in the section on
 
5206
<a href="#The_zero_value">initial values</a>.
4808
5207
</p>
4809
5208
 
4810
5209
<pre class="grammar">
4822
5221
 
4823
5222
 
4824
5223
<p>
4825
 
The arguments <code>n</code> and <code>m</code> must be of integer type.
4826
 
A <a href="#Run_time_panics">run-time panic</a> occurs if <code>n</code>
4827
 
is negative or larger than <code>m</code>, or if <code>n</code> or
4828
 
<code>m</code> cannot be represented by an <code>int</code>.
 
5224
The size arguments <code>n</code> and <code>m</code> must be of integer type or untyped.
 
5225
A <a href="#Constants">constant</a> size argument must be non-negative and
 
5226
representable by a value of type <code>int</code>.
 
5227
If both <code>n</code> and <code>m</code> are provided and are constant, then
 
5228
<code>n</code> must be no larger than <code>m</code>.
 
5229
If <code>n</code> is negative or larger than <code>m</code> at run time,
 
5230
a <a href="#Run_time_panics">run-time panic</a> occurs.
4829
5231
</p>
4830
5232
 
4831
5233
<pre>
4832
5234
s := make([]int, 10, 100)       // slice with len(s) == 10, cap(s) == 100
4833
 
s := make([]int, 10)            // slice with len(s) == cap(s) == 10
 
5235
s := make([]int, 1e3)           // slice with len(s) == cap(s) == 1000
 
5236
s := make([]int, 1&lt;&lt;63)         // illegal: len(s) is not representable by a value of type int
 
5237
s := make([]int, 10, 0)         // illegal: len(s) > cap(s)
4834
5238
c := make(chan int, 10)         // channel with a buffer size of 10
4835
5239
m := make(map[string]int, 100)  // map with initial space for 100 elements
4836
5240
</pre>
4839
5243
<h3 id="Appending_and_copying_slices">Appending to and copying slices</h3>
4840
5244
 
4841
5245
<p>
4842
 
Two built-in functions assist in common slice operations.
 
5246
The built-in functions <code>append</code> and <code>copy</code> assist in
 
5247
common slice operations.
 
5248
For both functions, the result is independent of whether the memory referenced
 
5249
by the arguments overlaps.
4843
5250
</p>
4844
5251
 
4845
5252
<p>
4870
5277
 
4871
5278
<pre>
4872
5279
s0 := []int{0, 0}
4873
 
s1 := append(s0, 2)        // append a single element     s1 == []int{0, 0, 2}
4874
 
s2 := append(s1, 3, 5, 7)  // append multiple elements    s2 == []int{0, 0, 2, 3, 5, 7}
4875
 
s3 := append(s2, s0...)    // append a slice              s3 == []int{0, 0, 2, 3, 5, 7, 0, 0}
 
5280
s1 := append(s0, 2)                // append a single element     s1 == []int{0, 0, 2}
 
5281
s2 := append(s1, 3, 5, 7)          // append multiple elements    s2 == []int{0, 0, 2, 3, 5, 7}
 
5282
s3 := append(s2, s0...)            // append a slice              s3 == []int{0, 0, 2, 3, 5, 7, 0, 0}
 
5283
s4 := append(s3[3:6], s3[2:]...)   // append overlapping slice    s4 == []int{3, 5, 7, 2, 3, 5, 7, 0, 0}
4876
5284
 
4877
5285
var t []interface{}
4878
 
t = append(t, 42, 3.1415, "foo")                          t == []interface{}{42, 3.1415, "foo"}
 
5286
t = append(t, 42, 3.1415, "foo")                                  t == []interface{}{42, 3.1415, "foo"}
4879
5287
 
4880
5288
var b []byte
4881
 
b = append(b, "bar"...)  // append string contents      b == []byte{'b', 'a', 'r' }
 
5289
b = append(b, "bar"...)            // append string contents      b == []byte{'b', 'a', 'r' }
4882
5290
</pre>
4883
5291
 
4884
5292
<p>
4885
5293
The function <code>copy</code> copies slice elements from
4886
5294
a source <code>src</code> to a destination <code>dst</code> and returns the
4887
 
number of elements copied. Source and destination may overlap.
 
5295
number of elements copied.
4888
5296
Both arguments must have <a href="#Type_identity">identical</a> element type <code>T</code> and must be
4889
5297
<a href="#Assignability">assignable</a> to a slice of type <code>[]T</code>.
4890
5298
The number of elements copied is the minimum of
4927
5335
</pre>
4928
5336
 
4929
5337
<p>
4930
 
If the element <code>m[k]</code> does not exist, <code>delete</code> is
4931
 
a no-op. Calling <code>delete</code> with a nil map causes a
4932
 
<a href="#Run_time_panics">run-time panic</a>.
 
5338
If the map <code>m</code> is <code>nil</code> or the element <code>m[k]</code>
 
5339
does not exist, <code>delete</code> is a no-op.
4933
5340
</p>
4934
5341
 
4935
5342
 
4988
5395
</pre>
4989
5396
 
4990
5397
<p>
4991
 
When a function <code>F</code> calls <code>panic</code>, normal
4992
 
execution of <code>F</code> stops immediately.  Any functions whose
4993
 
execution was <a href="#Defer_statements">deferred</a> by the
4994
 
invocation of <code>F</code> are run in the usual way, and then
4995
 
<code>F</code> returns to its caller.  To the caller, <code>F</code>
4996
 
then behaves like a call to <code>panic</code>, terminating its own
4997
 
execution and running deferred functions.  This continues until all
4998
 
functions in the goroutine have ceased execution, in reverse order.
4999
 
At that point, the program is
5000
 
terminated and the error condition is reported, including the value of
5001
 
the argument to <code>panic</code>.  This termination sequence is
5002
 
called <i>panicking</i>.
 
5398
While executing a function <code>F</code>,
 
5399
an explicit call to <code>panic</code> or a <a href="#Run_time_panics">run-time panic</a>
 
5400
terminates the execution of <code>F</code>.
 
5401
Any functions <a href="#Defer_statements">deferred</a> by <code>F</code>
 
5402
are then executed as usual.
 
5403
Next, any deferred functions run by <code>F's</code> caller are run,
 
5404
and so on up to any deferred by the top-level function in the executing goroutine.
 
5405
At that point, the program is terminated and the error
 
5406
condition is reported, including the value of the argument to <code>panic</code>.
 
5407
This termination sequence is called <i>panicking</i>.
5003
5408
</p>
5004
5409
 
5005
5410
<pre>
5010
5415
 
5011
5416
<p>
5012
5417
The <code>recover</code> function allows a program to manage behavior
5013
 
of a panicking goroutine.  Executing a <code>recover</code> call
5014
 
<i>inside</i> a deferred function (but not any function called by it) stops
5015
 
the panicking sequence by restoring normal execution, and retrieves
5016
 
the error value passed to the call of <code>panic</code>.  If
5017
 
<code>recover</code> is called outside the deferred function it will
5018
 
not stop a panicking sequence.  In this case, or when the goroutine
5019
 
is not panicking, or if the argument supplied to <code>panic</code>
5020
 
was <code>nil</code>, <code>recover</code> returns <code>nil</code>.
5021
 
</p>
 
5418
of a panicking goroutine.
 
5419
Suppose a function <code>G</code> defers a function <code>D</code> that calls
 
5420
<code>recover</code> and a panic occurs in a function on the same goroutine in which <code>G</code>
 
5421
is executing.
 
5422
When the running of deferred functions reaches <code>D</code>,
 
5423
the return value of <code>D</code>'s call to <code>recover</code> will be the value passed to the call of <code>panic</code>.
 
5424
If <code>D</code> returns normally, without starting a new
 
5425
<code>panic</code>, the panicking sequence stops. In that case,
 
5426
the state of functions called between <code>G</code> and the call to <code>panic</code>
 
5427
is discarded, and normal execution resumes.
 
5428
Any functions deferred by <code>G</code> before <code>D</code> are then run and <code>G</code>'s
 
5429
execution terminates by returning to its caller.
 
5430
</p>
 
5431
 
 
5432
<p>
 
5433
The return value of <code>recover</code> is <code>nil</code> if any of the following conditions holds:
 
5434
</p>
 
5435
<ul>
 
5436
<li>
 
5437
<code>panic</code>'s argument was <code>nil</code>;
 
5438
</li>
 
5439
<li>
 
5440
the goroutine is not panicking;
 
5441
</li>
 
5442
<li>
 
5443
<code>recover</code> was not called directly by a deferred function.
 
5444
</li>
 
5445
</ul>
5022
5446
 
5023
5447
<p>
5024
5448
The <code>protect</code> function in the example below invokes
5112
5536
An import declaration states that the source file containing the declaration
5113
5537
depends on functionality of the <i>imported</i> package
5114
5538
(<a href="#Program_initialization_and_execution">§Program initialization and execution</a>)
5115
 
and it enables access to <a href="#Exported_identifiers">exported</a> identifiers
 
5539
and enables access to <a href="#Exported_identifiers">exported</a> identifiers
5116
5540
of that package.
5117
5541
The import names an identifier (PackageName) to be used for access and an ImportPath
5118
5542
that specifies the package to be imported.
5145
5569
<p>
5146
5570
Implementation restriction: A compiler may restrict ImportPaths to
5147
5571
non-empty strings using only characters belonging to
5148
 
<a href="http://www.unicode.org/versions/Unicode6.0.0/">Unicode's</a>
 
5572
<a href="http://www.unicode.org/versions/Unicode6.2.0/">Unicode's</a>
5149
5573
L, M, N, P, and S general categories (the Graphic characters without
5150
5574
spaces) and may also exclude the characters
5151
5575
<code>!"#$%&amp;'()*,:;&lt;=&gt;?[\]^`{|}</code>
5166
5590
Import declaration          Local name of Sin
5167
5591
 
5168
5592
import   "lib/math"         math.Sin
5169
 
import M "lib/math"         M.Sin
 
5593
import m "lib/math"         m.Sin
5170
5594
import . "lib/math"         Sin
5171
5595
</pre>
5172
5596
 
5173
5597
<p>
5174
5598
An import declaration declares a dependency relation between
5175
5599
the importing and imported package.
5176
 
It is illegal for a package to import itself or to import a package without
 
5600
It is illegal for a package to import itself, directly or indirectly,
 
5601
or to directly import a package without
5177
5602
referring to any of its exported identifiers. To import a package solely for
5178
5603
its side-effects (initialization), use the <a href="#Blank_identifier">blank</a>
5179
5604
identifier as explicit package name:
5292
5717
</pre>
5293
5718
<p>
5294
5719
defined in its source.
5295
 
A package may contain multiple
5296
 
<code>init</code> functions, even
 
5720
A package-scope or file-scope identifier
 
5721
with name <code>init</code> may only be
 
5722
declared to be a function with this signature.
 
5723
Multiple such functions may be defined, even
5297
5724
within a single source file; they execute
5298
5725
in unspecified order.
5299
5726
</p>
5300
5727
<p>
5301
5728
Within a package, package-level variables are initialized,
5302
 
and constant values are determined, in
5303
 
data-dependent order: if the initializer of <code>A</code>
5304
 
depends on the value of <code>B</code>, <code>A</code>
 
5729
and constant values are determined, according to
 
5730
order of reference: if the initializer of <code>A</code>
 
5731
depends on <code>B</code>, <code>A</code>
5305
5732
will be set after <code>B</code>.
5306
 
It is an error if such dependencies form a cycle.
5307
 
Dependency analysis is done lexically: <code>A</code>
 
5733
Dependency analysis does not depend on the actual values
 
5734
of the items being initialized, only on their appearance
 
5735
in the source.
 
5736
<code>A</code>
5308
5737
depends on <code>B</code> if the value of <code>A</code>
5309
5738
contains a mention of <code>B</code>, contains a value
5310
5739
whose initializer
5311
5740
mentions <code>B</code>, or mentions a function that
5312
5741
mentions <code>B</code>, recursively.
 
5742
It is an error if such dependencies form a cycle.
5313
5743
If two items are not interdependent, they will be initialized
5314
 
in the order they appear in the source.
 
5744
in the order they appear in the source, possibly in multiple files,
 
5745
as presented to the compiler.
5315
5746
Since the dependency analysis is done per package, it can produce
5316
5747
unspecified results  if <code>A</code>'s initializer calls a function defined
5317
5748
in another package that refers to <code>B</code>.
5430
5861
</pre>
5431
5862
 
5432
5863
<p>
5433
 
Any pointer or value of <a href="#Types">underlying type</a> <code>uintptr</code> can be converted into
5434
 
a <code>Pointer</code> and vice versa.
5435
 
</p>
5436
 
<p>
5437
 
The function <code>Sizeof</code> takes an expression denoting a
5438
 
variable of any type and returns the size of the variable in bytes.
5439
 
</p>
5440
 
<p>
5441
 
The function <code>Offsetof</code> takes a selector (§<a href="#Selectors">Selectors</a>) denoting a struct
5442
 
field of any type and returns the field offset in bytes relative to the
5443
 
struct's address.
 
5864
Any pointer or value of <a href="#Types">underlying type</a> <code>uintptr</code> can be converted to
 
5865
a <code>Pointer</code> type and vice versa.
 
5866
</p>
 
5867
 
 
5868
<pre>
 
5869
var f float64
 
5870
bits = *(*uint64)(unsafe.Pointer(&amp;f))
 
5871
 
 
5872
type ptr unsafe.Pointer
 
5873
bits = *(*uint64)(ptr(&amp;f))
 
5874
</pre>
 
5875
 
 
5876
<p>
 
5877
The functions <code>Alignof</code> and <code>Sizeof</code> take an expression <code>x</code>
 
5878
of any type and return the alignment or size, respectively, of a hypothetical variable <code>v</code>
 
5879
as if <code>v</code> was declared via <code>var v = x</code>.
 
5880
</p>
 
5881
<p>
 
5882
The function <code>Offsetof</code> takes a (possibly parenthesized) <a href="#Selectors">selector</a>
 
5883
<code>s.f</code>, denoting a field <code>f</code> of the struct denoted by <code>s</code>
 
5884
or <code>*s</code>, and returns the field offset in bytes relative to the struct's address.
 
5885
If <code>f</code> is an <a href="#Struct_types">embedded field</a>, it must be reachable
 
5886
without pointer indirections through fields of the struct.
5444
5887
For a struct <code>s</code> with field <code>f</code>:
5445
5888
</p>
5446
5889
 
5465
5908
Calls to <code>Alignof</code>, <code>Offsetof</code>, and
5466
5909
<code>Sizeof</code> are compile-time constant expressions of type <code>uintptr</code>.
5467
5910
</p>
5468
 
<p>
5469
5911
 
5470
5912
<h3 id="Size_and_alignment_guarantees">Size and alignment guarantees</h3>
5471
5913
 
5472
5914
<p>
5473
 
For the numeric types (§<a href="#Numeric_types">Numeric types</a>), the following sizes are guaranteed:
 
5915
For the <a href="#Numeric_types">numeric types</a>, the following sizes are guaranteed:
5474
5916
</p>
5475
5917
 
5476
5918
<pre class="grammar">