github.com/euank/go@v0.0.0-20160829210321-495514729181/doc/go_spec.html (about)

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