github.com/hikaru7719/go@v0.0.0-20181025140707-c8b2ac68906a/doc/go_spec.html (about)

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