github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/doc/go_spec.html (about)

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