github.com/hlts2/go@v0.0.0-20170904000733-812b34efaed8/doc/go_spec.html (about)

     1  <!--{
     2  	"Title": "The Go Programming Language Specification",
     3  	"Subtitle": "Version of September 1, 2017",
     4  	"Path": "/ref/spec"
     5  }-->
     6  
     7  <h2 id="Introduction">Introduction</h2>
     8  
     9  <p>
    10  This is a reference manual for the Go programming language. For
    11  more information and other documents, see <a href="/">golang.org</a>.
    12  </p>
    13  
    14  <p>
    15  Go is a general-purpose language designed with systems programming
    16  in mind. It is strongly typed and garbage-collected and has explicit
    17  support for concurrent programming.  Programs are constructed from
    18  <i>packages</i>, whose properties allow efficient management of
    19  dependencies. The existing implementations use a traditional
    20  compile/link model to generate executable binaries.
    21  </p>
    22  
    23  <p>
    24  The grammar is compact and regular, allowing for easy analysis by
    25  automatic tools such as integrated development environments.
    26  </p>
    27  
    28  <h2 id="Notation">Notation</h2>
    29  <p>
    30  The syntax is specified using Extended Backus-Naur Form (EBNF):
    31  </p>
    32  
    33  <pre class="grammar">
    34  Production  = production_name "=" [ Expression ] "." .
    35  Expression  = Alternative { "|" Alternative } .
    36  Alternative = Term { Term } .
    37  Term        = production_name | token [ "…" token ] | Group | Option | Repetition .
    38  Group       = "(" Expression ")" .
    39  Option      = "[" Expression "]" .
    40  Repetition  = "{" Expression "}" .
    41  </pre>
    42  
    43  <p>
    44  Productions are expressions constructed from terms and the following
    45  operators, in increasing precedence:
    46  </p>
    47  <pre class="grammar">
    48  |   alternation
    49  ()  grouping
    50  []  option (0 or 1 times)
    51  {}  repetition (0 to n times)
    52  </pre>
    53  
    54  <p>
    55  Lower-case production names are used to identify lexical tokens.
    56  Non-terminals are in CamelCase. Lexical tokens are enclosed in
    57  double quotes <code>""</code> or back quotes <code>``</code>.
    58  </p>
    59  
    60  <p>
    61  The form <code>a … b</code> represents the set of characters from
    62  <code>a</code> through <code>b</code> as alternatives. The horizontal
    63  ellipsis <code>…</code> is also used elsewhere in the spec to informally denote various
    64  enumerations or code snippets that are not further specified. The character <code>…</code>
    65  (as opposed to the three characters <code>...</code>) is not a token of the Go
    66  language.
    67  </p>
    68  
    69  <h2 id="Source_code_representation">Source code representation</h2>
    70  
    71  <p>
    72  Source code is Unicode text encoded in
    73  <a href="http://en.wikipedia.org/wiki/UTF-8">UTF-8</a>. The text is not
    74  canonicalized, so a single accented code point is distinct from the
    75  same character constructed from combining an accent and a letter;
    76  those are treated as two code points.  For simplicity, this document
    77  will use the unqualified term <i>character</i> to refer to a Unicode code point
    78  in the source text.
    79  </p>
    80  <p>
    81  Each code point is distinct; for instance, upper and lower case letters
    82  are different characters.
    83  </p>
    84  <p>
    85  Implementation restriction: For compatibility with other tools, a
    86  compiler may disallow the NUL character (U+0000) in the source text.
    87  </p>
    88  <p>
    89  Implementation restriction: For compatibility with other tools, a
    90  compiler may ignore a UTF-8-encoded byte order mark
    91  (U+FEFF) if it is the first Unicode code point in the source text.
    92  A byte order mark may be disallowed anywhere else in the source.
    93  </p>
    94  
    95  <h3 id="Characters">Characters</h3>
    96  
    97  <p>
    98  The following terms are used to denote specific Unicode character classes:
    99  </p>
   100  <pre class="ebnf">
   101  newline        = /* the Unicode code point U+000A */ .
   102  unicode_char   = /* an arbitrary Unicode code point except newline */ .
   103  unicode_letter = /* a Unicode code point classified as "Letter" */ .
   104  unicode_digit  = /* a Unicode code point classified as "Number, decimal digit" */ .
   105  </pre>
   106  
   107  <p>
   108  In <a href="http://www.unicode.org/versions/Unicode8.0.0/">The Unicode Standard 8.0</a>,
   109  Section 4.5 "General Category" defines a set of character categories.
   110  Go treats all characters in any of the Letter categories Lu, Ll, Lt, Lm, or Lo
   111  as Unicode letters, and those in the Number category Nd as Unicode digits.
   112  </p>
   113  
   114  <h3 id="Letters_and_digits">Letters and digits</h3>
   115  
   116  <p>
   117  The underscore character <code>_</code> (U+005F) is considered a letter.
   118  </p>
   119  <pre class="ebnf">
   120  letter        = unicode_letter | "_" .
   121  decimal_digit = "0" … "9" .
   122  octal_digit   = "0" … "7" .
   123  hex_digit     = "0" … "9" | "A" … "F" | "a" … "f" .
   124  </pre>
   125  
   126  <h2 id="Lexical_elements">Lexical elements</h2>
   127  
   128  <h3 id="Comments">Comments</h3>
   129  
   130  <p>
   131  Comments serve as program documentation. There are two forms:
   132  </p>
   133  
   134  <ol>
   135  <li>
   136  <i>Line comments</i> start with the character sequence <code>//</code>
   137  and stop at the end of the line.
   138  </li>
   139  <li>
   140  <i>General comments</i> start with the character sequence <code>/*</code>
   141  and stop with the first subsequent character sequence <code>*/</code>.
   142  </li>
   143  </ol>
   144  
   145  <p>
   146  A comment cannot start inside a <a href="#Rune_literals">rune</a> or
   147  <a href="#String_literals">string literal</a>, or inside a comment.
   148  A general comment containing no newlines acts like a space.
   149  Any other comment acts like a newline.
   150  </p>
   151  
   152  <h3 id="Tokens">Tokens</h3>
   153  
   154  <p>
   155  Tokens form the vocabulary of the Go language.
   156  There are four classes: <i>identifiers</i>, <i>keywords</i>, <i>operators
   157  and punctuation</i>, and <i>literals</i>.  <i>White space</i>, formed from
   158  spaces (U+0020), horizontal tabs (U+0009),
   159  carriage returns (U+000D), and newlines (U+000A),
   160  is ignored except as it separates tokens
   161  that would otherwise combine into a single token. Also, a newline or end of file
   162  may trigger the insertion of a <a href="#Semicolons">semicolon</a>.
   163  While breaking the input into tokens,
   164  the next token is the longest sequence of characters that form a
   165  valid token.
   166  </p>
   167  
   168  <h3 id="Semicolons">Semicolons</h3>
   169  
   170  <p>
   171  The formal grammar uses semicolons <code>";"</code> as terminators in
   172  a number of productions. Go programs may omit most of these semicolons
   173  using the following two rules:
   174  </p>
   175  
   176  <ol>
   177  <li>
   178  When the input is broken into tokens, a semicolon is automatically inserted
   179  into the token stream immediately after a line's final token if that token is
   180  <ul>
   181  	<li>an
   182  	    <a href="#Identifiers">identifier</a>
   183  	</li>
   184  
   185  	<li>an
   186  	    <a href="#Integer_literals">integer</a>,
   187  	    <a href="#Floating-point_literals">floating-point</a>,
   188  	    <a href="#Imaginary_literals">imaginary</a>,
   189  	    <a href="#Rune_literals">rune</a>, or
   190  	    <a href="#String_literals">string</a> literal
   191  	</li>
   192  
   193  	<li>one of the <a href="#Keywords">keywords</a>
   194  	    <code>break</code>,
   195  	    <code>continue</code>,
   196  	    <code>fallthrough</code>, or
   197  	    <code>return</code>
   198  	</li>
   199  
   200  	<li>one of the <a href="#Operators_and_punctuation">operators and punctuation</a>
   201  	    <code>++</code>,
   202  	    <code>--</code>,
   203  	    <code>)</code>,
   204  	    <code>]</code>, or
   205  	    <code>}</code>
   206  	</li>
   207  </ul>
   208  </li>
   209  
   210  <li>
   211  To allow complex statements to occupy a single line, a semicolon
   212  may be omitted before a closing <code>")"</code> or <code>"}"</code>.
   213  </li>
   214  </ol>
   215  
   216  <p>
   217  To reflect idiomatic use, code examples in this document elide semicolons
   218  using these rules.
   219  </p>
   220  
   221  
   222  <h3 id="Identifiers">Identifiers</h3>
   223  
   224  <p>
   225  Identifiers name program entities such as variables and types.
   226  An identifier is a sequence of one or more letters and digits.
   227  The first character in an identifier must be a letter.
   228  </p>
   229  <pre class="ebnf">
   230  identifier = letter { letter | unicode_digit } .
   231  </pre>
   232  <pre>
   233  a
   234  _x9
   235  ThisVariableIsExported
   236  αβ
   237  </pre>
   238  
   239  <p>
   240  Some identifiers are <a href="#Predeclared_identifiers">predeclared</a>.
   241  </p>
   242  
   243  
   244  <h3 id="Keywords">Keywords</h3>
   245  
   246  <p>
   247  The following keywords are reserved and may not be used as identifiers.
   248  </p>
   249  <pre class="grammar">
   250  break        default      func         interface    select
   251  case         defer        go           map          struct
   252  chan         else         goto         package      switch
   253  const        fallthrough  if           range        type
   254  continue     for          import       return       var
   255  </pre>
   256  
   257  <h3 id="Operators_and_punctuation">Operators and punctuation</h3>
   258  
   259  <p>
   260  The following character sequences represent <a href="#Operators">operators</a>
   261  (including <a href="#assign_op">assignment operators</a>) and punctuation:
   262  </p>
   263  <pre class="grammar">
   264  +    &amp;     +=    &amp;=     &amp;&amp;    ==    !=    (    )
   265  -    |     -=    |=     ||    &lt;     &lt;=    [    ]
   266  *    ^     *=    ^=     &lt;-    &gt;     &gt;=    {    }
   267  /    &lt;&lt;    /=    &lt;&lt;=    ++    =     :=    ,    ;
   268  %    &gt;&gt;    %=    &gt;&gt;=    --    !     ...   .    :
   269       &amp;^          &amp;^=
   270  </pre>
   271  
   272  <h3 id="Integer_literals">Integer literals</h3>
   273  
   274  <p>
   275  An integer literal is a sequence of digits representing an
   276  <a href="#Constants">integer constant</a>.
   277  An optional prefix sets a non-decimal base: <code>0</code> for octal, <code>0x</code> or
   278  <code>0X</code> for hexadecimal.  In hexadecimal literals, letters
   279  <code>a-f</code> and <code>A-F</code> represent values 10 through 15.
   280  </p>
   281  <pre class="ebnf">
   282  int_lit     = decimal_lit | octal_lit | hex_lit .
   283  decimal_lit = ( "1" … "9" ) { decimal_digit } .
   284  octal_lit   = "0" { octal_digit } .
   285  hex_lit     = "0" ( "x" | "X" ) hex_digit { hex_digit } .
   286  </pre>
   287  
   288  <pre>
   289  42
   290  0600
   291  0xBadFace
   292  170141183460469231731687303715884105727
   293  </pre>
   294  
   295  <h3 id="Floating-point_literals">Floating-point literals</h3>
   296  <p>
   297  A floating-point literal is a decimal representation of a
   298  <a href="#Constants">floating-point constant</a>.
   299  It has an integer part, a decimal point, a fractional part,
   300  and an exponent part.  The integer and fractional part comprise
   301  decimal digits; the exponent part is an <code>e</code> or <code>E</code>
   302  followed by an optionally signed decimal exponent.  One of the
   303  integer part or the fractional part may be elided; one of the decimal
   304  point or the exponent may be elided.
   305  </p>
   306  <pre class="ebnf">
   307  float_lit = decimals "." [ decimals ] [ exponent ] |
   308              decimals exponent |
   309              "." decimals [ exponent ] .
   310  decimals  = decimal_digit { decimal_digit } .
   311  exponent  = ( "e" | "E" ) [ "+" | "-" ] decimals .
   312  </pre>
   313  
   314  <pre>
   315  0.
   316  72.40
   317  072.40  // == 72.40
   318  2.71828
   319  1.e+0
   320  6.67428e-11
   321  1E6
   322  .25
   323  .12345E+5
   324  </pre>
   325  
   326  <h3 id="Imaginary_literals">Imaginary literals</h3>
   327  <p>
   328  An imaginary literal is a decimal representation of the imaginary part of a
   329  <a href="#Constants">complex constant</a>.
   330  It consists of a
   331  <a href="#Floating-point_literals">floating-point literal</a>
   332  or decimal integer followed
   333  by the lower-case letter <code>i</code>.
   334  </p>
   335  <pre class="ebnf">
   336  imaginary_lit = (decimals | float_lit) "i" .
   337  </pre>
   338  
   339  <pre>
   340  0i
   341  011i  // == 11i
   342  0.i
   343  2.71828i
   344  1.e+0i
   345  6.67428e-11i
   346  1E6i
   347  .25i
   348  .12345E+5i
   349  </pre>
   350  
   351  
   352  <h3 id="Rune_literals">Rune literals</h3>
   353  
   354  <p>
   355  A rune literal represents a <a href="#Constants">rune constant</a>,
   356  an integer value identifying a Unicode code point.
   357  A rune literal is expressed as one or more characters enclosed in single quotes,
   358  as in <code>'x'</code> or <code>'\n'</code>.
   359  Within the quotes, any character may appear except newline and unescaped single
   360  quote. A single quoted character represents the Unicode value
   361  of the character itself,
   362  while multi-character sequences beginning with a backslash encode
   363  values in various formats.
   364  </p>
   365  <p>
   366  The simplest form represents the single character within the quotes;
   367  since Go source text is Unicode characters encoded in UTF-8, multiple
   368  UTF-8-encoded bytes may represent a single integer value.  For
   369  instance, the literal <code>'a'</code> holds a single byte representing
   370  a literal <code>a</code>, Unicode U+0061, value <code>0x61</code>, while
   371  <code>'ä'</code> holds two bytes (<code>0xc3</code> <code>0xa4</code>) representing
   372  a literal <code>a</code>-dieresis, U+00E4, value <code>0xe4</code>.
   373  </p>
   374  <p>
   375  Several backslash escapes allow arbitrary values to be encoded as
   376  ASCII text.  There are four ways to represent the integer value
   377  as a numeric constant: <code>\x</code> followed by exactly two hexadecimal
   378  digits; <code>\u</code> followed by exactly four hexadecimal digits;
   379  <code>\U</code> followed by exactly eight hexadecimal digits, and a
   380  plain backslash <code>\</code> followed by exactly three octal digits.
   381  In each case the value of the literal is the value represented by
   382  the digits in the corresponding base.
   383  </p>
   384  <p>
   385  Although these representations all result in an integer, they have
   386  different valid ranges.  Octal escapes must represent a value between
   387  0 and 255 inclusive.  Hexadecimal escapes satisfy this condition
   388  by construction. The escapes <code>\u</code> and <code>\U</code>
   389  represent Unicode code points so within them some values are illegal,
   390  in particular those above <code>0x10FFFF</code> and surrogate halves.
   391  </p>
   392  <p>
   393  After a backslash, certain single-character escapes represent special values:
   394  </p>
   395  <pre class="grammar">
   396  \a   U+0007 alert or bell
   397  \b   U+0008 backspace
   398  \f   U+000C form feed
   399  \n   U+000A line feed or newline
   400  \r   U+000D carriage return
   401  \t   U+0009 horizontal tab
   402  \v   U+000b vertical tab
   403  \\   U+005c backslash
   404  \'   U+0027 single quote  (valid escape only within rune literals)
   405  \"   U+0022 double quote  (valid escape only within string literals)
   406  </pre>
   407  <p>
   408  All other sequences starting with a backslash are illegal inside rune literals.
   409  </p>
   410  <pre class="ebnf">
   411  rune_lit         = "'" ( unicode_value | byte_value ) "'" .
   412  unicode_value    = unicode_char | little_u_value | big_u_value | escaped_char .
   413  byte_value       = octal_byte_value | hex_byte_value .
   414  octal_byte_value = `\` octal_digit octal_digit octal_digit .
   415  hex_byte_value   = `\` "x" hex_digit hex_digit .
   416  little_u_value   = `\` "u" hex_digit hex_digit hex_digit hex_digit .
   417  big_u_value      = `\` "U" hex_digit hex_digit hex_digit hex_digit
   418                             hex_digit hex_digit hex_digit hex_digit .
   419  escaped_char     = `\` ( "a" | "b" | "f" | "n" | "r" | "t" | "v" | `\` | "'" | `"` ) .
   420  </pre>
   421  
   422  <pre>
   423  'a'
   424  'ä'
   425  '本'
   426  '\t'
   427  '\000'
   428  '\007'
   429  '\377'
   430  '\x07'
   431  '\xff'
   432  '\u12e4'
   433  '\U00101234'
   434  '\''         // rune literal containing single quote character
   435  'aa'         // illegal: too many characters
   436  '\xa'        // illegal: too few hexadecimal digits
   437  '\0'         // illegal: too few octal digits
   438  '\uDFFF'     // illegal: surrogate half
   439  '\U00110000' // illegal: invalid Unicode code point
   440  </pre>
   441  
   442  
   443  <h3 id="String_literals">String literals</h3>
   444  
   445  <p>
   446  A string literal represents a <a href="#Constants">string constant</a>
   447  obtained from concatenating a sequence of characters. There are two forms:
   448  raw string literals and interpreted string literals.
   449  </p>
   450  <p>
   451  Raw string literals are character sequences between back quotes, as in
   452  <code>`foo`</code>.  Within the quotes, any character may appear except
   453  back quote. The value of a raw string literal is the
   454  string composed of the uninterpreted (implicitly UTF-8-encoded) characters
   455  between the quotes;
   456  in particular, backslashes have no special meaning and the string may
   457  contain newlines.
   458  Carriage return characters ('\r') inside raw string literals
   459  are discarded from the raw string value.
   460  </p>
   461  <p>
   462  Interpreted string literals are character sequences between double
   463  quotes, as in <code>&quot;bar&quot;</code>.
   464  Within the quotes, any character may appear except newline and unescaped double quote.
   465  The text between the quotes forms the
   466  value of the literal, with backslash escapes interpreted as they
   467  are in <a href="#Rune_literals">rune literals</a> (except that <code>\'</code> is illegal and
   468  <code>\"</code> is legal), with the same restrictions.
   469  The three-digit octal (<code>\</code><i>nnn</i>)
   470  and two-digit hexadecimal (<code>\x</code><i>nn</i>) escapes represent individual
   471  <i>bytes</i> of the resulting string; all other escapes represent
   472  the (possibly multi-byte) UTF-8 encoding of individual <i>characters</i>.
   473  Thus inside a string literal <code>\377</code> and <code>\xFF</code> represent
   474  a single byte of value <code>0xFF</code>=255, while <code>ÿ</code>,
   475  <code>\u00FF</code>, <code>\U000000FF</code> and <code>\xc3\xbf</code> represent
   476  the two bytes <code>0xc3</code> <code>0xbf</code> of the UTF-8 encoding of character
   477  U+00FF.
   478  </p>
   479  
   480  <pre class="ebnf">
   481  string_lit             = raw_string_lit | interpreted_string_lit .
   482  raw_string_lit         = "`" { unicode_char | newline } "`" .
   483  interpreted_string_lit = `"` { unicode_value | byte_value } `"` .
   484  </pre>
   485  
   486  <pre>
   487  `abc`                // same as "abc"
   488  `\n
   489  \n`                  // same as "\\n\n\\n"
   490  "\n"
   491  "\""                 // same as `"`
   492  "Hello, world!\n"
   493  "日本語"
   494  "\u65e5本\U00008a9e"
   495  "\xff\u00FF"
   496  "\uD800"             // illegal: surrogate half
   497  "\U00110000"         // illegal: invalid Unicode code point
   498  </pre>
   499  
   500  <p>
   501  These examples all represent the same string:
   502  </p>
   503  
   504  <pre>
   505  "日本語"                                 // UTF-8 input text
   506  `日本語`                                 // UTF-8 input text as a raw literal
   507  "\u65e5\u672c\u8a9e"                    // the explicit Unicode code points
   508  "\U000065e5\U0000672c\U00008a9e"        // the explicit Unicode code points
   509  "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e"  // the explicit UTF-8 bytes
   510  </pre>
   511  
   512  <p>
   513  If the source code represents a character as two code points, such as
   514  a combining form involving an accent and a letter, the result will be
   515  an error if placed in a rune literal (it is not a single code
   516  point), and will appear as two code points if placed in a string
   517  literal.
   518  </p>
   519  
   520  
   521  <h2 id="Constants">Constants</h2>
   522  
   523  <p>There are <i>boolean constants</i>,
   524  <i>rune constants</i>,
   525  <i>integer constants</i>,
   526  <i>floating-point constants</i>, <i>complex constants</i>,
   527  and <i>string constants</i>. Rune, integer, floating-point,
   528  and complex constants are
   529  collectively called <i>numeric constants</i>.
   530  </p>
   531  
   532  <p>
   533  A constant value is represented by a
   534  <a href="#Rune_literals">rune</a>,
   535  <a href="#Integer_literals">integer</a>,
   536  <a href="#Floating-point_literals">floating-point</a>,
   537  <a href="#Imaginary_literals">imaginary</a>,
   538  or
   539  <a href="#String_literals">string</a> literal,
   540  an identifier denoting a constant,
   541  a <a href="#Constant_expressions">constant expression</a>,
   542  a <a href="#Conversions">conversion</a> with a result that is a constant, or
   543  the result value of some built-in functions such as
   544  <code>unsafe.Sizeof</code> applied to any value,
   545  <code>cap</code> or <code>len</code> applied to
   546  <a href="#Length_and_capacity">some expressions</a>,
   547  <code>real</code> and <code>imag</code> applied to a complex constant
   548  and <code>complex</code> applied to numeric constants.
   549  The boolean truth values are represented by the predeclared constants
   550  <code>true</code> and <code>false</code>. The predeclared identifier
   551  <a href="#Iota">iota</a> denotes an integer constant.
   552  </p>
   553  
   554  <p>
   555  In general, complex constants are a form of
   556  <a href="#Constant_expressions">constant expression</a>
   557  and are discussed in that section.
   558  </p>
   559  
   560  <p>
   561  Numeric constants represent exact values of arbitrary precision and do not overflow.
   562  Consequently, there are no constants denoting the IEEE-754 negative zero, infinity,
   563  and not-a-number values.
   564  </p>
   565  
   566  <p>
   567  Constants may be <a href="#Types">typed</a> or <i>untyped</i>.
   568  Literal constants, <code>true</code>, <code>false</code>, <code>iota</code>,
   569  and certain <a href="#Constant_expressions">constant expressions</a>
   570  containing only untyped constant operands are untyped.
   571  </p>
   572  
   573  <p>
   574  A constant may be given a type explicitly by a <a href="#Constant_declarations">constant declaration</a>
   575  or <a href="#Conversions">conversion</a>, or implicitly when used in a
   576  <a href="#Variable_declarations">variable declaration</a> or an
   577  <a href="#Assignments">assignment</a> or as an
   578  operand in an <a href="#Expressions">expression</a>.
   579  It is an error if the constant value
   580  cannot be <a href="#Representability">represented</a> as a value of the respective type.
   581  </p>
   582  
   583  <p>
   584  An untyped constant has a <i>default type</i> which is the type to which the
   585  constant is implicitly converted in contexts where a typed value is required,
   586  for instance, in a <a href="#Short_variable_declarations">short variable declaration</a>
   587  such as <code>i := 0</code> where there is no explicit type.
   588  The default type of an untyped constant is <code>bool</code>, <code>rune</code>,
   589  <code>int</code>, <code>float64</code>, <code>complex128</code> or <code>string</code>
   590  respectively, depending on whether it is a boolean, rune, integer, floating-point,
   591  complex, or string constant.
   592  </p>
   593  
   594  <p>
   595  Implementation restriction: Although numeric constants have arbitrary
   596  precision in the language, a compiler may implement them using an
   597  internal representation with limited precision.  That said, every
   598  implementation must:
   599  </p>
   600  <ul>
   601  	<li>Represent integer constants with at least 256 bits.</li>
   602  
   603  	<li>Represent floating-point constants, including the parts of
   604  	    a complex constant, with a mantissa of at least 256 bits
   605  	    and a signed binary exponent of at least 16 bits.</li>
   606  
   607  	<li>Give an error if unable to represent an integer constant
   608  	    precisely.</li>
   609  
   610  	<li>Give an error if unable to represent a floating-point or
   611  	    complex constant due to overflow.</li>
   612  
   613  	<li>Round to the nearest representable constant if unable to
   614  	    represent a floating-point or complex constant due to limits
   615  	    on precision.</li>
   616  </ul>
   617  <p>
   618  These requirements apply both to literal constants and to the result
   619  of evaluating <a href="#Constant_expressions">constant
   620  expressions</a>.
   621  </p>
   622  
   623  <h2 id="Variables">Variables</h2>
   624  
   625  <p>
   626  A variable is a storage location for holding a <i>value</i>.
   627  The set of permissible values is determined by the
   628  variable's <i><a href="#Types">type</a></i>.
   629  </p>
   630  
   631  <p>
   632  A <a href="#Variable_declarations">variable declaration</a>
   633  or, for function parameters and results, the signature
   634  of a <a href="#Function_declarations">function declaration</a>
   635  or <a href="#Function_literals">function literal</a> reserves
   636  storage for a named variable.
   637  
   638  Calling the built-in function <a href="#Allocation"><code>new</code></a>
   639  or taking the address of a <a href="#Composite_literals">composite literal</a>
   640  allocates storage for a variable at run time.
   641  Such an anonymous variable is referred to via a (possibly implicit)
   642  <a href="#Address_operators">pointer indirection</a>.
   643  </p>
   644  
   645  <p>
   646  <i>Structured</i> variables of <a href="#Array_types">array</a>, <a href="#Slice_types">slice</a>,
   647  and <a href="#Struct_types">struct</a> types have elements and fields that may
   648  be <a href="#Address_operators">addressed</a> individually. Each such element
   649  acts like a variable.
   650  </p>
   651  
   652  <p>
   653  The <i>static type</i> (or just <i>type</i>) of a variable is the
   654  type given in its declaration, the type provided in the
   655  <code>new</code> call or composite literal, or the type of
   656  an element of a structured variable.
   657  Variables of interface type also have a distinct <i>dynamic type</i>,
   658  which is the concrete type of the value assigned to the variable at run time
   659  (unless the value is the predeclared identifier <code>nil</code>,
   660  which has no type).
   661  The dynamic type may vary during execution but values stored in interface
   662  variables are always <a href="#Assignability">assignable</a>
   663  to the static type of the variable.
   664  </p>
   665  
   666  <pre>
   667  var x interface{}  // x is nil and has static type interface{}
   668  var v *T           // v has value nil, static type *T
   669  x = 42             // x has value 42 and dynamic type int
   670  x = v              // x has value (*T)(nil) and dynamic type *T
   671  </pre>
   672  
   673  <p>
   674  A variable's value is retrieved by referring to the variable in an
   675  <a href="#Expressions">expression</a>; it is the most recent value
   676  <a href="#Assignments">assigned</a> to the variable.
   677  If a variable has not yet been assigned a value, its value is the
   678  <a href="#The_zero_value">zero value</a> for its type.
   679  </p>
   680  
   681  
   682  <h2 id="Types">Types</h2>
   683  
   684  <p>
   685  A type determines a set of values together with operations and methods specific
   686  to those values. A type may be denoted by a <i>type name</i>, if it has one,
   687  or specified using a <i>type literal</i>, which composes a type from existing types.
   688  </p>
   689  
   690  <pre class="ebnf">
   691  Type      = TypeName | TypeLit | "(" Type ")" .
   692  TypeName  = identifier | QualifiedIdent .
   693  TypeLit   = ArrayType | StructType | PointerType | FunctionType | InterfaceType |
   694  	    SliceType | MapType | ChannelType .
   695  </pre>
   696  
   697  <p>
   698  Named instances of the boolean, numeric, and string types are
   699  <a href="#Predeclared_identifiers">predeclared</a>.
   700  Other named types are introduced with <a href="#Type_declarations">type declarations</a>.
   701  <i>Composite types</i>&mdash;array, struct, pointer, function,
   702  interface, slice, map, and channel types&mdash;may be constructed using
   703  type literals.
   704  </p>
   705  
   706  <p>
   707  Each type <code>T</code> has an <i>underlying type</i>: If <code>T</code>
   708  is one of the predeclared boolean, numeric, or string types, or a type literal,
   709  the corresponding underlying
   710  type is <code>T</code> itself. Otherwise, <code>T</code>'s underlying type
   711  is the underlying type of the type to which <code>T</code> refers in its
   712  <a href="#Type_declarations">type declaration</a>.
   713  </p>
   714  
   715  <pre>
   716  type (
   717  	A1 = string
   718  	A2 = A1
   719  )
   720  
   721  type (
   722  	B1 string
   723  	B2 B1
   724  	B3 []B1
   725  	B4 B3
   726  )
   727  </pre>
   728  
   729  <p>
   730  The underlying type of <code>string</code>, <code>A1</code>, <code>A2</code>, <code>B1</code>,
   731  and <code>B2</code> is <code>string</code>.
   732  The underlying type of <code>[]B1</code>, <code>B3</code>, and <code>B4</code> is <code>[]B1</code>.
   733  </p>
   734  
   735  <h3 id="Method_sets">Method sets</h3>
   736  <p>
   737  A type may have a <i>method set</i> associated with it.
   738  The method set of an <a href="#Interface_types">interface type</a> is its interface.
   739  The method set of any other type <code>T</code> consists of all
   740  <a href="#Method_declarations">methods</a> declared with receiver type <code>T</code>.
   741  The method set of the corresponding <a href="#Pointer_types">pointer type</a> <code>*T</code>
   742  is the set of all methods declared with receiver <code>*T</code> or <code>T</code>
   743  (that is, it also contains the method set of <code>T</code>).
   744  Further rules apply to structs containing embedded fields, as described
   745  in the section on <a href="#Struct_types">struct types</a>.
   746  Any other type has an empty method set.
   747  In a method set, each method must have a
   748  <a href="#Uniqueness_of_identifiers">unique</a>
   749  non-<a href="#Blank_identifier">blank</a> <a href="#MethodName">method name</a>.
   750  </p>
   751  
   752  <p>
   753  The method set of a type determines the interfaces that the
   754  type <a href="#Interface_types">implements</a>
   755  and the methods that can be <a href="#Calls">called</a>
   756  using a receiver of that type.
   757  </p>
   758  
   759  <h3 id="Boolean_types">Boolean types</h3>
   760  
   761  <p>
   762  A <i>boolean type</i> represents the set of Boolean truth values
   763  denoted by the predeclared constants <code>true</code>
   764  and <code>false</code>. The predeclared boolean type is <code>bool</code>.
   765  </p>
   766  
   767  <h3 id="Numeric_types">Numeric types</h3>
   768  
   769  <p>
   770  A <i>numeric type</i> represents sets of integer or floating-point values.
   771  The predeclared architecture-independent numeric types are:
   772  </p>
   773  
   774  <pre class="grammar">
   775  uint8       the set of all unsigned  8-bit integers (0 to 255)
   776  uint16      the set of all unsigned 16-bit integers (0 to 65535)
   777  uint32      the set of all unsigned 32-bit integers (0 to 4294967295)
   778  uint64      the set of all unsigned 64-bit integers (0 to 18446744073709551615)
   779  
   780  int8        the set of all signed  8-bit integers (-128 to 127)
   781  int16       the set of all signed 16-bit integers (-32768 to 32767)
   782  int32       the set of all signed 32-bit integers (-2147483648 to 2147483647)
   783  int64       the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)
   784  
   785  float32     the set of all IEEE-754 32-bit floating-point numbers
   786  float64     the set of all IEEE-754 64-bit floating-point numbers
   787  
   788  complex64   the set of all complex numbers with float32 real and imaginary parts
   789  complex128  the set of all complex numbers with float64 real and imaginary parts
   790  
   791  byte        alias for uint8
   792  rune        alias for int32
   793  </pre>
   794  
   795  <p>
   796  The value of an <i>n</i>-bit integer is <i>n</i> bits wide and represented using
   797  <a href="http://en.wikipedia.org/wiki/Two's_complement">two's complement arithmetic</a>.
   798  </p>
   799  
   800  <p>
   801  There is also a set of predeclared numeric types with implementation-specific sizes:
   802  </p>
   803  
   804  <pre class="grammar">
   805  uint     either 32 or 64 bits
   806  int      same size as uint
   807  uintptr  an unsigned integer large enough to store the uninterpreted bits of a pointer value
   808  </pre>
   809  
   810  <p>
   811  To avoid portability issues all numeric types are distinct except
   812  <code>byte</code>, which is an alias for <code>uint8</code>, and
   813  <code>rune</code>, which is an alias for <code>int32</code>.
   814  Conversions
   815  are required when different numeric types are mixed in an expression
   816  or assignment. For instance, <code>int32</code> and <code>int</code>
   817  are not the same type even though they may have the same size on a
   818  particular architecture.
   819  
   820  
   821  <h3 id="String_types">String types</h3>
   822  
   823  <p>
   824  A <i>string type</i> represents the set of string values.
   825  A string value is a (possibly empty) sequence of bytes.
   826  Strings are immutable: once created,
   827  it is impossible to change the contents of a string.
   828  The predeclared string type is <code>string</code>.
   829  </p>
   830  
   831  <p>
   832  The length of a string <code>s</code> (its size in bytes) can be discovered using
   833  the built-in function <a href="#Length_and_capacity"><code>len</code></a>.
   834  The length is a compile-time constant if the string is a constant.
   835  A string's bytes can be accessed by integer <a href="#Index_expressions">indices</a>
   836  0 through <code>len(s)-1</code>.
   837  It is illegal to take the address of such an element; if
   838  <code>s[i]</code> is the <code>i</code>'th byte of a
   839  string, <code>&amp;s[i]</code> is invalid.
   840  </p>
   841  
   842  
   843  <h3 id="Array_types">Array types</h3>
   844  
   845  <p>
   846  An array is a numbered sequence of elements of a single
   847  type, called the element type.
   848  The number of elements is called the length and is never
   849  negative.
   850  </p>
   851  
   852  <pre class="ebnf">
   853  ArrayType   = "[" ArrayLength "]" ElementType .
   854  ArrayLength = Expression .
   855  ElementType = Type .
   856  </pre>
   857  
   858  <p>
   859  The length is part of the array's type; it must evaluate to a
   860  non-negative <a href="#Constants">constant</a>
   861  <a href="#Representability">representable</a> by a value
   862  of type <code>int</code>.
   863  The length of array <code>a</code> can be discovered
   864  using the built-in function <a href="#Length_and_capacity"><code>len</code></a>.
   865  The elements can be addressed by integer <a href="#Index_expressions">indices</a>
   866  0 through <code>len(a)-1</code>.
   867  Array types are always one-dimensional but may be composed to form
   868  multi-dimensional types.
   869  </p>
   870  
   871  <pre>
   872  [32]byte
   873  [2*N] struct { x, y int32 }
   874  [1000]*float64
   875  [3][5]int
   876  [2][2][2]float64  // same as [2]([2]([2]float64))
   877  </pre>
   878  
   879  <h3 id="Slice_types">Slice types</h3>
   880  
   881  <p>
   882  A slice is a descriptor for a contiguous segment of an <i>underlying array</i> and
   883  provides access to a numbered sequence of elements from that array.
   884  A slice type denotes the set of all slices of arrays of its element type.
   885  The value of an uninitialized slice is <code>nil</code>.
   886  </p>
   887  
   888  <pre class="ebnf">
   889  SliceType = "[" "]" ElementType .
   890  </pre>
   891  
   892  <p>
   893  Like arrays, slices are indexable and have a length.  The length of a
   894  slice <code>s</code> can be discovered by the built-in function
   895  <a href="#Length_and_capacity"><code>len</code></a>; unlike with arrays it may change during
   896  execution.  The elements can be addressed by integer <a href="#Index_expressions">indices</a>
   897  0 through <code>len(s)-1</code>.  The slice index of a
   898  given element may be less than the index of the same element in the
   899  underlying array.
   900  </p>
   901  <p>
   902  A slice, once initialized, is always associated with an underlying
   903  array that holds its elements.  A slice therefore shares storage
   904  with its array and with other slices of the same array; by contrast,
   905  distinct arrays always represent distinct storage.
   906  </p>
   907  <p>
   908  The array underlying a slice may extend past the end of the slice.
   909  The <i>capacity</i> is a measure of that extent: it is the sum of
   910  the length of the slice and the length of the array beyond the slice;
   911  a slice of length up to that capacity can be created by
   912  <a href="#Slice_expressions"><i>slicing</i></a> a new one from the original slice.
   913  The capacity of a slice <code>a</code> can be discovered using the
   914  built-in function <a href="#Length_and_capacity"><code>cap(a)</code></a>.
   915  </p>
   916  
   917  <p>
   918  A new, initialized slice value for a given element type <code>T</code> is
   919  made using the built-in function
   920  <a href="#Making_slices_maps_and_channels"><code>make</code></a>,
   921  which takes a slice type
   922  and parameters specifying the length and optionally the capacity.
   923  A slice created with <code>make</code> always allocates a new, hidden array
   924  to which the returned slice value refers. That is, executing
   925  </p>
   926  
   927  <pre>
   928  make([]T, length, capacity)
   929  </pre>
   930  
   931  <p>
   932  produces the same slice as allocating an array and <a href="#Slice_expressions">slicing</a>
   933  it, so these two expressions are equivalent:
   934  </p>
   935  
   936  <pre>
   937  make([]int, 50, 100)
   938  new([100]int)[0:50]
   939  </pre>
   940  
   941  <p>
   942  Like arrays, slices are always one-dimensional but may be composed to construct
   943  higher-dimensional objects.
   944  With arrays of arrays, the inner arrays are, by construction, always the same length;
   945  however with slices of slices (or arrays of slices), the inner lengths may vary dynamically.
   946  Moreover, the inner slices must be initialized individually.
   947  </p>
   948  
   949  <h3 id="Struct_types">Struct types</h3>
   950  
   951  <p>
   952  A struct is a sequence of named elements, called fields, each of which has a
   953  name and a type. Field names may be specified explicitly (IdentifierList) or
   954  implicitly (EmbeddedField).
   955  Within a struct, non-<a href="#Blank_identifier">blank</a> field names must
   956  be <a href="#Uniqueness_of_identifiers">unique</a>.
   957  </p>
   958  
   959  <pre class="ebnf">
   960  StructType    = "struct" "{" { FieldDecl ";" } "}" .
   961  FieldDecl     = (IdentifierList Type | EmbeddedField) [ Tag ] .
   962  EmbeddedField = [ "*" ] TypeName .
   963  Tag           = string_lit .
   964  </pre>
   965  
   966  <pre>
   967  // An empty struct.
   968  struct {}
   969  
   970  // A struct with 6 fields.
   971  struct {
   972  	x, y int
   973  	u float32
   974  	_ float32  // padding
   975  	A *[]int
   976  	F func()
   977  }
   978  </pre>
   979  
   980  <p>
   981  A field declared with a type but no explicit field name is called an <i>embedded field</i>.
   982  An embedded field must be specified as
   983  a type name <code>T</code> or as a pointer to a non-interface type name <code>*T</code>,
   984  and <code>T</code> itself may not be
   985  a pointer type. The unqualified type name acts as the field name.
   986  </p>
   987  
   988  <pre>
   989  // A struct with four embedded fields of types T1, *T2, P.T3 and *P.T4
   990  struct {
   991  	T1        // field name is T1
   992  	*T2       // field name is T2
   993  	P.T3      // field name is T3
   994  	*P.T4     // field name is T4
   995  	x, y int  // field names are x and y
   996  }
   997  </pre>
   998  
   999  <p>
  1000  The following declaration is illegal because field names must be unique
  1001  in a struct type:
  1002  </p>
  1003  
  1004  <pre>
  1005  struct {
  1006  	T     // conflicts with embedded field *T and *P.T
  1007  	*T    // conflicts with embedded field T and *P.T
  1008  	*P.T  // conflicts with embedded field T and *T
  1009  }
  1010  </pre>
  1011  
  1012  <p>
  1013  A field or <a href="#Method_declarations">method</a> <code>f</code> of an
  1014  embedded field in a struct <code>x</code> is called <i>promoted</i> if
  1015  <code>x.f</code> is a legal <a href="#Selectors">selector</a> that denotes
  1016  that field or method <code>f</code>.
  1017  </p>
  1018  
  1019  <p>
  1020  Promoted fields act like ordinary fields
  1021  of a struct except that they cannot be used as field names in
  1022  <a href="#Composite_literals">composite literals</a> of the struct.
  1023  </p>
  1024  
  1025  <p>
  1026  Given a struct type <code>S</code> and a type named <code>T</code>,
  1027  promoted methods are included in the method set of the struct as follows:
  1028  </p>
  1029  <ul>
  1030  	<li>
  1031  	If <code>S</code> contains an embedded field <code>T</code>,
  1032  	the <a href="#Method_sets">method sets</a> of <code>S</code>
  1033  	and <code>*S</code> both include promoted methods with receiver
  1034  	<code>T</code>. The method set of <code>*S</code> also
  1035  	includes promoted methods with receiver <code>*T</code>.
  1036  	</li>
  1037  
  1038  	<li>
  1039  	If <code>S</code> contains an embedded field <code>*T</code>,
  1040  	the method sets of <code>S</code> and <code>*S</code> both
  1041  	include promoted methods with receiver <code>T</code> or
  1042  	<code>*T</code>.
  1043  	</li>
  1044  </ul>
  1045  
  1046  <p>
  1047  A field declaration may be followed by an optional string literal <i>tag</i>,
  1048  which becomes an attribute for all the fields in the corresponding
  1049  field declaration. An empty tag string is equivalent to an absent tag.
  1050  The tags are made visible through a <a href="/pkg/reflect/#StructTag">reflection interface</a>
  1051  and take part in <a href="#Type_identity">type identity</a> for structs
  1052  but are otherwise ignored.
  1053  </p>
  1054  
  1055  <pre>
  1056  struct {
  1057  	x, y float64 ""  // an empty tag string is like an absent tag
  1058  	name string  "any string is permitted as a tag"
  1059  	_    [4]byte "ceci n'est pas un champ de structure"
  1060  }
  1061  
  1062  // A struct corresponding to a TimeStamp protocol buffer.
  1063  // The tag strings define the protocol buffer field numbers;
  1064  // they follow the convention outlined by the reflect package.
  1065  struct {
  1066  	microsec  uint64 `protobuf:"1"`
  1067  	serverIP6 uint64 `protobuf:"2"`
  1068  }
  1069  </pre>
  1070  
  1071  <h3 id="Pointer_types">Pointer types</h3>
  1072  
  1073  <p>
  1074  A pointer type denotes the set of all pointers to <a href="#Variables">variables</a> of a given
  1075  type, called the <i>base type</i> of the pointer.
  1076  The value of an uninitialized pointer is <code>nil</code>.
  1077  </p>
  1078  
  1079  <pre class="ebnf">
  1080  PointerType = "*" BaseType .
  1081  BaseType    = Type .
  1082  </pre>
  1083  
  1084  <pre>
  1085  *Point
  1086  *[4]int
  1087  </pre>
  1088  
  1089  <h3 id="Function_types">Function types</h3>
  1090  
  1091  <p>
  1092  A function type denotes the set of all functions with the same parameter
  1093  and result types. The value of an uninitialized variable of function type
  1094  is <code>nil</code>.
  1095  </p>
  1096  
  1097  <pre class="ebnf">
  1098  FunctionType   = "func" Signature .
  1099  Signature      = Parameters [ Result ] .
  1100  Result         = Parameters | Type .
  1101  Parameters     = "(" [ ParameterList [ "," ] ] ")" .
  1102  ParameterList  = ParameterDecl { "," ParameterDecl } .
  1103  ParameterDecl  = [ IdentifierList ] [ "..." ] Type .
  1104  </pre>
  1105  
  1106  <p>
  1107  Within a list of parameters or results, the names (IdentifierList)
  1108  must either all be present or all be absent. If present, each name
  1109  stands for one item (parameter or result) of the specified type and
  1110  all non-<a href="#Blank_identifier">blank</a> names in the signature
  1111  must be <a href="#Uniqueness_of_identifiers">unique</a>.
  1112  If absent, each type stands for one item of that type.
  1113  Parameter and result
  1114  lists are always parenthesized except that if there is exactly
  1115  one unnamed result it may be written as an unparenthesized type.
  1116  </p>
  1117  
  1118  <p>
  1119  The final incoming parameter in a function signature may have
  1120  a type prefixed with <code>...</code>.
  1121  A function with such a parameter is called <i>variadic</i> and
  1122  may be invoked with zero or more arguments for that parameter.
  1123  </p>
  1124  
  1125  <pre>
  1126  func()
  1127  func(x int) int
  1128  func(a, _ int, z float32) bool
  1129  func(a, b int, z float32) (bool)
  1130  func(prefix string, values ...int)
  1131  func(a, b int, z float64, opt ...interface{}) (success bool)
  1132  func(int, int, float64) (float64, *[]int)
  1133  func(n int) func(p *T)
  1134  </pre>
  1135  
  1136  
  1137  <h3 id="Interface_types">Interface types</h3>
  1138  
  1139  <p>
  1140  An interface type specifies a <a href="#Method_sets">method set</a> called its <i>interface</i>.
  1141  A variable of interface type can store a value of any type with a method set
  1142  that is any superset of the interface. Such a type is said to
  1143  <i>implement the interface</i>.
  1144  The value of an uninitialized variable of interface type is <code>nil</code>.
  1145  </p>
  1146  
  1147  <pre class="ebnf">
  1148  InterfaceType      = "interface" "{" { MethodSpec ";" } "}" .
  1149  MethodSpec         = MethodName Signature | InterfaceTypeName .
  1150  MethodName         = identifier .
  1151  InterfaceTypeName  = TypeName .
  1152  </pre>
  1153  
  1154  <p>
  1155  As with all method sets, in an interface type, each method must have a
  1156  <a href="#Uniqueness_of_identifiers">unique</a>
  1157  non-<a href="#Blank_identifier">blank</a> name.
  1158  </p>
  1159  
  1160  <pre>
  1161  // A simple File interface
  1162  interface {
  1163  	Read(b Buffer) bool
  1164  	Write(b Buffer) bool
  1165  	Close()
  1166  }
  1167  </pre>
  1168  
  1169  <p>
  1170  More than one type may implement an interface.
  1171  For instance, if two types <code>S1</code> and <code>S2</code>
  1172  have the method set
  1173  </p>
  1174  
  1175  <pre>
  1176  func (p T) Read(b Buffer) bool { return … }
  1177  func (p T) Write(b Buffer) bool { return … }
  1178  func (p T) Close() { … }
  1179  </pre>
  1180  
  1181  <p>
  1182  (where <code>T</code> stands for either <code>S1</code> or <code>S2</code>)
  1183  then the <code>File</code> interface is implemented by both <code>S1</code> and
  1184  <code>S2</code>, regardless of what other methods
  1185  <code>S1</code> and <code>S2</code> may have or share.
  1186  </p>
  1187  
  1188  <p>
  1189  A type implements any interface comprising any subset of its methods
  1190  and may therefore implement several distinct interfaces. For
  1191  instance, all types implement the <i>empty interface</i>:
  1192  </p>
  1193  
  1194  <pre>
  1195  interface{}
  1196  </pre>
  1197  
  1198  <p>
  1199  Similarly, consider this interface specification,
  1200  which appears within a <a href="#Type_declarations">type declaration</a>
  1201  to define an interface called <code>Locker</code>:
  1202  </p>
  1203  
  1204  <pre>
  1205  type Locker interface {
  1206  	Lock()
  1207  	Unlock()
  1208  }
  1209  </pre>
  1210  
  1211  <p>
  1212  If <code>S1</code> and <code>S2</code> also implement
  1213  </p>
  1214  
  1215  <pre>
  1216  func (p T) Lock() { … }
  1217  func (p T) Unlock() { … }
  1218  </pre>
  1219  
  1220  <p>
  1221  they implement the <code>Locker</code> interface as well
  1222  as the <code>File</code> interface.
  1223  </p>
  1224  
  1225  <p>
  1226  An interface <code>T</code> may use a (possibly qualified) interface type
  1227  name <code>E</code> in place of a method specification. This is called
  1228  <i>embedding</i> interface <code>E</code> in <code>T</code>; it adds
  1229  all (exported and non-exported) methods of <code>E</code> to the interface
  1230  <code>T</code>.
  1231  </p>
  1232  
  1233  <pre>
  1234  type ReadWriter interface {
  1235  	Read(b Buffer) bool
  1236  	Write(b Buffer) bool
  1237  }
  1238  
  1239  type File interface {
  1240  	ReadWriter  // same as adding the methods of ReadWriter
  1241  	Locker      // same as adding the methods of Locker
  1242  	Close()
  1243  }
  1244  
  1245  type LockedFile interface {
  1246  	Locker
  1247  	File        // illegal: Lock, Unlock not unique
  1248  	Lock()      // illegal: Lock not unique
  1249  }
  1250  </pre>
  1251  
  1252  <p>
  1253  An interface type <code>T</code> may not embed itself
  1254  or any interface type that embeds <code>T</code>, recursively.
  1255  </p>
  1256  
  1257  <pre>
  1258  // illegal: Bad cannot embed itself
  1259  type Bad interface {
  1260  	Bad
  1261  }
  1262  
  1263  // illegal: Bad1 cannot embed itself using Bad2
  1264  type Bad1 interface {
  1265  	Bad2
  1266  }
  1267  type Bad2 interface {
  1268  	Bad1
  1269  }
  1270  </pre>
  1271  
  1272  <h3 id="Map_types">Map types</h3>
  1273  
  1274  <p>
  1275  A map is an unordered group of elements of one type, called the
  1276  element type, indexed by a set of unique <i>keys</i> of another type,
  1277  called the key type.
  1278  The value of an uninitialized map is <code>nil</code>.
  1279  </p>
  1280  
  1281  <pre class="ebnf">
  1282  MapType     = "map" "[" KeyType "]" ElementType .
  1283  KeyType     = Type .
  1284  </pre>
  1285  
  1286  <p>
  1287  The <a href="#Comparison_operators">comparison operators</a>
  1288  <code>==</code> and <code>!=</code> must be fully defined
  1289  for operands of the key type; thus the key type must not be a function, map, or
  1290  slice.
  1291  If the key type is an interface type, these
  1292  comparison operators must be defined for the dynamic key values;
  1293  failure will cause a <a href="#Run_time_panics">run-time panic</a>.
  1294  
  1295  </p>
  1296  
  1297  <pre>
  1298  map[string]int
  1299  map[*T]struct{ x, y float64 }
  1300  map[string]interface{}
  1301  </pre>
  1302  
  1303  <p>
  1304  The number of map elements is called its length.
  1305  For a map <code>m</code>, it can be discovered using the
  1306  built-in function <a href="#Length_and_capacity"><code>len</code></a>
  1307  and may change during execution. Elements may be added during execution
  1308  using <a href="#Assignments">assignments</a> and retrieved with
  1309  <a href="#Index_expressions">index expressions</a>; they may be removed with the
  1310  <a href="#Deletion_of_map_elements"><code>delete</code></a> built-in function.
  1311  </p>
  1312  <p>
  1313  A new, empty map value is made using the built-in
  1314  function <a href="#Making_slices_maps_and_channels"><code>make</code></a>,
  1315  which takes the map type and an optional capacity hint as arguments:
  1316  </p>
  1317  
  1318  <pre>
  1319  make(map[string]int)
  1320  make(map[string]int, 100)
  1321  </pre>
  1322  
  1323  <p>
  1324  The initial capacity does not bound its size:
  1325  maps grow to accommodate the number of items
  1326  stored in them, with the exception of <code>nil</code> maps.
  1327  A <code>nil</code> map is equivalent to an empty map except that no elements
  1328  may be added.
  1329  
  1330  <h3 id="Channel_types">Channel types</h3>
  1331  
  1332  <p>
  1333  A channel provides a mechanism for
  1334  <a href="#Go_statements">concurrently executing functions</a>
  1335  to communicate by
  1336  <a href="#Send_statements">sending</a> and
  1337  <a href="#Receive_operator">receiving</a>
  1338  values of a specified element type.
  1339  The value of an uninitialized channel is <code>nil</code>.
  1340  </p>
  1341  
  1342  <pre class="ebnf">
  1343  ChannelType = ( "chan" | "chan" "&lt;-" | "&lt;-" "chan" ) ElementType .
  1344  </pre>
  1345  
  1346  <p>
  1347  The optional <code>&lt;-</code> operator specifies the channel <i>direction</i>,
  1348  <i>send</i> or <i>receive</i>. If no direction is given, the channel is
  1349  <i>bidirectional</i>.
  1350  A channel may be constrained only to send or only to receive by
  1351  <a href="#Conversions">conversion</a> or <a href="#Assignments">assignment</a>.
  1352  </p>
  1353  
  1354  <pre>
  1355  chan T          // can be used to send and receive values of type T
  1356  chan&lt;- float64  // can only be used to send float64s
  1357  &lt;-chan int      // can only be used to receive ints
  1358  </pre>
  1359  
  1360  <p>
  1361  The <code>&lt;-</code> operator associates with the leftmost <code>chan</code>
  1362  possible:
  1363  </p>
  1364  
  1365  <pre>
  1366  chan&lt;- chan int    // same as chan&lt;- (chan int)
  1367  chan&lt;- &lt;-chan int  // same as chan&lt;- (&lt;-chan int)
  1368  &lt;-chan &lt;-chan int  // same as &lt;-chan (&lt;-chan int)
  1369  chan (&lt;-chan int)
  1370  </pre>
  1371  
  1372  <p>
  1373  A new, initialized channel
  1374  value can be made using the built-in function
  1375  <a href="#Making_slices_maps_and_channels"><code>make</code></a>,
  1376  which takes the channel type and an optional <i>capacity</i> as arguments:
  1377  </p>
  1378  
  1379  <pre>
  1380  make(chan int, 100)
  1381  </pre>
  1382  
  1383  <p>
  1384  The capacity, in number of elements, sets the size of the buffer in the channel.
  1385  If the capacity is zero or absent, the channel is unbuffered and communication
  1386  succeeds only when both a sender and receiver are ready. Otherwise, the channel
  1387  is buffered and communication succeeds without blocking if the buffer
  1388  is not full (sends) or not empty (receives).
  1389  A <code>nil</code> channel is never ready for communication.
  1390  </p>
  1391  
  1392  <p>
  1393  A channel may be closed with the built-in function
  1394  <a href="#Close"><code>close</code></a>.
  1395  The multi-valued assignment form of the
  1396  <a href="#Receive_operator">receive operator</a>
  1397  reports whether a received value was sent before
  1398  the channel was closed.
  1399  </p>
  1400  
  1401  <p>
  1402  A single channel may be used in
  1403  <a href="#Send_statements">send statements</a>,
  1404  <a href="#Receive_operator">receive operations</a>,
  1405  and calls to the built-in functions
  1406  <a href="#Length_and_capacity"><code>cap</code></a> and
  1407  <a href="#Length_and_capacity"><code>len</code></a>
  1408  by any number of goroutines without further synchronization.
  1409  Channels act as first-in-first-out queues.
  1410  For example, if one goroutine sends values on a channel
  1411  and a second goroutine receives them, the values are
  1412  received in the order sent.
  1413  </p>
  1414  
  1415  <h2 id="Properties_of_types_and_values">Properties of types and values</h2>
  1416  
  1417  <h3 id="Type_identity">Type identity</h3>
  1418  
  1419  <p>
  1420  Two types are either <i>identical</i> or <i>different</i>.
  1421  </p>
  1422  
  1423  <p>
  1424  A <a href="#Type_definitions">defined type</a> is always different from any other type.
  1425  Otherwise, two types are identical if their <a href="#Types">underlying</a> type literals are
  1426  structurally equivalent; that is, they have the same literal structure and corresponding
  1427  components have identical types. In detail:
  1428  </p>
  1429  
  1430  <ul>
  1431  	<li>Two array types are identical if they have identical element types and
  1432  	    the same array length.</li>
  1433  
  1434  	<li>Two slice types are identical if they have identical element types.</li>
  1435  
  1436  	<li>Two struct types are identical if they have the same sequence of fields,
  1437  	    and if corresponding fields have the same names, and identical types,
  1438  	    and identical tags.
  1439  	    <a href="#Exported_identifiers">Non-exported</a> field names from different
  1440  	    packages are always different.</li>
  1441  
  1442  	<li>Two pointer types are identical if they have identical base types.</li>
  1443  
  1444  	<li>Two function types are identical if they have the same number of parameters
  1445  	    and result values, corresponding parameter and result types are
  1446  	    identical, and either both functions are variadic or neither is.
  1447  	    Parameter and result names are not required to match.</li>
  1448  
  1449  	<li>Two interface types are identical if they have the same set of methods
  1450  	    with the same names and identical function types.
  1451  	    <a href="#Exported_identifiers">Non-exported</a> method names from different
  1452  	    packages are always different. The order of the methods is irrelevant.</li>
  1453  
  1454  	<li>Two map types are identical if they have identical key and value types.</li>
  1455  
  1456  	<li>Two channel types are identical if they have identical value types and
  1457  	    the same direction.</li>
  1458  </ul>
  1459  
  1460  <p>
  1461  Given the declarations
  1462  </p>
  1463  
  1464  <pre>
  1465  type (
  1466  	A0 = []string
  1467  	A1 = A0
  1468  	A2 = struct{ a, b int }
  1469  	A3 = int
  1470  	A4 = func(A3, float64) *A0
  1471  	A5 = func(x int, _ float64) *[]string
  1472  )
  1473  
  1474  type (
  1475  	B0 A0
  1476  	B1 []string
  1477  	B2 struct{ a, b int }
  1478  	B3 struct{ a, c int }
  1479  	B4 func(int, float64) *B0
  1480  	B5 func(x int, y float64) *A1
  1481  )
  1482  
  1483  type	C0 = B0
  1484  </pre>
  1485  
  1486  <p>
  1487  these types are identical:
  1488  </p>
  1489  
  1490  <pre>
  1491  A0, A1, and []string
  1492  A2 and struct{ a, b int }
  1493  A3 and int
  1494  A4, func(int, float64) *[]string, and A5
  1495  
  1496  B0, B0, and C0
  1497  []int and []int
  1498  struct{ a, b *T5 } and struct{ a, b *T5 }
  1499  func(x int, y float64) *[]string, func(int, float64) (result *[]string), and A5
  1500  </pre>
  1501  
  1502  <p>
  1503  <code>B0</code> and <code>B1</code> are different because they are new types
  1504  created by distinct <a href="#Type_definitions">type definitions</a>;
  1505  <code>func(int, float64) *B0</code> and <code>func(x int, y float64) *[]string</code>
  1506  are different because <code>B0</code> is different from <code>[]string</code>.
  1507  </p>
  1508  
  1509  
  1510  <h3 id="Assignability">Assignability</h3>
  1511  
  1512  <p>
  1513  A value <code>x</code> is <i>assignable</i> to a <a href="#Variables">variable</a> of type <code>T</code>
  1514  ("<code>x</code> is assignable to <code>T</code>") if one of the following conditions applies:
  1515  </p>
  1516  
  1517  <ul>
  1518  <li>
  1519  <code>x</code>'s type is identical to <code>T</code>.
  1520  </li>
  1521  <li>
  1522  <code>x</code>'s type <code>V</code> and <code>T</code> have identical
  1523  <a href="#Types">underlying types</a> and at least one of <code>V</code>
  1524  or <code>T</code> is not a <a href="#Type_definitions">defined</a> type.
  1525  </li>
  1526  <li>
  1527  <code>T</code> is an interface type and
  1528  <code>x</code> <a href="#Interface_types">implements</a> <code>T</code>.
  1529  </li>
  1530  <li>
  1531  <code>x</code> is a bidirectional channel value, <code>T</code> is a channel type,
  1532  <code>x</code>'s type <code>V</code> and <code>T</code> have identical element types,
  1533  and at least one of <code>V</code> or <code>T</code> is not a defined type.
  1534  </li>
  1535  <li>
  1536  <code>x</code> is the predeclared identifier <code>nil</code> and <code>T</code>
  1537  is a pointer, function, slice, map, channel, or interface type.
  1538  </li>
  1539  <li>
  1540  <code>x</code> is an untyped <a href="#Constants">constant</a>
  1541  <a href="#Representability">representable</a>
  1542  by a value of type <code>T</code>.
  1543  </li>
  1544  </ul>
  1545  
  1546  
  1547  <h3 id="Representability">Representability</h3>
  1548  
  1549  <p>
  1550  A <a href="#Constants">constant</a> <code>x</code> is <i>representable</i>
  1551  by a value of type <code>T</code> if one of the following conditions applies:
  1552  </p>
  1553  
  1554  <ul>
  1555  <li>
  1556  <code>x</code> is in the set of values <a href="#Types">determined</a> by <code>T</code>.
  1557  </li>
  1558  
  1559  <li>
  1560  <code>T</code> is a floating-point type and <code>x</code> can be rounded to <code>T</code>'s
  1561  precision without overflow. Rounding uses IEEE 754 round-to-even rules but with an IEEE
  1562  negative zero further simplified to an unsigned zero. Note that constant values never result
  1563  in an IEEE negative zero, NaN, or infinity.
  1564  </li>
  1565  
  1566  <li>
  1567  <code>T</code> is a complex type, and <code>x</code>'s
  1568  <a href="#Complex_numbers">components</a> <code>real(x)</code> and <code>imag(x)</code>
  1569  are representable by values of <code>T</code>'s component type (<code>float32</code> or
  1570  <code>float64</code>).
  1571  </li>
  1572  </ul>
  1573  
  1574  <pre>
  1575  x                   T           x is representable by a value of T because
  1576  
  1577  'a'                 byte        97 is in the set of byte values
  1578  97                  rune        rune is an alias for int32, and 97 is in the set of 32-bit integers
  1579  "foo"               string      "foo" is in the set of string values
  1580  1024                int16       1024 is in the set of 16-bit integers
  1581  42.0                byte        42 is in the set of unsigned 8-bit integers
  1582  1e10                uint64      10000000000 is in the set of unsigned 64-bit integers
  1583  2.718281828459045   float32     2.718281828459045 rounds to 2.7182817 which is in the set of float32 values
  1584  -1e-1000            float64     -1e-1000 rounds to IEEE -0.0 which is further simplified to 0.0
  1585  0i                  int         0 is an integer value
  1586  (42 + 0i)           float32     42.0 (with zero imaginary part) is in the set of float32 values
  1587  </pre>
  1588  
  1589  <pre>
  1590  x                   T           x is not representable by a value of T because
  1591  
  1592  0                   bool        0 is not in the set of boolean values
  1593  'a'                 string      'a' is a rune, it is not in the set of string values
  1594  1024                byte        1024 is not in the set of unsigned 8-bit integers
  1595  -1                  uint16      -1 is not in the set of unsigned 16-bit integers
  1596  1.1                 int         1.1 is not an integer value
  1597  42i                 float32     (0 + 42i) is not in the set of float32 values
  1598  1e1000              float64     1e1000 overflows to IEEE +Inf after rounding
  1599  </pre>
  1600  
  1601  
  1602  <h2 id="Blocks">Blocks</h2>
  1603  
  1604  <p>
  1605  A <i>block</i> is a possibly empty sequence of declarations and statements
  1606  within matching brace brackets.
  1607  </p>
  1608  
  1609  <pre class="ebnf">
  1610  Block = "{" StatementList "}" .
  1611  StatementList = { Statement ";" } .
  1612  </pre>
  1613  
  1614  <p>
  1615  In addition to explicit blocks in the source code, there are implicit blocks:
  1616  </p>
  1617  
  1618  <ol>
  1619  	<li>The <i>universe block</i> encompasses all Go source text.</li>
  1620  
  1621  	<li>Each <a href="#Packages">package</a> has a <i>package block</i> containing all
  1622  	    Go source text for that package.</li>
  1623  
  1624  	<li>Each file has a <i>file block</i> containing all Go source text
  1625  	    in that file.</li>
  1626  
  1627  	<li>Each <a href="#If_statements">"if"</a>,
  1628  	    <a href="#For_statements">"for"</a>, and
  1629  	    <a href="#Switch_statements">"switch"</a>
  1630  	    statement is considered to be in its own implicit block.</li>
  1631  
  1632  	<li>Each clause in a <a href="#Switch_statements">"switch"</a>
  1633  	    or <a href="#Select_statements">"select"</a> statement
  1634  	    acts as an implicit block.</li>
  1635  </ol>
  1636  
  1637  <p>
  1638  Blocks nest and influence <a href="#Declarations_and_scope">scoping</a>.
  1639  </p>
  1640  
  1641  
  1642  <h2 id="Declarations_and_scope">Declarations and scope</h2>
  1643  
  1644  <p>
  1645  A <i>declaration</i> binds a non-<a href="#Blank_identifier">blank</a> identifier to a
  1646  <a href="#Constant_declarations">constant</a>,
  1647  <a href="#Type_declarations">type</a>,
  1648  <a href="#Variable_declarations">variable</a>,
  1649  <a href="#Function_declarations">function</a>,
  1650  <a href="#Labeled_statements">label</a>, or
  1651  <a href="#Import_declarations">package</a>.
  1652  Every identifier in a program must be declared.
  1653  No identifier may be declared twice in the same block, and
  1654  no identifier may be declared in both the file and package block.
  1655  </p>
  1656  
  1657  <p>
  1658  The <a href="#Blank_identifier">blank identifier</a> may be used like any other identifier
  1659  in a declaration, but it does not introduce a binding and thus is not declared.
  1660  In the package block, the identifier <code>init</code> may only be used for
  1661  <a href="#Package_initialization"><code>init</code> function</a> declarations,
  1662  and like the blank identifier it does not introduce a new binding.
  1663  </p>
  1664  
  1665  <pre class="ebnf">
  1666  Declaration   = ConstDecl | TypeDecl | VarDecl .
  1667  TopLevelDecl  = Declaration | FunctionDecl | MethodDecl .
  1668  </pre>
  1669  
  1670  <p>
  1671  The <i>scope</i> of a declared identifier is the extent of source text in which
  1672  the identifier denotes the specified constant, type, variable, function, label, or package.
  1673  </p>
  1674  
  1675  <p>
  1676  Go is lexically scoped using <a href="#Blocks">blocks</a>:
  1677  </p>
  1678  
  1679  <ol>
  1680  	<li>The scope of a <a href="#Predeclared_identifiers">predeclared identifier</a> is the universe block.</li>
  1681  
  1682  	<li>The scope of an identifier denoting a constant, type, variable,
  1683  	    or function (but not method) declared at top level (outside any
  1684  	    function) is the package block.</li>
  1685  
  1686  	<li>The scope of the package name of an imported package is the file block
  1687  	    of the file containing the import declaration.</li>
  1688  
  1689  	<li>The scope of an identifier denoting a method receiver, function parameter,
  1690  	    or result variable is the function body.</li>
  1691  
  1692  	<li>The scope of a constant or variable identifier declared
  1693  	    inside a function begins at the end of the ConstSpec or VarSpec
  1694  	    (ShortVarDecl for short variable declarations)
  1695  	    and ends at the end of the innermost containing block.</li>
  1696  
  1697  	<li>The scope of a type identifier declared inside a function
  1698  	    begins at the identifier in the TypeSpec
  1699  	    and ends at the end of the innermost containing block.</li>
  1700  </ol>
  1701  
  1702  <p>
  1703  An identifier declared in a block may be redeclared in an inner block.
  1704  While the identifier of the inner declaration is in scope, it denotes
  1705  the entity declared by the inner declaration.
  1706  </p>
  1707  
  1708  <p>
  1709  The <a href="#Package_clause">package clause</a> is not a declaration; the package name
  1710  does not appear in any scope. Its purpose is to identify the files belonging
  1711  to the same <a href="#Packages">package</a> and to specify the default package name for import
  1712  declarations.
  1713  </p>
  1714  
  1715  
  1716  <h3 id="Label_scopes">Label scopes</h3>
  1717  
  1718  <p>
  1719  Labels are declared by <a href="#Labeled_statements">labeled statements</a> and are
  1720  used in the <a href="#Break_statements">"break"</a>,
  1721  <a href="#Continue_statements">"continue"</a>, and
  1722  <a href="#Goto_statements">"goto"</a> statements.
  1723  It is illegal to define a label that is never used.
  1724  In contrast to other identifiers, labels are not block scoped and do
  1725  not conflict with identifiers that are not labels. The scope of a label
  1726  is the body of the function in which it is declared and excludes
  1727  the body of any nested function.
  1728  </p>
  1729  
  1730  
  1731  <h3 id="Blank_identifier">Blank identifier</h3>
  1732  
  1733  <p>
  1734  The <i>blank identifier</i> is represented by the underscore character <code>_</code>.
  1735  It serves as an anonymous placeholder instead of a regular (non-blank)
  1736  identifier and has special meaning in <a href="#Declarations_and_scope">declarations</a>,
  1737  as an <a href="#Operands">operand</a>, and in <a href="#Assignments">assignments</a>.
  1738  </p>
  1739  
  1740  
  1741  <h3 id="Predeclared_identifiers">Predeclared identifiers</h3>
  1742  
  1743  <p>
  1744  The following identifiers are implicitly declared in the
  1745  <a href="#Blocks">universe block</a>:
  1746  </p>
  1747  <pre class="grammar">
  1748  Types:
  1749  	bool byte complex64 complex128 error float32 float64
  1750  	int int8 int16 int32 int64 rune string
  1751  	uint uint8 uint16 uint32 uint64 uintptr
  1752  
  1753  Constants:
  1754  	true false iota
  1755  
  1756  Zero value:
  1757  	nil
  1758  
  1759  Functions:
  1760  	append cap close complex copy delete imag len
  1761  	make new panic print println real recover
  1762  </pre>
  1763  
  1764  
  1765  <h3 id="Exported_identifiers">Exported identifiers</h3>
  1766  
  1767  <p>
  1768  An identifier may be <i>exported</i> to permit access to it from another package.
  1769  An identifier is exported if both:
  1770  </p>
  1771  <ol>
  1772  	<li>the first character of the identifier's name is a Unicode upper case
  1773  	letter (Unicode class "Lu"); and</li>
  1774  	<li>the identifier is declared in the <a href="#Blocks">package block</a>
  1775  	or it is a <a href="#Struct_types">field name</a> or
  1776  	<a href="#MethodName">method name</a>.</li>
  1777  </ol>
  1778  <p>
  1779  All other identifiers are not exported.
  1780  </p>
  1781  
  1782  
  1783  <h3 id="Uniqueness_of_identifiers">Uniqueness of identifiers</h3>
  1784  
  1785  <p>
  1786  Given a set of identifiers, an identifier is called <i>unique</i> if it is
  1787  <i>different</i> from every other in the set.
  1788  Two identifiers are different if they are spelled differently, or if they
  1789  appear in different <a href="#Packages">packages</a> and are not
  1790  <a href="#Exported_identifiers">exported</a>. Otherwise, they are the same.
  1791  </p>
  1792  
  1793  <h3 id="Constant_declarations">Constant declarations</h3>
  1794  
  1795  <p>
  1796  A constant declaration binds a list of identifiers (the names of
  1797  the constants) to the values of a list of <a href="#Constant_expressions">constant expressions</a>.
  1798  The number of identifiers must be equal
  1799  to the number of expressions, and the <i>n</i>th identifier on
  1800  the left is bound to the value of the <i>n</i>th expression on the
  1801  right.
  1802  </p>
  1803  
  1804  <pre class="ebnf">
  1805  ConstDecl      = "const" ( ConstSpec | "(" { ConstSpec ";" } ")" ) .
  1806  ConstSpec      = IdentifierList [ [ Type ] "=" ExpressionList ] .
  1807  
  1808  IdentifierList = identifier { "," identifier } .
  1809  ExpressionList = Expression { "," Expression } .
  1810  </pre>
  1811  
  1812  <p>
  1813  If the type is present, all constants take the type specified, and
  1814  the expressions must be <a href="#Assignability">assignable</a> to that type.
  1815  If the type is omitted, the constants take the
  1816  individual types of the corresponding expressions.
  1817  If the expression values are untyped <a href="#Constants">constants</a>,
  1818  the declared constants remain untyped and the constant identifiers
  1819  denote the constant values. For instance, if the expression is a
  1820  floating-point literal, the constant identifier denotes a floating-point
  1821  constant, even if the literal's fractional part is zero.
  1822  </p>
  1823  
  1824  <pre>
  1825  const Pi float64 = 3.14159265358979323846
  1826  const zero = 0.0         // untyped floating-point constant
  1827  const (
  1828  	size int64 = 1024
  1829  	eof        = -1  // untyped integer constant
  1830  )
  1831  const a, b, c = 3, 4, "foo"  // a = 3, b = 4, c = "foo", untyped integer and string constants
  1832  const u, v float32 = 0, 3    // u = 0.0, v = 3.0
  1833  </pre>
  1834  
  1835  <p>
  1836  Within a parenthesized <code>const</code> declaration list the
  1837  expression list may be omitted from any but the first declaration.
  1838  Such an empty list is equivalent to the textual substitution of the
  1839  first preceding non-empty expression list and its type if any.
  1840  Omitting the list of expressions is therefore equivalent to
  1841  repeating the previous list.  The number of identifiers must be equal
  1842  to the number of expressions in the previous list.
  1843  Together with the <a href="#Iota"><code>iota</code> constant generator</a>
  1844  this mechanism permits light-weight declaration of sequential values:
  1845  </p>
  1846  
  1847  <pre>
  1848  const (
  1849  	Sunday = iota
  1850  	Monday
  1851  	Tuesday
  1852  	Wednesday
  1853  	Thursday
  1854  	Friday
  1855  	Partyday
  1856  	numberOfDays  // this constant is not exported
  1857  )
  1858  </pre>
  1859  
  1860  
  1861  <h3 id="Iota">Iota</h3>
  1862  
  1863  <p>
  1864  Within a <a href="#Constant_declarations">constant declaration</a>, the predeclared identifier
  1865  <code>iota</code> represents successive untyped integer <a href="#Constants">
  1866  constants</a>. It is reset to 0 whenever the reserved word <code>const</code>
  1867  appears in the source and increments after each <a href="#ConstSpec">ConstSpec</a>.
  1868  It can be used to construct a set of related constants:
  1869  </p>
  1870  
  1871  <pre>
  1872  const ( // iota is reset to 0
  1873  	c0 = iota  // c0 == 0
  1874  	c1 = iota  // c1 == 1
  1875  	c2 = iota  // c2 == 2
  1876  )
  1877  
  1878  const ( // iota is reset to 0
  1879  	a = 1 &lt;&lt; iota  // a == 1
  1880  	b = 1 &lt;&lt; iota  // b == 2
  1881  	c = 3          // c == 3  (iota is not used but still incremented)
  1882  	d = 1 &lt;&lt; iota  // d == 8
  1883  )
  1884  
  1885  const ( // iota is reset to 0
  1886  	u         = iota * 42  // u == 0     (untyped integer constant)
  1887  	v float64 = iota * 42  // v == 42.0  (float64 constant)
  1888  	w         = iota * 42  // w == 84    (untyped integer constant)
  1889  )
  1890  
  1891  const x = iota  // x == 0  (iota has been reset)
  1892  const y = iota  // y == 0  (iota has been reset)
  1893  </pre>
  1894  
  1895  <p>
  1896  Within an ExpressionList, the value of each <code>iota</code> is the same because
  1897  it is only incremented after each ConstSpec:
  1898  </p>
  1899  
  1900  <pre>
  1901  const (
  1902  	bit0, mask0 = 1 &lt;&lt; iota, 1&lt;&lt;iota - 1  // bit0 == 1, mask0 == 0
  1903  	bit1, mask1                           // bit1 == 2, mask1 == 1
  1904  	_, _                                  // skips iota == 2
  1905  	bit3, mask3                           // bit3 == 8, mask3 == 7
  1906  )
  1907  </pre>
  1908  
  1909  <p>
  1910  This last example exploits the implicit repetition of the
  1911  last non-empty expression list.
  1912  </p>
  1913  
  1914  
  1915  <h3 id="Type_declarations">Type declarations</h3>
  1916  
  1917  <p>
  1918  A type declaration binds an identifier, the <i>type name</i>, to a <a href="#Types">type</a>.
  1919  Type declarations come in two forms: alias declarations and type definitions.
  1920  <p>
  1921  
  1922  <pre class="ebnf">
  1923  TypeDecl = "type" ( TypeSpec | "(" { TypeSpec ";" } ")" ) .
  1924  TypeSpec = AliasDecl | TypeDef .
  1925  </pre>
  1926  
  1927  <h4 id="Alias_declarations">Alias declarations</h4>
  1928  
  1929  <p>
  1930  An alias declaration binds an identifier to the given type.
  1931  </p>
  1932  
  1933  <pre class="ebnf">
  1934  AliasDecl = identifier "=" Type .
  1935  </pre>
  1936  
  1937  <p>
  1938  Within the <a href="#Declarations_and_scope">scope</a> of
  1939  the identifier, it serves as an <i>alias</i> for the type.
  1940  </p>
  1941  
  1942  <pre>
  1943  type (
  1944  	nodeList = []*Node  // nodeList and []*Node are identical types
  1945  	Polar    = polar    // Polar and polar denote identical types
  1946  )
  1947  </pre>
  1948  
  1949  
  1950  <h4 id="Type_definitions">Type definitions</h4>
  1951  
  1952  <p>
  1953  A type definition creates a new, distinct type with the same
  1954  <a href="#Types">underlying type</a> and operations as the given type,
  1955  and binds an identifier to it.
  1956  </p>
  1957  
  1958  <pre class="ebnf">
  1959  TypeDef = identifier Type .
  1960  </pre>
  1961  
  1962  <p>
  1963  The new type is called a <i>defined type</i>.
  1964  It is <a href="#Type_identity">different</a> from any other type,
  1965  including the type it is created from.
  1966  </p>
  1967  
  1968  <pre>
  1969  type (
  1970  	Point struct{ x, y float64 }  // Point and struct{ x, y float64 } are different types
  1971  	polar Point                   // polar and Point denote different types
  1972  )
  1973  
  1974  type TreeNode struct {
  1975  	left, right *TreeNode
  1976  	value *Comparable
  1977  }
  1978  
  1979  type Block interface {
  1980  	BlockSize() int
  1981  	Encrypt(src, dst []byte)
  1982  	Decrypt(src, dst []byte)
  1983  }
  1984  </pre>
  1985  
  1986  <p>
  1987  A defined type may have <a href="#Method_declarations">methods</a> associated with it.
  1988  It does not inherit any methods bound to the given type,
  1989  but the <a href="#Method_sets">method set</a>
  1990  of an interface type or of elements of a composite type remains unchanged:
  1991  </p>
  1992  
  1993  <pre>
  1994  // A Mutex is a data type with two methods, Lock and Unlock.
  1995  type Mutex struct         { /* Mutex fields */ }
  1996  func (m *Mutex) Lock()    { /* Lock implementation */ }
  1997  func (m *Mutex) Unlock()  { /* Unlock implementation */ }
  1998  
  1999  // NewMutex has the same composition as Mutex but its method set is empty.
  2000  type NewMutex Mutex
  2001  
  2002  // The method set of PtrMutex's underlying type *Mutex remains unchanged,
  2003  // but the method set of PtrMutex is empty.
  2004  type PtrMutex *Mutex
  2005  
  2006  // The method set of *PrintableMutex contains the methods
  2007  // Lock and Unlock bound to its embedded field Mutex.
  2008  type PrintableMutex struct {
  2009  	Mutex
  2010  }
  2011  
  2012  // MyBlock is an interface type that has the same method set as Block.
  2013  type MyBlock Block
  2014  </pre>
  2015  
  2016  <p>
  2017  Type definitions may be used to define different boolean, numeric,
  2018  or string types and associate methods with them:
  2019  </p>
  2020  
  2021  <pre>
  2022  type TimeZone int
  2023  
  2024  const (
  2025  	EST TimeZone = -(5 + iota)
  2026  	CST
  2027  	MST
  2028  	PST
  2029  )
  2030  
  2031  func (tz TimeZone) String() string {
  2032  	return fmt.Sprintf("GMT%+dh", tz)
  2033  }
  2034  </pre>
  2035  
  2036  
  2037  <h3 id="Variable_declarations">Variable declarations</h3>
  2038  
  2039  <p>
  2040  A variable declaration creates one or more <a href="#Variables">variables</a>,
  2041  binds corresponding identifiers to them, and gives each a type and an initial value.
  2042  </p>
  2043  
  2044  <pre class="ebnf">
  2045  VarDecl     = "var" ( VarSpec | "(" { VarSpec ";" } ")" ) .
  2046  VarSpec     = IdentifierList ( Type [ "=" ExpressionList ] | "=" ExpressionList ) .
  2047  </pre>
  2048  
  2049  <pre>
  2050  var i int
  2051  var U, V, W float64
  2052  var k = 0
  2053  var x, y float32 = -1, -2
  2054  var (
  2055  	i       int
  2056  	u, v, s = 2.0, 3.0, "bar"
  2057  )
  2058  var re, im = complexSqrt(-1)
  2059  var _, found = entries[name]  // map lookup; only interested in "found"
  2060  </pre>
  2061  
  2062  <p>
  2063  If a list of expressions is given, the variables are initialized
  2064  with the expressions following the rules for <a href="#Assignments">assignments</a>.
  2065  Otherwise, each variable is initialized to its <a href="#The_zero_value">zero value</a>.
  2066  </p>
  2067  
  2068  <p>
  2069  If a type is present, each variable is given that type.
  2070  Otherwise, each variable is given the type of the corresponding
  2071  initialization value in the assignment.
  2072  If that value is an untyped constant, it is first
  2073  <a href="#Conversions">converted</a> to its <a href="#Constants">default type</a>;
  2074  if it is an untyped boolean value, it is first converted to type <code>bool</code>.
  2075  The predeclared value <code>nil</code> cannot be used to initialize a variable
  2076  with no explicit type.
  2077  </p>
  2078  
  2079  <pre>
  2080  var d = math.Sin(0.5)  // d is float64
  2081  var i = 42             // i is int
  2082  var t, ok = x.(T)      // t is T, ok is bool
  2083  var n = nil            // illegal
  2084  </pre>
  2085  
  2086  <p>
  2087  Implementation restriction: A compiler may make it illegal to declare a variable
  2088  inside a <a href="#Function_declarations">function body</a> if the variable is
  2089  never used.
  2090  </p>
  2091  
  2092  <h3 id="Short_variable_declarations">Short variable declarations</h3>
  2093  
  2094  <p>
  2095  A <i>short variable declaration</i> uses the syntax:
  2096  </p>
  2097  
  2098  <pre class="ebnf">
  2099  ShortVarDecl = IdentifierList ":=" ExpressionList .
  2100  </pre>
  2101  
  2102  <p>
  2103  It is shorthand for a regular <a href="#Variable_declarations">variable declaration</a>
  2104  with initializer expressions but no types:
  2105  </p>
  2106  
  2107  <pre class="grammar">
  2108  "var" IdentifierList = ExpressionList .
  2109  </pre>
  2110  
  2111  <pre>
  2112  i, j := 0, 10
  2113  f := func() int { return 7 }
  2114  ch := make(chan int)
  2115  r, w := os.Pipe(fd)  // os.Pipe() returns two values
  2116  _, y, _ := coord(p)  // coord() returns three values; only interested in y coordinate
  2117  </pre>
  2118  
  2119  <p>
  2120  Unlike regular variable declarations, a short variable declaration may <i>redeclare</i>
  2121  variables provided they were originally declared earlier in the same block
  2122  (or the parameter lists if the block is the function body) with the same type,
  2123  and at least one of the non-<a href="#Blank_identifier">blank</a> variables is new.
  2124  As a consequence, redeclaration can only appear in a multi-variable short declaration.
  2125  Redeclaration does not introduce a new variable; it just assigns a new value to the original.
  2126  </p>
  2127  
  2128  <pre>
  2129  field1, offset := nextField(str, 0)
  2130  field2, offset := nextField(str, offset)  // redeclares offset
  2131  a, a := 1, 2                              // illegal: double declaration of a or no new variable if a was declared elsewhere
  2132  </pre>
  2133  
  2134  <p>
  2135  Short variable declarations may appear only inside functions.
  2136  In some contexts such as the initializers for
  2137  <a href="#If_statements">"if"</a>,
  2138  <a href="#For_statements">"for"</a>, or
  2139  <a href="#Switch_statements">"switch"</a> statements,
  2140  they can be used to declare local temporary variables.
  2141  </p>
  2142  
  2143  <h3 id="Function_declarations">Function declarations</h3>
  2144  
  2145  <p>
  2146  A function declaration binds an identifier, the <i>function name</i>,
  2147  to a function.
  2148  </p>
  2149  
  2150  <pre class="ebnf">
  2151  FunctionDecl = "func" FunctionName ( Function | Signature ) .
  2152  FunctionName = identifier .
  2153  Function     = Signature FunctionBody .
  2154  FunctionBody = Block .
  2155  </pre>
  2156  
  2157  <p>
  2158  If the function's <a href="#Function_types">signature</a> declares
  2159  result parameters, the function body's statement list must end in
  2160  a <a href="#Terminating_statements">terminating statement</a>.
  2161  </p>
  2162  
  2163  <pre>
  2164  func IndexRune(s string, r rune) int {
  2165  	for i, c := range s {
  2166  		if c == r {
  2167  			return i
  2168  		}
  2169  	}
  2170  	// invalid: missing return statement
  2171  }
  2172  </pre>
  2173  
  2174  <p>
  2175  A function declaration may omit the body. Such a declaration provides the
  2176  signature for a function implemented outside Go, such as an assembly routine.
  2177  </p>
  2178  
  2179  <pre>
  2180  func min(x int, y int) int {
  2181  	if x &lt; y {
  2182  		return x
  2183  	}
  2184  	return y
  2185  }
  2186  
  2187  func flushICache(begin, end uintptr)  // implemented externally
  2188  </pre>
  2189  
  2190  <h3 id="Method_declarations">Method declarations</h3>
  2191  
  2192  <p>
  2193  A method is a <a href="#Function_declarations">function</a> with a <i>receiver</i>.
  2194  A method declaration binds an identifier, the <i>method name</i>, to a method,
  2195  and associates the method with the receiver's <i>base type</i>.
  2196  </p>
  2197  
  2198  <pre class="ebnf">
  2199  MethodDecl = "func" Receiver MethodName ( Function | Signature ) .
  2200  Receiver   = Parameters .
  2201  </pre>
  2202  
  2203  <p>
  2204  The receiver is specified via an extra parameter section preceding the method
  2205  name. That parameter section must declare a single non-variadic parameter, the receiver.
  2206  Its type must be of the form <code>T</code> or <code>*T</code> (possibly using
  2207  parentheses) where <code>T</code> is a type name. The type denoted by <code>T</code> is called
  2208  the receiver <i>base type</i>; it must not be a pointer or interface type and
  2209  it must be <a href="#Type_definitions">defined</a> in the same package as the method.
  2210  The method is said to be <i>bound</i> to the base type and the method name
  2211  is visible only within <a href="#Selectors">selectors</a> for type <code>T</code>
  2212  or <code>*T</code>.
  2213  </p>
  2214  
  2215  <p>
  2216  A non-<a href="#Blank_identifier">blank</a> receiver identifier must be
  2217  <a href="#Uniqueness_of_identifiers">unique</a> in the method signature.
  2218  If the receiver's value is not referenced inside the body of the method,
  2219  its identifier may be omitted in the declaration. The same applies in
  2220  general to parameters of functions and methods.
  2221  </p>
  2222  
  2223  <p>
  2224  For a base type, the non-blank names of methods bound to it must be unique.
  2225  If the base type is a <a href="#Struct_types">struct type</a>,
  2226  the non-blank method and field names must be distinct.
  2227  </p>
  2228  
  2229  <p>
  2230  Given type <code>Point</code>, the declarations
  2231  </p>
  2232  
  2233  <pre>
  2234  func (p *Point) Length() float64 {
  2235  	return math.Sqrt(p.x * p.x + p.y * p.y)
  2236  }
  2237  
  2238  func (p *Point) Scale(factor float64) {
  2239  	p.x *= factor
  2240  	p.y *= factor
  2241  }
  2242  </pre>
  2243  
  2244  <p>
  2245  bind the methods <code>Length</code> and <code>Scale</code>,
  2246  with receiver type <code>*Point</code>,
  2247  to the base type <code>Point</code>.
  2248  </p>
  2249  
  2250  <p>
  2251  The type of a method is the type of a function with the receiver as first
  2252  argument.  For instance, the method <code>Scale</code> has type
  2253  </p>
  2254  
  2255  <pre>
  2256  func(p *Point, factor float64)
  2257  </pre>
  2258  
  2259  <p>
  2260  However, a function declared this way is not a method.
  2261  </p>
  2262  
  2263  
  2264  <h2 id="Expressions">Expressions</h2>
  2265  
  2266  <p>
  2267  An expression specifies the computation of a value by applying
  2268  operators and functions to operands.
  2269  </p>
  2270  
  2271  <h3 id="Operands">Operands</h3>
  2272  
  2273  <p>
  2274  Operands denote the elementary values in an expression. An operand may be a
  2275  literal, a (possibly <a href="#Qualified_identifiers">qualified</a>)
  2276  non-<a href="#Blank_identifier">blank</a> identifier denoting a
  2277  <a href="#Constant_declarations">constant</a>,
  2278  <a href="#Variable_declarations">variable</a>, or
  2279  <a href="#Function_declarations">function</a>,
  2280  a <a href="#Method_expressions">method expression</a> yielding a function,
  2281  or a parenthesized expression.
  2282  </p>
  2283  
  2284  <p>
  2285  The <a href="#Blank_identifier">blank identifier</a> may appear as an
  2286  operand only on the left-hand side of an <a href="#Assignments">assignment</a>.
  2287  </p>
  2288  
  2289  <pre class="ebnf">
  2290  Operand     = Literal | OperandName | MethodExpr | "(" Expression ")" .
  2291  Literal     = BasicLit | CompositeLit | FunctionLit .
  2292  BasicLit    = int_lit | float_lit | imaginary_lit | rune_lit | string_lit .
  2293  OperandName = identifier | QualifiedIdent.
  2294  </pre>
  2295  
  2296  <h3 id="Qualified_identifiers">Qualified identifiers</h3>
  2297  
  2298  <p>
  2299  A qualified identifier is an identifier qualified with a package name prefix.
  2300  Both the package name and the identifier must not be
  2301  <a href="#Blank_identifier">blank</a>.
  2302  </p>
  2303  
  2304  <pre class="ebnf">
  2305  QualifiedIdent = PackageName "." identifier .
  2306  </pre>
  2307  
  2308  <p>
  2309  A qualified identifier accesses an identifier in a different package, which
  2310  must be <a href="#Import_declarations">imported</a>.
  2311  The identifier must be <a href="#Exported_identifiers">exported</a> and
  2312  declared in the <a href="#Blocks">package block</a> of that package.
  2313  </p>
  2314  
  2315  <pre>
  2316  math.Sin	// denotes the Sin function in package math
  2317  </pre>
  2318  
  2319  <h3 id="Composite_literals">Composite literals</h3>
  2320  
  2321  <p>
  2322  Composite literals construct values for structs, arrays, slices, and maps
  2323  and create a new value each time they are evaluated.
  2324  They consist of the type of the literal followed by a brace-bound list of elements.
  2325  Each element may optionally be preceded by a corresponding key.
  2326  </p>
  2327  
  2328  <pre class="ebnf">
  2329  CompositeLit  = LiteralType LiteralValue .
  2330  LiteralType   = StructType | ArrayType | "[" "..." "]" ElementType |
  2331                  SliceType | MapType | TypeName .
  2332  LiteralValue  = "{" [ ElementList [ "," ] ] "}" .
  2333  ElementList   = KeyedElement { "," KeyedElement } .
  2334  KeyedElement  = [ Key ":" ] Element .
  2335  Key           = FieldName | Expression | LiteralValue .
  2336  FieldName     = identifier .
  2337  Element       = Expression | LiteralValue .
  2338  </pre>
  2339  
  2340  <p>
  2341  The LiteralType's underlying type must be a struct, array, slice, or map type
  2342  (the grammar enforces this constraint except when the type is given
  2343  as a TypeName).
  2344  The types of the elements and keys must be <a href="#Assignability">assignable</a>
  2345  to the respective field, element, and key types of the literal type;
  2346  there is no additional conversion.
  2347  The key is interpreted as a field name for struct literals,
  2348  an index for array and slice literals, and a key for map literals.
  2349  For map literals, all elements must have a key. It is an error
  2350  to specify multiple elements with the same field name or
  2351  constant key value. For non-constant map keys, see the section on
  2352  <a href="#Order_of_evaluation">evaluation order</a>.
  2353  </p>
  2354  
  2355  <p>
  2356  For struct literals the following rules apply:
  2357  </p>
  2358  <ul>
  2359  	<li>A key must be a field name declared in the struct type.
  2360  	</li>
  2361  	<li>An element list that does not contain any keys must
  2362  	    list an element for each struct field in the
  2363  	    order in which the fields are declared.
  2364  	</li>
  2365  	<li>If any element has a key, every element must have a key.
  2366  	</li>
  2367  	<li>An element list that contains keys does not need to
  2368  	    have an element for each struct field. Omitted fields
  2369  	    get the zero value for that field.
  2370  	</li>
  2371  	<li>A literal may omit the element list; such a literal evaluates
  2372  	    to the zero value for its type.
  2373  	</li>
  2374  	<li>It is an error to specify an element for a non-exported
  2375  	    field of a struct belonging to a different package.
  2376  	</li>
  2377  </ul>
  2378  
  2379  <p>
  2380  Given the declarations
  2381  </p>
  2382  <pre>
  2383  type Point3D struct { x, y, z float64 }
  2384  type Line struct { p, q Point3D }
  2385  </pre>
  2386  
  2387  <p>
  2388  one may write
  2389  </p>
  2390  
  2391  <pre>
  2392  origin := Point3D{}                            // zero value for Point3D
  2393  line := Line{origin, Point3D{y: -4, z: 12.3}}  // zero value for line.q.x
  2394  </pre>
  2395  
  2396  <p>
  2397  For array and slice literals the following rules apply:
  2398  </p>
  2399  <ul>
  2400  	<li>Each element has an associated integer index marking
  2401  	    its position in the array.
  2402  	</li>
  2403  	<li>An element with a key uses the key as its index. The
  2404  	    key must be a non-negative constant
  2405  	    <a href="#Representability">representable</a> by
  2406  	    a value of type <code>int</code>; and if it is typed
  2407  	    it must be of integer type.
  2408  	</li>
  2409  	<li>An element without a key uses the previous element's index plus one.
  2410  	    If the first element has no key, its index is zero.
  2411  	</li>
  2412  </ul>
  2413  
  2414  <p>
  2415  <a href="#Address_operators">Taking the address</a> of a composite literal
  2416  generates a pointer to a unique <a href="#Variables">variable</a> initialized
  2417  with the literal's value.
  2418  </p>
  2419  <pre>
  2420  var pointer *Point3D = &amp;Point3D{y: 1000}
  2421  </pre>
  2422  
  2423  <p>
  2424  The length of an array literal is the length specified in the literal type.
  2425  If fewer elements than the length are provided in the literal, the missing
  2426  elements are set to the zero value for the array element type.
  2427  It is an error to provide elements with index values outside the index range
  2428  of the array. The notation <code>...</code> specifies an array length equal
  2429  to the maximum element index plus one.
  2430  </p>
  2431  
  2432  <pre>
  2433  buffer := [10]string{}             // len(buffer) == 10
  2434  intSet := [6]int{1, 2, 3, 5}       // len(intSet) == 6
  2435  days := [...]string{"Sat", "Sun"}  // len(days) == 2
  2436  </pre>
  2437  
  2438  <p>
  2439  A slice literal describes the entire underlying array literal.
  2440  Thus the length and capacity of a slice literal are the maximum
  2441  element index plus one. A slice literal has the form
  2442  </p>
  2443  
  2444  <pre>
  2445  []T{x1, x2, … xn}
  2446  </pre>
  2447  
  2448  <p>
  2449  and is shorthand for a slice operation applied to an array:
  2450  </p>
  2451  
  2452  <pre>
  2453  tmp := [n]T{x1, x2, … xn}
  2454  tmp[0 : n]
  2455  </pre>
  2456  
  2457  <p>
  2458  Within a composite literal of array, slice, or map type <code>T</code>,
  2459  elements or map keys that are themselves composite literals may elide the respective
  2460  literal type if it is identical to the element or key type of <code>T</code>.
  2461  Similarly, elements or keys that are addresses of composite literals may elide
  2462  the <code>&amp;T</code> when the element or key type is <code>*T</code>.
  2463  </p>
  2464  
  2465  <pre>
  2466  [...]Point{{1.5, -3.5}, {0, 0}}     // same as [...]Point{Point{1.5, -3.5}, Point{0, 0}}
  2467  [][]int{{1, 2, 3}, {4, 5}}          // same as [][]int{[]int{1, 2, 3}, []int{4, 5}}
  2468  [][]Point{{{0, 1}, {1, 2}}}         // same as [][]Point{[]Point{Point{0, 1}, Point{1, 2}}}
  2469  map[string]Point{"orig": {0, 0}}    // same as map[string]Point{"orig": Point{0, 0}}
  2470  map[Point]string{{0, 0}: "orig"}    // same as map[Point]string{Point{0, 0}: "orig"}
  2471  
  2472  type PPoint *Point
  2473  [2]*Point{{1.5, -3.5}, {}}          // same as [2]*Point{&amp;Point{1.5, -3.5}, &amp;Point{}}
  2474  [2]PPoint{{1.5, -3.5}, {}}          // same as [2]PPoint{PPoint(&amp;Point{1.5, -3.5}), PPoint(&amp;Point{})}
  2475  </pre>
  2476  
  2477  <p>
  2478  A parsing ambiguity arises when a composite literal using the
  2479  TypeName form of the LiteralType appears as an operand between the
  2480  <a href="#Keywords">keyword</a> and the opening brace of the block
  2481  of an "if", "for", or "switch" statement, and the composite literal
  2482  is not enclosed in parentheses, square brackets, or curly braces.
  2483  In this rare case, the opening brace of the literal is erroneously parsed
  2484  as the one introducing the block of statements. To resolve the ambiguity,
  2485  the composite literal must appear within parentheses.
  2486  </p>
  2487  
  2488  <pre>
  2489  if x == (T{a,b,c}[i]) { … }
  2490  if (x == T{a,b,c}[i]) { … }
  2491  </pre>
  2492  
  2493  <p>
  2494  Examples of valid array, slice, and map literals:
  2495  </p>
  2496  
  2497  <pre>
  2498  // list of prime numbers
  2499  primes := []int{2, 3, 5, 7, 9, 2147483647}
  2500  
  2501  // vowels[ch] is true if ch is a vowel
  2502  vowels := [128]bool{'a': true, 'e': true, 'i': true, 'o': true, 'u': true, 'y': true}
  2503  
  2504  // the array [10]float32{-1, 0, 0, 0, -0.1, -0.1, 0, 0, 0, -1}
  2505  filter := [10]float32{-1, 4: -0.1, -0.1, 9: -1}
  2506  
  2507  // frequencies in Hz for equal-tempered scale (A4 = 440Hz)
  2508  noteFrequency := map[string]float32{
  2509  	"C0": 16.35, "D0": 18.35, "E0": 20.60, "F0": 21.83,
  2510  	"G0": 24.50, "A0": 27.50, "B0": 30.87,
  2511  }
  2512  </pre>
  2513  
  2514  
  2515  <h3 id="Function_literals">Function literals</h3>
  2516  
  2517  <p>
  2518  A function literal represents an anonymous <a href="#Function_declarations">function</a>.
  2519  </p>
  2520  
  2521  <pre class="ebnf">
  2522  FunctionLit = "func" Function .
  2523  </pre>
  2524  
  2525  <pre>
  2526  func(a, b int, z float64) bool { return a*b &lt; int(z) }
  2527  </pre>
  2528  
  2529  <p>
  2530  A function literal can be assigned to a variable or invoked directly.
  2531  </p>
  2532  
  2533  <pre>
  2534  f := func(x, y int) int { return x + y }
  2535  func(ch chan int) { ch &lt;- ACK }(replyChan)
  2536  </pre>
  2537  
  2538  <p>
  2539  Function literals are <i>closures</i>: they may refer to variables
  2540  defined in a surrounding function. Those variables are then shared between
  2541  the surrounding function and the function literal, and they survive as long
  2542  as they are accessible.
  2543  </p>
  2544  
  2545  
  2546  <h3 id="Primary_expressions">Primary expressions</h3>
  2547  
  2548  <p>
  2549  Primary expressions are the operands for unary and binary expressions.
  2550  </p>
  2551  
  2552  <pre class="ebnf">
  2553  PrimaryExpr =
  2554  	Operand |
  2555  	Conversion |
  2556  	PrimaryExpr Selector |
  2557  	PrimaryExpr Index |
  2558  	PrimaryExpr Slice |
  2559  	PrimaryExpr TypeAssertion |
  2560  	PrimaryExpr Arguments .
  2561  
  2562  Selector       = "." identifier .
  2563  Index          = "[" Expression "]" .
  2564  Slice          = "[" [ Expression ] ":" [ Expression ] "]" |
  2565                   "[" [ Expression ] ":" Expression ":" Expression "]" .
  2566  TypeAssertion  = "." "(" Type ")" .
  2567  Arguments      = "(" [ ( ExpressionList | Type [ "," ExpressionList ] ) [ "..." ] [ "," ] ] ")" .
  2568  </pre>
  2569  
  2570  
  2571  <pre>
  2572  x
  2573  2
  2574  (s + ".txt")
  2575  f(3.1415, true)
  2576  Point{1, 2}
  2577  m["foo"]
  2578  s[i : j + 1]
  2579  obj.color
  2580  f.p[i].x()
  2581  </pre>
  2582  
  2583  
  2584  <h3 id="Selectors">Selectors</h3>
  2585  
  2586  <p>
  2587  For a <a href="#Primary_expressions">primary expression</a> <code>x</code>
  2588  that is not a <a href="#Package_clause">package name</a>, the
  2589  <i>selector expression</i>
  2590  </p>
  2591  
  2592  <pre>
  2593  x.f
  2594  </pre>
  2595  
  2596  <p>
  2597  denotes the field or method <code>f</code> of the value <code>x</code>
  2598  (or sometimes <code>*x</code>; see below).
  2599  The identifier <code>f</code> is called the (field or method) <i>selector</i>;
  2600  it must not be the <a href="#Blank_identifier">blank identifier</a>.
  2601  The type of the selector expression is the type of <code>f</code>.
  2602  If <code>x</code> is a package name, see the section on
  2603  <a href="#Qualified_identifiers">qualified identifiers</a>.
  2604  </p>
  2605  
  2606  <p>
  2607  A selector <code>f</code> may denote a field or method <code>f</code> of
  2608  a type <code>T</code>, or it may refer
  2609  to a field or method <code>f</code> of a nested
  2610  <a href="#Struct_types">embedded field</a> of <code>T</code>.
  2611  The number of embedded fields traversed
  2612  to reach <code>f</code> is called its <i>depth</i> in <code>T</code>.
  2613  The depth of a field or method <code>f</code>
  2614  declared in <code>T</code> is zero.
  2615  The depth of a field or method <code>f</code> declared in
  2616  an embedded field <code>A</code> in <code>T</code> is the
  2617  depth of <code>f</code> in <code>A</code> plus one.
  2618  </p>
  2619  
  2620  <p>
  2621  The following rules apply to selectors:
  2622  </p>
  2623  
  2624  <ol>
  2625  <li>
  2626  For a value <code>x</code> of type <code>T</code> or <code>*T</code>
  2627  where <code>T</code> is not a pointer or interface type,
  2628  <code>x.f</code> denotes the field or method at the shallowest depth
  2629  in <code>T</code> where there
  2630  is such an <code>f</code>.
  2631  If there is not exactly <a href="#Uniqueness_of_identifiers">one <code>f</code></a>
  2632  with shallowest depth, the selector expression is illegal.
  2633  </li>
  2634  
  2635  <li>
  2636  For a value <code>x</code> of type <code>I</code> where <code>I</code>
  2637  is an interface type, <code>x.f</code> denotes the actual method with name
  2638  <code>f</code> of the dynamic value of <code>x</code>.
  2639  If there is no method with name <code>f</code> in the
  2640  <a href="#Method_sets">method set</a> of <code>I</code>, the selector
  2641  expression is illegal.
  2642  </li>
  2643  
  2644  <li>
  2645  As an exception, if the type of <code>x</code> is a named pointer type
  2646  and <code>(*x).f</code> is a valid selector expression denoting a field
  2647  (but not a method), <code>x.f</code> is shorthand for <code>(*x).f</code>.
  2648  </li>
  2649  
  2650  <li>
  2651  In all other cases, <code>x.f</code> is illegal.
  2652  </li>
  2653  
  2654  <li>
  2655  If <code>x</code> is of pointer type and has the value
  2656  <code>nil</code> and <code>x.f</code> denotes a struct field,
  2657  assigning to or evaluating <code>x.f</code>
  2658  causes a <a href="#Run_time_panics">run-time panic</a>.
  2659  </li>
  2660  
  2661  <li>
  2662  If <code>x</code> is of interface type and has the value
  2663  <code>nil</code>, <a href="#Calls">calling</a> or
  2664  <a href="#Method_values">evaluating</a> the method <code>x.f</code>
  2665  causes a <a href="#Run_time_panics">run-time panic</a>.
  2666  </li>
  2667  </ol>
  2668  
  2669  <p>
  2670  For example, given the declarations:
  2671  </p>
  2672  
  2673  <pre>
  2674  type T0 struct {
  2675  	x int
  2676  }
  2677  
  2678  func (*T0) M0()
  2679  
  2680  type T1 struct {
  2681  	y int
  2682  }
  2683  
  2684  func (T1) M1()
  2685  
  2686  type T2 struct {
  2687  	z int
  2688  	T1
  2689  	*T0
  2690  }
  2691  
  2692  func (*T2) M2()
  2693  
  2694  type Q *T2
  2695  
  2696  var t T2     // with t.T0 != nil
  2697  var p *T2    // with p != nil and (*p).T0 != nil
  2698  var q Q = p
  2699  </pre>
  2700  
  2701  <p>
  2702  one may write:
  2703  </p>
  2704  
  2705  <pre>
  2706  t.z          // t.z
  2707  t.y          // t.T1.y
  2708  t.x          // (*t.T0).x
  2709  
  2710  p.z          // (*p).z
  2711  p.y          // (*p).T1.y
  2712  p.x          // (*(*p).T0).x
  2713  
  2714  q.x          // (*(*q).T0).x        (*q).x is a valid field selector
  2715  
  2716  p.M0()       // ((*p).T0).M0()      M0 expects *T0 receiver
  2717  p.M1()       // ((*p).T1).M1()      M1 expects T1 receiver
  2718  p.M2()       // p.M2()              M2 expects *T2 receiver
  2719  t.M2()       // (&amp;t).M2()           M2 expects *T2 receiver, see section on Calls
  2720  </pre>
  2721  
  2722  <p>
  2723  but the following is invalid:
  2724  </p>
  2725  
  2726  <pre>
  2727  q.M0()       // (*q).M0 is valid but not a field selector
  2728  </pre>
  2729  
  2730  
  2731  <h3 id="Method_expressions">Method expressions</h3>
  2732  
  2733  <p>
  2734  If <code>M</code> is in the <a href="#Method_sets">method set</a> of type <code>T</code>,
  2735  <code>T.M</code> is a function that is callable as a regular function
  2736  with the same arguments as <code>M</code> prefixed by an additional
  2737  argument that is the receiver of the method.
  2738  </p>
  2739  
  2740  <pre class="ebnf">
  2741  MethodExpr    = ReceiverType "." MethodName .
  2742  ReceiverType  = TypeName | "(" "*" TypeName ")" | "(" ReceiverType ")" .
  2743  </pre>
  2744  
  2745  <p>
  2746  Consider a struct type <code>T</code> with two methods,
  2747  <code>Mv</code>, whose receiver is of type <code>T</code>, and
  2748  <code>Mp</code>, whose receiver is of type <code>*T</code>.
  2749  </p>
  2750  
  2751  <pre>
  2752  type T struct {
  2753  	a int
  2754  }
  2755  func (tv  T) Mv(a int) int         { return 0 }  // value receiver
  2756  func (tp *T) Mp(f float32) float32 { return 1 }  // pointer receiver
  2757  
  2758  var t T
  2759  </pre>
  2760  
  2761  <p>
  2762  The expression
  2763  </p>
  2764  
  2765  <pre>
  2766  T.Mv
  2767  </pre>
  2768  
  2769  <p>
  2770  yields a function equivalent to <code>Mv</code> but
  2771  with an explicit receiver as its first argument; it has signature
  2772  </p>
  2773  
  2774  <pre>
  2775  func(tv T, a int) int
  2776  </pre>
  2777  
  2778  <p>
  2779  That function may be called normally with an explicit receiver, so
  2780  these five invocations are equivalent:
  2781  </p>
  2782  
  2783  <pre>
  2784  t.Mv(7)
  2785  T.Mv(t, 7)
  2786  (T).Mv(t, 7)
  2787  f1 := T.Mv; f1(t, 7)
  2788  f2 := (T).Mv; f2(t, 7)
  2789  </pre>
  2790  
  2791  <p>
  2792  Similarly, the expression
  2793  </p>
  2794  
  2795  <pre>
  2796  (*T).Mp
  2797  </pre>
  2798  
  2799  <p>
  2800  yields a function value representing <code>Mp</code> with signature
  2801  </p>
  2802  
  2803  <pre>
  2804  func(tp *T, f float32) float32
  2805  </pre>
  2806  
  2807  <p>
  2808  For a method with a value receiver, one can derive a function
  2809  with an explicit pointer receiver, so
  2810  </p>
  2811  
  2812  <pre>
  2813  (*T).Mv
  2814  </pre>
  2815  
  2816  <p>
  2817  yields a function value representing <code>Mv</code> with signature
  2818  </p>
  2819  
  2820  <pre>
  2821  func(tv *T, a int) int
  2822  </pre>
  2823  
  2824  <p>
  2825  Such a function indirects through the receiver to create a value
  2826  to pass as the receiver to the underlying method;
  2827  the method does not overwrite the value whose address is passed in
  2828  the function call.
  2829  </p>
  2830  
  2831  <p>
  2832  The final case, a value-receiver function for a pointer-receiver method,
  2833  is illegal because pointer-receiver methods are not in the method set
  2834  of the value type.
  2835  </p>
  2836  
  2837  <p>
  2838  Function values derived from methods are called with function call syntax;
  2839  the receiver is provided as the first argument to the call.
  2840  That is, given <code>f := T.Mv</code>, <code>f</code> is invoked
  2841  as <code>f(t, 7)</code> not <code>t.f(7)</code>.
  2842  To construct a function that binds the receiver, use a
  2843  <a href="#Function_literals">function literal</a> or
  2844  <a href="#Method_values">method value</a>.
  2845  </p>
  2846  
  2847  <p>
  2848  It is legal to derive a function value from a method of an interface type.
  2849  The resulting function takes an explicit receiver of that interface type.
  2850  </p>
  2851  
  2852  <h3 id="Method_values">Method values</h3>
  2853  
  2854  <p>
  2855  If the expression <code>x</code> has static type <code>T</code> and
  2856  <code>M</code> is in the <a href="#Method_sets">method set</a> of type <code>T</code>,
  2857  <code>x.M</code> is called a <i>method value</i>.
  2858  The method value <code>x.M</code> is a function value that is callable
  2859  with the same arguments as a method call of <code>x.M</code>.
  2860  The expression <code>x</code> is evaluated and saved during the evaluation of the
  2861  method value; the saved copy is then used as the receiver in any calls,
  2862  which may be executed later.
  2863  </p>
  2864  
  2865  <p>
  2866  The type <code>T</code> may be an interface or non-interface type.
  2867  </p>
  2868  
  2869  <p>
  2870  As in the discussion of <a href="#Method_expressions">method expressions</a> above,
  2871  consider a struct type <code>T</code> with two methods,
  2872  <code>Mv</code>, whose receiver is of type <code>T</code>, and
  2873  <code>Mp</code>, whose receiver is of type <code>*T</code>.
  2874  </p>
  2875  
  2876  <pre>
  2877  type T struct {
  2878  	a int
  2879  }
  2880  func (tv  T) Mv(a int) int         { return 0 }  // value receiver
  2881  func (tp *T) Mp(f float32) float32 { return 1 }  // pointer receiver
  2882  
  2883  var t T
  2884  var pt *T
  2885  func makeT() T
  2886  </pre>
  2887  
  2888  <p>
  2889  The expression
  2890  </p>
  2891  
  2892  <pre>
  2893  t.Mv
  2894  </pre>
  2895  
  2896  <p>
  2897  yields a function value of type
  2898  </p>
  2899  
  2900  <pre>
  2901  func(int) int
  2902  </pre>
  2903  
  2904  <p>
  2905  These two invocations are equivalent:
  2906  </p>
  2907  
  2908  <pre>
  2909  t.Mv(7)
  2910  f := t.Mv; f(7)
  2911  </pre>
  2912  
  2913  <p>
  2914  Similarly, the expression
  2915  </p>
  2916  
  2917  <pre>
  2918  pt.Mp
  2919  </pre>
  2920  
  2921  <p>
  2922  yields a function value of type
  2923  </p>
  2924  
  2925  <pre>
  2926  func(float32) float32
  2927  </pre>
  2928  
  2929  <p>
  2930  As with <a href="#Selectors">selectors</a>, a reference to a non-interface method with a value receiver
  2931  using a pointer will automatically dereference that pointer: <code>pt.Mv</code> is equivalent to <code>(*pt).Mv</code>.
  2932  </p>
  2933  
  2934  <p>
  2935  As with <a href="#Calls">method calls</a>, a reference to a non-interface method with a pointer receiver
  2936  using an addressable value will automatically take the address of that value: <code>t.Mp</code> is equivalent to <code>(&amp;t).Mp</code>.
  2937  </p>
  2938  
  2939  <pre>
  2940  f := t.Mv; f(7)   // like t.Mv(7)
  2941  f := pt.Mp; f(7)  // like pt.Mp(7)
  2942  f := pt.Mv; f(7)  // like (*pt).Mv(7)
  2943  f := t.Mp; f(7)   // like (&amp;t).Mp(7)
  2944  f := makeT().Mp   // invalid: result of makeT() is not addressable
  2945  </pre>
  2946  
  2947  <p>
  2948  Although the examples above use non-interface types, it is also legal to create a method value
  2949  from a value of interface type.
  2950  </p>
  2951  
  2952  <pre>
  2953  var i interface { M(int) } = myVal
  2954  f := i.M; f(7)  // like i.M(7)
  2955  </pre>
  2956  
  2957  
  2958  <h3 id="Index_expressions">Index expressions</h3>
  2959  
  2960  <p>
  2961  A primary expression of the form
  2962  </p>
  2963  
  2964  <pre>
  2965  a[x]
  2966  </pre>
  2967  
  2968  <p>
  2969  denotes the element of the array, pointer to array, slice, string or map <code>a</code> indexed by <code>x</code>.
  2970  The value <code>x</code> is called the <i>index</i> or <i>map key</i>, respectively.
  2971  The following rules apply:
  2972  </p>
  2973  
  2974  <p>
  2975  If <code>a</code> is not a map:
  2976  </p>
  2977  <ul>
  2978  	<li>the index <code>x</code> must be of integer type or an untyped constant</li>
  2979  	<li>a constant index must be non-negative and
  2980  	    <a href="#Representability">representable</a> by a value of type <code>int</code></li>
  2981  	<li>a constant index that is untyped is given type <code>int</code></li>
  2982  	<li>the index <code>x</code> is <i>in range</i> if <code>0 &lt;= x &lt; len(a)</code>,
  2983  	    otherwise it is <i>out of range</i></li>
  2984  </ul>
  2985  
  2986  <p>
  2987  For <code>a</code> of <a href="#Array_types">array type</a> <code>A</code>:
  2988  </p>
  2989  <ul>
  2990  	<li>a <a href="#Constants">constant</a> index must be in range</li>
  2991  	<li>if <code>x</code> is out of range at run time,
  2992  	    a <a href="#Run_time_panics">run-time panic</a> occurs</li>
  2993  	<li><code>a[x]</code> is the array element at index <code>x</code> and the type of
  2994  	    <code>a[x]</code> is the element type of <code>A</code></li>
  2995  </ul>
  2996  
  2997  <p>
  2998  For <code>a</code> of <a href="#Pointer_types">pointer</a> to array type:
  2999  </p>
  3000  <ul>
  3001  	<li><code>a[x]</code> is shorthand for <code>(*a)[x]</code></li>
  3002  </ul>
  3003  
  3004  <p>
  3005  For <code>a</code> of <a href="#Slice_types">slice type</a> <code>S</code>:
  3006  </p>
  3007  <ul>
  3008  	<li>if <code>x</code> is out of range at run time,
  3009  	    a <a href="#Run_time_panics">run-time panic</a> occurs</li>
  3010  	<li><code>a[x]</code> is the slice element at index <code>x</code> and the type of
  3011  	    <code>a[x]</code> is the element type of <code>S</code></li>
  3012  </ul>
  3013  
  3014  <p>
  3015  For <code>a</code> of <a href="#String_types">string type</a>:
  3016  </p>
  3017  <ul>
  3018  	<li>a <a href="#Constants">constant</a> index must be in range
  3019  	    if the string <code>a</code> is also constant</li>
  3020  	<li>if <code>x</code> is out of range at run time,
  3021  	    a <a href="#Run_time_panics">run-time panic</a> occurs</li>
  3022  	<li><code>a[x]</code> is the non-constant byte value at index <code>x</code> and the type of
  3023  	    <code>a[x]</code> is <code>byte</code></li>
  3024  	<li><code>a[x]</code> may not be assigned to</li>
  3025  </ul>
  3026  
  3027  <p>
  3028  For <code>a</code> of <a href="#Map_types">map type</a> <code>M</code>:
  3029  </p>
  3030  <ul>
  3031  	<li><code>x</code>'s type must be
  3032  	    <a href="#Assignability">assignable</a>
  3033  	    to the key type of <code>M</code></li>
  3034  	<li>if the map contains an entry with key <code>x</code>,
  3035  	    <code>a[x]</code> is the map value with key <code>x</code>
  3036  	    and the type of <code>a[x]</code> is the value type of <code>M</code></li>
  3037  	<li>if the map is <code>nil</code> or does not contain such an entry,
  3038  	    <code>a[x]</code> is the <a href="#The_zero_value">zero value</a>
  3039  	    for the value type of <code>M</code></li>
  3040  </ul>
  3041  
  3042  <p>
  3043  Otherwise <code>a[x]</code> is illegal.
  3044  </p>
  3045  
  3046  <p>
  3047  An index expression on a map <code>a</code> of type <code>map[K]V</code>
  3048  used in an <a href="#Assignments">assignment</a> or initialization of the special form
  3049  </p>
  3050  
  3051  <pre>
  3052  v, ok = a[x]
  3053  v, ok := a[x]
  3054  var v, ok = a[x]
  3055  var v, ok T = a[x]
  3056  </pre>
  3057  
  3058  <p>
  3059  yields an additional untyped boolean value. The value of <code>ok</code> is
  3060  <code>true</code> if the key <code>x</code> is present in the map, and
  3061  <code>false</code> otherwise.
  3062  </p>
  3063  
  3064  <p>
  3065  Assigning to an element of a <code>nil</code> map causes a
  3066  <a href="#Run_time_panics">run-time panic</a>.
  3067  </p>
  3068  
  3069  
  3070  <h3 id="Slice_expressions">Slice expressions</h3>
  3071  
  3072  <p>
  3073  Slice expressions construct a substring or slice from a string, array, pointer
  3074  to array, or slice. There are two variants: a simple form that specifies a low
  3075  and high bound, and a full form that also specifies a bound on the capacity.
  3076  </p>
  3077  
  3078  <h4>Simple slice expressions</h4>
  3079  
  3080  <p>
  3081  For a string, array, pointer to array, or slice <code>a</code>, the primary expression
  3082  </p>
  3083  
  3084  <pre>
  3085  a[low : high]
  3086  </pre>
  3087  
  3088  <p>
  3089  constructs a substring or slice. The <i>indices</i> <code>low</code> and
  3090  <code>high</code> select which elements of operand <code>a</code> appear
  3091  in the result. The result has indices starting at 0 and length equal to
  3092  <code>high</code>&nbsp;-&nbsp;<code>low</code>.
  3093  After slicing the array <code>a</code>
  3094  </p>
  3095  
  3096  <pre>
  3097  a := [5]int{1, 2, 3, 4, 5}
  3098  s := a[1:4]
  3099  </pre>
  3100  
  3101  <p>
  3102  the slice <code>s</code> has type <code>[]int</code>, length 3, capacity 4, and elements
  3103  </p>
  3104  
  3105  <pre>
  3106  s[0] == 2
  3107  s[1] == 3
  3108  s[2] == 4
  3109  </pre>
  3110  
  3111  <p>
  3112  For convenience, any of the indices may be omitted. A missing <code>low</code>
  3113  index defaults to zero; a missing <code>high</code> index defaults to the length of the
  3114  sliced operand:
  3115  </p>
  3116  
  3117  <pre>
  3118  a[2:]  // same as a[2 : len(a)]
  3119  a[:3]  // same as a[0 : 3]
  3120  a[:]   // same as a[0 : len(a)]
  3121  </pre>
  3122  
  3123  <p>
  3124  If <code>a</code> is a pointer to an array, <code>a[low : high]</code> is shorthand for
  3125  <code>(*a)[low : high]</code>.
  3126  </p>
  3127  
  3128  <p>
  3129  For arrays or strings, the indices are <i>in range</i> if
  3130  <code>0</code> &lt;= <code>low</code> &lt;= <code>high</code> &lt;= <code>len(a)</code>,
  3131  otherwise they are <i>out of range</i>.
  3132  For slices, the upper index bound is the slice capacity <code>cap(a)</code> rather than the length.
  3133  A <a href="#Constants">constant</a> index must be non-negative and
  3134  <a href="#Representability">representable</a> by a value of type
  3135  <code>int</code>; for arrays or constant strings, constant indices must also be in range.
  3136  If both indices are constant, they must satisfy <code>low &lt;= high</code>.
  3137  If the indices are out of range at run time, a <a href="#Run_time_panics">run-time panic</a> occurs.
  3138  </p>
  3139  
  3140  <p>
  3141  Except for <a href="#Constants">untyped strings</a>, if the sliced operand is a string or slice,
  3142  the result of the slice operation is a non-constant value of the same type as the operand.
  3143  For untyped string operands the result is a non-constant value of type <code>string</code>.
  3144  If the sliced operand is an array, it must be <a href="#Address_operators">addressable</a>
  3145  and the result of the slice operation is a slice with the same element type as the array.
  3146  </p>
  3147  
  3148  <p>
  3149  If the sliced operand of a valid slice expression is a <code>nil</code> slice, the result
  3150  is a <code>nil</code> slice. Otherwise, if the result is a slice, it shares its underlying
  3151  array with the operand.
  3152  </p>
  3153  
  3154  <h4>Full slice expressions</h4>
  3155  
  3156  <p>
  3157  For an array, pointer to array, or slice <code>a</code> (but not a string), the primary expression
  3158  </p>
  3159  
  3160  <pre>
  3161  a[low : high : max]
  3162  </pre>
  3163  
  3164  <p>
  3165  constructs a slice of the same type, and with the same length and elements as the simple slice
  3166  expression <code>a[low : high]</code>. Additionally, it controls the resulting slice's capacity
  3167  by setting it to <code>max - low</code>. Only the first index may be omitted; it defaults to 0.
  3168  After slicing the array <code>a</code>
  3169  </p>
  3170  
  3171  <pre>
  3172  a := [5]int{1, 2, 3, 4, 5}
  3173  t := a[1:3:5]
  3174  </pre>
  3175  
  3176  <p>
  3177  the slice <code>t</code> has type <code>[]int</code>, length 2, capacity 4, and elements
  3178  </p>
  3179  
  3180  <pre>
  3181  t[0] == 2
  3182  t[1] == 3
  3183  </pre>
  3184  
  3185  <p>
  3186  As for simple slice expressions, if <code>a</code> is a pointer to an array,
  3187  <code>a[low : high : max]</code> is shorthand for <code>(*a)[low : high : max]</code>.
  3188  If the sliced operand is an array, it must be <a href="#Address_operators">addressable</a>.
  3189  </p>
  3190  
  3191  <p>
  3192  The indices are <i>in range</i> if <code>0 &lt;= low &lt;= high &lt;= max &lt;= cap(a)</code>,
  3193  otherwise they are <i>out of range</i>.
  3194  A <a href="#Constants">constant</a> index must be non-negative and
  3195  <a href="#Representability">representable</a> by a value of type
  3196  <code>int</code>; for arrays, constant indices must also be in range.
  3197  If multiple indices are constant, the constants that are present must be in range relative to each
  3198  other.
  3199  If the indices are out of range at run time, a <a href="#Run_time_panics">run-time panic</a> occurs.
  3200  </p>
  3201  
  3202  <h3 id="Type_assertions">Type assertions</h3>
  3203  
  3204  <p>
  3205  For an expression <code>x</code> of <a href="#Interface_types">interface type</a>
  3206  and a type <code>T</code>, the primary expression
  3207  </p>
  3208  
  3209  <pre>
  3210  x.(T)
  3211  </pre>
  3212  
  3213  <p>
  3214  asserts that <code>x</code> is not <code>nil</code>
  3215  and that the value stored in <code>x</code> is of type <code>T</code>.
  3216  The notation <code>x.(T)</code> is called a <i>type assertion</i>.
  3217  </p>
  3218  <p>
  3219  More precisely, if <code>T</code> is not an interface type, <code>x.(T)</code> asserts
  3220  that the dynamic type of <code>x</code> is <a href="#Type_identity">identical</a>
  3221  to the type <code>T</code>.
  3222  In this case, <code>T</code> must <a href="#Method_sets">implement</a> the (interface) type of <code>x</code>;
  3223  otherwise the type assertion is invalid since it is not possible for <code>x</code>
  3224  to store a value of type <code>T</code>.
  3225  If <code>T</code> is an interface type, <code>x.(T)</code> asserts that the dynamic type
  3226  of <code>x</code> implements the interface <code>T</code>.
  3227  </p>
  3228  <p>
  3229  If the type assertion holds, the value of the expression is the value
  3230  stored in <code>x</code> and its type is <code>T</code>. If the type assertion is false,
  3231  a <a href="#Run_time_panics">run-time panic</a> occurs.
  3232  In other words, even though the dynamic type of <code>x</code>
  3233  is known only at run time, the type of <code>x.(T)</code> is
  3234  known to be <code>T</code> in a correct program.
  3235  </p>
  3236  
  3237  <pre>
  3238  var x interface{} = 7          // x has dynamic type int and value 7
  3239  i := x.(int)                   // i has type int and value 7
  3240  
  3241  type I interface { m() }
  3242  
  3243  func f(y I) {
  3244  	s := y.(string)        // illegal: string does not implement I (missing method m)
  3245  	r := y.(io.Reader)     // r has type io.Reader and the dynamic type of y must implement both I and io.Reader
  3246  	…
  3247  }
  3248  </pre>
  3249  
  3250  <p>
  3251  A type assertion used in an <a href="#Assignments">assignment</a> or initialization of the special form
  3252  </p>
  3253  
  3254  <pre>
  3255  v, ok = x.(T)
  3256  v, ok := x.(T)
  3257  var v, ok = x.(T)
  3258  var v, ok T1 = x.(T)
  3259  </pre>
  3260  
  3261  <p>
  3262  yields an additional untyped boolean value. The value of <code>ok</code> is <code>true</code>
  3263  if the assertion holds. Otherwise it is <code>false</code> and the value of <code>v</code> is
  3264  the <a href="#The_zero_value">zero value</a> for type <code>T</code>.
  3265  No run-time panic occurs in this case.
  3266  </p>
  3267  
  3268  
  3269  <h3 id="Calls">Calls</h3>
  3270  
  3271  <p>
  3272  Given an expression <code>f</code> of function type
  3273  <code>F</code>,
  3274  </p>
  3275  
  3276  <pre>
  3277  f(a1, a2, … an)
  3278  </pre>
  3279  
  3280  <p>
  3281  calls <code>f</code> with arguments <code>a1, a2, … an</code>.
  3282  Except for one special case, arguments must be single-valued expressions
  3283  <a href="#Assignability">assignable</a> to the parameter types of
  3284  <code>F</code> and are evaluated before the function is called.
  3285  The type of the expression is the result type
  3286  of <code>F</code>.
  3287  A method invocation is similar but the method itself
  3288  is specified as a selector upon a value of the receiver type for
  3289  the method.
  3290  </p>
  3291  
  3292  <pre>
  3293  math.Atan2(x, y)  // function call
  3294  var pt *Point
  3295  pt.Scale(3.5)     // method call with receiver pt
  3296  </pre>
  3297  
  3298  <p>
  3299  In a function call, the function value and arguments are evaluated in
  3300  <a href="#Order_of_evaluation">the usual order</a>.
  3301  After they are evaluated, the parameters of the call are passed by value to the function
  3302  and the called function begins execution.
  3303  The return parameters of the function are passed by value
  3304  back to the calling function when the function returns.
  3305  </p>
  3306  
  3307  <p>
  3308  Calling a <code>nil</code> function value
  3309  causes a <a href="#Run_time_panics">run-time panic</a>.
  3310  </p>
  3311  
  3312  <p>
  3313  As a special case, if the return values of a function or method
  3314  <code>g</code> are equal in number and individually
  3315  assignable to the parameters of another function or method
  3316  <code>f</code>, then the call <code>f(g(<i>parameters_of_g</i>))</code>
  3317  will invoke <code>f</code> after binding the return values of
  3318  <code>g</code> to the parameters of <code>f</code> in order.  The call
  3319  of <code>f</code> must contain no parameters other than the call of <code>g</code>,
  3320  and <code>g</code> must have at least one return value.
  3321  If <code>f</code> has a final <code>...</code> parameter, it is
  3322  assigned the return values of <code>g</code> that remain after
  3323  assignment of regular parameters.
  3324  </p>
  3325  
  3326  <pre>
  3327  func Split(s string, pos int) (string, string) {
  3328  	return s[0:pos], s[pos:]
  3329  }
  3330  
  3331  func Join(s, t string) string {
  3332  	return s + t
  3333  }
  3334  
  3335  if Join(Split(value, len(value)/2)) != value {
  3336  	log.Panic("test fails")
  3337  }
  3338  </pre>
  3339  
  3340  <p>
  3341  A method call <code>x.m()</code> is valid if the <a href="#Method_sets">method set</a>
  3342  of (the type of) <code>x</code> contains <code>m</code> and the
  3343  argument list can be assigned to the parameter list of <code>m</code>.
  3344  If <code>x</code> is <a href="#Address_operators">addressable</a> and <code>&amp;x</code>'s method
  3345  set contains <code>m</code>, <code>x.m()</code> is shorthand
  3346  for <code>(&amp;x).m()</code>:
  3347  </p>
  3348  
  3349  <pre>
  3350  var p Point
  3351  p.Scale(3.5)
  3352  </pre>
  3353  
  3354  <p>
  3355  There is no distinct method type and there are no method literals.
  3356  </p>
  3357  
  3358  <h3 id="Passing_arguments_to_..._parameters">Passing arguments to <code>...</code> parameters</h3>
  3359  
  3360  <p>
  3361  If <code>f</code> is <a href="#Function_types">variadic</a> with a final
  3362  parameter <code>p</code> of type <code>...T</code>, then within <code>f</code>
  3363  the type of <code>p</code> is equivalent to type <code>[]T</code>.
  3364  If <code>f</code> is invoked with no actual arguments for <code>p</code>,
  3365  the value passed to <code>p</code> is <code>nil</code>.
  3366  Otherwise, the value passed is a new slice
  3367  of type <code>[]T</code> with a new underlying array whose successive elements
  3368  are the actual arguments, which all must be <a href="#Assignability">assignable</a>
  3369  to <code>T</code>. The length and capacity of the slice is therefore
  3370  the number of arguments bound to <code>p</code> and may differ for each
  3371  call site.
  3372  </p>
  3373  
  3374  <p>
  3375  Given the function and calls
  3376  </p>
  3377  <pre>
  3378  func Greeting(prefix string, who ...string)
  3379  Greeting("nobody")
  3380  Greeting("hello:", "Joe", "Anna", "Eileen")
  3381  </pre>
  3382  
  3383  <p>
  3384  within <code>Greeting</code>, <code>who</code> will have the value
  3385  <code>nil</code> in the first call, and
  3386  <code>[]string{"Joe", "Anna", "Eileen"}</code> in the second.
  3387  </p>
  3388  
  3389  <p>
  3390  If the final argument is assignable to a slice type <code>[]T</code>, it may be
  3391  passed unchanged as the value for a <code>...T</code> parameter if the argument
  3392  is followed by <code>...</code>. In this case no new slice is created.
  3393  </p>
  3394  
  3395  <p>
  3396  Given the slice <code>s</code> and call
  3397  </p>
  3398  
  3399  <pre>
  3400  s := []string{"James", "Jasmine"}
  3401  Greeting("goodbye:", s...)
  3402  </pre>
  3403  
  3404  <p>
  3405  within <code>Greeting</code>, <code>who</code> will have the same value as <code>s</code>
  3406  with the same underlying array.
  3407  </p>
  3408  
  3409  
  3410  <h3 id="Operators">Operators</h3>
  3411  
  3412  <p>
  3413  Operators combine operands into expressions.
  3414  </p>
  3415  
  3416  <pre class="ebnf">
  3417  Expression = UnaryExpr | Expression binary_op Expression .
  3418  UnaryExpr  = PrimaryExpr | unary_op UnaryExpr .
  3419  
  3420  binary_op  = "||" | "&amp;&amp;" | rel_op | add_op | mul_op .
  3421  rel_op     = "==" | "!=" | "&lt;" | "&lt;=" | ">" | ">=" .
  3422  add_op     = "+" | "-" | "|" | "^" .
  3423  mul_op     = "*" | "/" | "%" | "&lt;&lt;" | "&gt;&gt;" | "&amp;" | "&amp;^" .
  3424  
  3425  unary_op   = "+" | "-" | "!" | "^" | "*" | "&amp;" | "&lt;-" .
  3426  </pre>
  3427  
  3428  <p>
  3429  Comparisons are discussed <a href="#Comparison_operators">elsewhere</a>.
  3430  For other binary operators, the operand types must be <a href="#Type_identity">identical</a>
  3431  unless the operation involves shifts or untyped <a href="#Constants">constants</a>.
  3432  For operations involving constants only, see the section on
  3433  <a href="#Constant_expressions">constant expressions</a>.
  3434  </p>
  3435  
  3436  <p>
  3437  Except for shift operations, if one operand is an untyped <a href="#Constants">constant</a>
  3438  and the other operand is not, the constant is <a href="#Conversions">converted</a>
  3439  to the type of the other operand.
  3440  </p>
  3441  
  3442  <p>
  3443  The right operand in a shift expression must have unsigned integer type
  3444  or be an untyped constant <a href="#Representability">representable</a> by a
  3445  value of type <code>uint</code>.
  3446  If the left operand of a non-constant shift expression is an untyped constant,
  3447  it is first converted to the type it would assume if the shift expression were
  3448  replaced by its left operand alone.
  3449  </p>
  3450  
  3451  <pre>
  3452  var s uint = 33
  3453  var i = 1&lt;&lt;s                  // 1 has type int
  3454  var j int32 = 1&lt;&lt;s            // 1 has type int32; j == 0
  3455  var k = uint64(1&lt;&lt;s)          // 1 has type uint64; k == 1&lt;&lt;33
  3456  var m int = 1.0&lt;&lt;s            // 1.0 has type int; m == 0 if ints are 32bits in size
  3457  var n = 1.0&lt;&lt;s == j           // 1.0 has type int32; n == true
  3458  var o = 1&lt;&lt;s == 2&lt;&lt;s          // 1 and 2 have type int; o == true if ints are 32bits in size
  3459  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
  3460  var u = 1.0&lt;&lt;s                // illegal: 1.0 has type float64, cannot shift
  3461  var u1 = 1.0&lt;&lt;s != 0          // illegal: 1.0 has type float64, cannot shift
  3462  var u2 = 1&lt;&lt;s != 1.0          // illegal: 1 has type float64, cannot shift
  3463  var v float32 = 1&lt;&lt;s          // illegal: 1 has type float32, cannot shift
  3464  var w int64 = 1.0&lt;&lt;33         // 1.0&lt;&lt;33 is a constant shift expression
  3465  var x = a[1.0&lt;&lt;s]             // 1.0 has type int; x == a[0] if ints are 32bits in size
  3466  var a = make([]byte, 1.0&lt;&lt;s)  // 1.0 has type int; len(a) == 0 if ints are 32bits in size
  3467  </pre>
  3468  
  3469  
  3470  <h4 id="Operator_precedence">Operator precedence</h4>
  3471  <p>
  3472  Unary operators have the highest precedence.
  3473  As the  <code>++</code> and <code>--</code> operators form
  3474  statements, not expressions, they fall
  3475  outside the operator hierarchy.
  3476  As a consequence, statement <code>*p++</code> is the same as <code>(*p)++</code>.
  3477  <p>
  3478  There are five precedence levels for binary operators.
  3479  Multiplication operators bind strongest, followed by addition
  3480  operators, comparison operators, <code>&amp;&amp;</code> (logical AND),
  3481  and finally <code>||</code> (logical OR):
  3482  </p>
  3483  
  3484  <pre class="grammar">
  3485  Precedence    Operator
  3486      5             *  /  %  &lt;&lt;  &gt;&gt;  &amp;  &amp;^
  3487      4             +  -  |  ^
  3488      3             ==  !=  &lt;  &lt;=  &gt;  &gt;=
  3489      2             &amp;&amp;
  3490      1             ||
  3491  </pre>
  3492  
  3493  <p>
  3494  Binary operators of the same precedence associate from left to right.
  3495  For instance, <code>x / y * z</code> is the same as <code>(x / y) * z</code>.
  3496  </p>
  3497  
  3498  <pre>
  3499  +x
  3500  23 + 3*x[i]
  3501  x &lt;= f()
  3502  ^a &gt;&gt; b
  3503  f() || g()
  3504  x == y+1 &amp;&amp; &lt;-chanPtr &gt; 0
  3505  </pre>
  3506  
  3507  
  3508  <h3 id="Arithmetic_operators">Arithmetic operators</h3>
  3509  <p>
  3510  Arithmetic operators apply to numeric values and yield a result of the same
  3511  type as the first operand. The four standard arithmetic operators (<code>+</code>,
  3512  <code>-</code>, <code>*</code>, <code>/</code>) apply to integer,
  3513  floating-point, and complex types; <code>+</code> also applies to strings.
  3514  The bitwise logical and shift operators apply to integers only.
  3515  </p>
  3516  
  3517  <pre class="grammar">
  3518  +    sum                    integers, floats, complex values, strings
  3519  -    difference             integers, floats, complex values
  3520  *    product                integers, floats, complex values
  3521  /    quotient               integers, floats, complex values
  3522  %    remainder              integers
  3523  
  3524  &amp;    bitwise AND            integers
  3525  |    bitwise OR             integers
  3526  ^    bitwise XOR            integers
  3527  &amp;^   bit clear (AND NOT)    integers
  3528  
  3529  &lt;&lt;   left shift             integer &lt;&lt; unsigned integer
  3530  &gt;&gt;   right shift            integer &gt;&gt; unsigned integer
  3531  </pre>
  3532  
  3533  
  3534  <h4 id="Integer_operators">Integer operators</h4>
  3535  
  3536  <p>
  3537  For two integer values <code>x</code> and <code>y</code>, the integer quotient
  3538  <code>q = x / y</code> and remainder <code>r = x % y</code> satisfy the following
  3539  relationships:
  3540  </p>
  3541  
  3542  <pre>
  3543  x = q*y + r  and  |r| &lt; |y|
  3544  </pre>
  3545  
  3546  <p>
  3547  with <code>x / y</code> truncated towards zero
  3548  (<a href="http://en.wikipedia.org/wiki/Modulo_operation">"truncated division"</a>).
  3549  </p>
  3550  
  3551  <pre>
  3552   x     y     x / y     x % y
  3553   5     3       1         2
  3554  -5     3      -1        -2
  3555   5    -3      -1         2
  3556  -5    -3       1        -2
  3557  </pre>
  3558  
  3559  <p>
  3560  As an exception to this rule, if the dividend <code>x</code> is the most
  3561  negative value for the int type of <code>x</code>, the quotient
  3562  <code>q = x / -1</code> is equal to <code>x</code> (and <code>r = 0</code>).
  3563  </p>
  3564  
  3565  <pre>
  3566  			 x, q
  3567  int8                     -128
  3568  int16                  -32768
  3569  int32             -2147483648
  3570  int64    -9223372036854775808
  3571  </pre>
  3572  
  3573  <p>
  3574  If the divisor is a <a href="#Constants">constant</a>, it must not be zero.
  3575  If the divisor is zero at run time, a <a href="#Run_time_panics">run-time panic</a> occurs.
  3576  If the dividend is non-negative and the divisor is a constant power of 2,
  3577  the division may be replaced by a right shift, and computing the remainder may
  3578  be replaced by a bitwise AND operation:
  3579  </p>
  3580  
  3581  <pre>
  3582   x     x / 4     x % 4     x &gt;&gt; 2     x &amp; 3
  3583   11      2         3         2          3
  3584  -11     -2        -3        -3          1
  3585  </pre>
  3586  
  3587  <p>
  3588  The shift operators shift the left operand by the shift count specified by the
  3589  right operand. They implement arithmetic shifts if the left operand is a signed
  3590  integer and logical shifts if it is an unsigned integer.
  3591  There is no upper limit on the shift count. Shifts behave
  3592  as if the left operand is shifted <code>n</code> times by 1 for a shift
  3593  count of <code>n</code>.
  3594  As a result, <code>x &lt;&lt; 1</code> is the same as <code>x*2</code>
  3595  and <code>x &gt;&gt; 1</code> is the same as
  3596  <code>x/2</code> but truncated towards negative infinity.
  3597  </p>
  3598  
  3599  <p>
  3600  For integer operands, the unary operators
  3601  <code>+</code>, <code>-</code>, and <code>^</code> are defined as
  3602  follows:
  3603  </p>
  3604  
  3605  <pre class="grammar">
  3606  +x                          is 0 + x
  3607  -x    negation              is 0 - x
  3608  ^x    bitwise complement    is m ^ x  with m = "all bits set to 1" for unsigned x
  3609                                        and  m = -1 for signed x
  3610  </pre>
  3611  
  3612  
  3613  <h4 id="Integer_overflow">Integer overflow</h4>
  3614  
  3615  <p>
  3616  For unsigned integer values, the operations <code>+</code>,
  3617  <code>-</code>, <code>*</code>, and <code>&lt;&lt;</code> are
  3618  computed modulo 2<sup><i>n</i></sup>, where <i>n</i> is the bit width of
  3619  the <a href="#Numeric_types">unsigned integer</a>'s type.
  3620  Loosely speaking, these unsigned integer operations
  3621  discard high bits upon overflow, and programs may rely on ``wrap around''.
  3622  </p>
  3623  <p>
  3624  For signed integers, the operations <code>+</code>,
  3625  <code>-</code>, <code>*</code>, and <code>&lt;&lt;</code> may legally
  3626  overflow and the resulting value exists and is deterministically defined
  3627  by the signed integer representation, the operation, and its operands.
  3628  No exception is raised as a result of overflow. A
  3629  compiler may not optimize code under the assumption that overflow does
  3630  not occur. For instance, it may not assume that <code>x &lt; x + 1</code> is always true.
  3631  </p>
  3632  
  3633  
  3634  <h4 id="Floating_point_operators">Floating-point operators</h4>
  3635  
  3636  <p>
  3637  For floating-point and complex numbers,
  3638  <code>+x</code> is the same as <code>x</code>,
  3639  while <code>-x</code> is the negation of <code>x</code>.
  3640  The result of a floating-point or complex division by zero is not specified beyond the
  3641  IEEE-754 standard; whether a <a href="#Run_time_panics">run-time panic</a>
  3642  occurs is implementation-specific.
  3643  </p>
  3644  
  3645  <p>
  3646  An implementation may combine multiple floating-point operations into a single
  3647  fused operation, possibly across statements, and produce a result that differs
  3648  from the value obtained by executing and rounding the instructions individually.
  3649  A floating-point type <a href="#Conversions">conversion</a> explicitly rounds to
  3650  the precision of the target type, preventing fusion that would discard that rounding.
  3651  </p>
  3652  
  3653  <p>
  3654  For instance, some architectures provide a "fused multiply and add" (FMA) instruction
  3655  that computes <code>x*y + z</code> without rounding the intermediate result <code>x*y</code>.
  3656  These examples show when a Go implementation can use that instruction:
  3657  </p>
  3658  
  3659  <pre>
  3660  // FMA allowed for computing r, because x*y is not explicitly rounded:
  3661  r  = x*y + z
  3662  r  = z;   r += x*y
  3663  t  = x*y; r = t + z
  3664  *p = x*y; r = *p + z
  3665  r  = x*y + float64(z)
  3666  
  3667  // FMA disallowed for computing r, because it would omit rounding of x*y:
  3668  r  = float64(x*y) + z
  3669  r  = z; r += float64(x*y)
  3670  t  = float64(x*y); r = t + z
  3671  </pre>
  3672  
  3673  <h4 id="String_concatenation">String concatenation</h4>
  3674  
  3675  <p>
  3676  Strings can be concatenated using the <code>+</code> operator
  3677  or the <code>+=</code> assignment operator:
  3678  </p>
  3679  
  3680  <pre>
  3681  s := "hi" + string(c)
  3682  s += " and good bye"
  3683  </pre>
  3684  
  3685  <p>
  3686  String addition creates a new string by concatenating the operands.
  3687  </p>
  3688  
  3689  
  3690  <h3 id="Comparison_operators">Comparison operators</h3>
  3691  
  3692  <p>
  3693  Comparison operators compare two operands and yield an untyped boolean value.
  3694  </p>
  3695  
  3696  <pre class="grammar">
  3697  ==    equal
  3698  !=    not equal
  3699  &lt;     less
  3700  &lt;=    less or equal
  3701  &gt;     greater
  3702  &gt;=    greater or equal
  3703  </pre>
  3704  
  3705  <p>
  3706  In any comparison, the first operand
  3707  must be <a href="#Assignability">assignable</a>
  3708  to the type of the second operand, or vice versa.
  3709  </p>
  3710  <p>
  3711  The equality operators <code>==</code> and <code>!=</code> apply
  3712  to operands that are <i>comparable</i>.
  3713  The ordering operators <code>&lt;</code>, <code>&lt;=</code>, <code>&gt;</code>, and <code>&gt;=</code>
  3714  apply to operands that are <i>ordered</i>.
  3715  These terms and the result of the comparisons are defined as follows:
  3716  </p>
  3717  
  3718  <ul>
  3719  	<li>
  3720  	Boolean values are comparable.
  3721  	Two boolean values are equal if they are either both
  3722  	<code>true</code> or both <code>false</code>.
  3723  	</li>
  3724  
  3725  	<li>
  3726  	Integer values are comparable and ordered, in the usual way.
  3727  	</li>
  3728  
  3729  	<li>
  3730  	Floating-point values are comparable and ordered,
  3731  	as defined by the IEEE-754 standard.
  3732  	</li>
  3733  
  3734  	<li>
  3735  	Complex values are comparable.
  3736  	Two complex values <code>u</code> and <code>v</code> are
  3737  	equal if both <code>real(u) == real(v)</code> and
  3738  	<code>imag(u) == imag(v)</code>.
  3739  	</li>
  3740  
  3741  	<li>
  3742  	String values are comparable and ordered, lexically byte-wise.
  3743  	</li>
  3744  
  3745  	<li>
  3746  	Pointer values are comparable.
  3747  	Two pointer values are equal if they point to the same variable or if both have value <code>nil</code>.
  3748  	Pointers to distinct <a href="#Size_and_alignment_guarantees">zero-size</a> variables may or may not be equal.
  3749  	</li>
  3750  
  3751  	<li>
  3752  	Channel values are comparable.
  3753  	Two channel values are equal if they were created by the same call to
  3754  	<a href="#Making_slices_maps_and_channels"><code>make</code></a>
  3755  	or if both have value <code>nil</code>.
  3756  	</li>
  3757  
  3758  	<li>
  3759  	Interface values are comparable.
  3760  	Two interface values are equal if they have <a href="#Type_identity">identical</a> dynamic types
  3761  	and equal dynamic values or if both have value <code>nil</code>.
  3762  	</li>
  3763  
  3764  	<li>
  3765  	A value <code>x</code> of non-interface type <code>X</code> and
  3766  	a value <code>t</code> of interface type <code>T</code> are comparable when values
  3767  	of type <code>X</code> are comparable and
  3768  	<code>X</code> implements <code>T</code>.
  3769  	They are equal if <code>t</code>'s dynamic type is identical to <code>X</code>
  3770  	and <code>t</code>'s dynamic value is equal to <code>x</code>.
  3771  	</li>
  3772  
  3773  	<li>
  3774  	Struct values are comparable if all their fields are comparable.
  3775  	Two struct values are equal if their corresponding
  3776  	non-<a href="#Blank_identifier">blank</a> fields are equal.
  3777  	</li>
  3778  
  3779  	<li>
  3780  	Array values are comparable if values of the array element type are comparable.
  3781  	Two array values are equal if their corresponding elements are equal.
  3782  	</li>
  3783  </ul>
  3784  
  3785  <p>
  3786  A comparison of two interface values with identical dynamic types
  3787  causes a <a href="#Run_time_panics">run-time panic</a> if values
  3788  of that type are not comparable.  This behavior applies not only to direct interface
  3789  value comparisons but also when comparing arrays of interface values
  3790  or structs with interface-valued fields.
  3791  </p>
  3792  
  3793  <p>
  3794  Slice, map, and function values are not comparable.
  3795  However, as a special case, a slice, map, or function value may
  3796  be compared to the predeclared identifier <code>nil</code>.
  3797  Comparison of pointer, channel, and interface values to <code>nil</code>
  3798  is also allowed and follows from the general rules above.
  3799  </p>
  3800  
  3801  <pre>
  3802  const c = 3 &lt; 4            // c is the untyped boolean constant true
  3803  
  3804  type MyBool bool
  3805  var x, y int
  3806  var (
  3807  	// The result of a comparison is an untyped boolean.
  3808  	// The usual assignment rules apply.
  3809  	b3        = x == y // b3 has type bool
  3810  	b4 bool   = x == y // b4 has type bool
  3811  	b5 MyBool = x == y // b5 has type MyBool
  3812  )
  3813  </pre>
  3814  
  3815  <h3 id="Logical_operators">Logical operators</h3>
  3816  
  3817  <p>
  3818  Logical operators apply to <a href="#Boolean_types">boolean</a> values
  3819  and yield a result of the same type as the operands.
  3820  The right operand is evaluated conditionally.
  3821  </p>
  3822  
  3823  <pre class="grammar">
  3824  &amp;&amp;    conditional AND    p &amp;&amp; q  is  "if p then q else false"
  3825  ||    conditional OR     p || q  is  "if p then true else q"
  3826  !     NOT                !p      is  "not p"
  3827  </pre>
  3828  
  3829  
  3830  <h3 id="Address_operators">Address operators</h3>
  3831  
  3832  <p>
  3833  For an operand <code>x</code> of type <code>T</code>, the address operation
  3834  <code>&amp;x</code> generates a pointer of type <code>*T</code> to <code>x</code>.
  3835  The operand must be <i>addressable</i>,
  3836  that is, either a variable, pointer indirection, or slice indexing
  3837  operation; or a field selector of an addressable struct operand;
  3838  or an array indexing operation of an addressable array.
  3839  As an exception to the addressability requirement, <code>x</code> may also be a
  3840  (possibly parenthesized)
  3841  <a href="#Composite_literals">composite literal</a>.
  3842  If the evaluation of <code>x</code> would cause a <a href="#Run_time_panics">run-time panic</a>,
  3843  then the evaluation of <code>&amp;x</code> does too.
  3844  </p>
  3845  
  3846  <p>
  3847  For an operand <code>x</code> of pointer type <code>*T</code>, the pointer
  3848  indirection <code>*x</code> denotes the <a href="#Variables">variable</a> of type <code>T</code> pointed
  3849  to by <code>x</code>.
  3850  If <code>x</code> is <code>nil</code>, an attempt to evaluate <code>*x</code>
  3851  will cause a <a href="#Run_time_panics">run-time panic</a>.
  3852  </p>
  3853  
  3854  <pre>
  3855  &amp;x
  3856  &amp;a[f(2)]
  3857  &amp;Point{2, 3}
  3858  *p
  3859  *pf(x)
  3860  
  3861  var x *int = nil
  3862  *x   // causes a run-time panic
  3863  &amp;*x  // causes a run-time panic
  3864  </pre>
  3865  
  3866  
  3867  <h3 id="Receive_operator">Receive operator</h3>
  3868  
  3869  <p>
  3870  For an operand <code>ch</code> of <a href="#Channel_types">channel type</a>,
  3871  the value of the receive operation <code>&lt;-ch</code> is the value received
  3872  from the channel <code>ch</code>. The channel direction must permit receive operations,
  3873  and the type of the receive operation is the element type of the channel.
  3874  The expression blocks until a value is available.
  3875  Receiving from a <code>nil</code> channel blocks forever.
  3876  A receive operation on a <a href="#Close">closed</a> channel can always proceed
  3877  immediately, yielding the element type's <a href="#The_zero_value">zero value</a>
  3878  after any previously sent values have been received.
  3879  </p>
  3880  
  3881  <pre>
  3882  v1 := &lt;-ch
  3883  v2 = &lt;-ch
  3884  f(&lt;-ch)
  3885  &lt;-strobe  // wait until clock pulse and discard received value
  3886  </pre>
  3887  
  3888  <p>
  3889  A receive expression used in an <a href="#Assignments">assignment</a> or initialization of the special form
  3890  </p>
  3891  
  3892  <pre>
  3893  x, ok = &lt;-ch
  3894  x, ok := &lt;-ch
  3895  var x, ok = &lt;-ch
  3896  var x, ok T = &lt;-ch
  3897  </pre>
  3898  
  3899  <p>
  3900  yields an additional untyped boolean result reporting whether the
  3901  communication succeeded. The value of <code>ok</code> is <code>true</code>
  3902  if the value received was delivered by a successful send operation to the
  3903  channel, or <code>false</code> if it is a zero value generated because the
  3904  channel is closed and empty.
  3905  </p>
  3906  
  3907  
  3908  <h3 id="Conversions">Conversions</h3>
  3909  
  3910  <p>
  3911  Conversions are expressions of the form <code>T(x)</code>
  3912  where <code>T</code> is a type and <code>x</code> is an expression
  3913  that can be converted to type <code>T</code>.
  3914  </p>
  3915  
  3916  <pre class="ebnf">
  3917  Conversion = Type "(" Expression [ "," ] ")" .
  3918  </pre>
  3919  
  3920  <p>
  3921  If the type starts with the operator <code>*</code> or <code>&lt;-</code>,
  3922  or if the type starts with the keyword <code>func</code>
  3923  and has no result list, it must be parenthesized when
  3924  necessary to avoid ambiguity:
  3925  </p>
  3926  
  3927  <pre>
  3928  *Point(p)        // same as *(Point(p))
  3929  (*Point)(p)      // p is converted to *Point
  3930  &lt;-chan int(c)    // same as &lt;-(chan int(c))
  3931  (&lt;-chan int)(c)  // c is converted to &lt;-chan int
  3932  func()(x)        // function signature func() x
  3933  (func())(x)      // x is converted to func()
  3934  (func() int)(x)  // x is converted to func() int
  3935  func() int(x)    // x is converted to func() int (unambiguous)
  3936  </pre>
  3937  
  3938  <p>
  3939  A <a href="#Constants">constant</a> value <code>x</code> can be converted to
  3940  type <code>T</code> if <code>x</code> is <a href="#Representability">representable</a>
  3941  by a value of <code>T</code>.
  3942  As a special case, an integer constant <code>x</code> can be converted to a
  3943  <a href="#String_types">string type</a> using the
  3944  <a href="#Conversions_to_and_from_a_string_type">same rule</a>
  3945  as for non-constant <code>x</code>.
  3946  </p>
  3947  
  3948  <p>
  3949  Converting a constant yields a typed constant as result.
  3950  </p>
  3951  
  3952  <pre>
  3953  uint(iota)               // iota value of type uint
  3954  float32(2.718281828)     // 2.718281828 of type float32
  3955  complex128(1)            // 1.0 + 0.0i of type complex128
  3956  float32(0.49999999)      // 0.5 of type float32
  3957  float64(-1e-1000)        // 0.0 of type float64
  3958  string('x')              // "x" of type string
  3959  string(0x266c)           // "♬" of type string
  3960  MyString("foo" + "bar")  // "foobar" of type MyString
  3961  string([]byte{'a'})      // not a constant: []byte{'a'} is not a constant
  3962  (*int)(nil)              // not a constant: nil is not a constant, *int is not a boolean, numeric, or string type
  3963  int(1.2)                 // illegal: 1.2 cannot be represented as an int
  3964  string(65.0)             // illegal: 65.0 is not an integer constant
  3965  </pre>
  3966  
  3967  <p>
  3968  A non-constant value <code>x</code> can be converted to type <code>T</code>
  3969  in any of these cases:
  3970  </p>
  3971  
  3972  <ul>
  3973  	<li>
  3974  	<code>x</code> is <a href="#Assignability">assignable</a>
  3975  	to <code>T</code>.
  3976  	</li>
  3977  	<li>
  3978  	ignoring struct tags (see below),
  3979  	<code>x</code>'s type and <code>T</code> have <a href="#Type_identity">identical</a>
  3980  	<a href="#Types">underlying types</a>.
  3981  	</li>
  3982  	<li>
  3983  	ignoring struct tags (see below),
  3984  	<code>x</code>'s type and <code>T</code> are pointer types
  3985  	that are not <a href="#Type_definitions">defined types</a>,
  3986  	and their pointer base types have identical underlying types.
  3987  	</li>
  3988  	<li>
  3989  	<code>x</code>'s type and <code>T</code> are both integer or floating
  3990  	point types.
  3991  	</li>
  3992  	<li>
  3993  	<code>x</code>'s type and <code>T</code> are both complex types.
  3994  	</li>
  3995  	<li>
  3996  	<code>x</code> is an integer or a slice of bytes or runes
  3997  	and <code>T</code> is a string type.
  3998  	</li>
  3999  	<li>
  4000  	<code>x</code> is a string and <code>T</code> is a slice of bytes or runes.
  4001  	</li>
  4002  </ul>
  4003  
  4004  <p>
  4005  <a href="#Struct_types">Struct tags</a> are ignored when comparing struct types
  4006  for identity for the purpose of conversion:
  4007  </p>
  4008  
  4009  <pre>
  4010  type Person struct {
  4011  	Name    string
  4012  	Address *struct {
  4013  		Street string
  4014  		City   string
  4015  	}
  4016  }
  4017  
  4018  var data *struct {
  4019  	Name    string `json:"name"`
  4020  	Address *struct {
  4021  		Street string `json:"street"`
  4022  		City   string `json:"city"`
  4023  	} `json:"address"`
  4024  }
  4025  
  4026  var person = (*Person)(data)  // ignoring tags, the underlying types are identical
  4027  </pre>
  4028  
  4029  <p>
  4030  Specific rules apply to (non-constant) conversions between numeric types or
  4031  to and from a string type.
  4032  These conversions may change the representation of <code>x</code>
  4033  and incur a run-time cost.
  4034  All other conversions only change the type but not the representation
  4035  of <code>x</code>.
  4036  </p>
  4037  
  4038  <p>
  4039  There is no linguistic mechanism to convert between pointers and integers.
  4040  The package <a href="#Package_unsafe"><code>unsafe</code></a>
  4041  implements this functionality under
  4042  restricted circumstances.
  4043  </p>
  4044  
  4045  <h4>Conversions between numeric types</h4>
  4046  
  4047  <p>
  4048  For the conversion of non-constant numeric values, the following rules apply:
  4049  </p>
  4050  
  4051  <ol>
  4052  <li>
  4053  When converting between integer types, if the value is a signed integer, it is
  4054  sign extended to implicit infinite precision; otherwise it is zero extended.
  4055  It is then truncated to fit in the result type's size.
  4056  For example, if <code>v := uint16(0x10F0)</code>, then <code>uint32(int8(v)) == 0xFFFFFFF0</code>.
  4057  The conversion always yields a valid value; there is no indication of overflow.
  4058  </li>
  4059  <li>
  4060  When converting a floating-point number to an integer, the fraction is discarded
  4061  (truncation towards zero).
  4062  </li>
  4063  <li>
  4064  When converting an integer or floating-point number to a floating-point type,
  4065  or a complex number to another complex type, the result value is rounded
  4066  to the precision specified by the destination type.
  4067  For instance, the value of a variable <code>x</code> of type <code>float32</code>
  4068  may be stored using additional precision beyond that of an IEEE-754 32-bit number,
  4069  but float32(x) represents the result of rounding <code>x</code>'s value to
  4070  32-bit precision. Similarly, <code>x + 0.1</code> may use more than 32 bits
  4071  of precision, but <code>float32(x + 0.1)</code> does not.
  4072  </li>
  4073  </ol>
  4074  
  4075  <p>
  4076  In all non-constant conversions involving floating-point or complex values,
  4077  if the result type cannot represent the value the conversion
  4078  succeeds but the result value is implementation-dependent.
  4079  </p>
  4080  
  4081  <h4 id="Conversions_to_and_from_a_string_type">Conversions to and from a string type</h4>
  4082  
  4083  <ol>
  4084  <li>
  4085  Converting a signed or unsigned integer value to a string type yields a
  4086  string containing the UTF-8 representation of the integer. Values outside
  4087  the range of valid Unicode code points are converted to <code>"\uFFFD"</code>.
  4088  
  4089  <pre>
  4090  string('a')       // "a"
  4091  string(-1)        // "\ufffd" == "\xef\xbf\xbd"
  4092  string(0xf8)      // "\u00f8" == "ø" == "\xc3\xb8"
  4093  type MyString string
  4094  MyString(0x65e5)  // "\u65e5" == "日" == "\xe6\x97\xa5"
  4095  </pre>
  4096  </li>
  4097  
  4098  <li>
  4099  Converting a slice of bytes to a string type yields
  4100  a string whose successive bytes are the elements of the slice.
  4101  
  4102  <pre>
  4103  string([]byte{'h', 'e', 'l', 'l', '\xc3', '\xb8'})   // "hellø"
  4104  string([]byte{})                                     // ""
  4105  string([]byte(nil))                                  // ""
  4106  
  4107  type MyBytes []byte
  4108  string(MyBytes{'h', 'e', 'l', 'l', '\xc3', '\xb8'})  // "hellø"
  4109  </pre>
  4110  </li>
  4111  
  4112  <li>
  4113  Converting a slice of runes to a string type yields
  4114  a string that is the concatenation of the individual rune values
  4115  converted to strings.
  4116  
  4117  <pre>
  4118  string([]rune{0x767d, 0x9d6c, 0x7fd4})   // "\u767d\u9d6c\u7fd4" == "白鵬翔"
  4119  string([]rune{})                         // ""
  4120  string([]rune(nil))                      // ""
  4121  
  4122  type MyRunes []rune
  4123  string(MyRunes{0x767d, 0x9d6c, 0x7fd4})  // "\u767d\u9d6c\u7fd4" == "白鵬翔"
  4124  </pre>
  4125  </li>
  4126  
  4127  <li>
  4128  Converting a value of a string type to a slice of bytes type
  4129  yields a slice whose successive elements are the bytes of the string.
  4130  
  4131  <pre>
  4132  []byte("hellø")   // []byte{'h', 'e', 'l', 'l', '\xc3', '\xb8'}
  4133  []byte("")        // []byte{}
  4134  
  4135  MyBytes("hellø")  // []byte{'h', 'e', 'l', 'l', '\xc3', '\xb8'}
  4136  </pre>
  4137  </li>
  4138  
  4139  <li>
  4140  Converting a value of a string type to a slice of runes type
  4141  yields a slice containing the individual Unicode code points of the string.
  4142  
  4143  <pre>
  4144  []rune(MyString("白鵬翔"))  // []rune{0x767d, 0x9d6c, 0x7fd4}
  4145  []rune("")                 // []rune{}
  4146  
  4147  MyRunes("白鵬翔")           // []rune{0x767d, 0x9d6c, 0x7fd4}
  4148  </pre>
  4149  </li>
  4150  </ol>
  4151  
  4152  
  4153  <h3 id="Constant_expressions">Constant expressions</h3>
  4154  
  4155  <p>
  4156  Constant expressions may contain only <a href="#Constants">constant</a>
  4157  operands and are evaluated at compile time.
  4158  </p>
  4159  
  4160  <p>
  4161  Untyped boolean, numeric, and string constants may be used as operands
  4162  wherever it is legal to use an operand of boolean, numeric, or string type,
  4163  respectively.
  4164  Except for shift operations, if the operands of a binary operation are
  4165  different kinds of untyped constants, the operation and, for non-boolean operations, the result use
  4166  the kind that appears later in this list: integer, rune, floating-point, complex.
  4167  For example, an untyped integer constant divided by an
  4168  untyped complex constant yields an untyped complex constant.
  4169  </p>
  4170  
  4171  <p>
  4172  A constant <a href="#Comparison_operators">comparison</a> always yields
  4173  an untyped boolean constant.  If the left operand of a constant
  4174  <a href="#Operators">shift expression</a> is an untyped constant, the
  4175  result is an integer constant; otherwise it is a constant of the same
  4176  type as the left operand, which must be of
  4177  <a href="#Numeric_types">integer type</a>.
  4178  Applying all other operators to untyped constants results in an untyped
  4179  constant of the same kind (that is, a boolean, integer, floating-point,
  4180  complex, or string constant).
  4181  </p>
  4182  
  4183  <pre>
  4184  const a = 2 + 3.0          // a == 5.0   (untyped floating-point constant)
  4185  const b = 15 / 4           // b == 3     (untyped integer constant)
  4186  const c = 15 / 4.0         // c == 3.75  (untyped floating-point constant)
  4187  const Θ float64 = 3/2      // Θ == 1.0   (type float64, 3/2 is integer division)
  4188  const Π float64 = 3/2.     // Π == 1.5   (type float64, 3/2. is float division)
  4189  const d = 1 &lt;&lt; 3.0         // d == 8     (untyped integer constant)
  4190  const e = 1.0 &lt;&lt; 3         // e == 8     (untyped integer constant)
  4191  const f = int32(1) &lt;&lt; 33   // illegal    (constant 8589934592 overflows int32)
  4192  const g = float64(2) &gt;&gt; 1  // illegal    (float64(2) is a typed floating-point constant)
  4193  const h = "foo" &gt; "bar"    // h == true  (untyped boolean constant)
  4194  const j = true             // j == true  (untyped boolean constant)
  4195  const k = 'w' + 1          // k == 'x'   (untyped rune constant)
  4196  const l = "hi"             // l == "hi"  (untyped string constant)
  4197  const m = string(k)        // m == "x"   (type string)
  4198  const Σ = 1 - 0.707i       //            (untyped complex constant)
  4199  const Δ = Σ + 2.0e-4       //            (untyped complex constant)
  4200  const Φ = iota*1i - 1/1i   //            (untyped complex constant)
  4201  </pre>
  4202  
  4203  <p>
  4204  Applying the built-in function <code>complex</code> to untyped
  4205  integer, rune, or floating-point constants yields
  4206  an untyped complex constant.
  4207  </p>
  4208  
  4209  <pre>
  4210  const ic = complex(0, c)   // ic == 3.75i  (untyped complex constant)
  4211  const iΘ = complex(0, Θ)   // iΘ == 1i     (type complex128)
  4212  </pre>
  4213  
  4214  <p>
  4215  Constant expressions are always evaluated exactly; intermediate values and the
  4216  constants themselves may require precision significantly larger than supported
  4217  by any predeclared type in the language. The following are legal declarations:
  4218  </p>
  4219  
  4220  <pre>
  4221  const Huge = 1 &lt;&lt; 100         // Huge == 1267650600228229401496703205376  (untyped integer constant)
  4222  const Four int8 = Huge &gt;&gt; 98  // Four == 4                                (type int8)
  4223  </pre>
  4224  
  4225  <p>
  4226  The divisor of a constant division or remainder operation must not be zero:
  4227  </p>
  4228  
  4229  <pre>
  4230  3.14 / 0.0   // illegal: division by zero
  4231  </pre>
  4232  
  4233  <p>
  4234  The values of <i>typed</i> constants must always be accurately
  4235  <a href="#Representability">representable</a> by values
  4236  of the constant type. The following constant expressions are illegal:
  4237  </p>
  4238  
  4239  <pre>
  4240  uint(-1)     // -1 cannot be represented as a uint
  4241  int(3.14)    // 3.14 cannot be represented as an int
  4242  int64(Huge)  // 1267650600228229401496703205376 cannot be represented as an int64
  4243  Four * 300   // operand 300 cannot be represented as an int8 (type of Four)
  4244  Four * 100   // product 400 cannot be represented as an int8 (type of Four)
  4245  </pre>
  4246  
  4247  <p>
  4248  The mask used by the unary bitwise complement operator <code>^</code> matches
  4249  the rule for non-constants: the mask is all 1s for unsigned constants
  4250  and -1 for signed and untyped constants.
  4251  </p>
  4252  
  4253  <pre>
  4254  ^1         // untyped integer constant, equal to -2
  4255  uint8(^1)  // illegal: same as uint8(-2), -2 cannot be represented as a uint8
  4256  ^uint8(1)  // typed uint8 constant, same as 0xFF ^ uint8(1) = uint8(0xFE)
  4257  int8(^1)   // same as int8(-2)
  4258  ^int8(1)   // same as -1 ^ int8(1) = -2
  4259  </pre>
  4260  
  4261  <p>
  4262  Implementation restriction: A compiler may use rounding while
  4263  computing untyped floating-point or complex constant expressions; see
  4264  the implementation restriction in the section
  4265  on <a href="#Constants">constants</a>.  This rounding may cause a
  4266  floating-point constant expression to be invalid in an integer
  4267  context, even if it would be integral when calculated using infinite
  4268  precision, and vice versa.
  4269  </p>
  4270  
  4271  
  4272  <h3 id="Order_of_evaluation">Order of evaluation</h3>
  4273  
  4274  <p>
  4275  At package level, <a href="#Package_initialization">initialization dependencies</a>
  4276  determine the evaluation order of individual initialization expressions in
  4277  <a href="#Variable_declarations">variable declarations</a>.
  4278  Otherwise, when evaluating the <a href="#Operands">operands</a> of an
  4279  expression, assignment, or
  4280  <a href="#Return_statements">return statement</a>,
  4281  all function calls, method calls, and
  4282  communication operations are evaluated in lexical left-to-right
  4283  order.
  4284  </p>
  4285  
  4286  <p>
  4287  For example, in the (function-local) assignment
  4288  </p>
  4289  <pre>
  4290  y[f()], ok = g(h(), i()+x[j()], &lt;-c), k()
  4291  </pre>
  4292  <p>
  4293  the function calls and communication happen in the order
  4294  <code>f()</code>, <code>h()</code>, <code>i()</code>, <code>j()</code>,
  4295  <code>&lt;-c</code>, <code>g()</code>, and <code>k()</code>.
  4296  However, the order of those events compared to the evaluation
  4297  and indexing of <code>x</code> and the evaluation
  4298  of <code>y</code> is not specified.
  4299  </p>
  4300  
  4301  <pre>
  4302  a := 1
  4303  f := func() int { a++; return a }
  4304  x := []int{a, f()}            // x may be [1, 2] or [2, 2]: evaluation order between a and f() is not specified
  4305  m := map[int]int{a: 1, a: 2}  // m may be {2: 1} or {2: 2}: evaluation order between the two map assignments is not specified
  4306  n := map[int]int{a: f()}      // n may be {2: 3} or {3: 3}: evaluation order between the key and the value is not specified
  4307  </pre>
  4308  
  4309  <p>
  4310  At package level, initialization dependencies override the left-to-right rule
  4311  for individual initialization expressions, but not for operands within each
  4312  expression:
  4313  </p>
  4314  
  4315  <pre>
  4316  var a, b, c = f() + v(), g(), sqr(u()) + v()
  4317  
  4318  func f() int        { return c }
  4319  func g() int        { return a }
  4320  func sqr(x int) int { return x*x }
  4321  
  4322  // functions u and v are independent of all other variables and functions
  4323  </pre>
  4324  
  4325  <p>
  4326  The function calls happen in the order
  4327  <code>u()</code>, <code>sqr()</code>, <code>v()</code>,
  4328  <code>f()</code>, <code>v()</code>, and <code>g()</code>.
  4329  </p>
  4330  
  4331  <p>
  4332  Floating-point operations within a single expression are evaluated according to
  4333  the associativity of the operators.  Explicit parentheses affect the evaluation
  4334  by overriding the default associativity.
  4335  In the expression <code>x + (y + z)</code> the addition <code>y + z</code>
  4336  is performed before adding <code>x</code>.
  4337  </p>
  4338  
  4339  <h2 id="Statements">Statements</h2>
  4340  
  4341  <p>
  4342  Statements control execution.
  4343  </p>
  4344  
  4345  <pre class="ebnf">
  4346  Statement =
  4347  	Declaration | LabeledStmt | SimpleStmt |
  4348  	GoStmt | ReturnStmt | BreakStmt | ContinueStmt | GotoStmt |
  4349  	FallthroughStmt | Block | IfStmt | SwitchStmt | SelectStmt | ForStmt |
  4350  	DeferStmt .
  4351  
  4352  SimpleStmt = EmptyStmt | ExpressionStmt | SendStmt | IncDecStmt | Assignment | ShortVarDecl .
  4353  </pre>
  4354  
  4355  <h3 id="Terminating_statements">Terminating statements</h3>
  4356  
  4357  <p>
  4358  A terminating statement is one of the following:
  4359  </p>
  4360  
  4361  <ol>
  4362  <li>
  4363  	A <a href="#Return_statements">"return"</a> or
  4364      	<a href="#Goto_statements">"goto"</a> statement.
  4365  	<!-- ul below only for regular layout -->
  4366  	<ul> </ul>
  4367  </li>
  4368  
  4369  <li>
  4370  	A call to the built-in function
  4371  	<a href="#Handling_panics"><code>panic</code></a>.
  4372  	<!-- ul below only for regular layout -->
  4373  	<ul> </ul>
  4374  </li>
  4375  
  4376  <li>
  4377  	A <a href="#Blocks">block</a> in which the statement list ends in a terminating statement.
  4378  	<!-- ul below only for regular layout -->
  4379  	<ul> </ul>
  4380  </li>
  4381  
  4382  <li>
  4383  	An <a href="#If_statements">"if" statement</a> in which:
  4384  	<ul>
  4385  	<li>the "else" branch is present, and</li>
  4386  	<li>both branches are terminating statements.</li>
  4387  	</ul>
  4388  </li>
  4389  
  4390  <li>
  4391  	A <a href="#For_statements">"for" statement</a> in which:
  4392  	<ul>
  4393  	<li>there are no "break" statements referring to the "for" statement, and</li>
  4394  	<li>the loop condition is absent.</li>
  4395  	</ul>
  4396  </li>
  4397  
  4398  <li>
  4399  	A <a href="#Switch_statements">"switch" statement</a> in which:
  4400  	<ul>
  4401  	<li>there are no "break" statements referring to the "switch" statement,</li>
  4402  	<li>there is a default case, and</li>
  4403  	<li>the statement lists in each case, including the default, end in a terminating
  4404  	    statement, or a possibly labeled <a href="#Fallthrough_statements">"fallthrough"
  4405  	    statement</a>.</li>
  4406  	</ul>
  4407  </li>
  4408  
  4409  <li>
  4410  	A <a href="#Select_statements">"select" statement</a> in which:
  4411  	<ul>
  4412  	<li>there are no "break" statements referring to the "select" statement, and</li>
  4413  	<li>the statement lists in each case, including the default if present,
  4414  	    end in a terminating statement.</li>
  4415  	</ul>
  4416  </li>
  4417  
  4418  <li>
  4419  	A <a href="#Labeled_statements">labeled statement</a> labeling
  4420  	a terminating statement.
  4421  </li>
  4422  </ol>
  4423  
  4424  <p>
  4425  All other statements are not terminating.
  4426  </p>
  4427  
  4428  <p>
  4429  A <a href="#Blocks">statement list</a> ends in a terminating statement if the list
  4430  is not empty and its final non-empty statement is terminating.
  4431  </p>
  4432  
  4433  
  4434  <h3 id="Empty_statements">Empty statements</h3>
  4435  
  4436  <p>
  4437  The empty statement does nothing.
  4438  </p>
  4439  
  4440  <pre class="ebnf">
  4441  EmptyStmt = .
  4442  </pre>
  4443  
  4444  
  4445  <h3 id="Labeled_statements">Labeled statements</h3>
  4446  
  4447  <p>
  4448  A labeled statement may be the target of a <code>goto</code>,
  4449  <code>break</code> or <code>continue</code> statement.
  4450  </p>
  4451  
  4452  <pre class="ebnf">
  4453  LabeledStmt = Label ":" Statement .
  4454  Label       = identifier .
  4455  </pre>
  4456  
  4457  <pre>
  4458  Error: log.Panic("error encountered")
  4459  </pre>
  4460  
  4461  
  4462  <h3 id="Expression_statements">Expression statements</h3>
  4463  
  4464  <p>
  4465  With the exception of specific built-in functions,
  4466  function and method <a href="#Calls">calls</a> and
  4467  <a href="#Receive_operator">receive operations</a>
  4468  can appear in statement context. Such statements may be parenthesized.
  4469  </p>
  4470  
  4471  <pre class="ebnf">
  4472  ExpressionStmt = Expression .
  4473  </pre>
  4474  
  4475  <p>
  4476  The following built-in functions are not permitted in statement context:
  4477  </p>
  4478  
  4479  <pre>
  4480  append cap complex imag len make new real
  4481  unsafe.Alignof unsafe.Offsetof unsafe.Sizeof
  4482  </pre>
  4483  
  4484  <pre>
  4485  h(x+y)
  4486  f.Close()
  4487  &lt;-ch
  4488  (&lt;-ch)
  4489  len("foo")  // illegal if len is the built-in function
  4490  </pre>
  4491  
  4492  
  4493  <h3 id="Send_statements">Send statements</h3>
  4494  
  4495  <p>
  4496  A send statement sends a value on a channel.
  4497  The channel expression must be of <a href="#Channel_types">channel type</a>,
  4498  the channel direction must permit send operations,
  4499  and the type of the value to be sent must be <a href="#Assignability">assignable</a>
  4500  to the channel's element type.
  4501  </p>
  4502  
  4503  <pre class="ebnf">
  4504  SendStmt = Channel "&lt;-" Expression .
  4505  Channel  = Expression .
  4506  </pre>
  4507  
  4508  <p>
  4509  Both the channel and the value expression are evaluated before communication
  4510  begins. Communication blocks until the send can proceed.
  4511  A send on an unbuffered channel can proceed if a receiver is ready.
  4512  A send on a buffered channel can proceed if there is room in the buffer.
  4513  A send on a closed channel proceeds by causing a <a href="#Run_time_panics">run-time panic</a>.
  4514  A send on a <code>nil</code> channel blocks forever.
  4515  </p>
  4516  
  4517  <pre>
  4518  ch &lt;- 3  // send value 3 to channel ch
  4519  </pre>
  4520  
  4521  
  4522  <h3 id="IncDec_statements">IncDec statements</h3>
  4523  
  4524  <p>
  4525  The "++" and "--" statements increment or decrement their operands
  4526  by the untyped <a href="#Constants">constant</a> <code>1</code>.
  4527  As with an assignment, the operand must be <a href="#Address_operators">addressable</a>
  4528  or a map index expression.
  4529  </p>
  4530  
  4531  <pre class="ebnf">
  4532  IncDecStmt = Expression ( "++" | "--" ) .
  4533  </pre>
  4534  
  4535  <p>
  4536  The following <a href="#Assignments">assignment statements</a> are semantically
  4537  equivalent:
  4538  </p>
  4539  
  4540  <pre class="grammar">
  4541  IncDec statement    Assignment
  4542  x++                 x += 1
  4543  x--                 x -= 1
  4544  </pre>
  4545  
  4546  
  4547  <h3 id="Assignments">Assignments</h3>
  4548  
  4549  <pre class="ebnf">
  4550  Assignment = ExpressionList assign_op ExpressionList .
  4551  
  4552  assign_op = [ add_op | mul_op ] "=" .
  4553  </pre>
  4554  
  4555  <p>
  4556  Each left-hand side operand must be <a href="#Address_operators">addressable</a>,
  4557  a map index expression, or (for <code>=</code> assignments only) the
  4558  <a href="#Blank_identifier">blank identifier</a>.
  4559  Operands may be parenthesized.
  4560  </p>
  4561  
  4562  <pre>
  4563  x = 1
  4564  *p = f()
  4565  a[i] = 23
  4566  (k) = &lt;-ch  // same as: k = &lt;-ch
  4567  </pre>
  4568  
  4569  <p>
  4570  An <i>assignment operation</i> <code>x</code> <i>op</i><code>=</code>
  4571  <code>y</code> where <i>op</i> is a binary <a href="#Arithmetic_operators">arithmetic operator</a>
  4572  is equivalent to <code>x</code> <code>=</code> <code>x</code> <i>op</i>
  4573  <code>(y)</code> but evaluates <code>x</code>
  4574  only once.  The <i>op</i><code>=</code> construct is a single token.
  4575  In assignment operations, both the left- and right-hand expression lists
  4576  must contain exactly one single-valued expression, and the left-hand
  4577  expression must not be the blank identifier.
  4578  </p>
  4579  
  4580  <pre>
  4581  a[i] &lt;&lt;= 2
  4582  i &amp;^= 1&lt;&lt;n
  4583  </pre>
  4584  
  4585  <p>
  4586  A tuple assignment assigns the individual elements of a multi-valued
  4587  operation to a list of variables.  There are two forms.  In the
  4588  first, the right hand operand is a single multi-valued expression
  4589  such as a function call, a <a href="#Channel_types">channel</a> or
  4590  <a href="#Map_types">map</a> operation, or a <a href="#Type_assertions">type assertion</a>.
  4591  The number of operands on the left
  4592  hand side must match the number of values.  For instance, if
  4593  <code>f</code> is a function returning two values,
  4594  </p>
  4595  
  4596  <pre>
  4597  x, y = f()
  4598  </pre>
  4599  
  4600  <p>
  4601  assigns the first value to <code>x</code> and the second to <code>y</code>.
  4602  In the second form, the number of operands on the left must equal the number
  4603  of expressions on the right, each of which must be single-valued, and the
  4604  <i>n</i>th expression on the right is assigned to the <i>n</i>th
  4605  operand on the left:
  4606  </p>
  4607  
  4608  <pre>
  4609  one, two, three = '一', '二', '三'
  4610  </pre>
  4611  
  4612  <p>
  4613  The <a href="#Blank_identifier">blank identifier</a> provides a way to
  4614  ignore right-hand side values in an assignment:
  4615  </p>
  4616  
  4617  <pre>
  4618  _ = x       // evaluate x but ignore it
  4619  x, _ = f()  // evaluate f() but ignore second result value
  4620  </pre>
  4621  
  4622  <p>
  4623  The assignment proceeds in two phases.
  4624  First, the operands of <a href="#Index_expressions">index expressions</a>
  4625  and <a href="#Address_operators">pointer indirections</a>
  4626  (including implicit pointer indirections in <a href="#Selectors">selectors</a>)
  4627  on the left and the expressions on the right are all
  4628  <a href="#Order_of_evaluation">evaluated in the usual order</a>.
  4629  Second, the assignments are carried out in left-to-right order.
  4630  </p>
  4631  
  4632  <pre>
  4633  a, b = b, a  // exchange a and b
  4634  
  4635  x := []int{1, 2, 3}
  4636  i := 0
  4637  i, x[i] = 1, 2  // set i = 1, x[0] = 2
  4638  
  4639  i = 0
  4640  x[i], i = 2, 1  // set x[0] = 2, i = 1
  4641  
  4642  x[0], x[0] = 1, 2  // set x[0] = 1, then x[0] = 2 (so x[0] == 2 at end)
  4643  
  4644  x[1], x[3] = 4, 5  // set x[1] = 4, then panic setting x[3] = 5.
  4645  
  4646  type Point struct { x, y int }
  4647  var p *Point
  4648  x[2], p.x = 6, 7  // set x[2] = 6, then panic setting p.x = 7
  4649  
  4650  i = 2
  4651  x = []int{3, 5, 7}
  4652  for i, x[i] = range x {  // set i, x[2] = 0, x[0]
  4653  	break
  4654  }
  4655  // after this loop, i == 0 and x == []int{3, 5, 3}
  4656  </pre>
  4657  
  4658  <p>
  4659  In assignments, each value must be <a href="#Assignability">assignable</a>
  4660  to the type of the operand to which it is assigned, with the following special cases:
  4661  </p>
  4662  
  4663  <ol>
  4664  <li>
  4665  	Any typed value may be assigned to the blank identifier.
  4666  </li>
  4667  
  4668  <li>
  4669  	If an untyped constant
  4670  	is assigned to a variable of interface type or the blank identifier,
  4671  	the constant is first <a href="#Conversions">converted</a> to its
  4672  	 <a href="#Constants">default type</a>.
  4673  </li>
  4674  
  4675  <li>
  4676  	If an untyped boolean value is assigned to a variable of interface type or
  4677  	the blank identifier, it is first converted to type <code>bool</code>.
  4678  </li>
  4679  </ol>
  4680  
  4681  <h3 id="If_statements">If statements</h3>
  4682  
  4683  <p>
  4684  "If" statements specify the conditional execution of two branches
  4685  according to the value of a boolean expression.  If the expression
  4686  evaluates to true, the "if" branch is executed, otherwise, if
  4687  present, the "else" branch is executed.
  4688  </p>
  4689  
  4690  <pre class="ebnf">
  4691  IfStmt = "if" [ SimpleStmt ";" ] Expression Block [ "else" ( IfStmt | Block ) ] .
  4692  </pre>
  4693  
  4694  <pre>
  4695  if x &gt; max {
  4696  	x = max
  4697  }
  4698  </pre>
  4699  
  4700  <p>
  4701  The expression may be preceded by a simple statement, which
  4702  executes before the expression is evaluated.
  4703  </p>
  4704  
  4705  <pre>
  4706  if x := f(); x &lt; y {
  4707  	return x
  4708  } else if x &gt; z {
  4709  	return z
  4710  } else {
  4711  	return y
  4712  }
  4713  </pre>
  4714  
  4715  
  4716  <h3 id="Switch_statements">Switch statements</h3>
  4717  
  4718  <p>
  4719  "Switch" statements provide multi-way execution.
  4720  An expression or type specifier is compared to the "cases"
  4721  inside the "switch" to determine which branch
  4722  to execute.
  4723  </p>
  4724  
  4725  <pre class="ebnf">
  4726  SwitchStmt = ExprSwitchStmt | TypeSwitchStmt .
  4727  </pre>
  4728  
  4729  <p>
  4730  There are two forms: expression switches and type switches.
  4731  In an expression switch, the cases contain expressions that are compared
  4732  against the value of the switch expression.
  4733  In a type switch, the cases contain types that are compared against the
  4734  type of a specially annotated switch expression.
  4735  The switch expression is evaluated exactly once in a switch statement.
  4736  </p>
  4737  
  4738  <h4 id="Expression_switches">Expression switches</h4>
  4739  
  4740  <p>
  4741  In an expression switch,
  4742  the switch expression is evaluated and
  4743  the case expressions, which need not be constants,
  4744  are evaluated left-to-right and top-to-bottom; the first one that equals the
  4745  switch expression
  4746  triggers execution of the statements of the associated case;
  4747  the other cases are skipped.
  4748  If no case matches and there is a "default" case,
  4749  its statements are executed.
  4750  There can be at most one default case and it may appear anywhere in the
  4751  "switch" statement.
  4752  A missing switch expression is equivalent to the boolean value
  4753  <code>true</code>.
  4754  </p>
  4755  
  4756  <pre class="ebnf">
  4757  ExprSwitchStmt = "switch" [ SimpleStmt ";" ] [ Expression ] "{" { ExprCaseClause } "}" .
  4758  ExprCaseClause = ExprSwitchCase ":" StatementList .
  4759  ExprSwitchCase = "case" ExpressionList | "default" .
  4760  </pre>
  4761  
  4762  <p>
  4763  If the switch expression evaluates to an untyped constant, it is first
  4764  <a href="#Conversions">converted</a> to its <a href="#Constants">default type</a>;
  4765  if it is an untyped boolean value, it is first converted to type <code>bool</code>.
  4766  The predeclared untyped value <code>nil</code> cannot be used as a switch expression.
  4767  </p>
  4768  
  4769  <p>
  4770  If a case expression is untyped, it is first <a href="#Conversions">converted</a>
  4771  to the type of the switch expression.
  4772  For each (possibly converted) case expression <code>x</code> and the value <code>t</code>
  4773  of the switch expression, <code>x == t</code> must be a valid <a href="#Comparison_operators">comparison</a>.
  4774  </p>
  4775  
  4776  <p>
  4777  In other words, the switch expression is treated as if it were used to declare and
  4778  initialize a temporary variable <code>t</code> without explicit type; it is that
  4779  value of <code>t</code> against which each case expression <code>x</code> is tested
  4780  for equality.
  4781  </p>
  4782  
  4783  <p>
  4784  In a case or default clause, the last non-empty statement
  4785  may be a (possibly <a href="#Labeled_statements">labeled</a>)
  4786  <a href="#Fallthrough_statements">"fallthrough" statement</a> to
  4787  indicate that control should flow from the end of this clause to
  4788  the first statement of the next clause.
  4789  Otherwise control flows to the end of the "switch" statement.
  4790  A "fallthrough" statement may appear as the last statement of all
  4791  but the last clause of an expression switch.
  4792  </p>
  4793  
  4794  <p>
  4795  The switch expression may be preceded by a simple statement, which
  4796  executes before the expression is evaluated.
  4797  </p>
  4798  
  4799  <pre>
  4800  switch tag {
  4801  default: s3()
  4802  case 0, 1, 2, 3: s1()
  4803  case 4, 5, 6, 7: s2()
  4804  }
  4805  
  4806  switch x := f(); {  // missing switch expression means "true"
  4807  case x &lt; 0: return -x
  4808  default: return x
  4809  }
  4810  
  4811  switch {
  4812  case x &lt; y: f1()
  4813  case x &lt; z: f2()
  4814  case x == 4: f3()
  4815  }
  4816  </pre>
  4817  
  4818  <p>
  4819  Implementation restriction: A compiler may disallow multiple case
  4820  expressions evaluating to the same constant.
  4821  For instance, the current compilers disallow duplicate integer,
  4822  floating point, or string constants in case expressions.
  4823  </p>
  4824  
  4825  <h4 id="Type_switches">Type switches</h4>
  4826  
  4827  <p>
  4828  A type switch compares types rather than values. It is otherwise similar
  4829  to an expression switch. It is marked by a special switch expression that
  4830  has the form of a <a href="#Type_assertions">type assertion</a>
  4831  using the reserved word <code>type</code> rather than an actual type:
  4832  </p>
  4833  
  4834  <pre>
  4835  switch x.(type) {
  4836  // cases
  4837  }
  4838  </pre>
  4839  
  4840  <p>
  4841  Cases then match actual types <code>T</code> against the dynamic type of the
  4842  expression <code>x</code>. As with type assertions, <code>x</code> must be of
  4843  <a href="#Interface_types">interface type</a>, and each non-interface type
  4844  <code>T</code> listed in a case must implement the type of <code>x</code>.
  4845  The types listed in the cases of a type switch must all be
  4846  <a href="#Type_identity">different</a>.
  4847  </p>
  4848  
  4849  <pre class="ebnf">
  4850  TypeSwitchStmt  = "switch" [ SimpleStmt ";" ] TypeSwitchGuard "{" { TypeCaseClause } "}" .
  4851  TypeSwitchGuard = [ identifier ":=" ] PrimaryExpr "." "(" "type" ")" .
  4852  TypeCaseClause  = TypeSwitchCase ":" StatementList .
  4853  TypeSwitchCase  = "case" TypeList | "default" .
  4854  TypeList        = Type { "," Type } .
  4855  </pre>
  4856  
  4857  <p>
  4858  The TypeSwitchGuard may include a
  4859  <a href="#Short_variable_declarations">short variable declaration</a>.
  4860  When that form is used, the variable is declared at the end of the
  4861  TypeSwitchCase in the <a href="#Blocks">implicit block</a> of each clause.
  4862  In clauses with a case listing exactly one type, the variable
  4863  has that type; otherwise, the variable has the type of the expression
  4864  in the TypeSwitchGuard.
  4865  </p>
  4866  
  4867  <p>
  4868  Instead of a type, a case may use the predeclared identifier
  4869  <a href="#Predeclared_identifiers"><code>nil</code></a>;
  4870  that case is selected when the expression in the TypeSwitchGuard
  4871  is a <code>nil</code> interface value.
  4872  There may be at most one <code>nil</code> case.
  4873  </p>
  4874  
  4875  <p>
  4876  Given an expression <code>x</code> of type <code>interface{}</code>,
  4877  the following type switch:
  4878  </p>
  4879  
  4880  <pre>
  4881  switch i := x.(type) {
  4882  case nil:
  4883  	printString("x is nil")                // type of i is type of x (interface{})
  4884  case int:
  4885  	printInt(i)                            // type of i is int
  4886  case float64:
  4887  	printFloat64(i)                        // type of i is float64
  4888  case func(int) float64:
  4889  	printFunction(i)                       // type of i is func(int) float64
  4890  case bool, string:
  4891  	printString("type is bool or string")  // type of i is type of x (interface{})
  4892  default:
  4893  	printString("don't know the type")     // type of i is type of x (interface{})
  4894  }
  4895  </pre>
  4896  
  4897  <p>
  4898  could be rewritten:
  4899  </p>
  4900  
  4901  <pre>
  4902  v := x  // x is evaluated exactly once
  4903  if v == nil {
  4904  	i := v                                 // type of i is type of x (interface{})
  4905  	printString("x is nil")
  4906  } else if i, isInt := v.(int); isInt {
  4907  	printInt(i)                            // type of i is int
  4908  } else if i, isFloat64 := v.(float64); isFloat64 {
  4909  	printFloat64(i)                        // type of i is float64
  4910  } else if i, isFunc := v.(func(int) float64); isFunc {
  4911  	printFunction(i)                       // type of i is func(int) float64
  4912  } else {
  4913  	_, isBool := v.(bool)
  4914  	_, isString := v.(string)
  4915  	if isBool || isString {
  4916  		i := v                         // type of i is type of x (interface{})
  4917  		printString("type is bool or string")
  4918  	} else {
  4919  		i := v                         // type of i is type of x (interface{})
  4920  		printString("don't know the type")
  4921  	}
  4922  }
  4923  </pre>
  4924  
  4925  <p>
  4926  The type switch guard may be preceded by a simple statement, which
  4927  executes before the guard is evaluated.
  4928  </p>
  4929  
  4930  <p>
  4931  The "fallthrough" statement is not permitted in a type switch.
  4932  </p>
  4933  
  4934  <h3 id="For_statements">For statements</h3>
  4935  
  4936  <p>
  4937  A "for" statement specifies repeated execution of a block. There are three forms:
  4938  The iteration may be controlled by a single condition, a "for" clause, or a "range" clause.
  4939  </p>
  4940  
  4941  <pre class="ebnf">
  4942  ForStmt = "for" [ Condition | ForClause | RangeClause ] Block .
  4943  Condition = Expression .
  4944  </pre>
  4945  
  4946  <h4 id="For_condition">For statements with single condition</h4>
  4947  
  4948  <p>
  4949  In its simplest form, a "for" statement specifies the repeated execution of
  4950  a block as long as a boolean condition evaluates to true.
  4951  The condition is evaluated before each iteration.
  4952  If the condition is absent, it is equivalent to the boolean value
  4953  <code>true</code>.
  4954  </p>
  4955  
  4956  <pre>
  4957  for a &lt; b {
  4958  	a *= 2
  4959  }
  4960  </pre>
  4961  
  4962  <h4 id="For_clause">For statements with <code>for</code> clause</h4>
  4963  
  4964  <p>
  4965  A "for" statement with a ForClause is also controlled by its condition, but
  4966  additionally it may specify an <i>init</i>
  4967  and a <i>post</i> statement, such as an assignment,
  4968  an increment or decrement statement. The init statement may be a
  4969  <a href="#Short_variable_declarations">short variable declaration</a>, but the post statement must not.
  4970  Variables declared by the init statement are re-used in each iteration.
  4971  </p>
  4972  
  4973  <pre class="ebnf">
  4974  ForClause = [ InitStmt ] ";" [ Condition ] ";" [ PostStmt ] .
  4975  InitStmt = SimpleStmt .
  4976  PostStmt = SimpleStmt .
  4977  </pre>
  4978  
  4979  <pre>
  4980  for i := 0; i &lt; 10; i++ {
  4981  	f(i)
  4982  }
  4983  </pre>
  4984  
  4985  <p>
  4986  If non-empty, the init statement is executed once before evaluating the
  4987  condition for the first iteration;
  4988  the post statement is executed after each execution of the block (and
  4989  only if the block was executed).
  4990  Any element of the ForClause may be empty but the
  4991  <a href="#Semicolons">semicolons</a> are
  4992  required unless there is only a condition.
  4993  If the condition is absent, it is equivalent to the boolean value
  4994  <code>true</code>.
  4995  </p>
  4996  
  4997  <pre>
  4998  for cond { S() }    is the same as    for ; cond ; { S() }
  4999  for      { S() }    is the same as    for true     { S() }
  5000  </pre>
  5001  
  5002  <h4 id="For_range">For statements with <code>range</code> clause</h4>
  5003  
  5004  <p>
  5005  A "for" statement with a "range" clause
  5006  iterates through all entries of an array, slice, string or map,
  5007  or values received on a channel. For each entry it assigns <i>iteration values</i>
  5008  to corresponding <i>iteration variables</i> if present and then executes the block.
  5009  </p>
  5010  
  5011  <pre class="ebnf">
  5012  RangeClause = [ ExpressionList "=" | IdentifierList ":=" ] "range" Expression .
  5013  </pre>
  5014  
  5015  <p>
  5016  The expression on the right in the "range" clause is called the <i>range expression</i>,
  5017  which may be an array, pointer to an array, slice, string, map, or channel permitting
  5018  <a href="#Receive_operator">receive operations</a>.
  5019  As with an assignment, if present the operands on the left must be
  5020  <a href="#Address_operators">addressable</a> or map index expressions; they
  5021  denote the iteration variables. If the range expression is a channel, at most
  5022  one iteration variable is permitted, otherwise there may be up to two.
  5023  If the last iteration variable is the <a href="#Blank_identifier">blank identifier</a>,
  5024  the range clause is equivalent to the same clause without that identifier.
  5025  </p>
  5026  
  5027  <p>
  5028  The range expression is evaluated once before beginning the loop,
  5029  with one exception: if the range expression is an array or a pointer to an array
  5030  and at most one iteration variable is present, only the range expression's
  5031  length is evaluated; if that length is constant,
  5032  <a href="#Length_and_capacity">by definition</a>
  5033  the range expression itself will not be evaluated.
  5034  </p>
  5035  
  5036  <p>
  5037  Function calls on the left are evaluated once per iteration.
  5038  For each iteration, iteration values are produced as follows
  5039  if the respective iteration variables are present:
  5040  </p>
  5041  
  5042  <pre class="grammar">
  5043  Range expression                          1st value          2nd value
  5044  
  5045  array or slice  a  [n]E, *[n]E, or []E    index    i  int    a[i]       E
  5046  string          s  string type            index    i  int    see below  rune
  5047  map             m  map[K]V                key      k  K      m[k]       V
  5048  channel         c  chan E, &lt;-chan E       element  e  E
  5049  </pre>
  5050  
  5051  <ol>
  5052  <li>
  5053  For an array, pointer to array, or slice value <code>a</code>, the index iteration
  5054  values are produced in increasing order, starting at element index 0.
  5055  If at most one iteration variable is present, the range loop produces
  5056  iteration values from 0 up to <code>len(a)-1</code> and does not index into the array
  5057  or slice itself. For a <code>nil</code> slice, the number of iterations is 0.
  5058  </li>
  5059  
  5060  <li>
  5061  For a string value, the "range" clause iterates over the Unicode code points
  5062  in the string starting at byte index 0.  On successive iterations, the index value will be the
  5063  index of the first byte of successive UTF-8-encoded code points in the string,
  5064  and the second value, of type <code>rune</code>, will be the value of
  5065  the corresponding code point.  If the iteration encounters an invalid
  5066  UTF-8 sequence, the second value will be <code>0xFFFD</code>,
  5067  the Unicode replacement character, and the next iteration will advance
  5068  a single byte in the string.
  5069  </li>
  5070  
  5071  <li>
  5072  The iteration order over maps is not specified
  5073  and is not guaranteed to be the same from one iteration to the next.
  5074  If a map entry that has not yet been reached is removed during iteration,
  5075  the corresponding iteration value will not be produced. If a map entry is
  5076  created during iteration, that entry may be produced during the iteration or
  5077  may be skipped. The choice may vary for each entry created and from one
  5078  iteration to the next.
  5079  If the map is <code>nil</code>, the number of iterations is 0.
  5080  </li>
  5081  
  5082  <li>
  5083  For channels, the iteration values produced are the successive values sent on
  5084  the channel until the channel is <a href="#Close">closed</a>. If the channel
  5085  is <code>nil</code>, the range expression blocks forever.
  5086  </li>
  5087  </ol>
  5088  
  5089  <p>
  5090  The iteration values are assigned to the respective
  5091  iteration variables as in an <a href="#Assignments">assignment statement</a>.
  5092  </p>
  5093  
  5094  <p>
  5095  The iteration variables may be declared by the "range" clause using a form of
  5096  <a href="#Short_variable_declarations">short variable declaration</a>
  5097  (<code>:=</code>).
  5098  In this case their types are set to the types of the respective iteration values
  5099  and their <a href="#Declarations_and_scope">scope</a> is the block of the "for"
  5100  statement; they are re-used in each iteration.
  5101  If the iteration variables are declared outside the "for" statement,
  5102  after execution their values will be those of the last iteration.
  5103  </p>
  5104  
  5105  <pre>
  5106  var testdata *struct {
  5107  	a *[7]int
  5108  }
  5109  for i, _ := range testdata.a {
  5110  	// testdata.a is never evaluated; len(testdata.a) is constant
  5111  	// i ranges from 0 to 6
  5112  	f(i)
  5113  }
  5114  
  5115  var a [10]string
  5116  for i, s := range a {
  5117  	// type of i is int
  5118  	// type of s is string
  5119  	// s == a[i]
  5120  	g(i, s)
  5121  }
  5122  
  5123  var key string
  5124  var val interface {}  // value type of m is assignable to val
  5125  m := map[string]int{"mon":0, "tue":1, "wed":2, "thu":3, "fri":4, "sat":5, "sun":6}
  5126  for key, val = range m {
  5127  	h(key, val)
  5128  }
  5129  // key == last map key encountered in iteration
  5130  // val == map[key]
  5131  
  5132  var ch chan Work = producer()
  5133  for w := range ch {
  5134  	doWork(w)
  5135  }
  5136  
  5137  // empty a channel
  5138  for range ch {}
  5139  </pre>
  5140  
  5141  
  5142  <h3 id="Go_statements">Go statements</h3>
  5143  
  5144  <p>
  5145  A "go" statement starts the execution of a function call
  5146  as an independent concurrent thread of control, or <i>goroutine</i>,
  5147  within the same address space.
  5148  </p>
  5149  
  5150  <pre class="ebnf">
  5151  GoStmt = "go" Expression .
  5152  </pre>
  5153  
  5154  <p>
  5155  The expression must be a function or method call; it cannot be parenthesized.
  5156  Calls of built-in functions are restricted as for
  5157  <a href="#Expression_statements">expression statements</a>.
  5158  </p>
  5159  
  5160  <p>
  5161  The function value and parameters are
  5162  <a href="#Calls">evaluated as usual</a>
  5163  in the calling goroutine, but
  5164  unlike with a regular call, program execution does not wait
  5165  for the invoked function to complete.
  5166  Instead, the function begins executing independently
  5167  in a new goroutine.
  5168  When the function terminates, its goroutine also terminates.
  5169  If the function has any return values, they are discarded when the
  5170  function completes.
  5171  </p>
  5172  
  5173  <pre>
  5174  go Server()
  5175  go func(ch chan&lt;- bool) { for { sleep(10); ch &lt;- true }} (c)
  5176  </pre>
  5177  
  5178  
  5179  <h3 id="Select_statements">Select statements</h3>
  5180  
  5181  <p>
  5182  A "select" statement chooses which of a set of possible
  5183  <a href="#Send_statements">send</a> or
  5184  <a href="#Receive_operator">receive</a>
  5185  operations will proceed.
  5186  It looks similar to a
  5187  <a href="#Switch_statements">"switch"</a> statement but with the
  5188  cases all referring to communication operations.
  5189  </p>
  5190  
  5191  <pre class="ebnf">
  5192  SelectStmt = "select" "{" { CommClause } "}" .
  5193  CommClause = CommCase ":" StatementList .
  5194  CommCase   = "case" ( SendStmt | RecvStmt ) | "default" .
  5195  RecvStmt   = [ ExpressionList "=" | IdentifierList ":=" ] RecvExpr .
  5196  RecvExpr   = Expression .
  5197  </pre>
  5198  
  5199  <p>
  5200  A case with a RecvStmt may assign the result of a RecvExpr to one or
  5201  two variables, which may be declared using a
  5202  <a href="#Short_variable_declarations">short variable declaration</a>.
  5203  The RecvExpr must be a (possibly parenthesized) receive operation.
  5204  There can be at most one default case and it may appear anywhere
  5205  in the list of cases.
  5206  </p>
  5207  
  5208  <p>
  5209  Execution of a "select" statement proceeds in several steps:
  5210  </p>
  5211  
  5212  <ol>
  5213  <li>
  5214  For all the cases in the statement, the channel operands of receive operations
  5215  and the channel and right-hand-side expressions of send statements are
  5216  evaluated exactly once, in source order, upon entering the "select" statement.
  5217  The result is a set of channels to receive from or send to,
  5218  and the corresponding values to send.
  5219  Any side effects in that evaluation will occur irrespective of which (if any)
  5220  communication operation is selected to proceed.
  5221  Expressions on the left-hand side of a RecvStmt with a short variable declaration
  5222  or assignment are not yet evaluated.
  5223  </li>
  5224  
  5225  <li>
  5226  If one or more of the communications can proceed,
  5227  a single one that can proceed is chosen via a uniform pseudo-random selection.
  5228  Otherwise, if there is a default case, that case is chosen.
  5229  If there is no default case, the "select" statement blocks until
  5230  at least one of the communications can proceed.
  5231  </li>
  5232  
  5233  <li>
  5234  Unless the selected case is the default case, the respective communication
  5235  operation is executed.
  5236  </li>
  5237  
  5238  <li>
  5239  If the selected case is a RecvStmt with a short variable declaration or
  5240  an assignment, the left-hand side expressions are evaluated and the
  5241  received value (or values) are assigned.
  5242  </li>
  5243  
  5244  <li>
  5245  The statement list of the selected case is executed.
  5246  </li>
  5247  </ol>
  5248  
  5249  <p>
  5250  Since communication on <code>nil</code> channels can never proceed,
  5251  a select with only <code>nil</code> channels and no default case blocks forever.
  5252  </p>
  5253  
  5254  <pre>
  5255  var a []int
  5256  var c, c1, c2, c3, c4 chan int
  5257  var i1, i2 int
  5258  select {
  5259  case i1 = &lt;-c1:
  5260  	print("received ", i1, " from c1\n")
  5261  case c2 &lt;- i2:
  5262  	print("sent ", i2, " to c2\n")
  5263  case i3, ok := (&lt;-c3):  // same as: i3, ok := &lt;-c3
  5264  	if ok {
  5265  		print("received ", i3, " from c3\n")
  5266  	} else {
  5267  		print("c3 is closed\n")
  5268  	}
  5269  case a[f()] = &lt;-c4:
  5270  	// same as:
  5271  	// case t := &lt;-c4
  5272  	//	a[f()] = t
  5273  default:
  5274  	print("no communication\n")
  5275  }
  5276  
  5277  for {  // send random sequence of bits to c
  5278  	select {
  5279  	case c &lt;- 0:  // note: no statement, no fallthrough, no folding of cases
  5280  	case c &lt;- 1:
  5281  	}
  5282  }
  5283  
  5284  select {}  // block forever
  5285  </pre>
  5286  
  5287  
  5288  <h3 id="Return_statements">Return statements</h3>
  5289  
  5290  <p>
  5291  A "return" statement in a function <code>F</code> terminates the execution
  5292  of <code>F</code>, and optionally provides one or more result values.
  5293  Any functions <a href="#Defer_statements">deferred</a> by <code>F</code>
  5294  are executed before <code>F</code> returns to its caller.
  5295  </p>
  5296  
  5297  <pre class="ebnf">
  5298  ReturnStmt = "return" [ ExpressionList ] .
  5299  </pre>
  5300  
  5301  <p>
  5302  In a function without a result type, a "return" statement must not
  5303  specify any result values.
  5304  </p>
  5305  <pre>
  5306  func noResult() {
  5307  	return
  5308  }
  5309  </pre>
  5310  
  5311  <p>
  5312  There are three ways to return values from a function with a result
  5313  type:
  5314  </p>
  5315  
  5316  <ol>
  5317  	<li>The return value or values may be explicitly listed
  5318  		in the "return" statement. Each expression must be single-valued
  5319  		and <a href="#Assignability">assignable</a>
  5320  		to the corresponding element of the function's result type.
  5321  <pre>
  5322  func simpleF() int {
  5323  	return 2
  5324  }
  5325  
  5326  func complexF1() (re float64, im float64) {
  5327  	return -7.0, -4.0
  5328  }
  5329  </pre>
  5330  	</li>
  5331  	<li>The expression list in the "return" statement may be a single
  5332  		call to a multi-valued function. The effect is as if each value
  5333  		returned from that function were assigned to a temporary
  5334  		variable with the type of the respective value, followed by a
  5335  		"return" statement listing these variables, at which point the
  5336  		rules of the previous case apply.
  5337  <pre>
  5338  func complexF2() (re float64, im float64) {
  5339  	return complexF1()
  5340  }
  5341  </pre>
  5342  	</li>
  5343  	<li>The expression list may be empty if the function's result
  5344  		type specifies names for its <a href="#Function_types">result parameters</a>.
  5345  		The result parameters act as ordinary local variables
  5346  		and the function may assign values to them as necessary.
  5347  		The "return" statement returns the values of these variables.
  5348  <pre>
  5349  func complexF3() (re float64, im float64) {
  5350  	re = 7.0
  5351  	im = 4.0
  5352  	return
  5353  }
  5354  
  5355  func (devnull) Write(p []byte) (n int, _ error) {
  5356  	n = len(p)
  5357  	return
  5358  }
  5359  </pre>
  5360  	</li>
  5361  </ol>
  5362  
  5363  <p>
  5364  Regardless of how they are declared, all the result values are initialized to
  5365  the <a href="#The_zero_value">zero values</a> for their type upon entry to the
  5366  function. A "return" statement that specifies results sets the result parameters before
  5367  any deferred functions are executed.
  5368  </p>
  5369  
  5370  <p>
  5371  Implementation restriction: A compiler may disallow an empty expression list
  5372  in a "return" statement if a different entity (constant, type, or variable)
  5373  with the same name as a result parameter is in
  5374  <a href="#Declarations_and_scope">scope</a> at the place of the return.
  5375  </p>
  5376  
  5377  <pre>
  5378  func f(n int) (res int, err error) {
  5379  	if _, err := f(n-1); err != nil {
  5380  		return  // invalid return statement: err is shadowed
  5381  	}
  5382  	return
  5383  }
  5384  </pre>
  5385  
  5386  <h3 id="Break_statements">Break statements</h3>
  5387  
  5388  <p>
  5389  A "break" statement terminates execution of the innermost
  5390  <a href="#For_statements">"for"</a>,
  5391  <a href="#Switch_statements">"switch"</a>, or
  5392  <a href="#Select_statements">"select"</a> statement
  5393  within the same function.
  5394  </p>
  5395  
  5396  <pre class="ebnf">
  5397  BreakStmt = "break" [ Label ] .
  5398  </pre>
  5399  
  5400  <p>
  5401  If there is a label, it must be that of an enclosing
  5402  "for", "switch", or "select" statement,
  5403  and that is the one whose execution terminates.
  5404  </p>
  5405  
  5406  <pre>
  5407  OuterLoop:
  5408  	for i = 0; i &lt; n; i++ {
  5409  		for j = 0; j &lt; m; j++ {
  5410  			switch a[i][j] {
  5411  			case nil:
  5412  				state = Error
  5413  				break OuterLoop
  5414  			case item:
  5415  				state = Found
  5416  				break OuterLoop
  5417  			}
  5418  		}
  5419  	}
  5420  </pre>
  5421  
  5422  <h3 id="Continue_statements">Continue statements</h3>
  5423  
  5424  <p>
  5425  A "continue" statement begins the next iteration of the
  5426  innermost <a href="#For_statements">"for" loop</a> at its post statement.
  5427  The "for" loop must be within the same function.
  5428  </p>
  5429  
  5430  <pre class="ebnf">
  5431  ContinueStmt = "continue" [ Label ] .
  5432  </pre>
  5433  
  5434  <p>
  5435  If there is a label, it must be that of an enclosing
  5436  "for" statement, and that is the one whose execution
  5437  advances.
  5438  </p>
  5439  
  5440  <pre>
  5441  RowLoop:
  5442  	for y, row := range rows {
  5443  		for x, data := range row {
  5444  			if data == endOfRow {
  5445  				continue RowLoop
  5446  			}
  5447  			row[x] = data + bias(x, y)
  5448  		}
  5449  	}
  5450  </pre>
  5451  
  5452  <h3 id="Goto_statements">Goto statements</h3>
  5453  
  5454  <p>
  5455  A "goto" statement transfers control to the statement with the corresponding label
  5456  within the same function.
  5457  </p>
  5458  
  5459  <pre class="ebnf">
  5460  GotoStmt = "goto" Label .
  5461  </pre>
  5462  
  5463  <pre>
  5464  goto Error
  5465  </pre>
  5466  
  5467  <p>
  5468  Executing the "goto" statement must not cause any variables to come into
  5469  <a href="#Declarations_and_scope">scope</a> that were not already in scope at the point of the goto.
  5470  For instance, this example:
  5471  </p>
  5472  
  5473  <pre>
  5474  	goto L  // BAD
  5475  	v := 3
  5476  L:
  5477  </pre>
  5478  
  5479  <p>
  5480  is erroneous because the jump to label <code>L</code> skips
  5481  the creation of <code>v</code>.
  5482  </p>
  5483  
  5484  <p>
  5485  A "goto" statement outside a <a href="#Blocks">block</a> cannot jump to a label inside that block.
  5486  For instance, this example:
  5487  </p>
  5488  
  5489  <pre>
  5490  if n%2 == 1 {
  5491  	goto L1
  5492  }
  5493  for n &gt; 0 {
  5494  	f()
  5495  	n--
  5496  L1:
  5497  	f()
  5498  	n--
  5499  }
  5500  </pre>
  5501  
  5502  <p>
  5503  is erroneous because the label <code>L1</code> is inside
  5504  the "for" statement's block but the <code>goto</code> is not.
  5505  </p>
  5506  
  5507  <h3 id="Fallthrough_statements">Fallthrough statements</h3>
  5508  
  5509  <p>
  5510  A "fallthrough" statement transfers control to the first statement of the
  5511  next case clause in an <a href="#Expression_switches">expression "switch" statement</a>.
  5512  It may be used only as the final non-empty statement in such a clause.
  5513  </p>
  5514  
  5515  <pre class="ebnf">
  5516  FallthroughStmt = "fallthrough" .
  5517  </pre>
  5518  
  5519  
  5520  <h3 id="Defer_statements">Defer statements</h3>
  5521  
  5522  <p>
  5523  A "defer" statement invokes a function whose execution is deferred
  5524  to the moment the surrounding function returns, either because the
  5525  surrounding function executed a <a href="#Return_statements">return statement</a>,
  5526  reached the end of its <a href="#Function_declarations">function body</a>,
  5527  or because the corresponding goroutine is <a href="#Handling_panics">panicking</a>.
  5528  </p>
  5529  
  5530  <pre class="ebnf">
  5531  DeferStmt = "defer" Expression .
  5532  </pre>
  5533  
  5534  <p>
  5535  The expression must be a function or method call; it cannot be parenthesized.
  5536  Calls of built-in functions are restricted as for
  5537  <a href="#Expression_statements">expression statements</a>.
  5538  </p>
  5539  
  5540  <p>
  5541  Each time a "defer" statement
  5542  executes, the function value and parameters to the call are
  5543  <a href="#Calls">evaluated as usual</a>
  5544  and saved anew but the actual function is not invoked.
  5545  Instead, deferred functions are invoked immediately before
  5546  the surrounding function returns, in the reverse order
  5547  they were deferred.
  5548  If a deferred function value evaluates
  5549  to <code>nil</code>, execution <a href="#Handling_panics">panics</a>
  5550  when the function is invoked, not when the "defer" statement is executed.
  5551  </p>
  5552  
  5553  <p>
  5554  For instance, if the deferred function is
  5555  a <a href="#Function_literals">function literal</a> and the surrounding
  5556  function has <a href="#Function_types">named result parameters</a> that
  5557  are in scope within the literal, the deferred function may access and modify
  5558  the result parameters before they are returned.
  5559  If the deferred function has any return values, they are discarded when
  5560  the function completes.
  5561  (See also the section on <a href="#Handling_panics">handling panics</a>.)
  5562  </p>
  5563  
  5564  <pre>
  5565  lock(l)
  5566  defer unlock(l)  // unlocking happens before surrounding function returns
  5567  
  5568  // prints 3 2 1 0 before surrounding function returns
  5569  for i := 0; i &lt;= 3; i++ {
  5570  	defer fmt.Print(i)
  5571  }
  5572  
  5573  // f returns 1
  5574  func f() (result int) {
  5575  	defer func() {
  5576  		result++
  5577  	}()
  5578  	return 0
  5579  }
  5580  </pre>
  5581  
  5582  <h2 id="Built-in_functions">Built-in functions</h2>
  5583  
  5584  <p>
  5585  Built-in functions are
  5586  <a href="#Predeclared_identifiers">predeclared</a>.
  5587  They are called like any other function but some of them
  5588  accept a type instead of an expression as the first argument.
  5589  </p>
  5590  
  5591  <p>
  5592  The built-in functions do not have standard Go types,
  5593  so they can only appear in <a href="#Calls">call expressions</a>;
  5594  they cannot be used as function values.
  5595  </p>
  5596  
  5597  <h3 id="Close">Close</h3>
  5598  
  5599  <p>
  5600  For a channel <code>c</code>, the built-in function <code>close(c)</code>
  5601  records that no more values will be sent on the channel.
  5602  It is an error if <code>c</code> is a receive-only channel.
  5603  Sending to or closing a closed channel causes a <a href="#Run_time_panics">run-time panic</a>.
  5604  Closing the nil channel also causes a <a href="#Run_time_panics">run-time panic</a>.
  5605  After calling <code>close</code>, and after any previously
  5606  sent values have been received, receive operations will return
  5607  the zero value for the channel's type without blocking.
  5608  The multi-valued <a href="#Receive_operator">receive operation</a>
  5609  returns a received value along with an indication of whether the channel is closed.
  5610  </p>
  5611  
  5612  
  5613  <h3 id="Length_and_capacity">Length and capacity</h3>
  5614  
  5615  <p>
  5616  The built-in functions <code>len</code> and <code>cap</code> take arguments
  5617  of various types and return a result of type <code>int</code>.
  5618  The implementation guarantees that the result always fits into an <code>int</code>.
  5619  </p>
  5620  
  5621  <pre class="grammar">
  5622  Call      Argument type    Result
  5623  
  5624  len(s)    string type      string length in bytes
  5625            [n]T, *[n]T      array length (== n)
  5626            []T              slice length
  5627            map[K]T          map length (number of defined keys)
  5628            chan T           number of elements queued in channel buffer
  5629  
  5630  cap(s)    [n]T, *[n]T      array length (== n)
  5631            []T              slice capacity
  5632            chan T           channel buffer capacity
  5633  </pre>
  5634  
  5635  <p>
  5636  The capacity of a slice is the number of elements for which there is
  5637  space allocated in the underlying array.
  5638  At any time the following relationship holds:
  5639  </p>
  5640  
  5641  <pre>
  5642  0 &lt;= len(s) &lt;= cap(s)
  5643  </pre>
  5644  
  5645  <p>
  5646  The length of a <code>nil</code> slice, map or channel is 0.
  5647  The capacity of a <code>nil</code> slice or channel is 0.
  5648  </p>
  5649  
  5650  <p>
  5651  The expression <code>len(s)</code> is <a href="#Constants">constant</a> if
  5652  <code>s</code> is a string constant. The expressions <code>len(s)</code> and
  5653  <code>cap(s)</code> are constants if the type of <code>s</code> is an array
  5654  or pointer to an array and the expression <code>s</code> does not contain
  5655  <a href="#Receive_operator">channel receives</a> or (non-constant)
  5656  <a href="#Calls">function calls</a>; in this case <code>s</code> is not evaluated.
  5657  Otherwise, invocations of <code>len</code> and <code>cap</code> are not
  5658  constant and <code>s</code> is evaluated.
  5659  </p>
  5660  
  5661  <pre>
  5662  const (
  5663  	c1 = imag(2i)                    // imag(2i) = 2.0 is a constant
  5664  	c2 = len([10]float64{2})         // [10]float64{2} contains no function calls
  5665  	c3 = len([10]float64{c1})        // [10]float64{c1} contains no function calls
  5666  	c4 = len([10]float64{imag(2i)})  // imag(2i) is a constant and no function call is issued
  5667  	c5 = len([10]float64{imag(z)})   // invalid: imag(z) is a (non-constant) function call
  5668  )
  5669  var z complex128
  5670  </pre>
  5671  
  5672  <h3 id="Allocation">Allocation</h3>
  5673  
  5674  <p>
  5675  The built-in function <code>new</code> takes a type <code>T</code>,
  5676  allocates storage for a <a href="#Variables">variable</a> of that type
  5677  at run time, and returns a value of type <code>*T</code>
  5678  <a href="#Pointer_types">pointing</a> to it.
  5679  The variable is initialized as described in the section on
  5680  <a href="#The_zero_value">initial values</a>.
  5681  </p>
  5682  
  5683  <pre class="grammar">
  5684  new(T)
  5685  </pre>
  5686  
  5687  <p>
  5688  For instance
  5689  </p>
  5690  
  5691  <pre>
  5692  type S struct { a int; b float64 }
  5693  new(S)
  5694  </pre>
  5695  
  5696  <p>
  5697  allocates storage for a variable of type <code>S</code>,
  5698  initializes it (<code>a=0</code>, <code>b=0.0</code>),
  5699  and returns a value of type <code>*S</code> containing the address
  5700  of the location.
  5701  </p>
  5702  
  5703  <h3 id="Making_slices_maps_and_channels">Making slices, maps and channels</h3>
  5704  
  5705  <p>
  5706  The built-in function <code>make</code> takes a type <code>T</code>,
  5707  which must be a slice, map or channel type,
  5708  optionally followed by a type-specific list of expressions.
  5709  It returns a value of type <code>T</code> (not <code>*T</code>).
  5710  The memory is initialized as described in the section on
  5711  <a href="#The_zero_value">initial values</a>.
  5712  </p>
  5713  
  5714  <pre class="grammar">
  5715  Call             Type T     Result
  5716  
  5717  make(T, n)       slice      slice of type T with length n and capacity n
  5718  make(T, n, m)    slice      slice of type T with length n and capacity m
  5719  
  5720  make(T)          map        map of type T
  5721  make(T, n)       map        map of type T with initial space for approximately n elements
  5722  
  5723  make(T)          channel    unbuffered channel of type T
  5724  make(T, n)       channel    buffered channel of type T, buffer size n
  5725  </pre>
  5726  
  5727  
  5728  <p>
  5729  Each of the size arguments <code>n</code> and <code>m</code> must be of integer type
  5730  or an untyped <a href="#Constants">constant</a>.
  5731  A constant size argument must be non-negative and <a href="#Representability">representable</a>
  5732  by a value of type <code>int</code>; if it is an untyped constant it is given type <code>int</code>.
  5733  If both <code>n</code> and <code>m</code> are provided and are constant, then
  5734  <code>n</code> must be no larger than <code>m</code>.
  5735  If <code>n</code> is negative or larger than <code>m</code> at run time,
  5736  a <a href="#Run_time_panics">run-time panic</a> occurs.
  5737  </p>
  5738  
  5739  <pre>
  5740  s := make([]int, 10, 100)       // slice with len(s) == 10, cap(s) == 100
  5741  s := make([]int, 1e3)           // slice with len(s) == cap(s) == 1000
  5742  s := make([]int, 1&lt;&lt;63)         // illegal: len(s) is not representable by a value of type int
  5743  s := make([]int, 10, 0)         // illegal: len(s) > cap(s)
  5744  c := make(chan int, 10)         // channel with a buffer size of 10
  5745  m := make(map[string]int, 100)  // map with initial space for approximately 100 elements
  5746  </pre>
  5747  
  5748  <p>
  5749  Calling <code>make</code> with a map type and size hint <code>n</code> will
  5750  create a map with initial space to hold <code>n</code> map elements.
  5751  The precise behavior is implementation-dependent.
  5752  </p>
  5753  
  5754  
  5755  <h3 id="Appending_and_copying_slices">Appending to and copying slices</h3>
  5756  
  5757  <p>
  5758  The built-in functions <code>append</code> and <code>copy</code> assist in
  5759  common slice operations.
  5760  For both functions, the result is independent of whether the memory referenced
  5761  by the arguments overlaps.
  5762  </p>
  5763  
  5764  <p>
  5765  The <a href="#Function_types">variadic</a> function <code>append</code>
  5766  appends zero or more values <code>x</code>
  5767  to <code>s</code> of type <code>S</code>, which must be a slice type, and
  5768  returns the resulting slice, also of type <code>S</code>.
  5769  The values <code>x</code> are passed to a parameter of type <code>...T</code>
  5770  where <code>T</code> is the <a href="#Slice_types">element type</a> of
  5771  <code>S</code> and the respective
  5772  <a href="#Passing_arguments_to_..._parameters">parameter passing rules</a> apply.
  5773  As a special case, <code>append</code> also accepts a first argument
  5774  assignable to type <code>[]byte</code> with a second argument of
  5775  string type followed by <code>...</code>. This form appends the
  5776  bytes of the string.
  5777  </p>
  5778  
  5779  <pre class="grammar">
  5780  append(s S, x ...T) S  // T is the element type of S
  5781  </pre>
  5782  
  5783  <p>
  5784  If the capacity of <code>s</code> is not large enough to fit the additional
  5785  values, <code>append</code> allocates a new, sufficiently large underlying
  5786  array that fits both the existing slice elements and the additional values.
  5787  Otherwise, <code>append</code> re-uses the underlying array.
  5788  </p>
  5789  
  5790  <pre>
  5791  s0 := []int{0, 0}
  5792  s1 := append(s0, 2)                // append a single element     s1 == []int{0, 0, 2}
  5793  s2 := append(s1, 3, 5, 7)          // append multiple elements    s2 == []int{0, 0, 2, 3, 5, 7}
  5794  s3 := append(s2, s0...)            // append a slice              s3 == []int{0, 0, 2, 3, 5, 7, 0, 0}
  5795  s4 := append(s3[3:6], s3[2:]...)   // append overlapping slice    s4 == []int{3, 5, 7, 2, 3, 5, 7, 0, 0}
  5796  
  5797  var t []interface{}
  5798  t = append(t, 42, 3.1415, "foo")   //                             t == []interface{}{42, 3.1415, "foo"}
  5799  
  5800  var b []byte
  5801  b = append(b, "bar"...)            // append string contents      b == []byte{'b', 'a', 'r' }
  5802  </pre>
  5803  
  5804  <p>
  5805  The function <code>copy</code> copies slice elements from
  5806  a source <code>src</code> to a destination <code>dst</code> and returns the
  5807  number of elements copied.
  5808  Both arguments must have <a href="#Type_identity">identical</a> element type <code>T</code> and must be
  5809  <a href="#Assignability">assignable</a> to a slice of type <code>[]T</code>.
  5810  The number of elements copied is the minimum of
  5811  <code>len(src)</code> and <code>len(dst)</code>.
  5812  As a special case, <code>copy</code> also accepts a destination argument assignable
  5813  to type <code>[]byte</code> with a source argument of a string type.
  5814  This form copies the bytes from the string into the byte slice.
  5815  </p>
  5816  
  5817  <pre class="grammar">
  5818  copy(dst, src []T) int
  5819  copy(dst []byte, src string) int
  5820  </pre>
  5821  
  5822  <p>
  5823  Examples:
  5824  </p>
  5825  
  5826  <pre>
  5827  var a = [...]int{0, 1, 2, 3, 4, 5, 6, 7}
  5828  var s = make([]int, 6)
  5829  var b = make([]byte, 5)
  5830  n1 := copy(s, a[0:])            // n1 == 6, s == []int{0, 1, 2, 3, 4, 5}
  5831  n2 := copy(s, s[2:])            // n2 == 4, s == []int{2, 3, 4, 5, 4, 5}
  5832  n3 := copy(b, "Hello, World!")  // n3 == 5, b == []byte("Hello")
  5833  </pre>
  5834  
  5835  
  5836  <h3 id="Deletion_of_map_elements">Deletion of map elements</h3>
  5837  
  5838  <p>
  5839  The built-in function <code>delete</code> removes the element with key
  5840  <code>k</code> from a <a href="#Map_types">map</a> <code>m</code>. The
  5841  type of <code>k</code> must be <a href="#Assignability">assignable</a>
  5842  to the key type of <code>m</code>.
  5843  </p>
  5844  
  5845  <pre class="grammar">
  5846  delete(m, k)  // remove element m[k] from map m
  5847  </pre>
  5848  
  5849  <p>
  5850  If the map <code>m</code> is <code>nil</code> or the element <code>m[k]</code>
  5851  does not exist, <code>delete</code> is a no-op.
  5852  </p>
  5853  
  5854  
  5855  <h3 id="Complex_numbers">Manipulating complex numbers</h3>
  5856  
  5857  <p>
  5858  Three functions assemble and disassemble complex numbers.
  5859  The built-in function <code>complex</code> constructs a complex
  5860  value from a floating-point real and imaginary part, while
  5861  <code>real</code> and <code>imag</code>
  5862  extract the real and imaginary parts of a complex value.
  5863  </p>
  5864  
  5865  <pre class="grammar">
  5866  complex(realPart, imaginaryPart floatT) complexT
  5867  real(complexT) floatT
  5868  imag(complexT) floatT
  5869  </pre>
  5870  
  5871  <p>
  5872  The type of the arguments and return value correspond.
  5873  For <code>complex</code>, the two arguments must be of the same
  5874  floating-point type and the return type is the complex type
  5875  with the corresponding floating-point constituents:
  5876  <code>complex64</code> for <code>float32</code> arguments, and
  5877  <code>complex128</code> for <code>float64</code> arguments.
  5878  If one of the arguments evaluates to an untyped constant, it is first
  5879  <a href="#Conversions">converted</a> to the type of the other argument.
  5880  If both arguments evaluate to untyped constants, they must be non-complex
  5881  numbers or their imaginary parts must be zero, and the return value of
  5882  the function is an untyped complex constant.
  5883  </p>
  5884  
  5885  <p>
  5886  For <code>real</code> and <code>imag</code>, the argument must be
  5887  of complex type, and the return type is the corresponding floating-point
  5888  type: <code>float32</code> for a <code>complex64</code> argument, and
  5889  <code>float64</code> for a <code>complex128</code> argument.
  5890  If the argument evaluates to an untyped constant, it must be a number,
  5891  and the return value of the function is an untyped floating-point constant.
  5892  </p>
  5893  
  5894  <p>
  5895  The <code>real</code> and <code>imag</code> functions together form the inverse of
  5896  <code>complex</code>, so for a value <code>z</code> of a complex type <code>Z</code>,
  5897  <code>z&nbsp;==&nbsp;Z(complex(real(z),&nbsp;imag(z)))</code>.
  5898  </p>
  5899  
  5900  <p>
  5901  If the operands of these functions are all constants, the return
  5902  value is a constant.
  5903  </p>
  5904  
  5905  <pre>
  5906  var a = complex(2, -2)             // complex128
  5907  const b = complex(1.0, -1.4)       // untyped complex constant 1 - 1.4i
  5908  x := float32(math.Cos(math.Pi/2))  // float32
  5909  var c64 = complex(5, -x)           // complex64
  5910  var s uint = complex(1, 0)         // untyped complex constant 1 + 0i can be converted to uint
  5911  _ = complex(1, 2&lt;&lt;s)               // illegal: 2 assumes floating-point type, cannot shift
  5912  var rl = real(c64)                 // float32
  5913  var im = imag(a)                   // float64
  5914  const c = imag(b)                  // untyped constant -1.4
  5915  _ = imag(3 &lt;&lt; s)                   // illegal: 3 assumes complex type, cannot shift
  5916  </pre>
  5917  
  5918  <h3 id="Handling_panics">Handling panics</h3>
  5919  
  5920  <p> Two built-in functions, <code>panic</code> and <code>recover</code>,
  5921  assist in reporting and handling <a href="#Run_time_panics">run-time panics</a>
  5922  and program-defined error conditions.
  5923  </p>
  5924  
  5925  <pre class="grammar">
  5926  func panic(interface{})
  5927  func recover() interface{}
  5928  </pre>
  5929  
  5930  <p>
  5931  While executing a function <code>F</code>,
  5932  an explicit call to <code>panic</code> or a <a href="#Run_time_panics">run-time panic</a>
  5933  terminates the execution of <code>F</code>.
  5934  Any functions <a href="#Defer_statements">deferred</a> by <code>F</code>
  5935  are then executed as usual.
  5936  Next, any deferred functions run by <code>F's</code> caller are run,
  5937  and so on up to any deferred by the top-level function in the executing goroutine.
  5938  At that point, the program is terminated and the error
  5939  condition is reported, including the value of the argument to <code>panic</code>.
  5940  This termination sequence is called <i>panicking</i>.
  5941  </p>
  5942  
  5943  <pre>
  5944  panic(42)
  5945  panic("unreachable")
  5946  panic(Error("cannot parse"))
  5947  </pre>
  5948  
  5949  <p>
  5950  The <code>recover</code> function allows a program to manage behavior
  5951  of a panicking goroutine.
  5952  Suppose a function <code>G</code> defers a function <code>D</code> that calls
  5953  <code>recover</code> and a panic occurs in a function on the same goroutine in which <code>G</code>
  5954  is executing.
  5955  When the running of deferred functions reaches <code>D</code>,
  5956  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>.
  5957  If <code>D</code> returns normally, without starting a new
  5958  <code>panic</code>, the panicking sequence stops. In that case,
  5959  the state of functions called between <code>G</code> and the call to <code>panic</code>
  5960  is discarded, and normal execution resumes.
  5961  Any functions deferred by <code>G</code> before <code>D</code> are then run and <code>G</code>'s
  5962  execution terminates by returning to its caller.
  5963  </p>
  5964  
  5965  <p>
  5966  The return value of <code>recover</code> is <code>nil</code> if any of the following conditions holds:
  5967  </p>
  5968  <ul>
  5969  <li>
  5970  <code>panic</code>'s argument was <code>nil</code>;
  5971  </li>
  5972  <li>
  5973  the goroutine is not panicking;
  5974  </li>
  5975  <li>
  5976  <code>recover</code> was not called directly by a deferred function.
  5977  </li>
  5978  </ul>
  5979  
  5980  <p>
  5981  The <code>protect</code> function in the example below invokes
  5982  the function argument <code>g</code> and protects callers from
  5983  run-time panics raised by <code>g</code>.
  5984  </p>
  5985  
  5986  <pre>
  5987  func protect(g func()) {
  5988  	defer func() {
  5989  		log.Println("done")  // Println executes normally even if there is a panic
  5990  		if x := recover(); x != nil {
  5991  			log.Printf("run time panic: %v", x)
  5992  		}
  5993  	}()
  5994  	log.Println("start")
  5995  	g()
  5996  }
  5997  </pre>
  5998  
  5999  
  6000  <h3 id="Bootstrapping">Bootstrapping</h3>
  6001  
  6002  <p>
  6003  Current implementations provide several built-in functions useful during
  6004  bootstrapping. These functions are documented for completeness but are not
  6005  guaranteed to stay in the language. They do not return a result.
  6006  </p>
  6007  
  6008  <pre class="grammar">
  6009  Function   Behavior
  6010  
  6011  print      prints all arguments; formatting of arguments is implementation-specific
  6012  println    like print but prints spaces between arguments and a newline at the end
  6013  </pre>
  6014  
  6015  <p>
  6016  Implementation restriction: <code>print</code> and <code>println</code> need not
  6017  accept arbitrary argument types, but printing of boolean, numeric, and string
  6018  <a href="#Types">types</a> must be supported.
  6019  </p>
  6020  
  6021  <h2 id="Packages">Packages</h2>
  6022  
  6023  <p>
  6024  Go programs are constructed by linking together <i>packages</i>.
  6025  A package in turn is constructed from one or more source files
  6026  that together declare constants, types, variables and functions
  6027  belonging to the package and which are accessible in all files
  6028  of the same package. Those elements may be
  6029  <a href="#Exported_identifiers">exported</a> and used in another package.
  6030  </p>
  6031  
  6032  <h3 id="Source_file_organization">Source file organization</h3>
  6033  
  6034  <p>
  6035  Each source file consists of a package clause defining the package
  6036  to which it belongs, followed by a possibly empty set of import
  6037  declarations that declare packages whose contents it wishes to use,
  6038  followed by a possibly empty set of declarations of functions,
  6039  types, variables, and constants.
  6040  </p>
  6041  
  6042  <pre class="ebnf">
  6043  SourceFile       = PackageClause ";" { ImportDecl ";" } { TopLevelDecl ";" } .
  6044  </pre>
  6045  
  6046  <h3 id="Package_clause">Package clause</h3>
  6047  
  6048  <p>
  6049  A package clause begins each source file and defines the package
  6050  to which the file belongs.
  6051  </p>
  6052  
  6053  <pre class="ebnf">
  6054  PackageClause  = "package" PackageName .
  6055  PackageName    = identifier .
  6056  </pre>
  6057  
  6058  <p>
  6059  The PackageName must not be the <a href="#Blank_identifier">blank identifier</a>.
  6060  </p>
  6061  
  6062  <pre>
  6063  package math
  6064  </pre>
  6065  
  6066  <p>
  6067  A set of files sharing the same PackageName form the implementation of a package.
  6068  An implementation may require that all source files for a package inhabit the same directory.
  6069  </p>
  6070  
  6071  <h3 id="Import_declarations">Import declarations</h3>
  6072  
  6073  <p>
  6074  An import declaration states that the source file containing the declaration
  6075  depends on functionality of the <i>imported</i> package
  6076  (<a href="#Program_initialization_and_execution">§Program initialization and execution</a>)
  6077  and enables access to <a href="#Exported_identifiers">exported</a> identifiers
  6078  of that package.
  6079  The import names an identifier (PackageName) to be used for access and an ImportPath
  6080  that specifies the package to be imported.
  6081  </p>
  6082  
  6083  <pre class="ebnf">
  6084  ImportDecl       = "import" ( ImportSpec | "(" { ImportSpec ";" } ")" ) .
  6085  ImportSpec       = [ "." | PackageName ] ImportPath .
  6086  ImportPath       = string_lit .
  6087  </pre>
  6088  
  6089  <p>
  6090  The PackageName is used in <a href="#Qualified_identifiers">qualified identifiers</a>
  6091  to access exported identifiers of the package within the importing source file.
  6092  It is declared in the <a href="#Blocks">file block</a>.
  6093  If the PackageName is omitted, it defaults to the identifier specified in the
  6094  <a href="#Package_clause">package clause</a> of the imported package.
  6095  If an explicit period (<code>.</code>) appears instead of a name, all the
  6096  package's exported identifiers declared in that package's
  6097  <a href="#Blocks">package block</a> will be declared in the importing source
  6098  file's file block and must be accessed without a qualifier.
  6099  </p>
  6100  
  6101  <p>
  6102  The interpretation of the ImportPath is implementation-dependent but
  6103  it is typically a substring of the full file name of the compiled
  6104  package and may be relative to a repository of installed packages.
  6105  </p>
  6106  
  6107  <p>
  6108  Implementation restriction: A compiler may restrict ImportPaths to
  6109  non-empty strings using only characters belonging to
  6110  <a href="http://www.unicode.org/versions/Unicode6.3.0/">Unicode's</a>
  6111  L, M, N, P, and S general categories (the Graphic characters without
  6112  spaces) and may also exclude the characters
  6113  <code>!"#$%&amp;'()*,:;&lt;=&gt;?[\]^`{|}</code>
  6114  and the Unicode replacement character U+FFFD.
  6115  </p>
  6116  
  6117  <p>
  6118  Assume we have compiled a package containing the package clause
  6119  <code>package math</code>, which exports function <code>Sin</code>, and
  6120  installed the compiled package in the file identified by
  6121  <code>"lib/math"</code>.
  6122  This table illustrates how <code>Sin</code> is accessed in files
  6123  that import the package after the
  6124  various types of import declaration.
  6125  </p>
  6126  
  6127  <pre class="grammar">
  6128  Import declaration          Local name of Sin
  6129  
  6130  import   "lib/math"         math.Sin
  6131  import m "lib/math"         m.Sin
  6132  import . "lib/math"         Sin
  6133  </pre>
  6134  
  6135  <p>
  6136  An import declaration declares a dependency relation between
  6137  the importing and imported package.
  6138  It is illegal for a package to import itself, directly or indirectly,
  6139  or to directly import a package without
  6140  referring to any of its exported identifiers. To import a package solely for
  6141  its side-effects (initialization), use the <a href="#Blank_identifier">blank</a>
  6142  identifier as explicit package name:
  6143  </p>
  6144  
  6145  <pre>
  6146  import _ "lib/math"
  6147  </pre>
  6148  
  6149  
  6150  <h3 id="An_example_package">An example package</h3>
  6151  
  6152  <p>
  6153  Here is a complete Go package that implements a concurrent prime sieve.
  6154  </p>
  6155  
  6156  <pre>
  6157  package main
  6158  
  6159  import "fmt"
  6160  
  6161  // Send the sequence 2, 3, 4, … to channel 'ch'.
  6162  func generate(ch chan&lt;- int) {
  6163  	for i := 2; ; i++ {
  6164  		ch &lt;- i  // Send 'i' to channel 'ch'.
  6165  	}
  6166  }
  6167  
  6168  // Copy the values from channel 'src' to channel 'dst',
  6169  // removing those divisible by 'prime'.
  6170  func filter(src &lt;-chan int, dst chan&lt;- int, prime int) {
  6171  	for i := range src {  // Loop over values received from 'src'.
  6172  		if i%prime != 0 {
  6173  			dst &lt;- i  // Send 'i' to channel 'dst'.
  6174  		}
  6175  	}
  6176  }
  6177  
  6178  // The prime sieve: Daisy-chain filter processes together.
  6179  func sieve() {
  6180  	ch := make(chan int)  // Create a new channel.
  6181  	go generate(ch)       // Start generate() as a subprocess.
  6182  	for {
  6183  		prime := &lt;-ch
  6184  		fmt.Print(prime, "\n")
  6185  		ch1 := make(chan int)
  6186  		go filter(ch, ch1, prime)
  6187  		ch = ch1
  6188  	}
  6189  }
  6190  
  6191  func main() {
  6192  	sieve()
  6193  }
  6194  </pre>
  6195  
  6196  <h2 id="Program_initialization_and_execution">Program initialization and execution</h2>
  6197  
  6198  <h3 id="The_zero_value">The zero value</h3>
  6199  <p>
  6200  When storage is allocated for a <a href="#Variables">variable</a>,
  6201  either through a declaration or a call of <code>new</code>, or when
  6202  a new value is created, either through a composite literal or a call
  6203  of <code>make</code>,
  6204  and no explicit initialization is provided, the variable or value is
  6205  given a default value.  Each element of such a variable or value is
  6206  set to the <i>zero value</i> for its type: <code>false</code> for booleans,
  6207  <code>0</code> for numeric types, <code>""</code>
  6208  for strings, and <code>nil</code> for pointers, functions, interfaces, slices, channels, and maps.
  6209  This initialization is done recursively, so for instance each element of an
  6210  array of structs will have its fields zeroed if no value is specified.
  6211  </p>
  6212  <p>
  6213  These two simple declarations are equivalent:
  6214  </p>
  6215  
  6216  <pre>
  6217  var i int
  6218  var i int = 0
  6219  </pre>
  6220  
  6221  <p>
  6222  After
  6223  </p>
  6224  
  6225  <pre>
  6226  type T struct { i int; f float64; next *T }
  6227  t := new(T)
  6228  </pre>
  6229  
  6230  <p>
  6231  the following holds:
  6232  </p>
  6233  
  6234  <pre>
  6235  t.i == 0
  6236  t.f == 0.0
  6237  t.next == nil
  6238  </pre>
  6239  
  6240  <p>
  6241  The same would also be true after
  6242  </p>
  6243  
  6244  <pre>
  6245  var t T
  6246  </pre>
  6247  
  6248  <h3 id="Package_initialization">Package initialization</h3>
  6249  
  6250  <p>
  6251  Within a package, package-level variables are initialized in
  6252  <i>declaration order</i> but after any of the variables
  6253  they <i>depend</i> on.
  6254  </p>
  6255  
  6256  <p>
  6257  More precisely, a package-level variable is considered <i>ready for
  6258  initialization</i> if it is not yet initialized and either has
  6259  no <a href="#Variable_declarations">initialization expression</a> or
  6260  its initialization expression has no dependencies on uninitialized variables.
  6261  Initialization proceeds by repeatedly initializing the next package-level
  6262  variable that is earliest in declaration order and ready for initialization,
  6263  until there are no variables ready for initialization.
  6264  </p>
  6265  
  6266  <p>
  6267  If any variables are still uninitialized when this
  6268  process ends, those variables are part of one or more initialization cycles,
  6269  and the program is not valid.
  6270  </p>
  6271  
  6272  <p>
  6273  The declaration order of variables declared in multiple files is determined
  6274  by the order in which the files are presented to the compiler: Variables
  6275  declared in the first file are declared before any of the variables declared
  6276  in the second file, and so on.
  6277  </p>
  6278  
  6279  <p>
  6280  Dependency analysis does not rely on the actual values of the
  6281  variables, only on lexical <i>references</i> to them in the source,
  6282  analyzed transitively. For instance, if a variable <code>x</code>'s
  6283  initialization expression refers to a function whose body refers to
  6284  variable <code>y</code> then <code>x</code> depends on <code>y</code>.
  6285  Specifically:
  6286  </p>
  6287  
  6288  <ul>
  6289  <li>
  6290  A reference to a variable or function is an identifier denoting that
  6291  variable or function.
  6292  </li>
  6293  
  6294  <li>
  6295  A reference to a method <code>m</code> is a
  6296  <a href="#Method_values">method value</a> or
  6297  <a href="#Method_expressions">method expression</a> of the form
  6298  <code>t.m</code>, where the (static) type of <code>t</code> is
  6299  not an interface type, and the method <code>m</code> is in the
  6300  <a href="#Method_sets">method set</a> of <code>t</code>.
  6301  It is immaterial whether the resulting function value
  6302  <code>t.m</code> is invoked.
  6303  </li>
  6304  
  6305  <li>
  6306  A variable, function, or method <code>x</code> depends on a variable
  6307  <code>y</code> if <code>x</code>'s initialization expression or body
  6308  (for functions and methods) contains a reference to <code>y</code>
  6309  or to a function or method that depends on <code>y</code>.
  6310  </li>
  6311  </ul>
  6312  
  6313  <p>
  6314  Dependency analysis is performed per package; only references referring
  6315  to variables, functions, and methods declared in the current package
  6316  are considered.
  6317  </p>
  6318  
  6319  <p>
  6320  For example, given the declarations
  6321  </p>
  6322  
  6323  <pre>
  6324  var (
  6325  	a = c + b
  6326  	b = f()
  6327  	c = f()
  6328  	d = 3
  6329  )
  6330  
  6331  func f() int {
  6332  	d++
  6333  	return d
  6334  }
  6335  </pre>
  6336  
  6337  <p>
  6338  the initialization order is <code>d</code>, <code>b</code>, <code>c</code>, <code>a</code>.
  6339  </p>
  6340  
  6341  <p>
  6342  Variables may also be initialized using functions named <code>init</code>
  6343  declared in the package block, with no arguments and no result parameters.
  6344  </p>
  6345  
  6346  <pre>
  6347  func init() { … }
  6348  </pre>
  6349  
  6350  <p>
  6351  Multiple such functions may be defined per package, even within a single
  6352  source file. In the package block, the <code>init</code> identifier can
  6353  be used only to declare <code>init</code> functions, yet the identifier
  6354  itself is not <a href="#Declarations_and_scope">declared</a>. Thus
  6355  <code>init</code> functions cannot be referred to from anywhere
  6356  in a program.
  6357  </p>
  6358  
  6359  <p>
  6360  A package with no imports is initialized by assigning initial values
  6361  to all its package-level variables followed by calling all <code>init</code>
  6362  functions in the order they appear in the source, possibly in multiple files,
  6363  as presented to the compiler.
  6364  If a package has imports, the imported packages are initialized
  6365  before initializing the package itself. If multiple packages import
  6366  a package, the imported package will be initialized only once.
  6367  The importing of packages, by construction, guarantees that there
  6368  can be no cyclic initialization dependencies.
  6369  </p>
  6370  
  6371  <p>
  6372  Package initialization&mdash;variable initialization and the invocation of
  6373  <code>init</code> functions&mdash;happens in a single goroutine,
  6374  sequentially, one package at a time.
  6375  An <code>init</code> function may launch other goroutines, which can run
  6376  concurrently with the initialization code. However, initialization
  6377  always sequences
  6378  the <code>init</code> functions: it will not invoke the next one
  6379  until the previous one has returned.
  6380  </p>
  6381  
  6382  <p>
  6383  To ensure reproducible initialization behavior, build systems are encouraged
  6384  to present multiple files belonging to the same package in lexical file name
  6385  order to a compiler.
  6386  </p>
  6387  
  6388  
  6389  <h3 id="Program_execution">Program execution</h3>
  6390  <p>
  6391  A complete program is created by linking a single, unimported package
  6392  called the <i>main package</i> with all the packages it imports, transitively.
  6393  The main package must
  6394  have package name <code>main</code> and
  6395  declare a function <code>main</code> that takes no
  6396  arguments and returns no value.
  6397  </p>
  6398  
  6399  <pre>
  6400  func main() { … }
  6401  </pre>
  6402  
  6403  <p>
  6404  Program execution begins by initializing the main package and then
  6405  invoking the function <code>main</code>.
  6406  When that function invocation returns, the program exits.
  6407  It does not wait for other (non-<code>main</code>) goroutines to complete.
  6408  </p>
  6409  
  6410  <h2 id="Errors">Errors</h2>
  6411  
  6412  <p>
  6413  The predeclared type <code>error</code> is defined as
  6414  </p>
  6415  
  6416  <pre>
  6417  type error interface {
  6418  	Error() string
  6419  }
  6420  </pre>
  6421  
  6422  <p>
  6423  It is the conventional interface for representing an error condition,
  6424  with the nil value representing no error.
  6425  For instance, a function to read data from a file might be defined:
  6426  </p>
  6427  
  6428  <pre>
  6429  func Read(f *File, b []byte) (n int, err error)
  6430  </pre>
  6431  
  6432  <h2 id="Run_time_panics">Run-time panics</h2>
  6433  
  6434  <p>
  6435  Execution errors such as attempting to index an array out
  6436  of bounds trigger a <i>run-time panic</i> equivalent to a call of
  6437  the built-in function <a href="#Handling_panics"><code>panic</code></a>
  6438  with a value of the implementation-defined interface type <code>runtime.Error</code>.
  6439  That type satisfies the predeclared interface type
  6440  <a href="#Errors"><code>error</code></a>.
  6441  The exact error values that
  6442  represent distinct run-time error conditions are unspecified.
  6443  </p>
  6444  
  6445  <pre>
  6446  package runtime
  6447  
  6448  type Error interface {
  6449  	error
  6450  	// and perhaps other methods
  6451  }
  6452  </pre>
  6453  
  6454  <h2 id="System_considerations">System considerations</h2>
  6455  
  6456  <h3 id="Package_unsafe">Package <code>unsafe</code></h3>
  6457  
  6458  <p>
  6459  The built-in package <code>unsafe</code>, known to the compiler,
  6460  provides facilities for low-level programming including operations
  6461  that violate the type system. A package using <code>unsafe</code>
  6462  must be vetted manually for type safety and may not be portable.
  6463  The package provides the following interface:
  6464  </p>
  6465  
  6466  <pre class="grammar">
  6467  package unsafe
  6468  
  6469  type ArbitraryType int  // shorthand for an arbitrary Go type; it is not a real type
  6470  type Pointer *ArbitraryType
  6471  
  6472  func Alignof(variable ArbitraryType) uintptr
  6473  func Offsetof(selector ArbitraryType) uintptr
  6474  func Sizeof(variable ArbitraryType) uintptr
  6475  </pre>
  6476  
  6477  <p>
  6478  A <code>Pointer</code> is a <a href="#Pointer_types">pointer type</a> but a <code>Pointer</code>
  6479  value may not be <a href="#Address_operators">dereferenced</a>.
  6480  Any pointer or value of <a href="#Types">underlying type</a> <code>uintptr</code> can be converted to
  6481  a type of underlying type <code>Pointer</code> and vice versa.
  6482  The effect of converting between <code>Pointer</code> and <code>uintptr</code> is implementation-defined.
  6483  </p>
  6484  
  6485  <pre>
  6486  var f float64
  6487  bits = *(*uint64)(unsafe.Pointer(&amp;f))
  6488  
  6489  type ptr unsafe.Pointer
  6490  bits = *(*uint64)(ptr(&amp;f))
  6491  
  6492  var p ptr = nil
  6493  </pre>
  6494  
  6495  <p>
  6496  The functions <code>Alignof</code> and <code>Sizeof</code> take an expression <code>x</code>
  6497  of any type and return the alignment or size, respectively, of a hypothetical variable <code>v</code>
  6498  as if <code>v</code> was declared via <code>var v = x</code>.
  6499  </p>
  6500  <p>
  6501  The function <code>Offsetof</code> takes a (possibly parenthesized) <a href="#Selectors">selector</a>
  6502  <code>s.f</code>, denoting a field <code>f</code> of the struct denoted by <code>s</code>
  6503  or <code>*s</code>, and returns the field offset in bytes relative to the struct's address.
  6504  If <code>f</code> is an <a href="#Struct_types">embedded field</a>, it must be reachable
  6505  without pointer indirections through fields of the struct.
  6506  For a struct <code>s</code> with field <code>f</code>:
  6507  </p>
  6508  
  6509  <pre>
  6510  uintptr(unsafe.Pointer(&amp;s)) + unsafe.Offsetof(s.f) == uintptr(unsafe.Pointer(&amp;s.f))
  6511  </pre>
  6512  
  6513  <p>
  6514  Computer architectures may require memory addresses to be <i>aligned</i>;
  6515  that is, for addresses of a variable to be a multiple of a factor,
  6516  the variable's type's <i>alignment</i>.  The function <code>Alignof</code>
  6517  takes an expression denoting a variable of any type and returns the
  6518  alignment of the (type of the) variable in bytes.  For a variable
  6519  <code>x</code>:
  6520  </p>
  6521  
  6522  <pre>
  6523  uintptr(unsafe.Pointer(&amp;x)) % unsafe.Alignof(x) == 0
  6524  </pre>
  6525  
  6526  <p>
  6527  Calls to <code>Alignof</code>, <code>Offsetof</code>, and
  6528  <code>Sizeof</code> are compile-time constant expressions of type <code>uintptr</code>.
  6529  </p>
  6530  
  6531  <h3 id="Size_and_alignment_guarantees">Size and alignment guarantees</h3>
  6532  
  6533  <p>
  6534  For the <a href="#Numeric_types">numeric types</a>, the following sizes are guaranteed:
  6535  </p>
  6536  
  6537  <pre class="grammar">
  6538  type                                 size in bytes
  6539  
  6540  byte, uint8, int8                     1
  6541  uint16, int16                         2
  6542  uint32, int32, float32                4
  6543  uint64, int64, float64, complex64     8
  6544  complex128                           16
  6545  </pre>
  6546  
  6547  <p>
  6548  The following minimal alignment properties are guaranteed:
  6549  </p>
  6550  <ol>
  6551  <li>For a variable <code>x</code> of any type: <code>unsafe.Alignof(x)</code> is at least 1.
  6552  </li>
  6553  
  6554  <li>For a variable <code>x</code> of struct type: <code>unsafe.Alignof(x)</code> is the largest of
  6555     all the values <code>unsafe.Alignof(x.f)</code> for each field <code>f</code> of <code>x</code>, but at least 1.
  6556  </li>
  6557  
  6558  <li>For a variable <code>x</code> of array type: <code>unsafe.Alignof(x)</code> is the same as
  6559  	the alignment of a variable of the array's element type.
  6560  </li>
  6561  </ol>
  6562  
  6563  <p>
  6564  A struct or array type has size zero if it contains no fields (or elements, respectively) that have a size greater than zero. Two distinct zero-size variables may have the same address in memory.
  6565  </p>