blob: fe35aaccb4a8a36585479481096a3be8e8dc5dd7 [file] [log] [blame]
Andrew Gerrand7cb21a72012-01-19 11:24:54 +11001<!--{
2 "Title": "The Go Programming Language Specification",
Robert Griesemer57c81ef2015-12-15 13:13:38 -08003 "Subtitle": "Version of December 15, 2015",
Andrew Gerrand48ba6fe2013-10-04 09:45:06 +10004 "Path": "/ref/spec"
Andrew Gerrand7cb21a72012-01-19 11:24:54 +11005}-->
Robert Griesemerdf49fb32008-08-28 17:47:53 -07006
Russ Cox7c4f7cc2009-08-20 11:11:03 -07007<h2 id="Introduction">Introduction</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07008
Robert Griesemerc2d55862009-02-19 16:49:10 -08009<p>
Rob Pike4501d342009-02-19 17:31:36 -080010This is a reference manual for the Go programming language. For
Andrew Gerrand43ad89d2014-07-25 10:28:39 +100011more information and other documents, see <a href="/">golang.org</a>.
Rob Pike4501d342009-02-19 17:31:36 -080012</p>
Robert Griesemer67153582008-12-16 14:45:09 -080013
Robert Griesemerc2d55862009-02-19 16:49:10 -080014<p>
Rob Pike4501d342009-02-19 17:31:36 -080015Go is a general-purpose language designed with systems programming
Rob Pike678625d2009-09-15 09:54:22 -070016in mind. It is strongly typed and garbage-collected and has explicit
Rob Pike4501d342009-02-19 17:31:36 -080017support for concurrent programming. Programs are constructed from
18<i>packages</i>, whose properties allow efficient management of
19dependencies. The existing implementations use a traditional
20compile/link model to generate executable binaries.
21</p>
22
Robert Griesemerc2d55862009-02-19 16:49:10 -080023<p>
Rob Pike4501d342009-02-19 17:31:36 -080024The grammar is compact and regular, allowing for easy analysis by
25automatic tools such as integrated development environments.
26</p>
Larry Hosken698c6c02009-09-17 08:05:12 -070027
Russ Cox7c4f7cc2009-08-20 11:11:03 -070028<h2 id="Notation">Notation</h2>
Rob Pike4501d342009-02-19 17:31:36 -080029<p>
Robert Griesemer4d230302008-12-17 15:39:15 -080030The syntax is specified using Extended Backus-Naur Form (EBNF):
Rob Pike4501d342009-02-19 17:31:36 -080031</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -070032
Rob Pikeff70f092009-02-20 13:36:14 -080033<pre class="grammar">
Robert Griesemer32b822f2011-05-13 12:54:51 -070034Production = production_name "=" [ Expression ] "." .
Rob Pike4501d342009-02-19 17:31:36 -080035Expression = Alternative { "|" Alternative } .
Robert Griesemerc2d55862009-02-19 16:49:10 -080036Alternative = Term { Term } .
Robert Griesemer3c7271f2011-05-24 14:18:44 -070037Term = production_name | token [ "…" token ] | Group | Option | Repetition .
Rob Pike4501d342009-02-19 17:31:36 -080038Group = "(" Expression ")" .
Rob Pikec956e902009-04-14 20:10:49 -070039Option = "[" Expression "]" .
Rob Pike4501d342009-02-19 17:31:36 -080040Repetition = "{" Expression "}" .
Robert Griesemerc2d55862009-02-19 16:49:10 -080041</pre>
Robert Griesemerbbfe3122008-10-09 17:12:09 -070042
Rob Pike4501d342009-02-19 17:31:36 -080043<p>
44Productions are expressions constructed from terms and the following
45operators, in increasing precedence:
46</p>
Rob Pikeff70f092009-02-20 13:36:14 -080047<pre class="grammar">
Rob Pike4501d342009-02-19 17:31:36 -080048| alternation
49() grouping
50[] option (0 or 1 times)
51{} repetition (0 to n times)
Robert Griesemerc2d55862009-02-19 16:49:10 -080052</pre>
Robert Griesemer4d230302008-12-17 15:39:15 -080053
Robert Griesemerc2d55862009-02-19 16:49:10 -080054<p>
Rob Pike4501d342009-02-19 17:31:36 -080055Lower-case production names are used to identify lexical tokens.
Robert Griesemer7c1cb372012-02-29 10:39:20 -080056Non-terminals are in CamelCase. Lexical tokens are enclosed in
Rob Pike678625d2009-09-15 09:54:22 -070057double quotes <code>""</code> or back quotes <code>``</code>.
Rob Pike4501d342009-02-19 17:31:36 -080058</p>
59
Robert Griesemerc2d55862009-02-19 16:49:10 -080060<p>
Robert Griesemer3c7271f2011-05-24 14:18:44 -070061The form <code>a … b</code> represents the set of characters from
62<code>a</code> through <code>b</code> as alternatives. The horizontal
Jeremy Jackins7e054262012-03-19 08:26:36 +110063ellipsis <code></code> is also used elsewhere in the spec to informally denote various
Rob Pike8040f9b2012-02-13 14:38:31 +110064enumerations or code snippets that are not further specified. The character <code></code>
Robert Griesemer3c7271f2011-05-24 14:18:44 -070065(as opposed to the three characters <code>...</code>) is not a token of the Go
66language.
Rob Pike4501d342009-02-19 17:31:36 -080067</p>
68
Russ Cox7c4f7cc2009-08-20 11:11:03 -070069<h2 id="Source_code_representation">Source code representation</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -070070
Robert Griesemerc2d55862009-02-19 16:49:10 -080071<p>
Robert Griesemere9192752009-12-01 16:15:53 -080072Source code is Unicode text encoded in
73<a href="http://en.wikipedia.org/wiki/UTF-8">UTF-8</a>. The text is not
Rob Pikeff70f092009-02-20 13:36:14 -080074canonicalized, so a single accented code point is distinct from the
75same character constructed from combining an accent and a letter;
76those are treated as two code points. For simplicity, this document
Rob Pike9dfc6f62012-08-29 14:46:57 -070077will use the unqualified term <i>character</i> to refer to a Unicode code point
78in the source text.
Rob Pikeff70f092009-02-20 13:36:14 -080079</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -080080<p>
Rob Pikeff70f092009-02-20 13:36:14 -080081Each code point is distinct; for instance, upper and lower case letters
82are different characters.
83</p>
Russ Coxb7d9ffe2010-02-16 16:47:18 -080084<p>
Robert Griesemerf42e8832010-02-17 15:50:34 -080085Implementation restriction: For compatibility with other tools, a
86compiler may disallow the NUL character (U+0000) in the source text.
Russ Coxb7d9ffe2010-02-16 16:47:18 -080087</p>
Rob Pikeafac01d2012-09-06 10:37:13 -070088<p>
89Implementation restriction: For compatibility with other tools, a
Rob Pike488350a2012-09-07 10:28:24 -070090compiler may ignore a UTF-8-encoded byte order mark
91(U+FEFF) if it is the first Unicode code point in the source text.
Rob Pike548c65a2013-04-11 11:33:25 -070092A byte order mark may be disallowed anywhere else in the source.
Rob Pikeafac01d2012-09-06 10:37:13 -070093</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -070094
Russ Cox7c4f7cc2009-08-20 11:11:03 -070095<h3 id="Characters">Characters</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -070096
Robert Griesemerc2d55862009-02-19 16:49:10 -080097<p>
Rob Pike4501d342009-02-19 17:31:36 -080098The following terms are used to denote specific Unicode character classes:
99</p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700100<pre class="ebnf">
Robert Griesemer0e8032c2011-05-05 09:03:00 -0700101newline = /* the Unicode code point U+000A */ .
102unicode_char = /* an arbitrary Unicode code point except newline */ .
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700103unicode_letter = /* a Unicode code point classified as "Letter" */ .
Robert Griesemer838b5ad2011-02-03 12:27:41 -0800104unicode_digit = /* a Unicode code point classified as "Decimal Digit" */ .
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700105</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700106
Rob Pike678625d2009-09-15 09:54:22 -0700107<p>
Rob Pikee6863e72014-02-25 14:15:49 -0800108In <a href="http://www.unicode.org/versions/Unicode6.3.0/">The Unicode Standard 6.3</a>,
Robert Griesemer838b5ad2011-02-03 12:27:41 -0800109Section 4.5 "General Category"
Rob Pike678625d2009-09-15 09:54:22 -0700110defines a set of character categories. Go treats
111those characters in category Lu, Ll, Lt, Lm, or Lo as Unicode letters,
112and those in category Nd as Unicode digits.
113</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700114
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700115<h3 id="Letters_and_digits">Letters and digits</h3>
Rob Pikeff70f092009-02-20 13:36:14 -0800116
117<p>
Rob Pikef27e9f02009-02-23 19:22:05 -0800118The underscore character <code>_</code> (U+005F) is considered a letter.
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700119</p>
120<pre class="ebnf">
Robert Griesemerc2d55862009-02-19 16:49:10 -0800121letter = unicode_letter | "_" .
Robert Griesemer3c7271f2011-05-24 14:18:44 -0700122decimal_digit = "0" … "9" .
123octal_digit = "0" … "7" .
124hex_digit = "0" … "9" | "A" … "F" | "a" … "f" .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800125</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700126
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700127<h2 id="Lexical_elements">Lexical elements</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700128
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700129<h3 id="Comments">Comments</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700130
Rob Pikeff70f092009-02-20 13:36:14 -0800131<p>
Robert Griesemer37a09752015-05-29 17:36:26 -0700132Comments serve as program documentation. There are two forms:
Rob Pikeff70f092009-02-20 13:36:14 -0800133</p>
134
Robert Griesemer130ac742009-12-10 16:43:01 -0800135<ol>
136<li>
137<i>Line comments</i> start with the character sequence <code>//</code>
Robert Griesemer37a09752015-05-29 17:36:26 -0700138and stop at the end of the line.
Robert Griesemer130ac742009-12-10 16:43:01 -0800139</li>
140<li>
141<i>General comments</i> start with the character sequence <code>/*</code>
Robert Griesemer37a09752015-05-29 17:36:26 -0700142and stop with the first subsequent character sequence <code>*/</code>.
Robert Griesemer130ac742009-12-10 16:43:01 -0800143</li>
144</ol>
145
146<p>
Robert Griesemer37a09752015-05-29 17:36:26 -0700147A comment cannot start inside a <a href="#Rune_literals">rune</a> or
148<a href="#String_literals">string literal</a>, or inside a comment.
149A general comment containing no newlines acts like a space.
150Any other comment acts like a newline.
Robert Griesemer130ac742009-12-10 16:43:01 -0800151</p>
152
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700153<h3 id="Tokens">Tokens</h3>
Rob Pikeff70f092009-02-20 13:36:14 -0800154
155<p>
156Tokens form the vocabulary of the Go language.
Robert Griesemereb109a72009-12-28 14:40:42 -0800157There are four classes: <i>identifiers</i>, <i>keywords</i>, <i>operators
158and delimiters</i>, and <i>literals</i>. <i>White space</i>, formed from
Rob Pike4fe41922009-11-07 22:00:59 -0800159spaces (U+0020), horizontal tabs (U+0009),
160carriage returns (U+000D), and newlines (U+000A),
161is ignored except as it separates tokens
Robert Griesemer0e66a132010-09-27 18:59:11 -0700162that would otherwise combine into a single token. Also, a newline or end of file
Robert Griesemereb109a72009-12-28 14:40:42 -0800163may trigger the insertion of a <a href="#Semicolons">semicolon</a>.
Robert Griesemer130ac742009-12-10 16:43:01 -0800164While breaking the input into tokens,
Rob Pikeff70f092009-02-20 13:36:14 -0800165the next token is the longest sequence of characters that form a
166valid token.
167</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700168
Robert Griesemer130ac742009-12-10 16:43:01 -0800169<h3 id="Semicolons">Semicolons</h3>
170
171<p>
172The formal grammar uses semicolons <code>";"</code> as terminators in
173a number of productions. Go programs may omit most of these semicolons
174using the following two rules:
175</p>
176
177<ol>
178<li>
Robert Griesemer130ac742009-12-10 16:43:01 -0800179When the input is broken into tokens, a semicolon is automatically inserted
Robert Griesemer37a09752015-05-29 17:36:26 -0700180into the token stream immediately after a line's final token if that token is
Robert Griesemer130ac742009-12-10 16:43:01 -0800181<ul>
Robert Griesemer3af480372010-05-14 13:11:48 -0700182 <li>an
183 <a href="#Identifiers">identifier</a>
Robert Griesemer130ac742009-12-10 16:43:01 -0800184 </li>
Charles L. Dorian44262d12011-11-01 15:13:33 +0900185
Robert Griesemer3af480372010-05-14 13:11:48 -0700186 <li>an
187 <a href="#Integer_literals">integer</a>,
188 <a href="#Floating-point_literals">floating-point</a>,
189 <a href="#Imaginary_literals">imaginary</a>,
Rob Pike9dfc6f62012-08-29 14:46:57 -0700190 <a href="#Rune_literals">rune</a>, or
Robert Griesemer3af480372010-05-14 13:11:48 -0700191 <a href="#String_literals">string</a> literal
192 </li>
Charles L. Dorian44262d12011-11-01 15:13:33 +0900193
Robert Griesemer3af480372010-05-14 13:11:48 -0700194 <li>one of the <a href="#Keywords">keywords</a>
195 <code>break</code>,
196 <code>continue</code>,
197 <code>fallthrough</code>, or
198 <code>return</code>
199 </li>
Charles L. Dorian44262d12011-11-01 15:13:33 +0900200
Robert Griesemer3af480372010-05-14 13:11:48 -0700201 <li>one of the <a href="#Operators_and_Delimiters">operators and delimiters</a>
202 <code>++</code>,
203 <code>--</code>,
204 <code>)</code>,
205 <code>]</code>, or
206 <code>}</code>
Robert Griesemer130ac742009-12-10 16:43:01 -0800207 </li>
208</ul>
209</li>
210
211<li>
212To allow complex statements to occupy a single line, a semicolon
213may be omitted before a closing <code>")"</code> or <code>"}"</code>.
214</li>
215</ol>
216
217<p>
218To reflect idiomatic use, code examples in this document elide semicolons
219using these rules.
220</p>
221
222
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700223<h3 id="Identifiers">Identifiers</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700224
Rob Pikeff70f092009-02-20 13:36:14 -0800225<p>
226Identifiers name program entities such as variables and types.
227An identifier is a sequence of one or more letters and digits.
228The first character in an identifier must be a letter.
229</p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700230<pre class="ebnf">
Robert Griesemerd36d1912009-09-18 11:58:35 -0700231identifier = letter { letter | unicode_digit } .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800232</pre>
Robert Griesemerc2d55862009-02-19 16:49:10 -0800233<pre>
234a
235_x9
236ThisVariableIsExported
237αβ
238</pre>
Robert Griesemer130ac742009-12-10 16:43:01 -0800239
240<p>
Robert Griesemer4e56b332009-09-10 10:14:00 -0700241Some identifiers are <a href="#Predeclared_identifiers">predeclared</a>.
Robert Griesemer130ac742009-12-10 16:43:01 -0800242</p>
243
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -0700244
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700245<h3 id="Keywords">Keywords</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700246
Rob Pikeff70f092009-02-20 13:36:14 -0800247<p>
248The following keywords are reserved and may not be used as identifiers.
249</p>
250<pre class="grammar">
251break default func interface select
252case defer go map struct
253chan else goto package switch
254const fallthrough if range type
255continue for import return var
256</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700257
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700258<h3 id="Operators_and_Delimiters">Operators and Delimiters</h3>
Rob Pikeff70f092009-02-20 13:36:14 -0800259
260<p>
Robert Griesemerd36d1912009-09-18 11:58:35 -0700261The following character sequences represent <a href="#Operators">operators</a>, delimiters, and other special tokens:
Rob Pikeff70f092009-02-20 13:36:14 -0800262</p>
263<pre class="grammar">
264+ &amp; += &amp;= &amp;&amp; == != ( )
265- | -= |= || &lt; &lt;= [ ]
266* ^ *= ^= &lt;- &gt; &gt;= { }
Robert Griesemer4ed666e2009-08-27 16:45:42 -0700267/ &lt;&lt; /= &lt;&lt;= ++ = := , ;
268% &gt;&gt; %= &gt;&gt;= -- ! ... . :
Rob Pikecd04ec92009-03-11 21:59:05 -0700269 &amp;^ &amp;^=
Rob Pikeff70f092009-02-20 13:36:14 -0800270</pre>
271
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700272<h3 id="Integer_literals">Integer literals</h3>
Rob Pikeff70f092009-02-20 13:36:14 -0800273
274<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700275An integer literal is a sequence of digits representing an
276<a href="#Constants">integer constant</a>.
277An optional prefix sets a non-decimal base: <code>0</code> for octal, <code>0x</code> or
Rob Pikef27e9f02009-02-23 19:22:05 -0800278<code>0X</code> for hexadecimal. In hexadecimal literals, letters
279<code>a-f</code> and <code>A-F</code> represent values 10 through 15.
Rob Pikeff70f092009-02-20 13:36:14 -0800280</p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700281<pre class="ebnf">
Robert Griesemerd36d1912009-09-18 11:58:35 -0700282int_lit = decimal_lit | octal_lit | hex_lit .
Robert Griesemer3c7271f2011-05-24 14:18:44 -0700283decimal_lit = ( "1" … "9" ) { decimal_digit } .
Robert Griesemerd36d1912009-09-18 11:58:35 -0700284octal_lit = "0" { octal_digit } .
285hex_lit = "0" ( "x" | "X" ) hex_digit { hex_digit } .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800286</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700287
Robert Griesemerc2d55862009-02-19 16:49:10 -0800288<pre>
28942
2900600
2910xBadFace
292170141183460469231731687303715884105727
293</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700294
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700295<h3 id="Floating-point_literals">Floating-point literals</h3>
Rob Pikeff70f092009-02-20 13:36:14 -0800296<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700297A floating-point literal is a decimal representation of a
298<a href="#Constants">floating-point constant</a>.
299It has an integer part, a decimal point, a fractional part,
Rob Pikeff70f092009-02-20 13:36:14 -0800300and an exponent part. The integer and fractional part comprise
Rob Pikef27e9f02009-02-23 19:22:05 -0800301decimal digits; the exponent part is an <code>e</code> or <code>E</code>
Rob Pikeff70f092009-02-20 13:36:14 -0800302followed by an optionally signed decimal exponent. One of the
303integer part or the fractional part may be elided; one of the decimal
304point or the exponent may be elided.
305</p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700306<pre class="ebnf">
Robert Griesemerd36d1912009-09-18 11:58:35 -0700307float_lit = decimals "." [ decimals ] [ exponent ] |
308 decimals exponent |
309 "." decimals [ exponent ] .
310decimals = decimal_digit { decimal_digit } .
311exponent = ( "e" | "E" ) [ "+" | "-" ] decimals .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800312</pre>
Robert Griesemerad711102008-09-11 17:48:20 -0700313
Robert Griesemerc2d55862009-02-19 16:49:10 -0800314<pre>
3150.
Rob Pike72970872010-03-04 12:35:16 -080031672.40
317072.40 // == 72.40
Robert Griesemerc2d55862009-02-19 16:49:10 -08003182.71828
3191.e+0
3206.67428e-11
3211E6
322.25
323.12345E+5
324</pre>
Robert Griesemerad711102008-09-11 17:48:20 -0700325
Rob Pike72970872010-03-04 12:35:16 -0800326<h3 id="Imaginary_literals">Imaginary literals</h3>
327<p>
328An imaginary literal is a decimal representation of the imaginary part of a
329<a href="#Constants">complex constant</a>.
330It consists of a
331<a href="#Floating-point_literals">floating-point literal</a>
332or decimal integer followed
333by the lower-case letter <code>i</code>.
334</p>
335<pre class="ebnf">
336imaginary_lit = (decimals | float_lit) "i" .
337</pre>
338
339<pre>
3400i
341011i // == 11i
3420.i
3432.71828i
3441.e+0i
3456.67428e-11i
3461E6i
347.25i
348.12345E+5i
349</pre>
350
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700351
Rob Pike9dfc6f62012-08-29 14:46:57 -0700352<h3 id="Rune_literals">Rune literals</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700353
Rob Pike4501d342009-02-19 17:31:36 -0800354<p>
Rob Pike9dfc6f62012-08-29 14:46:57 -0700355A rune literal represents a <a href="#Constants">rune constant</a>,
356an integer value identifying a Unicode code point.
Robert Griesemer37a09752015-05-29 17:36:26 -0700357A rune literal is expressed as one or more characters enclosed in single quotes,
358as in <code>'x'</code> or <code>'\n'</code>.
359Within the quotes, any character may appear except newline and unescaped single
360quote. A single quoted character represents the Unicode value
Rob Pike9dfc6f62012-08-29 14:46:57 -0700361of the character itself,
Rob Pikeff70f092009-02-20 13:36:14 -0800362while multi-character sequences beginning with a backslash encode
363values in various formats.
Rob Pike4501d342009-02-19 17:31:36 -0800364</p>
Rob Pikeff70f092009-02-20 13:36:14 -0800365<p>
366The simplest form represents the single character within the quotes;
367since Go source text is Unicode characters encoded in UTF-8, multiple
368UTF-8-encoded bytes may represent a single integer value. For
Rob Pikef27e9f02009-02-23 19:22:05 -0800369instance, the literal <code>'a'</code> holds a single byte representing
370a literal <code>a</code>, Unicode U+0061, value <code>0x61</code>, while
371<code>'ä'</code> holds two bytes (<code>0xc3</code> <code>0xa4</code>) representing
372a literal <code>a</code>-dieresis, U+00E4, value <code>0xe4</code>.
Rob Pikeff70f092009-02-20 13:36:14 -0800373</p>
374<p>
Rob Pike9dfc6f62012-08-29 14:46:57 -0700375Several backslash escapes allow arbitrary values to be encoded as
Oling Cat845f4d62012-09-05 14:53:13 +1000376ASCII text. There are four ways to represent the integer value
Rob Pikef27e9f02009-02-23 19:22:05 -0800377as a numeric constant: <code>\x</code> followed by exactly two hexadecimal
378digits; <code>\u</code> followed by exactly four hexadecimal digits;
379<code>\U</code> followed by exactly eight hexadecimal digits, and a
380plain backslash <code>\</code> followed by exactly three octal digits.
Rob Pikeff70f092009-02-20 13:36:14 -0800381In each case the value of the literal is the value represented by
382the digits in the corresponding base.
383</p>
384<p>
385Although these representations all result in an integer, they have
386different valid ranges. Octal escapes must represent a value between
Rob Pike678625d2009-09-15 09:54:22 -07003870 and 255 inclusive. Hexadecimal escapes satisfy this condition
388by construction. The escapes <code>\u</code> and <code>\U</code>
Rob Pikeff70f092009-02-20 13:36:14 -0800389represent Unicode code points so within them some values are illegal,
Rob Pikef27e9f02009-02-23 19:22:05 -0800390in particular those above <code>0x10FFFF</code> and surrogate halves.
Rob Pikeff70f092009-02-20 13:36:14 -0800391</p>
392<p>
393After a backslash, certain single-character escapes represent special values:
394</p>
395<pre class="grammar">
396\a U+0007 alert or bell
397\b U+0008 backspace
398\f U+000C form feed
399\n U+000A line feed or newline
400\r U+000D carriage return
401\t U+0009 horizontal tab
402\v U+000b vertical tab
403\\ U+005c backslash
Rob Pike9dfc6f62012-08-29 14:46:57 -0700404\' U+0027 single quote (valid escape only within rune literals)
Rob Pikeff70f092009-02-20 13:36:14 -0800405\" U+0022 double quote (valid escape only within string literals)
Robert Griesemerc2d55862009-02-19 16:49:10 -0800406</pre>
Robert Griesemerc2d55862009-02-19 16:49:10 -0800407<p>
Rob Pike9dfc6f62012-08-29 14:46:57 -0700408All other sequences starting with a backslash are illegal inside rune literals.
Rob Pike4501d342009-02-19 17:31:36 -0800409</p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700410<pre class="ebnf">
Robert Griesemerc863db42013-01-07 18:02:58 -0800411rune_lit = "'" ( unicode_value | byte_value ) "'" .
Rob Pikeff70f092009-02-20 13:36:14 -0800412unicode_value = unicode_char | little_u_value | big_u_value | escaped_char .
413byte_value = octal_byte_value | hex_byte_value .
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700414octal_byte_value = `\` octal_digit octal_digit octal_digit .
415hex_byte_value = `\` "x" hex_digit hex_digit .
416little_u_value = `\` "u" hex_digit hex_digit hex_digit hex_digit .
417big_u_value = `\` "U" hex_digit hex_digit hex_digit hex_digit
Rob Pikeff70f092009-02-20 13:36:14 -0800418 hex_digit hex_digit hex_digit hex_digit .
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700419escaped_char = `\` ( "a" | "b" | "f" | "n" | "r" | "t" | "v" | `\` | "'" | `"` ) .
Rob Pikeff70f092009-02-20 13:36:14 -0800420</pre>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700421
Robert Griesemerc2d55862009-02-19 16:49:10 -0800422<pre>
423'a'
424'ä'
425'本'
426'\t'
427'\000'
428'\007'
429'\377'
430'\x07'
431'\xff'
432'\u12e4'
433'\U00101234'
Robert Griesemer37a09752015-05-29 17:36:26 -0700434'\'' // rune literal containing single quote character
Rob Pike9dfc6f62012-08-29 14:46:57 -0700435'aa' // illegal: too many characters
436'\xa' // illegal: too few hexadecimal digits
437'\0' // illegal: too few octal digits
438'\uDFFF' // illegal: surrogate half
439'\U00110000' // illegal: invalid Unicode code point
Robert Griesemerc2d55862009-02-19 16:49:10 -0800440</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700441
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700442
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700443<h3 id="String_literals">String literals</h3>
Rob Pikeff70f092009-02-20 13:36:14 -0800444
445<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700446A string literal represents a <a href="#Constants">string constant</a>
447obtained from concatenating a sequence of characters. There are two forms:
448raw string literals and interpreted string literals.
Rob Pikeff70f092009-02-20 13:36:14 -0800449</p>
450<p>
Robert Griesemer37a09752015-05-29 17:36:26 -0700451Raw string literals are character sequences between back quotes, as in
452<code>`foo`</code>. Within the quotes, any character may appear except
Robert Griesemerdb7a6222009-06-18 13:51:14 -0700453back quote. The value of a raw string literal is the
Rob Pike9dfc6f62012-08-29 14:46:57 -0700454string composed of the uninterpreted (implicitly UTF-8-encoded) characters
455between the quotes;
Robert Griesemerdb7a6222009-06-18 13:51:14 -0700456in particular, backslashes have no special meaning and the string may
Robert Griesemer11b7c892011-12-15 10:51:51 -0800457contain newlines.
Ian Lance Taylor2a627da2014-05-16 12:20:03 -0700458Carriage return characters ('\r') inside raw string literals
Rob Pikec26ca912011-12-14 21:52:41 -0800459are discarded from the raw string value.
Rob Pikeff70f092009-02-20 13:36:14 -0800460</p>
461<p>
462Interpreted string literals are character sequences between double
Robert Griesemer37a09752015-05-29 17:36:26 -0700463quotes, as in <code>&quot;bar&quot;</code>.
464Within the quotes, any character may appear except newline and unescaped double quote.
465The text between the quotes forms the
Rob Pikeff70f092009-02-20 13:36:14 -0800466value of the literal, with backslash escapes interpreted as they
Robin Eklindf82097f2014-09-03 10:44:33 -0700467are in <a href="#Rune_literals">rune literals</a> (except that <code>\'</code> is illegal and
Rob Pike9dfc6f62012-08-29 14:46:57 -0700468<code>\"</code> is legal), with the same restrictions.
469The three-digit octal (<code>\</code><i>nnn</i>)
Robert Griesemere9192752009-12-01 16:15:53 -0800470and two-digit hexadecimal (<code>\x</code><i>nn</i>) escapes represent individual
Rob Pikeff70f092009-02-20 13:36:14 -0800471<i>bytes</i> of the resulting string; all other escapes represent
472the (possibly multi-byte) UTF-8 encoding of individual <i>characters</i>.
Rob Pikef27e9f02009-02-23 19:22:05 -0800473Thus inside a string literal <code>\377</code> and <code>\xFF</code> represent
474a single byte of value <code>0xFF</code>=255, while <code>ÿ</code>,
475<code>\u00FF</code>, <code>\U000000FF</code> and <code>\xc3\xbf</code> represent
Robert Griesemere1e76192009-09-25 14:11:03 -0700476the two bytes <code>0xc3</code> <code>0xbf</code> of the UTF-8 encoding of character
Rob Pikeff70f092009-02-20 13:36:14 -0800477U+00FF.
478</p>
479
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700480<pre class="ebnf">
Rob Pikeff70f092009-02-20 13:36:14 -0800481string_lit = raw_string_lit | interpreted_string_lit .
Robert Griesemer0e8032c2011-05-05 09:03:00 -0700482raw_string_lit = "`" { unicode_char | newline } "`" .
Robert Griesemerd4d4ff02009-10-19 13:13:59 -0700483interpreted_string_lit = `"` { unicode_value | byte_value } `"` .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800484</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700485
Robert Griesemerc2d55862009-02-19 16:49:10 -0800486<pre>
Robert Griesemer37a09752015-05-29 17:36:26 -0700487`abc` // same as "abc"
Robert Griesemerdb7a6222009-06-18 13:51:14 -0700488`\n
Robert Griesemer37a09752015-05-29 17:36:26 -0700489\n` // same as "\\n\n\\n"
Robert Griesemerc2d55862009-02-19 16:49:10 -0800490"\n"
Robert Griesemer37a09752015-05-29 17:36:26 -0700491"\"" // same as `"`
Robert Griesemerc2d55862009-02-19 16:49:10 -0800492"Hello, world!\n"
493"日本語"
494"\u65e5本\U00008a9e"
495"\xff\u00FF"
Robert Griesemer37a09752015-05-29 17:36:26 -0700496"\uD800" // illegal: surrogate half
497"\U00110000" // illegal: invalid Unicode code point
Robert Griesemerc2d55862009-02-19 16:49:10 -0800498</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700499
Rob Pikeff70f092009-02-20 13:36:14 -0800500<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700501These examples all represent the same string:
Rob Pikeff70f092009-02-20 13:36:14 -0800502</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700503
Robert Griesemerc2d55862009-02-19 16:49:10 -0800504<pre>
Rob Pikeff70f092009-02-20 13:36:14 -0800505"日本語" // UTF-8 input text
506`日本語` // UTF-8 input text as a raw literal
Rob Pike9dfc6f62012-08-29 14:46:57 -0700507"\u65e5\u672c\u8a9e" // the explicit Unicode code points
508"\U000065e5\U0000672c\U00008a9e" // the explicit Unicode code points
509"\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e" // the explicit UTF-8 bytes
Robert Griesemerc2d55862009-02-19 16:49:10 -0800510</pre>
Robert Griesemercd499272008-09-29 12:09:00 -0700511
Robert Griesemerc2d55862009-02-19 16:49:10 -0800512<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700513If the source code represents a character as two code points, such as
514a combining form involving an accent and a letter, the result will be
Rob Pike9dfc6f62012-08-29 14:46:57 -0700515an error if placed in a rune literal (it is not a single code
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700516point), and will appear as two code points if placed in a string
517literal.
Rob Pikeff70f092009-02-20 13:36:14 -0800518</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700519
Robert Griesemer19b1d352009-09-24 19:36:48 -0700520
521<h2 id="Constants">Constants</h2>
522
Russ Coxa9336352011-12-08 21:48:19 -0500523<p>There are <i>boolean constants</i>,
Rob Pike9dfc6f62012-08-29 14:46:57 -0700524<i>rune constants</i>,
Russ Coxa9336352011-12-08 21:48:19 -0500525<i>integer constants</i>,
Rob Pike72970872010-03-04 12:35:16 -0800526<i>floating-point constants</i>, <i>complex constants</i>,
Robert Griesemerf3310122013-07-25 09:35:55 -0700527and <i>string constants</i>. Rune, integer, floating-point,
Rob Pike72970872010-03-04 12:35:16 -0800528and complex constants are
Robert Griesemer19b1d352009-09-24 19:36:48 -0700529collectively called <i>numeric constants</i>.
530</p>
Rob Pike678625d2009-09-15 09:54:22 -0700531
532<p>
Russ Coxa9336352011-12-08 21:48:19 -0500533A constant value is represented by a
Rob Pike9dfc6f62012-08-29 14:46:57 -0700534<a href="#Rune_literals">rune</a>,
Robert Griesemer19b1d352009-09-24 19:36:48 -0700535<a href="#Integer_literals">integer</a>,
536<a href="#Floating-point_literals">floating-point</a>,
Rob Pike72970872010-03-04 12:35:16 -0800537<a href="#Imaginary_literals">imaginary</a>,
Russ Coxa9336352011-12-08 21:48:19 -0500538or
Robert Griesemer19b1d352009-09-24 19:36:48 -0700539<a href="#String_literals">string</a> literal,
540an identifier denoting a constant,
Robert Griesemer27693562011-06-13 16:47:33 -0700541a <a href="#Constant_expressions">constant expression</a>,
542a <a href="#Conversions">conversion</a> with a result that is a constant, or
Russ Coxf4429182010-07-01 17:49:47 -0700543the result value of some built-in functions such as
544<code>unsafe.Sizeof</code> applied to any value,
545<code>cap</code> or <code>len</code> applied to
546<a href="#Length_and_capacity">some expressions</a>,
Rob Pike72970872010-03-04 12:35:16 -0800547<code>real</code> and <code>imag</code> applied to a complex constant
Robert Griesemerb94c0d22011-01-19 23:07:21 -0500548and <code>complex</code> applied to numeric constants.
Robert Griesemer19b1d352009-09-24 19:36:48 -0700549The boolean truth values are represented by the predeclared constants
550<code>true</code> and <code>false</code>. The predeclared identifier
551<a href="#Iota">iota</a> denotes an integer constant.
Rob Pike678625d2009-09-15 09:54:22 -0700552</p>
553
Robert Griesemer19b1d352009-09-24 19:36:48 -0700554<p>
Rob Pike72970872010-03-04 12:35:16 -0800555In general, complex constants are a form of
556<a href="#Constant_expressions">constant expression</a>
557and are discussed in that section.
558</p>
559
560<p>
Robert Griesemer55ecda42015-09-17 18:10:20 -0700561Numeric constants represent exact values of arbitrary precision and do not overflow.
562Consequently, there are no constants denoting the IEEE-754 negative zero, infinity,
563and not-a-number values.
Robert Griesemer19b1d352009-09-24 19:36:48 -0700564</p>
565
566<p>
Robert Griesemer47094dc2014-09-30 11:44:29 -0700567Constants may be <a href="#Types">typed</a> or <i>untyped</i>.
Robert Griesemer19b1d352009-09-24 19:36:48 -0700568Literal constants, <code>true</code>, <code>false</code>, <code>iota</code>,
569and certain <a href="#Constant_expressions">constant expressions</a>
570containing only untyped constant operands are untyped.
571</p>
572
573<p>
574A constant may be given a type explicitly by a <a href="#Constant_declarations">constant declaration</a>
575or <a href="#Conversions">conversion</a>, or implicitly when used in a
576<a href="#Variable_declarations">variable declaration</a> or an
577<a href="#Assignments">assignment</a> or as an
578operand in an <a href="#Expressions">expression</a>.
579It is an error if the constant value
Robert Griesemerdfc5bb52011-01-19 10:33:41 -0800580cannot be represented as a value of the respective type.
Robert Griesemere9192752009-12-01 16:15:53 -0800581For instance, <code>3.0</code> can be given any integer or any
Rob Pike4fe41922009-11-07 22:00:59 -0800582floating-point type, while <code>2147483648.0</code> (equal to <code>1&lt;&lt;31</code>)
583can be given the types <code>float32</code>, <code>float64</code>, or <code>uint32</code> but
584not <code>int32</code> or <code>string</code>.
Robert Griesemer19b1d352009-09-24 19:36:48 -0700585</p>
586
587<p>
Robert Griesemer47094dc2014-09-30 11:44:29 -0700588An untyped constant has a <i>default type</i> which is the type to which the
589constant is implicitly converted in contexts where a typed value is required,
590for instance, in a <a href="#Short_variable_declarations">short variable declaration</a>
591such as <code>i := 0</code> where there is no explicit type.
592The default type of an untyped constant is <code>bool</code>, <code>rune</code>,
593<code>int</code>, <code>float64</code>, <code>complex128</code> or <code>string</code>
594respectively, depending on whether it is a boolean, rune, integer, floating-point,
595complex, or string constant.
596</p>
597
598<p>
Ian Lance Taylor9126c652012-02-13 11:25:56 -0800599Implementation restriction: Although numeric constants have arbitrary
600precision in the language, a compiler may implement them using an
601internal representation with limited precision. That said, every
602implementation must:
Robert Griesemer19b1d352009-09-24 19:36:48 -0700603</p>
Ian Lance Taylor9126c652012-02-13 11:25:56 -0800604<ul>
605 <li>Represent integer constants with at least 256 bits.</li>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700606
Ian Lance Taylor9126c652012-02-13 11:25:56 -0800607 <li>Represent floating-point constants, including the parts of
608 a complex constant, with a mantissa of at least 256 bits
609 and a signed exponent of at least 32 bits.</li>
610
611 <li>Give an error if unable to represent an integer constant
612 precisely.</li>
613
614 <li>Give an error if unable to represent a floating-point or
615 complex constant due to overflow.</li>
616
617 <li>Round to the nearest representable constant if unable to
618 represent a floating-point or complex constant due to limits
619 on precision.</li>
620</ul>
621<p>
622These requirements apply both to literal constants and to the result
623of evaluating <a href="#Constant_expressions">constant
624expressions</a>.
625</p>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700626
Robert Griesemer6962c152014-10-16 15:08:49 -0700627<h2 id="Variables">Variables</h2>
628
629<p>
630A variable is a storage location for holding a <i>value</i>.
631The set of permissible values is determined by the
632variable's <i><a href="#Types">type</a></i>.
633</p>
634
635<p>
636A <a href="#Variable_declarations">variable declaration</a>
637or, for function parameters and results, the signature
638of a <a href="#Function_declarations">function declaration</a>
639or <a href="#Function_literals">function literal</a> reserves
640storage for a named variable.
641
642Calling the built-in function <a href="#Allocation"><code>new</code></a>
643or taking the address of a <a href="#Composite_literals">composite literal</a>
644allocates storage for a variable at run time.
645Such an anonymous variable is referred to via a (possibly implicit)
646<a href="#Address_operators">pointer indirection</a>.
647</p>
648
649<p>
650<i>Structured</i> variables of <a href="#Array_types">array</a>, <a href="#Slice_types">slice</a>,
651and <a href="#Struct_types">struct</a> types have elements and fields that may
652be <a href="#Address_operators">addressed</a> individually. Each such element
653acts like a variable.
654</p>
655
656<p>
Robert Griesemer2d9378c2015-07-27 13:30:16 -0700657The <i>static type</i> (or just <i>type</i>) of a variable is the
Robert Griesemer6962c152014-10-16 15:08:49 -0700658type given in its declaration, the type provided in the
659<code>new</code> call or composite literal, or the type of
660an element of a structured variable.
661Variables of interface type also have a distinct <i>dynamic type</i>,
662which is the concrete type of the value assigned to the variable at run time
663(unless the value is the predeclared identifier <code>nil</code>,
664which has no type).
665The dynamic type may vary during execution but values stored in interface
666variables are always <a href="#Assignability">assignable</a>
Robert Griesemer2d9378c2015-07-27 13:30:16 -0700667to the static type of the variable.
668</p>
Robert Griesemer6962c152014-10-16 15:08:49 -0700669
670<pre>
671var x interface{} // x is nil and has static type interface{}
672var v *T // v has value nil, static type *T
673x = 42 // x has value 42 and dynamic type int
674x = v // x has value (*T)(nil) and dynamic type *T
675</pre>
676
677<p>
678A variable's value is retrieved by referring to the variable in an
679<a href="#Expressions">expression</a>; it is the most recent value
680<a href="#Assignments">assigned</a> to the variable.
681If a variable has not yet been assigned a value, its value is the
682<a href="#The_zero_value">zero value</a> for its type.
683</p>
684
685
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700686<h2 id="Types">Types</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700687
Robert Griesemerc2d55862009-02-19 16:49:10 -0800688<p>
Robert Griesemer56809d02009-05-20 11:02:48 -0700689A type determines the set of values and operations specific to values of that
Robert Griesemer8268ead2013-07-09 21:12:53 -0700690type. Types may be <i>named</i> or <i>unnamed</i>. Named types are specified
691by a (possibly <a href="#Qualified_identifiers">qualified</a>)
692<a href="#Type_declarations"><i>type name</i></a>; unnamed types are specified
693using a <i>type literal</i>, which composes a new type from existing types.
Rob Pike5af7de32009-02-24 15:17:59 -0800694</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700695
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700696<pre class="ebnf">
Rob Pike8f2330d2009-02-25 16:20:44 -0800697Type = TypeName | TypeLit | "(" Type ")" .
Robert Griesemer809e06b2012-06-26 11:49:19 -0700698TypeName = identifier | QualifiedIdent .
Rob Pike8f2330d2009-02-25 16:20:44 -0800699TypeLit = ArrayType | StructType | PointerType | FunctionType | InterfaceType |
700 SliceType | MapType | ChannelType .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800701</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -0800702
Robert Griesemerc2d55862009-02-19 16:49:10 -0800703<p>
Robert Griesemerfc61b772009-09-28 14:10:20 -0700704Named instances of the boolean, numeric, and string types are
705<a href="#Predeclared_identifiers">predeclared</a>.
706<i>Composite types</i>&mdash;array, struct, pointer, function,
707interface, slice, map, and channel types&mdash;may be constructed using
708type literals.
Rob Pike5af7de32009-02-24 15:17:59 -0800709</p>
Robert Griesemer7abfcd92008-10-07 17:14:30 -0700710
Rob Pike5af7de32009-02-24 15:17:59 -0800711<p>
Robert Griesemer7bc03712010-06-07 15:49:39 -0700712Each type <code>T</code> has an <i>underlying type</i>: If <code>T</code>
Robert Griesemer8d77d2c2014-03-05 19:37:44 -0800713is one of the predeclared boolean, numeric, or string types, or a type literal,
714the corresponding underlying
Robert Griesemer7bc03712010-06-07 15:49:39 -0700715type is <code>T</code> itself. Otherwise, <code>T</code>'s underlying type
716is the underlying type of the type to which <code>T</code> refers in its
717<a href="#Type_declarations">type declaration</a>.
718</p>
719
720<pre>
721 type T1 string
722 type T2 T1
723 type T3 []T1
724 type T4 T3
725</pre>
726
727<p>
728The underlying type of <code>string</code>, <code>T1</code>, and <code>T2</code>
729is <code>string</code>. The underlying type of <code>[]T1</code>, <code>T3</code>,
730and <code>T4</code> is <code>[]T1</code>.
731</p>
732
Robert Griesemer2a838d62011-02-08 13:31:01 -0800733<h3 id="Method_sets">Method sets</h3>
Robert Griesemer7bc03712010-06-07 15:49:39 -0700734<p>
Robert Griesemer2c83f1e2014-05-22 12:23:25 -0700735A type may have a <i>method set</i> associated with it.
Robert Griesemer19b1d352009-09-24 19:36:48 -0700736The method set of an <a href="#Interface_types">interface type</a> is its interface.
Robert Griesemer2c83f1e2014-05-22 12:23:25 -0700737The method set of any other type <code>T</code> consists of all
738<a href="#Method_declarations">methods</a> declared with receiver type <code>T</code>.
739The method set of the corresponding <a href="#Pointer_types">pointer type</a> <code>*T</code>
740is the set of all methods declared with receiver <code>*T</code> or <code>T</code>
Robert Griesemer56809d02009-05-20 11:02:48 -0700741(that is, it also contains the method set of <code>T</code>).
Robert Griesemer787adb62012-06-04 14:24:10 -0700742Further rules apply to structs containing anonymous fields, as described
743in the section on <a href="#Struct_types">struct types</a>.
Robert Griesemer56809d02009-05-20 11:02:48 -0700744Any other type has an empty method set.
Robert Griesemer103c9db2012-03-01 13:57:49 -0800745In a method set, each method must have a
Robert Griesemer2c83f1e2014-05-22 12:23:25 -0700746<a href="#Uniqueness_of_identifiers">unique</a>
747non-<a href="#Blank_identifier">blank</a> <a href="#MethodName">method name</a>.
Rob Pike5af7de32009-02-24 15:17:59 -0800748</p>
Robert Griesemer1f3e8422008-09-29 18:41:30 -0700749
Russ Coxfd2a5112012-02-08 14:28:51 -0500750<p>
751The method set of a type determines the interfaces that the
752type <a href="#Interface_types">implements</a>
753and the methods that can be <a href="#Calls">called</a>
754using a receiver of that type.
755</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700756
Robert Griesemer19b1d352009-09-24 19:36:48 -0700757<h3 id="Boolean_types">Boolean types</h3>
758
Stefan Nilssonc50074e2012-02-29 15:07:52 -0800759<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700760A <i>boolean type</i> represents the set of Boolean truth values
761denoted by the predeclared constants <code>true</code>
762and <code>false</code>. The predeclared boolean type is <code>bool</code>.
Stefan Nilssonc50074e2012-02-29 15:07:52 -0800763</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700764
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700765<h3 id="Numeric_types">Numeric types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700766
Rob Pike5af7de32009-02-24 15:17:59 -0800767<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700768A <i>numeric type</i> represents sets of integer or floating-point values.
769The predeclared architecture-independent numeric types are:
Rob Pike5af7de32009-02-24 15:17:59 -0800770</p>
Robert Griesemerebf14c62008-10-30 14:50:23 -0700771
Rob Pikeff70f092009-02-20 13:36:14 -0800772<pre class="grammar">
Rob Pike72970872010-03-04 12:35:16 -0800773uint8 the set of all unsigned 8-bit integers (0 to 255)
774uint16 the set of all unsigned 16-bit integers (0 to 65535)
775uint32 the set of all unsigned 32-bit integers (0 to 4294967295)
776uint64 the set of all unsigned 64-bit integers (0 to 18446744073709551615)
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700777
Rob Pike72970872010-03-04 12:35:16 -0800778int8 the set of all signed 8-bit integers (-128 to 127)
779int16 the set of all signed 16-bit integers (-32768 to 32767)
780int32 the set of all signed 32-bit integers (-2147483648 to 2147483647)
781int64 the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700782
Rob Pike72970872010-03-04 12:35:16 -0800783float32 the set of all IEEE-754 32-bit floating-point numbers
784float64 the set of all IEEE-754 64-bit floating-point numbers
785
786complex64 the set of all complex numbers with float32 real and imaginary parts
787complex128 the set of all complex numbers with float64 real and imaginary parts
Rob Pike5af7de32009-02-24 15:17:59 -0800788
Robert Griesemerb910a272011-11-01 01:09:22 -0400789byte alias for uint8
Russ Coxd7f050a2011-12-09 00:11:43 -0500790rune alias for int32
Robert Griesemerc2d55862009-02-19 16:49:10 -0800791</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700792
Rob Pike5af7de32009-02-24 15:17:59 -0800793<p>
Robert Griesemere9192752009-12-01 16:15:53 -0800794The value of an <i>n</i>-bit integer is <i>n</i> bits wide and represented using
795<a href="http://en.wikipedia.org/wiki/Two's_complement">two's complement arithmetic</a>.
Rob Pike5af7de32009-02-24 15:17:59 -0800796</p>
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -0800797
Rob Pike5af7de32009-02-24 15:17:59 -0800798<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700799There is also a set of predeclared numeric types with implementation-specific sizes:
Rob Pike5af7de32009-02-24 15:17:59 -0800800</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700801
Rob Pikeff70f092009-02-20 13:36:14 -0800802<pre class="grammar">
Robert Griesemercfe92112009-06-18 13:29:40 -0700803uint either 32 or 64 bits
Robert Griesemer97025eb2011-01-13 10:24:04 -0800804int same size as uint
Robert Griesemercfe92112009-06-18 13:29:40 -0700805uintptr an unsigned integer large enough to store the uninterpreted bits of a pointer value
Robert Griesemerc2d55862009-02-19 16:49:10 -0800806</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700807
Robert Griesemerc2d55862009-02-19 16:49:10 -0800808<p>
Rob Pikeda389742009-03-02 19:13:40 -0800809To avoid portability issues all numeric types are distinct except
Robert Griesemerb910a272011-11-01 01:09:22 -0400810<code>byte</code>, which is an alias for <code>uint8</code>, and
Russ Coxd7f050a2011-12-09 00:11:43 -0500811<code>rune</code>, which is an alias for <code>int32</code>.
Rob Pikeda389742009-03-02 19:13:40 -0800812Conversions
Robert Griesemer7bc03712010-06-07 15:49:39 -0700813are required when different numeric types are mixed in an expression
Rob Pike5af7de32009-02-24 15:17:59 -0800814or assignment. For instance, <code>int32</code> and <code>int</code>
Russ Cox5958dd62009-03-04 17:19:21 -0800815are not the same type even though they may have the same size on a
Rob Pike5af7de32009-02-24 15:17:59 -0800816particular architecture.
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700817
818
Robert Griesemer19b1d352009-09-24 19:36:48 -0700819<h3 id="String_types">String types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700820
Rob Pike4501d342009-02-19 17:31:36 -0800821<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700822A <i>string type</i> represents the set of string values.
Robert Griesemercc065932012-09-14 11:31:56 -0700823A string value is a (possibly empty) sequence of bytes.
824Strings are immutable: once created,
Rob Pike5af7de32009-02-24 15:17:59 -0800825it is impossible to change the contents of a string.
Robert Griesemer19b1d352009-09-24 19:36:48 -0700826The predeclared string type is <code>string</code>.
Robert Griesemercc065932012-09-14 11:31:56 -0700827</p>
Rob Pike5af7de32009-02-24 15:17:59 -0800828
829<p>
Robert Griesemercc065932012-09-14 11:31:56 -0700830The length of a string <code>s</code> (its size in bytes) can be discovered using
831the built-in function <a href="#Length_and_capacity"><code>len</code></a>.
832The length is a compile-time constant if the string is a constant.
Robert Griesemer9c9e8112012-12-10 11:55:57 -0800833A string's bytes can be accessed by integer <a href="#Index_expressions">indices</a>
8340 through <code>len(s)-1</code>.
Robert Griesemercc065932012-09-14 11:31:56 -0700835It is illegal to take the address of such an element; if
836<code>s[i]</code> is the <code>i</code>'th byte of a
837string, <code>&amp;s[i]</code> is invalid.
Rob Pike4501d342009-02-19 17:31:36 -0800838</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700839
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700840
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700841<h3 id="Array_types">Array types</h3>
Russ Cox461dd912009-03-04 14:44:51 -0800842
843<p>
844An array is a numbered sequence of elements of a single
Robert Griesemer4023dce2009-08-14 17:41:52 -0700845type, called the element type.
846The number of elements is called the length and is never
Russ Cox461dd912009-03-04 14:44:51 -0800847negative.
848</p>
849
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700850<pre class="ebnf">
Russ Cox461dd912009-03-04 14:44:51 -0800851ArrayType = "[" ArrayLength "]" ElementType .
852ArrayLength = Expression .
Robert Griesemer4023dce2009-08-14 17:41:52 -0700853ElementType = Type .
Russ Cox461dd912009-03-04 14:44:51 -0800854</pre>
855
856<p>
Robert Griesemer2c83f1e2014-05-22 12:23:25 -0700857The length is part of the array's type; it must evaluate to a
858non-negative <a href="#Constants">constant</a> representable by a value
Robert Griesemer39067062012-12-12 11:06:26 -0800859of type <code>int</code>.
860The length of array <code>a</code> can be discovered
Robert Griesemercc065932012-09-14 11:31:56 -0700861using the built-in function <a href="#Length_and_capacity"><code>len</code></a>.
Robert Griesemer9c9e8112012-12-10 11:55:57 -0800862The elements can be addressed by integer <a href="#Index_expressions">indices</a>
Robert Griesemer39067062012-12-12 11:06:26 -08008630 through <code>len(a)-1</code>.
Rob Pikeff6a8fd2009-11-20 15:47:15 -0800864Array types are always one-dimensional but may be composed to form
865multi-dimensional types.
Russ Cox461dd912009-03-04 14:44:51 -0800866</p>
867
868<pre>
869[32]byte
870[2*N] struct { x, y int32 }
871[1000]*float64
Rob Pikeff6a8fd2009-11-20 15:47:15 -0800872[3][5]int
873[2][2][2]float64 // same as [2]([2]([2]float64))
Russ Cox461dd912009-03-04 14:44:51 -0800874</pre>
875
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700876<h3 id="Slice_types">Slice types</h3>
Russ Cox461dd912009-03-04 14:44:51 -0800877
878<p>
Robert Griesemer15da9972013-10-16 16:16:54 -0700879A slice is a descriptor for a contiguous segment of an <i>underlying array</i> and
Robert Griesemerb34f0552013-04-02 23:17:37 -0700880provides access to a numbered sequence of elements from that array.
881A slice type denotes the set of all slices of arrays of its element type.
Robert Griesemer7bc03712010-06-07 15:49:39 -0700882The value of an uninitialized slice is <code>nil</code>.
Russ Cox461dd912009-03-04 14:44:51 -0800883</p>
884
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700885<pre class="ebnf">
Russ Cox461dd912009-03-04 14:44:51 -0800886SliceType = "[" "]" ElementType .
887</pre>
888
889<p>
890Like arrays, slices are indexable and have a length. The length of a
891slice <code>s</code> can be discovered by the built-in function
Robert Griesemercc065932012-09-14 11:31:56 -0700892<a href="#Length_and_capacity"><code>len</code></a>; unlike with arrays it may change during
Robert Griesemer9c9e8112012-12-10 11:55:57 -0800893execution. The elements can be addressed by integer <a href="#Index_expressions">indices</a>
8940 through <code>len(s)-1</code>. The slice index of a
Russ Cox461dd912009-03-04 14:44:51 -0800895given element may be less than the index of the same element in the
896underlying array.
897</p>
898<p>
899A slice, once initialized, is always associated with an underlying
Rob Pike7ec08562010-01-09 07:32:26 +1100900array that holds its elements. A slice therefore shares storage
Russ Cox461dd912009-03-04 14:44:51 -0800901with its array and with other slices of the same array; by contrast,
902distinct arrays always represent distinct storage.
903</p>
904<p>
905The array underlying a slice may extend past the end of the slice.
Russ Cox5958dd62009-03-04 17:19:21 -0800906The <i>capacity</i> is a measure of that extent: it is the sum of
Russ Cox461dd912009-03-04 14:44:51 -0800907the length of the slice and the length of the array beyond the slice;
Robert Griesemer462a17e2013-03-22 15:36:04 -0700908a slice of length up to that capacity can be created by
Robert Griesemere333b962013-09-11 17:18:52 -0700909<a href="#Slice_expressions"><i>slicing</i></a> a new one from the original slice.
Russ Cox461dd912009-03-04 14:44:51 -0800910The capacity of a slice <code>a</code> can be discovered using the
Robert Griesemer0c2e6b32010-07-13 11:54:57 -0700911built-in function <a href="#Length_and_capacity"><code>cap(a)</code></a>.
Russ Cox461dd912009-03-04 14:44:51 -0800912</p>
913
Russ Cox461dd912009-03-04 14:44:51 -0800914<p>
Robert Griesemer0c2e6b32010-07-13 11:54:57 -0700915A new, initialized slice value for a given element type <code>T</code> is
916made using the built-in function
917<a href="#Making_slices_maps_and_channels"><code>make</code></a>,
918which takes a slice type
Robert Griesemer15da9972013-10-16 16:16:54 -0700919and parameters specifying the length and optionally the capacity.
920A slice created with <code>make</code> always allocates a new, hidden array
921to which the returned slice value refers. That is, executing
Russ Cox461dd912009-03-04 14:44:51 -0800922</p>
923
924<pre>
925make([]T, length, capacity)
926</pre>
927
928<p>
Robert Griesemer15da9972013-10-16 16:16:54 -0700929produces the same slice as allocating an array and <a href="#Slice_expressions">slicing</a>
930it, so these two expressions are equivalent:
Russ Cox461dd912009-03-04 14:44:51 -0800931</p>
932
933<pre>
934make([]int, 50, 100)
935new([100]int)[0:50]
936</pre>
937
Rob Pikeff6a8fd2009-11-20 15:47:15 -0800938<p>
939Like arrays, slices are always one-dimensional but may be composed to construct
940higher-dimensional objects.
941With arrays of arrays, the inner arrays are, by construction, always the same length;
Robert Griesemer15da9972013-10-16 16:16:54 -0700942however with slices of slices (or arrays of slices), the inner lengths may vary dynamically.
943Moreover, the inner slices must be initialized individually.
Rob Pikeff6a8fd2009-11-20 15:47:15 -0800944</p>
Russ Cox461dd912009-03-04 14:44:51 -0800945
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700946<h3 id="Struct_types">Struct types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700947
Rob Pike5af7de32009-02-24 15:17:59 -0800948<p>
Robert Griesemerd3b15652009-11-16 08:58:55 -0800949A struct is a sequence of named elements, called fields, each of which has a
950name and a type. Field names may be specified explicitly (IdentifierList) or
951implicitly (AnonymousField).
952Within a struct, non-<a href="#Blank_identifier">blank</a> field names must
Robert Griesemer103c9db2012-03-01 13:57:49 -0800953be <a href="#Uniqueness_of_identifiers">unique</a>.
Rob Pike5af7de32009-02-24 15:17:59 -0800954</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700955
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700956<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -0800957StructType = "struct" "{" { FieldDecl ";" } "}" .
Robert Griesemerd3b15652009-11-16 08:58:55 -0800958FieldDecl = (IdentifierList Type | AnonymousField) [ Tag ] .
959AnonymousField = [ "*" ] TypeName .
Robert Griesemer130ac742009-12-10 16:43:01 -0800960Tag = string_lit .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800961</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700962
Robert Griesemerc2d55862009-02-19 16:49:10 -0800963<pre>
964// An empty struct.
965struct {}
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700966
Robert Griesemer4e56b332009-09-10 10:14:00 -0700967// A struct with 6 fields.
Robert Griesemerc2d55862009-02-19 16:49:10 -0800968struct {
Robert Griesemer130ac742009-12-10 16:43:01 -0800969 x, y int
Robert Griesemerb94c0d22011-01-19 23:07:21 -0500970 u float32
971 _ float32 // padding
Robert Griesemer130ac742009-12-10 16:43:01 -0800972 A *[]int
973 F func()
Robert Griesemerc2d55862009-02-19 16:49:10 -0800974}
975</pre>
Robert Griesemer1f3e8422008-09-29 18:41:30 -0700976
Rob Pike5af7de32009-02-24 15:17:59 -0800977<p>
Russ Cox7c5d6402012-02-08 15:37:58 -0500978A field declared with a type but no explicit field name is an <i>anonymous field</i>,
979also called an <i>embedded</i> field or an embedding of the type in the struct.
980An embedded type must be specified as
Russ Coxbee2d5b2010-09-30 14:59:41 -0400981a type name <code>T</code> or as a pointer to a non-interface type name <code>*T</code>,
Ian Lance Taylor3e804ba2009-08-17 11:40:57 -0700982and <code>T</code> itself may not be
Robert Griesemerd3b15652009-11-16 08:58:55 -0800983a pointer type. The unqualified type name acts as the field name.
Rob Pike5af7de32009-02-24 15:17:59 -0800984</p>
Robert Griesemer1f3e8422008-09-29 18:41:30 -0700985
Robert Griesemerc2d55862009-02-19 16:49:10 -0800986<pre>
987// A struct with four anonymous fields of type T1, *T2, P.T3 and *P.T4
988struct {
Robert Griesemer130ac742009-12-10 16:43:01 -0800989 T1 // field name is T1
990 *T2 // field name is T2
991 P.T3 // field name is T3
992 *P.T4 // field name is T4
993 x, y int // field names are x and y
Robert Griesemerc2d55862009-02-19 16:49:10 -0800994}
995</pre>
Robert Griesemer1f3e8422008-09-29 18:41:30 -0700996
Rob Pike5af7de32009-02-24 15:17:59 -0800997<p>
Robert Griesemerd3b15652009-11-16 08:58:55 -0800998The following declaration is illegal because field names must be unique
999in a struct type:
Rob Pike5af7de32009-02-24 15:17:59 -08001000</p>
Robert Griesemer071c91b2008-10-23 12:04:45 -07001001
Robert Griesemerc2d55862009-02-19 16:49:10 -08001002<pre>
1003struct {
David Symonds72a29792011-11-29 15:47:36 -08001004 T // conflicts with anonymous field *T and *P.T
1005 *T // conflicts with anonymous field T and *P.T
1006 *P.T // conflicts with anonymous field T and *T
Robert Griesemerc2d55862009-02-19 16:49:10 -08001007}
1008</pre>
Robert Griesemer1f3e8422008-09-29 18:41:30 -07001009
Robert Griesemerc2d55862009-02-19 16:49:10 -08001010<p>
Robert Griesemer787adb62012-06-04 14:24:10 -07001011A field or <a href="#Method_declarations">method</a> <code>f</code> of an
1012anonymous field in a struct <code>x</code> is called <i>promoted</i> if
1013<code>x.f</code> is a legal <a href="#Selectors">selector</a> that denotes
1014that field or method <code>f</code>.
1015</p>
1016
1017<p>
1018Promoted fields act like ordinary fields
1019of a struct except that they cannot be used as field names in
1020<a href="#Composite_literals">composite literals</a> of the struct.
1021</p>
1022
1023<p>
1024Given a struct type <code>S</code> and a type named <code>T</code>,
1025promoted methods are included in the method set of the struct as follows:
Rob Pike5af7de32009-02-24 15:17:59 -08001026</p>
Robert Griesemer56809d02009-05-20 11:02:48 -07001027<ul>
Robert Griesemer787adb62012-06-04 14:24:10 -07001028 <li>
1029 If <code>S</code> contains an anonymous field <code>T</code>,
1030 the <a href="#Method_sets">method sets</a> of <code>S</code>
1031 and <code>*S</code> both include promoted methods with receiver
1032 <code>T</code>. The method set of <code>*S</code> also
1033 includes promoted methods with receiver <code>*T</code>.
Robert Griesemer56809d02009-05-20 11:02:48 -07001034 </li>
Robin Eklind1d46fc42012-12-11 12:17:53 -05001035
Robert Griesemer787adb62012-06-04 14:24:10 -07001036 <li>
1037 If <code>S</code> contains an anonymous field <code>*T</code>,
1038 the method sets of <code>S</code> and <code>*S</code> both
1039 include promoted methods with receiver <code>T</code> or
1040 <code>*T</code>.
Robert Griesemer56809d02009-05-20 11:02:48 -07001041 </li>
1042</ul>
Robert Griesemer787adb62012-06-04 14:24:10 -07001043
Rob Pike5af7de32009-02-24 15:17:59 -08001044<p>
Robert Griesemer56809d02009-05-20 11:02:48 -07001045A field declaration may be followed by an optional string literal <i>tag</i>,
Robert Griesemerd3b15652009-11-16 08:58:55 -08001046which becomes an attribute for all the fields in the corresponding
Rob Pike5af7de32009-02-24 15:17:59 -08001047field declaration. The tags are made
Robert Griesemer458632a22012-12-04 13:09:02 -08001048visible through a <a href="/pkg/reflect/#StructTag">reflection interface</a>
Emil Hessman13141312014-01-03 22:48:03 -08001049and take part in <a href="#Type_identity">type identity</a> for structs
Rob Pike5af7de32009-02-24 15:17:59 -08001050but are otherwise ignored.
1051</p>
Robert Griesemer2e90e542008-10-30 15:52:37 -07001052
Robert Griesemerc2d55862009-02-19 16:49:10 -08001053<pre>
Robert Griesemerc2d55862009-02-19 16:49:10 -08001054struct {
Robert Griesemer7305b552015-11-30 14:18:09 -08001055 x, y float64 "" // an empty tag string is like an absent tag
1056 name string "any string is permitted as a tag"
1057 _ [4]byte "ceci n'est pas un champ de structure"
1058}
1059
1060// A struct corresponding to a TimeStamp protocol buffer.
1061// The tag strings define the protocol buffer field numbers;
1062// they follow the convention outlined by the reflect package.
1063struct {
1064 microsec uint64 `protobuf:"1"`
1065 serverIP6 uint64 `protobuf:"2"`
Robert Griesemerc2d55862009-02-19 16:49:10 -08001066}
1067</pre>
Robert Griesemer2e90e542008-10-30 15:52:37 -07001068
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001069<h3 id="Pointer_types">Pointer types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001070
Rob Pike5af7de32009-02-24 15:17:59 -08001071<p>
Robert Griesemer6962c152014-10-16 15:08:49 -07001072A pointer type denotes the set of all pointers to <a href="#Variables">variables</a> of a given
Rob Pikeda389742009-03-02 19:13:40 -08001073type, called the <i>base type</i> of the pointer.
Peter Mundy5928e1d2010-11-09 10:10:57 -08001074The value of an uninitialized pointer is <code>nil</code>.
Rob Pike5af7de32009-02-24 15:17:59 -08001075</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001076
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001077<pre class="ebnf">
Robert Griesemerc2d55862009-02-19 16:49:10 -08001078PointerType = "*" BaseType .
Robin Eklindcac006a2014-08-30 10:27:01 -07001079BaseType = Type .
Robert Griesemerc2d55862009-02-19 16:49:10 -08001080</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001081
Robert Griesemerc2d55862009-02-19 16:49:10 -08001082<pre>
Robert Griesemer953f2de2012-03-01 10:35:15 -08001083*Point
1084*[4]int
Robert Griesemerc2d55862009-02-19 16:49:10 -08001085</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001086
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001087<h3 id="Function_types">Function types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001088
Rob Pike8f2330d2009-02-25 16:20:44 -08001089<p>
Robert Griesemer7231ceb2008-09-08 15:01:04 -07001090A function type denotes the set of all functions with the same parameter
Peter Mundy5928e1d2010-11-09 10:10:57 -08001091and result types. The value of an uninitialized variable of function type
Robert Griesemer7bc03712010-06-07 15:49:39 -07001092is <code>nil</code>.
Rob Pike8f2330d2009-02-25 16:20:44 -08001093</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001094
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001095<pre class="ebnf">
Rob Pike8f2330d2009-02-25 16:20:44 -08001096FunctionType = "func" Signature .
1097Signature = Parameters [ Result ] .
Robert Griesemer4023dce2009-08-14 17:41:52 -07001098Result = Parameters | Type .
Robert Griesemer130ac742009-12-10 16:43:01 -08001099Parameters = "(" [ ParameterList [ "," ] ] ")" .
Rob Pike8f2330d2009-02-25 16:20:44 -08001100ParameterList = ParameterDecl { "," ParameterDecl } .
Russ Cox95625922010-06-12 11:37:13 -07001101ParameterDecl = [ IdentifierList ] [ "..." ] Type .
Robert Griesemerc2d55862009-02-19 16:49:10 -08001102</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001103
Robert Griesemerc2d55862009-02-19 16:49:10 -08001104<p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001105Within a list of parameters or results, the names (IdentifierList)
1106must either all be present or all be absent. If present, each name
Robert Griesemer85e451e2012-11-29 14:47:47 -08001107stands for one item (parameter or result) of the specified type and
1108all non-<a href="#Blank_identifier">blank</a> names in the signature
1109must be <a href="#Uniqueness_of_identifiers">unique</a>.
1110If absent, each type stands for one item of that type.
1111Parameter and result
Rob Pike8f2330d2009-02-25 16:20:44 -08001112lists are always parenthesized except that if there is exactly
Robert Griesemer73ca1272010-07-09 13:02:54 -07001113one unnamed result it may be written as an unparenthesized type.
Rob Pike8f2330d2009-02-25 16:20:44 -08001114</p>
Russ Cox95625922010-06-12 11:37:13 -07001115
Robert Griesemerac771a82010-09-24 14:08:28 -07001116<p>
Robert Griesemer57c81ef2015-12-15 13:13:38 -08001117The final incoming parameter in a function signature may have
Robert Griesemerac771a82010-09-24 14:08:28 -07001118a type prefixed with <code>...</code>.
1119A function with such a parameter is called <i>variadic</i> and
1120may be invoked with zero or more arguments for that parameter.
Rob Pike8f2330d2009-02-25 16:20:44 -08001121</p>
Robert Griesemer2bfa9572008-10-24 13:13:12 -07001122
Robert Griesemerc2d55862009-02-19 16:49:10 -08001123<pre>
Russ Cox46871692010-01-26 10:25:56 -08001124func()
Robert Griesemer953f2de2012-03-01 10:35:15 -08001125func(x int) int
1126func(a, _ int, z float32) bool
Robert Griesemerb94c0d22011-01-19 23:07:21 -05001127func(a, b int, z float32) (bool)
Robert Griesemer953f2de2012-03-01 10:35:15 -08001128func(prefix string, values ...int)
Robert Griesemerb94c0d22011-01-19 23:07:21 -05001129func(a, b int, z float64, opt ...interface{}) (success bool)
1130func(int, int, float64) (float64, *[]int)
Russ Cox46871692010-01-26 10:25:56 -08001131func(n int) func(p *T)
Robert Griesemerc2d55862009-02-19 16:49:10 -08001132</pre>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08001133
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001134
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001135<h3 id="Interface_types">Interface types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001136
Rob Pike8f2330d2009-02-25 16:20:44 -08001137<p>
Robert Griesemer2a838d62011-02-08 13:31:01 -08001138An interface type specifies a <a href="#Method_sets">method set</a> called its <i>interface</i>.
Robert Griesemer56809d02009-05-20 11:02:48 -07001139A variable of interface type can store a value of any type with a method set
1140that is any superset of the interface. Such a type is said to
Robert Griesemer7bc03712010-06-07 15:49:39 -07001141<i>implement the interface</i>.
Peter Mundy5928e1d2010-11-09 10:10:57 -08001142The value of an uninitialized variable of interface type is <code>nil</code>.
Rob Pike8f2330d2009-02-25 16:20:44 -08001143</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001144
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001145<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -08001146InterfaceType = "interface" "{" { MethodSpec ";" } "}" .
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07001147MethodSpec = MethodName Signature | InterfaceTypeName .
1148MethodName = identifier .
Rob Pike8f2330d2009-02-25 16:20:44 -08001149InterfaceTypeName = TypeName .
Robert Griesemerc2d55862009-02-19 16:49:10 -08001150</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001151
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07001152<p>
Robert Griesemer103c9db2012-03-01 13:57:49 -08001153As with all method sets, in an interface type, each method must have a
Robert Griesemer2c83f1e2014-05-22 12:23:25 -07001154<a href="#Uniqueness_of_identifiers">unique</a>
1155non-<a href="#Blank_identifier">blank</a> name.
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07001156</p>
1157
Robert Griesemerc2d55862009-02-19 16:49:10 -08001158<pre>
Rob Pike8f2330d2009-02-25 16:20:44 -08001159// A simple File interface
Robert Griesemerc2d55862009-02-19 16:49:10 -08001160interface {
Robert Griesemer130ac742009-12-10 16:43:01 -08001161 Read(b Buffer) bool
1162 Write(b Buffer) bool
1163 Close()
Robert Griesemerc2d55862009-02-19 16:49:10 -08001164}
1165</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001166
Rob Pike8f2330d2009-02-25 16:20:44 -08001167<p>
Robert Griesemer56809d02009-05-20 11:02:48 -07001168More than one type may implement an interface.
Rob Pike8f2330d2009-02-25 16:20:44 -08001169For instance, if two types <code>S1</code> and <code>S2</code>
Robert Griesemer56809d02009-05-20 11:02:48 -07001170have the method set
Rob Pike8f2330d2009-02-25 16:20:44 -08001171</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001172
Robert Griesemerc2d55862009-02-19 16:49:10 -08001173<pre>
Robert Griesemer3c7271f2011-05-24 14:18:44 -07001174func (p T) Read(b Buffer) bool { return … }
1175func (p T) Write(b Buffer) bool { return … }
1176func (p T) Close() { … }
Robert Griesemerc2d55862009-02-19 16:49:10 -08001177</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001178
Rob Pike8f2330d2009-02-25 16:20:44 -08001179<p>
1180(where <code>T</code> stands for either <code>S1</code> or <code>S2</code>)
1181then the <code>File</code> interface is implemented by both <code>S1</code> and
1182<code>S2</code>, regardless of what other methods
1183<code>S1</code> and <code>S2</code> may have or share.
1184</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001185
Rob Pike8f2330d2009-02-25 16:20:44 -08001186<p>
1187A type implements any interface comprising any subset of its methods
1188and may therefore implement several distinct interfaces. For
1189instance, all types implement the <i>empty interface</i>:
1190</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001191
Robert Griesemerc2d55862009-02-19 16:49:10 -08001192<pre>
Robert Griesemer19b1d352009-09-24 19:36:48 -07001193interface{}
Robert Griesemerc2d55862009-02-19 16:49:10 -08001194</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001195
Rob Pike8f2330d2009-02-25 16:20:44 -08001196<p>
1197Similarly, consider this interface specification,
Robert Griesemer4ed666e2009-08-27 16:45:42 -07001198which appears within a <a href="#Type_declarations">type declaration</a>
Robert Griesemerbb29c5a2014-09-25 12:49:42 -07001199to define an interface called <code>Locker</code>:
Rob Pike8f2330d2009-02-25 16:20:44 -08001200</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001201
Robert Griesemerc2d55862009-02-19 16:49:10 -08001202<pre>
Robert Griesemerbb29c5a2014-09-25 12:49:42 -07001203type Locker interface {
Robert Griesemer130ac742009-12-10 16:43:01 -08001204 Lock()
1205 Unlock()
Robert Griesemerc2d55862009-02-19 16:49:10 -08001206}
1207</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001208
Rob Pike8f2330d2009-02-25 16:20:44 -08001209<p>
1210If <code>S1</code> and <code>S2</code> also implement
1211</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001212
Robert Griesemerc2d55862009-02-19 16:49:10 -08001213<pre>
Robert Griesemer3c7271f2011-05-24 14:18:44 -07001214func (p T) Lock() { … }
1215func (p T) Unlock() { … }
Robert Griesemerc2d55862009-02-19 16:49:10 -08001216</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001217
Robert Griesemerc2d55862009-02-19 16:49:10 -08001218<p>
Robert Griesemerbb29c5a2014-09-25 12:49:42 -07001219they implement the <code>Locker</code> interface as well
Rob Pike8f2330d2009-02-25 16:20:44 -08001220as the <code>File</code> interface.
1221</p>
Robert Griesemerbb29c5a2014-09-25 12:49:42 -07001222
Rob Pike8f2330d2009-02-25 16:20:44 -08001223<p>
Robert Griesemerbb29c5a2014-09-25 12:49:42 -07001224An interface <code>T</code> may use a (possibly qualified) interface type
1225name <code>E</code> in place of a method specification. This is called
1226<i>embedding</i> interface <code>E</code> in <code>T</code>; it adds
1227all (exported and non-exported) methods of <code>E</code> to the interface
1228<code>T</code>.
Rob Pike8f2330d2009-02-25 16:20:44 -08001229</p>
Robert Griesemer38c232f2009-02-11 15:09:15 -08001230
Robert Griesemerc2d55862009-02-19 16:49:10 -08001231<pre>
Robert Griesemerbb29c5a2014-09-25 12:49:42 -07001232type ReadWriter interface {
Robert Griesemer130ac742009-12-10 16:43:01 -08001233 Read(b Buffer) bool
1234 Write(b Buffer) bool
Robert Griesemerc2d55862009-02-19 16:49:10 -08001235}
Robert Griesemer38c232f2009-02-11 15:09:15 -08001236
Robert Griesemerc2d55862009-02-19 16:49:10 -08001237type File interface {
Robert Griesemerbb29c5a2014-09-25 12:49:42 -07001238 ReadWriter // same as adding the methods of ReadWriter
1239 Locker // same as adding the methods of Locker
Robert Griesemer130ac742009-12-10 16:43:01 -08001240 Close()
Robert Griesemerc2d55862009-02-19 16:49:10 -08001241}
Robert Griesemerbb29c5a2014-09-25 12:49:42 -07001242
1243type LockedFile interface {
1244 Locker
1245 File // illegal: Lock, Unlock not unique
1246 Lock() // illegal: Lock not unique
1247}
Robert Griesemerc2d55862009-02-19 16:49:10 -08001248</pre>
Robert Griesemer38c232f2009-02-11 15:09:15 -08001249
Russ Cox388816a2012-02-08 14:35:00 -05001250<p>
Russ Cox7c5d6402012-02-08 15:37:58 -05001251An interface type <code>T</code> may not embed itself
1252or any interface type that embeds <code>T</code>, recursively.
Russ Cox388816a2012-02-08 14:35:00 -05001253</p>
1254
1255<pre>
1256// illegal: Bad cannot embed itself
1257type Bad interface {
1258 Bad
1259}
1260
1261// illegal: Bad1 cannot embed itself using Bad2
1262type Bad1 interface {
1263 Bad2
1264}
1265type Bad2 interface {
1266 Bad1
1267}
1268</pre>
1269
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001270<h3 id="Map_types">Map types</h3>
Robert Griesemera3294712009-01-05 11:17:26 -08001271
Rob Pike8f2330d2009-02-25 16:20:44 -08001272<p>
1273A map is an unordered group of elements of one type, called the
Robert Griesemer0660d242009-11-15 17:42:27 -08001274element type, indexed by a set of unique <i>keys</i> of another type,
Robert Griesemer4023dce2009-08-14 17:41:52 -07001275called the key type.
Robert Griesemer7bc03712010-06-07 15:49:39 -07001276The value of an uninitialized map is <code>nil</code>.
Rob Pike8f2330d2009-02-25 16:20:44 -08001277</p>
Robert Griesemera3294712009-01-05 11:17:26 -08001278
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001279<pre class="ebnf">
Robert Griesemer19b1d352009-09-24 19:36:48 -07001280MapType = "map" "[" KeyType "]" ElementType .
Robert Griesemer4023dce2009-08-14 17:41:52 -07001281KeyType = Type .
Robert Griesemerc2d55862009-02-19 16:49:10 -08001282</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001283
Robert Griesemerc2d55862009-02-19 16:49:10 -08001284<p>
Robert Griesemer462a17e2013-03-22 15:36:04 -07001285The <a href="#Comparison_operators">comparison operators</a>
1286<code>==</code> and <code>!=</code> must be fully defined
Robert Griesemer9c3d8762012-01-30 15:31:33 -08001287for operands of the key type; thus the key type must not be a function, map, or
1288slice.
Robert Griesemer1d282a82010-06-03 16:55:50 -07001289If the key type is an interface type, these
Rob Pike8f2330d2009-02-25 16:20:44 -08001290comparison operators must be defined for the dynamic key values;
Rob Pike5bb29fb2010-03-25 17:59:59 -07001291failure will cause a <a href="#Run_time_panics">run-time panic</a>.
Rob Pike8f2330d2009-02-25 16:20:44 -08001292
1293</p>
Robert Griesemera3294712009-01-05 11:17:26 -08001294
Robert Griesemerc2d55862009-02-19 16:49:10 -08001295<pre>
David Symonds72a29792011-11-29 15:47:36 -08001296map[string]int
1297map[*T]struct{ x, y float64 }
1298map[string]interface{}
Robert Griesemerc2d55862009-02-19 16:49:10 -08001299</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001300
Rob Pike5af7de32009-02-24 15:17:59 -08001301<p>
Robert Griesemer0c2e6b32010-07-13 11:54:57 -07001302The number of map elements is called its length.
1303For a map <code>m</code>, it can be discovered using the
Robert Griesemercc065932012-09-14 11:31:56 -07001304built-in function <a href="#Length_and_capacity"><code>len</code></a>
Robert Griesemer3e0c0a82011-10-17 12:53:10 -07001305and may change during execution. Elements may be added during execution
1306using <a href="#Assignments">assignments</a> and retrieved with
Robert Griesemer9c9e8112012-12-10 11:55:57 -08001307<a href="#Index_expressions">index expressions</a>; they may be removed with the
Robert Griesemer3e0c0a82011-10-17 12:53:10 -07001308<a href="#Deletion_of_map_elements"><code>delete</code></a> built-in function.
Rob Pike5af7de32009-02-24 15:17:59 -08001309</p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001310<p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001311A new, empty map value is made using the built-in
Robert Griesemer0c2e6b32010-07-13 11:54:57 -07001312function <a href="#Making_slices_maps_and_channels"><code>make</code></a>,
1313which takes the map type and an optional capacity hint as arguments:
Rob Pike8f2330d2009-02-25 16:20:44 -08001314</p>
1315
1316<pre>
David Symonds72a29792011-11-29 15:47:36 -08001317make(map[string]int)
1318make(map[string]int, 100)
Rob Pike8f2330d2009-02-25 16:20:44 -08001319</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001320
Rob Pikeda389742009-03-02 19:13:40 -08001321<p>
1322The initial capacity does not bound its size:
1323maps grow to accommodate the number of items
Robert Griesemer54731032011-05-12 09:15:59 -07001324stored in them, with the exception of <code>nil</code> maps.
1325A <code>nil</code> map is equivalent to an empty map except that no elements
1326may be added.
Rob Pikeda389742009-03-02 19:13:40 -08001327
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001328<h3 id="Channel_types">Channel types</h3>
Robert Griesemera3294712009-01-05 11:17:26 -08001329
Rob Pike8f2330d2009-02-25 16:20:44 -08001330<p>
Robert Griesemer97aa90d2014-05-07 10:40:39 -07001331A channel provides a mechanism for
1332<a href="#Go_statements">concurrently executing functions</a>
1333to communicate by
1334<a href="#Send_statements">sending</a> and
1335<a href="#Receive_operator">receiving</a>
1336values of a specified element type.
Robert Griesemer7bc03712010-06-07 15:49:39 -07001337The value of an uninitialized channel is <code>nil</code>.
Rob Pike8f2330d2009-02-25 16:20:44 -08001338</p>
Robert Griesemera3294712009-01-05 11:17:26 -08001339
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001340<pre class="ebnf">
Robert Griesemer97aa90d2014-05-07 10:40:39 -07001341ChannelType = ( "chan" | "chan" "&lt;-" | "&lt;-" "chan" ) ElementType .
Robert Griesemerc2d55862009-02-19 16:49:10 -08001342</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001343
Rob Pike8f2330d2009-02-25 16:20:44 -08001344<p>
Robert Griesemer97aa90d2014-05-07 10:40:39 -07001345The optional <code>&lt;-</code> operator specifies the channel <i>direction</i>,
Robert Griesemer56ca6972010-05-07 18:22:40 -07001346<i>send</i> or <i>receive</i>. If no direction is given, the channel is
Robert Griesemer97aa90d2014-05-07 10:40:39 -07001347<i>bidirectional</i>.
Robert Griesemer56ca6972010-05-07 18:22:40 -07001348A channel may be constrained only to send or only to receive by
1349<a href="#Conversions">conversion</a> or <a href="#Assignments">assignment</a>.
1350</p>
1351
1352<pre>
David Symonds72a29792011-11-29 15:47:36 -08001353chan T // can be used to send and receive values of type T
1354chan&lt;- float64 // can only be used to send float64s
1355&lt;-chan int // can only be used to receive ints
Robert Griesemer56ca6972010-05-07 18:22:40 -07001356</pre>
1357
1358<p>
1359The <code>&lt;-</code> operator associates with the leftmost <code>chan</code>
1360possible:
Robert Griesemer1c369bd2010-01-27 09:35:39 -08001361</p>
1362
1363<pre>
David Symonds72a29792011-11-29 15:47:36 -08001364chan&lt;- chan int // same as chan&lt;- (chan int)
1365chan&lt;- &lt;-chan int // same as chan&lt;- (&lt;-chan int)
1366&lt;-chan &lt;-chan int // same as &lt;-chan (&lt;-chan int)
Robert Griesemer1c369bd2010-01-27 09:35:39 -08001367chan (&lt;-chan int)
1368</pre>
1369
1370<p>
Robert Griesemer7bc03712010-06-07 15:49:39 -07001371A new, initialized channel
Robert Griesemer56ca6972010-05-07 18:22:40 -07001372value can be made using the built-in function
1373<a href="#Making_slices_maps_and_channels"><code>make</code></a>,
Robert Griesemer97aa90d2014-05-07 10:40:39 -07001374which takes the channel type and an optional <i>capacity</i> as arguments:
Rob Pike8f2330d2009-02-25 16:20:44 -08001375</p>
Robert Griesemera3294712009-01-05 11:17:26 -08001376
Robert Griesemerc2d55862009-02-19 16:49:10 -08001377<pre>
Rob Pikeda389742009-03-02 19:13:40 -08001378make(chan int, 100)
Robert Griesemerc2d55862009-02-19 16:49:10 -08001379</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001380
Rob Pike8f2330d2009-02-25 16:20:44 -08001381<p>
Robert Griesemer97aa90d2014-05-07 10:40:39 -07001382The capacity, in number of elements, sets the size of the buffer in the channel.
1383If the capacity is zero or absent, the channel is unbuffered and communication
Robert Griesemer61d8a332014-05-14 11:47:19 -07001384succeeds only when both a sender and receiver are ready. Otherwise, the channel
1385is buffered and communication succeeds without blocking if the buffer
Robert Griesemer97aa90d2014-05-07 10:40:39 -07001386is not full (sends) or not empty (receives).
Robert Griesemer54731032011-05-12 09:15:59 -07001387A <code>nil</code> channel is never ready for communication.
Rob Pike8f2330d2009-02-25 16:20:44 -08001388</p>
Robert Griesemera3294712009-01-05 11:17:26 -08001389
Rob Pike94b67eb2009-03-24 17:40:47 -07001390<p>
Russ Cox9f2cb862011-03-11 14:47:02 -05001391A channel may be closed with the built-in function
Robert Griesemer97aa90d2014-05-07 10:40:39 -07001392<a href="#Close"><code>close</code></a>.
1393The multi-valued assignment form of the
Russ Cox9f2cb862011-03-11 14:47:02 -05001394<a href="#Receive_operator">receive operator</a>
Robert Griesemer97aa90d2014-05-07 10:40:39 -07001395reports whether a received value was sent before
1396the channel was closed.
1397</p>
1398
1399<p>
1400A single channel may be used in
1401<a href="#Send_statements">send statements</a>,
1402<a href="#Receive_operator">receive operations</a>,
1403and calls to the built-in functions
1404<a href="#Length_and_capacity"><code>cap</code></a> and
1405<a href="#Length_and_capacity"><code>len</code></a>
1406by any number of goroutines without further synchronization.
1407Channels act as first-in-first-out queues.
1408For example, if one goroutine sends values on a channel
1409and a second goroutine receives them, the values are
1410received in the order sent.
Rob Pike94b67eb2009-03-24 17:40:47 -07001411</p>
1412
Rob Pike83cbca52009-08-21 14:18:08 -07001413<h2 id="Properties_of_types_and_values">Properties of types and values</h2>
Robert Griesemer434c6052008-11-07 13:34:37 -08001414
Robert Griesemer7bc03712010-06-07 15:49:39 -07001415<h3 id="Type_identity">Type identity</h3>
1416
Rob Pike4501d342009-02-19 17:31:36 -08001417<p>
Robert Griesemer7bc03712010-06-07 15:49:39 -07001418Two types are either <i>identical</i> or <i>different</i>.
Robert Griesemer19b1d352009-09-24 19:36:48 -07001419</p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001420
Robert Griesemerc2d55862009-02-19 16:49:10 -08001421<p>
Robert Griesemer8268ead2013-07-09 21:12:53 -07001422Two <a href="#Types">named types</a> are identical if their type names originate in the same
Robert Griesemer82fc28c2012-03-12 20:27:27 -07001423<a href="#Type_declarations">TypeSpec</a>.
Robert Griesemer8268ead2013-07-09 21:12:53 -07001424A named and an <a href="#Types">unnamed type</a> are always different. Two unnamed types are identical
Robert Griesemer7bc03712010-06-07 15:49:39 -07001425if the corresponding type literals are identical, that is, if they have the same
Robert Griesemer63f01492010-05-28 14:17:30 -07001426literal structure and corresponding components have identical types. In detail:
Rob Pike4501d342009-02-19 17:31:36 -08001427</p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001428
Robert Griesemerc2d55862009-02-19 16:49:10 -08001429<ul>
Robert Griesemer533dfd62009-05-13 16:56:00 -07001430 <li>Two array types are identical if they have identical element types and
1431 the same array length.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001432
Robert Griesemer533dfd62009-05-13 16:56:00 -07001433 <li>Two slice types are identical if they have identical element types.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001434
Robert Griesemer533dfd62009-05-13 16:56:00 -07001435 <li>Two struct types are identical if they have the same sequence of fields,
Russ Coxe4953512010-06-21 12:42:33 -07001436 and if corresponding fields have the same names, and identical types,
1437 and identical tags.
Robert Griesemer63f01492010-05-28 14:17:30 -07001438 Two anonymous fields are considered to have the same name. Lower-case field
1439 names from different packages are always different.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001440
Robert Griesemer533dfd62009-05-13 16:56:00 -07001441 <li>Two pointer types are identical if they have identical base types.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001442
Robert Griesemer533dfd62009-05-13 16:56:00 -07001443 <li>Two function types are identical if they have the same number of parameters
Russ Cox95625922010-06-12 11:37:13 -07001444 and result values, corresponding parameter and result types are
1445 identical, and either both functions are variadic or neither is.
Robert Griesemer533dfd62009-05-13 16:56:00 -07001446 Parameter and result names are not required to match.</li>
Robert Griesemera3294712009-01-05 11:17:26 -08001447
Robert Griesemer533dfd62009-05-13 16:56:00 -07001448 <li>Two interface types are identical if they have the same set of methods
Robert Griesemer63f01492010-05-28 14:17:30 -07001449 with the same names and identical function types. Lower-case method names from
1450 different packages are always different. The order of the methods is irrelevant.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001451
Robert Griesemer533dfd62009-05-13 16:56:00 -07001452 <li>Two map types are identical if they have identical key and value types.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001453
Robert Griesemer533dfd62009-05-13 16:56:00 -07001454 <li>Two channel types are identical if they have identical value types and
1455 the same direction.</li>
Robert Griesemerc2d55862009-02-19 16:49:10 -08001456</ul>
Robert Griesemer434c6052008-11-07 13:34:37 -08001457
Robert Griesemerc2d55862009-02-19 16:49:10 -08001458<p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001459Given the declarations
1460</p>
Robert Griesemer434c6052008-11-07 13:34:37 -08001461
Robert Griesemerc2d55862009-02-19 16:49:10 -08001462<pre>
1463type (
Robert Griesemer130ac742009-12-10 16:43:01 -08001464 T0 []string
1465 T1 []string
David Symonds72a29792011-11-29 15:47:36 -08001466 T2 struct{ a, b int }
1467 T3 struct{ a, c int }
Robert Griesemerb94c0d22011-01-19 23:07:21 -05001468 T4 func(int, float64) *T0
1469 T5 func(x int, y float64) *[]string
Robert Griesemerc2d55862009-02-19 16:49:10 -08001470)
1471</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -08001472
Rob Pike8f2330d2009-02-25 16:20:44 -08001473<p>
Robert Griesemer533dfd62009-05-13 16:56:00 -07001474these types are identical:
Rob Pike8f2330d2009-02-25 16:20:44 -08001475</p>
Robert Griesemer434c6052008-11-07 13:34:37 -08001476
Robert Griesemerc2d55862009-02-19 16:49:10 -08001477<pre>
1478T0 and T0
1479[]int and []int
David Symonds72a29792011-11-29 15:47:36 -08001480struct{ a, b *T5 } and struct{ a, b *T5 }
Robert Griesemerb94c0d22011-01-19 23:07:21 -05001481func(x int, y float64) *[]string and func(int, float64) (result *[]string)
Robert Griesemerc2d55862009-02-19 16:49:10 -08001482</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -08001483
Rob Pike8f2330d2009-02-25 16:20:44 -08001484<p>
Robert Griesemer7bc03712010-06-07 15:49:39 -07001485<code>T0</code> and <code>T1</code> are different because they are named types
Robert Griesemerb94c0d22011-01-19 23:07:21 -05001486with distinct declarations; <code>func(int, float64) *T0</code> and
1487<code>func(x int, y float64) *[]string</code> are different because <code>T0</code>
Robert Griesemer7bc03712010-06-07 15:49:39 -07001488is different from <code>[]string</code>.
Robert Griesemer533dfd62009-05-13 16:56:00 -07001489</p>
1490
Robert Griesemer434c6052008-11-07 13:34:37 -08001491
Robert Griesemer440cc952010-06-07 17:40:21 -07001492<h3 id="Assignability">Assignability</h3>
Rob Pike5af7de32009-02-24 15:17:59 -08001493
Rob Pike5af7de32009-02-24 15:17:59 -08001494<p>
Robert Griesemer6962c152014-10-16 15:08:49 -07001495A value <code>x</code> is <i>assignable</i> to a <a href="#Variables">variable</a> of type <code>T</code>
Robert Griesemer440cc952010-06-07 17:40:21 -07001496("<code>x</code> is assignable to <code>T</code>") in any of these cases:
Rob Pike5af7de32009-02-24 15:17:59 -08001497</p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07001498
Rob Pike5af7de32009-02-24 15:17:59 -08001499<ul>
1500<li>
Robert Griesemer7bc03712010-06-07 15:49:39 -07001501<code>x</code>'s type is identical to <code>T</code>.
1502</li>
1503<li>
Rob Pike68f16092010-09-01 10:40:50 +10001504<code>x</code>'s type <code>V</code> and <code>T</code> have identical
1505<a href="#Types">underlying types</a> and at least one of <code>V</code>
Robert Griesemer8268ead2013-07-09 21:12:53 -07001506or <code>T</code> is not a <a href="#Types">named type</a>.
Rob Pike5af7de32009-02-24 15:17:59 -08001507</li>
1508<li>
Robert Griesemer19b1d352009-09-24 19:36:48 -07001509<code>T</code> is an interface type and
Robert Griesemer7bc03712010-06-07 15:49:39 -07001510<code>x</code> <a href="#Interface_types">implements</a> <code>T</code>.
Robert Griesemere2cb60b2009-06-19 13:03:01 -07001511</li>
1512<li>
Robert Griesemer7bc03712010-06-07 15:49:39 -07001513<code>x</code> is a bidirectional channel value, <code>T</code> is a channel type,
1514<code>x</code>'s type <code>V</code> and <code>T</code> have identical element types,
Rob Pike68f16092010-09-01 10:40:50 +10001515and at least one of <code>V</code> or <code>T</code> is not a named type.
Robert Griesemer7bc03712010-06-07 15:49:39 -07001516</li>
1517<li>
1518<code>x</code> is the predeclared identifier <code>nil</code> and <code>T</code>
1519is a pointer, function, slice, map, channel, or interface type.
1520</li>
1521<li>
1522<code>x</code> is an untyped <a href="#Constants">constant</a> representable
1523by a value of type <code>T</code>.
Robert Griesemer4e56b332009-09-10 10:14:00 -07001524</li>
Rob Pike5af7de32009-02-24 15:17:59 -08001525</ul>
1526
Rob Pikea9ed30f2009-02-23 19:26:07 -08001527
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001528<h2 id="Blocks">Blocks</h2>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001529
1530<p>
Robert Griesemer9905cec2013-03-04 13:55:35 -08001531A <i>block</i> is a possibly empty sequence of declarations and statements
1532within matching brace brackets.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001533</p>
1534
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001535<pre class="ebnf">
Robert Griesemer9905cec2013-03-04 13:55:35 -08001536Block = "{" StatementList "}" .
1537StatementList = { Statement ";" } .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001538</pre>
Russ Cox5958dd62009-03-04 17:19:21 -08001539
Rob Pikea9ed30f2009-02-23 19:26:07 -08001540<p>
Robert Griesemer0a162a12009-08-19 16:44:04 -07001541In addition to explicit blocks in the source code, there are implicit blocks:
Rob Pikea9ed30f2009-02-23 19:26:07 -08001542</p>
1543
1544<ol>
Robert Griesemer0a162a12009-08-19 16:44:04 -07001545 <li>The <i>universe block</i> encompasses all Go source text.</li>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001546
Robert Griesemer4e56b332009-09-10 10:14:00 -07001547 <li>Each <a href="#Packages">package</a> has a <i>package block</i> containing all
Robert Griesemer0a162a12009-08-19 16:44:04 -07001548 Go source text for that package.</li>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001549
Robert Griesemer0a162a12009-08-19 16:44:04 -07001550 <li>Each file has a <i>file block</i> containing all Go source text
1551 in that file.</li>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001552
Robert Griesemer9905cec2013-03-04 13:55:35 -08001553 <li>Each <a href="#If_statements">"if"</a>,
1554 <a href="#For_statements">"for"</a>, and
1555 <a href="#Switch_statements">"switch"</a>
Robert Griesemer0a162a12009-08-19 16:44:04 -07001556 statement is considered to be in its own implicit block.</li>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001557
Robert Griesemer9905cec2013-03-04 13:55:35 -08001558 <li>Each clause in a <a href="#Switch_statements">"switch"</a>
1559 or <a href="#Select_statements">"select"</a> statement
Robert Griesemer0a162a12009-08-19 16:44:04 -07001560 acts as an implicit block.</li>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001561</ol>
1562
Robert Griesemer0a162a12009-08-19 16:44:04 -07001563<p>
Robert Griesemer4e56b332009-09-10 10:14:00 -07001564Blocks nest and influence <a href="#Declarations_and_scope">scoping</a>.
Robert Griesemer0a162a12009-08-19 16:44:04 -07001565</p>
1566
1567
Robert Griesemeraeaab592009-08-31 17:30:55 -07001568<h2 id="Declarations_and_scope">Declarations and scope</h2>
Robert Griesemer0a162a12009-08-19 16:44:04 -07001569
1570<p>
Robert Griesemer4cc71e32013-10-03 16:38:22 -07001571A <i>declaration</i> binds a non-<a href="#Blank_identifier">blank</a> identifier to a
1572<a href="#Constant_declarations">constant</a>,
1573<a href="#Type_declarations">type</a>,
1574<a href="#Variable_declarations">variable</a>,
1575<a href="#Function_declarations">function</a>,
1576<a href="#Labeled_statements">label</a>, or
1577<a href="#Import_declarations">package</a>.
Robert Griesemer0a162a12009-08-19 16:44:04 -07001578Every identifier in a program must be declared.
1579No identifier may be declared twice in the same block, and
1580no identifier may be declared in both the file and package block.
1581</p>
1582
Robert Griesemerf57bf7a2013-11-12 21:06:54 -05001583<p>
1584The <a href="#Blank_identifier">blank identifier</a> may be used like any other identifier
1585in a declaration, but it does not introduce a binding and thus is not declared.
Robert Griesemera4366982014-05-20 13:51:39 -07001586In the package block, the identifier <code>init</code> may only be used for
1587<a href="#Package_initialization"><code>init</code> function</a> declarations,
1588and like the blank identifier it does not introduce a new binding.
Robert Griesemerf57bf7a2013-11-12 21:06:54 -05001589</p>
1590
Robert Griesemer0a162a12009-08-19 16:44:04 -07001591<pre class="ebnf">
1592Declaration = ConstDecl | TypeDecl | VarDecl .
1593TopLevelDecl = Declaration | FunctionDecl | MethodDecl .
1594</pre>
1595
1596<p>
1597The <i>scope</i> of a declared identifier is the extent of source text in which
Robert Griesemer4cc71e32013-10-03 16:38:22 -07001598the identifier denotes the specified constant, type, variable, function, label, or package.
Robert Griesemer0a162a12009-08-19 16:44:04 -07001599</p>
1600
1601<p>
Robert Griesemer4cc71e32013-10-03 16:38:22 -07001602Go is lexically scoped using <a href="#Blocks">blocks</a>:
Robert Griesemer0a162a12009-08-19 16:44:04 -07001603</p>
1604
1605<ol>
Robert Griesemer4cc71e32013-10-03 16:38:22 -07001606 <li>The scope of a <a href="#Predeclared_identifiers">predeclared identifier</a> is the universe block.</li>
Robert Griesemer0a162a12009-08-19 16:44:04 -07001607
1608 <li>The scope of an identifier denoting a constant, type, variable,
Robert Griesemer967a2b32011-03-03 15:24:28 -08001609 or function (but not method) declared at top level (outside any
1610 function) is the package block.</li>
Robert Griesemer0a162a12009-08-19 16:44:04 -07001611
Robert Griesemere1267632012-11-21 14:40:50 -08001612 <li>The scope of the package name of an imported package is the file block
Robert Griesemer0a162a12009-08-19 16:44:04 -07001613 of the file containing the import declaration.</li>
1614
Robert Griesemer85e451e2012-11-29 14:47:47 -08001615 <li>The scope of an identifier denoting a method receiver, function parameter,
1616 or result variable is the function body.</li>
Robert Griesemer0a162a12009-08-19 16:44:04 -07001617
1618 <li>The scope of a constant or variable identifier declared
1619 inside a function begins at the end of the ConstSpec or VarSpec
Robert Griesemer95b81372011-06-12 12:09:50 -07001620 (ShortVarDecl for short variable declarations)
Robert Griesemer0a162a12009-08-19 16:44:04 -07001621 and ends at the end of the innermost containing block.</li>
1622
1623 <li>The scope of a type identifier declared inside a function
Russ Cox16b95ba2009-08-20 10:22:52 -07001624 begins at the identifier in the TypeSpec
Robert Griesemer0a162a12009-08-19 16:44:04 -07001625 and ends at the end of the innermost containing block.</li>
1626</ol>
1627
1628<p>
1629An identifier declared in a block may be redeclared in an inner block.
1630While the identifier of the inner declaration is in scope, it denotes
1631the entity declared by the inner declaration.
1632</p>
1633
1634<p>
Robert Griesemer506c0082009-09-08 15:41:14 -07001635The <a href="#Package_clause">package clause</a> is not a declaration; the package name
Robert Griesemer0a162a12009-08-19 16:44:04 -07001636does not appear in any scope. Its purpose is to identify the files belonging
Robert Griesemer997851e2009-09-25 15:36:25 -07001637to the same <a href="#Packages">package</a> and to specify the default package name for import
Robert Griesemer0a162a12009-08-19 16:44:04 -07001638declarations.
1639</p>
1640
1641
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001642<h3 id="Label_scopes">Label scopes</h3>
Robert Griesemer0a162a12009-08-19 16:44:04 -07001643
1644<p>
Robert Griesemer506c0082009-09-08 15:41:14 -07001645Labels are declared by <a href="#Labeled_statements">labeled statements</a> and are
Robert Griesemer462a17e2013-03-22 15:36:04 -07001646used in the <a href="#Break_statements">"break"</a>,
1647<a href="#Continue_statements">"continue"</a>, and
1648<a href="#Goto_statements">"goto"</a> statements.
Russ Cox108564d2011-03-15 13:51:24 -04001649It is illegal to define a label that is never used.
Robert Griesemer0a162a12009-08-19 16:44:04 -07001650In contrast to other identifiers, labels are not block scoped and do
1651not conflict with identifiers that are not labels. The scope of a label
1652is the body of the function in which it is declared and excludes
1653the body of any nested function.
1654</p>
1655
1656
Robert Griesemer103c9db2012-03-01 13:57:49 -08001657<h3 id="Blank_identifier">Blank identifier</h3>
1658
1659<p>
Robert Griesemerf57bf7a2013-11-12 21:06:54 -05001660The <i>blank identifier</i> is represented by the underscore character <code>_</code>.
1661It serves as an anonymous placeholder instead of a regular (non-blank)
1662identifier and has special meaning in <a href="#Declarations_and_scope">declarations</a>,
1663as an <a href="#Operands">operand</a>, and in <a href="#Assignments">assignments</a>.
Robert Griesemer103c9db2012-03-01 13:57:49 -08001664</p>
1665
1666
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001667<h3 id="Predeclared_identifiers">Predeclared identifiers</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001668
1669<p>
Robert Griesemer103c9db2012-03-01 13:57:49 -08001670The following identifiers are implicitly declared in the
1671<a href="#Blocks">universe block</a>:
Rob Pikea9ed30f2009-02-23 19:26:07 -08001672</p>
1673<pre class="grammar">
Russ Coxd9877e22011-11-01 21:45:02 -04001674Types:
1675 bool byte complex64 complex128 error float32 float64
1676 int int8 int16 int32 int64 rune string
1677 uint uint8 uint16 uint32 uint64 uintptr
Rob Pikea9ed30f2009-02-23 19:26:07 -08001678
1679Constants:
Robert Griesemer19b1d352009-09-24 19:36:48 -07001680 true false iota
1681
1682Zero value:
1683 nil
Rob Pikea9ed30f2009-02-23 19:26:07 -08001684
1685Functions:
Robert Griesemerb910a272011-11-01 01:09:22 -04001686 append cap close complex copy delete imag len
Robert Griesemer07e983a2010-10-25 16:50:31 -07001687 make new panic print println real recover
Rob Pikea9ed30f2009-02-23 19:26:07 -08001688</pre>
1689
Robert Griesemeraeaab592009-08-31 17:30:55 -07001690
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001691<h3 id="Exported_identifiers">Exported identifiers</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001692
1693<p>
Robert Griesemer103c9db2012-03-01 13:57:49 -08001694An identifier may be <i>exported</i> to permit access to it from another package.
1695An identifier is exported if both:
Rob Pikea9ed30f2009-02-23 19:26:07 -08001696</p>
1697<ol>
Robert Griesemer103c9db2012-03-01 13:57:49 -08001698 <li>the first character of the identifier's name is a Unicode upper case
1699 letter (Unicode class "Lu"); and</li>
1700 <li>the identifier is declared in the <a href="#Blocks">package block</a>
1701 or it is a <a href="#Struct_types">field name</a> or
1702 <a href="#MethodName">method name</a>.</li>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001703</ol>
1704<p>
Robert Griesemeraeaab592009-08-31 17:30:55 -07001705All other identifiers are not exported.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001706</p>
1707
Robert Griesemeraeaab592009-08-31 17:30:55 -07001708
Robert Griesemer103c9db2012-03-01 13:57:49 -08001709<h3 id="Uniqueness_of_identifiers">Uniqueness of identifiers</h3>
Robert Griesemer4e56b332009-09-10 10:14:00 -07001710
1711<p>
Robert Griesemer103c9db2012-03-01 13:57:49 -08001712Given a set of identifiers, an identifier is called <i>unique</i> if it is
1713<i>different</i> from every other in the set.
1714Two identifiers are different if they are spelled differently, or if they
1715appear in different <a href="#Packages">packages</a> and are not
Shenghou Ma2195f1a2012-03-30 14:04:03 +08001716<a href="#Exported_identifiers">exported</a>. Otherwise, they are the same.
Robert Griesemer4e56b332009-09-10 10:14:00 -07001717</p>
1718
Robert Griesemer19b1d352009-09-24 19:36:48 -07001719<h3 id="Constant_declarations">Constant declarations</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001720
1721<p>
1722A constant declaration binds a list of identifiers (the names of
Robert Griesemer506c0082009-09-08 15:41:14 -07001723the constants) to the values of a list of <a href="#Constant_expressions">constant expressions</a>.
1724The number of identifiers must be equal
1725to the number of expressions, and the <i>n</i>th identifier on
1726the left is bound to the value of the <i>n</i>th expression on the
Rob Pikea9ed30f2009-02-23 19:26:07 -08001727right.
1728</p>
1729
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001730<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -08001731ConstDecl = "const" ( ConstSpec | "(" { ConstSpec ";" } ")" ) .
Robert Griesemer4023dce2009-08-14 17:41:52 -07001732ConstSpec = IdentifierList [ [ Type ] "=" ExpressionList ] .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001733
1734IdentifierList = identifier { "," identifier } .
1735ExpressionList = Expression { "," Expression } .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001736</pre>
1737
1738<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07001739If the type is present, all constants take the type specified, and
Robert Griesemer440cc952010-06-07 17:40:21 -07001740the expressions must be <a href="#Assignability">assignable</a> to that type.
Robert Griesemer4023dce2009-08-14 17:41:52 -07001741If the type is omitted, the constants take the
Robert Griesemer19b1d352009-09-24 19:36:48 -07001742individual types of the corresponding expressions.
1743If the expression values are untyped <a href="#Constants">constants</a>,
1744the declared constants remain untyped and the constant identifiers
1745denote the constant values. For instance, if the expression is a
1746floating-point literal, the constant identifier denotes a floating-point
1747constant, even if the literal's fractional part is zero.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001748</p>
1749
1750<pre>
1751const Pi float64 = 3.14159265358979323846
David Symonds72a29792011-11-29 15:47:36 -08001752const zero = 0.0 // untyped floating-point constant
Rob Pikea9ed30f2009-02-23 19:26:07 -08001753const (
Robert Griesemer130ac742009-12-10 16:43:01 -08001754 size int64 = 1024
David Symonds72a29792011-11-29 15:47:36 -08001755 eof = -1 // untyped integer constant
Rob Pikea9ed30f2009-02-23 19:26:07 -08001756)
Robert Griesemer19b1d352009-09-24 19:36:48 -07001757const a, b, c = 3, 4, "foo" // a = 3, b = 4, c = "foo", untyped integer and string constants
Robert Griesemerb94c0d22011-01-19 23:07:21 -05001758const u, v float32 = 0, 3 // u = 0.0, v = 3.0
Rob Pikea9ed30f2009-02-23 19:26:07 -08001759</pre>
1760
1761<p>
1762Within a parenthesized <code>const</code> declaration list the
1763expression list may be omitted from any but the first declaration.
1764Such an empty list is equivalent to the textual substitution of the
Rob Pike678625d2009-09-15 09:54:22 -07001765first preceding non-empty expression list and its type if any.
Russ Cox5958dd62009-03-04 17:19:21 -08001766Omitting the list of expressions is therefore equivalent to
1767repeating the previous list. The number of identifiers must be equal
1768to the number of expressions in the previous list.
Robert Griesemer506c0082009-09-08 15:41:14 -07001769Together with the <a href="#Iota"><code>iota</code> constant generator</a>
1770this mechanism permits light-weight declaration of sequential values:
Rob Pikea9ed30f2009-02-23 19:26:07 -08001771</p>
1772
1773<pre>
1774const (
Robert Griesemer130ac742009-12-10 16:43:01 -08001775 Sunday = iota
1776 Monday
1777 Tuesday
1778 Wednesday
1779 Thursday
1780 Friday
1781 Partyday
1782 numberOfDays // this constant is not exported
Rob Pikea9ed30f2009-02-23 19:26:07 -08001783)
1784</pre>
1785
1786
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001787<h3 id="Iota">Iota</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001788
1789<p>
Robert Griesemer39f009c2010-04-29 10:57:27 -07001790Within a <a href="#Constant_declarations">constant declaration</a>, the predeclared identifier
Robert Griesemer19b1d352009-09-24 19:36:48 -07001791<code>iota</code> represents successive untyped integer <a href="#Constants">
1792constants</a>. It is reset to 0 whenever the reserved word <code>const</code>
Robert Griesemer39f009c2010-04-29 10:57:27 -07001793appears in the source and increments after each <a href="#ConstSpec">ConstSpec</a>.
1794It can be used to construct a set of related constants:
Rob Pikea9ed30f2009-02-23 19:26:07 -08001795</p>
1796
1797<pre>
Robert Griesemer09e900e2015-11-25 13:18:49 -08001798const ( // iota is reset to 0
Robert Griesemer130ac742009-12-10 16:43:01 -08001799 c0 = iota // c0 == 0
1800 c1 = iota // c1 == 1
1801 c2 = iota // c2 == 2
Rob Pikea9ed30f2009-02-23 19:26:07 -08001802)
1803
Robert Griesemer09e900e2015-11-25 13:18:49 -08001804const ( // iota is reset to 0
1805 a = 1 &lt;&lt; iota // a == 1
Robert Griesemer130ac742009-12-10 16:43:01 -08001806 b = 1 &lt;&lt; iota // b == 2
Robert Griesemer09e900e2015-11-25 13:18:49 -08001807 c = 3 // c == 3 (iota is not used but still incremented)
1808 d = 1 &lt;&lt; iota // d == 8
Rob Pikea9ed30f2009-02-23 19:26:07 -08001809)
1810
Robert Griesemer09e900e2015-11-25 13:18:49 -08001811const ( // iota is reset to 0
Robert Griesemerb94c0d22011-01-19 23:07:21 -05001812 u = iota * 42 // u == 0 (untyped integer constant)
1813 v float64 = iota * 42 // v == 42.0 (float64 constant)
1814 w = iota * 42 // w == 84 (untyped integer constant)
Rob Pikea9ed30f2009-02-23 19:26:07 -08001815)
1816
Robert Griesemer09e900e2015-11-25 13:18:49 -08001817const x = iota // x == 0 (iota has been reset)
1818const y = iota // y == 0 (iota has been reset)
Rob Pikea9ed30f2009-02-23 19:26:07 -08001819</pre>
1820
1821<p>
1822Within an ExpressionList, the value of each <code>iota</code> is the same because
Robert Griesemer39f009c2010-04-29 10:57:27 -07001823it is only incremented after each ConstSpec:
Rob Pikea9ed30f2009-02-23 19:26:07 -08001824</p>
1825
1826<pre>
1827const (
David Symonds72a29792011-11-29 15:47:36 -08001828 bit0, mask0 = 1 &lt;&lt; iota, 1&lt;&lt;iota - 1 // bit0 == 1, mask0 == 0
1829 bit1, mask1 // bit1 == 2, mask1 == 1
1830 _, _ // skips iota == 2
1831 bit3, mask3 // bit3 == 8, mask3 == 7
Rob Pikea9ed30f2009-02-23 19:26:07 -08001832)
1833</pre>
1834
1835<p>
1836This last example exploits the implicit repetition of the
1837last non-empty expression list.
1838</p>
1839
1840
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001841<h3 id="Type_declarations">Type declarations</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001842
1843<p>
Robert Griesemerfc61b772009-09-28 14:10:20 -07001844A type declaration binds an identifier, the <i>type name</i>, to a new type
Robert Griesemer6f3b84a2014-05-07 08:42:08 -07001845that has the same <a href="#Types">underlying type</a> as an existing type,
1846and operations defined for the existing type are also defined for the new type.
1847The new type is <a href="#Type_identity">different</a> from the existing type.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001848</p>
1849
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001850<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -08001851TypeDecl = "type" ( TypeSpec | "(" { TypeSpec ";" } ")" ) .
Robert Griesemerbdec3302009-08-31 17:57:14 -07001852TypeSpec = identifier Type .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001853</pre>
1854
1855<pre>
Robert Griesemerbdec3302009-08-31 17:57:14 -07001856type IntArray [16]int
Rob Pikea9ed30f2009-02-23 19:26:07 -08001857
1858type (
David Symonds72a29792011-11-29 15:47:36 -08001859 Point struct{ x, y float64 }
Rob Pikea9ed30f2009-02-23 19:26:07 -08001860 Polar Point
1861)
1862
1863type TreeNode struct {
Robert Griesemer130ac742009-12-10 16:43:01 -08001864 left, right *TreeNode
1865 value *Comparable
Rob Pikea9ed30f2009-02-23 19:26:07 -08001866}
1867
Rob Pike217408a2011-11-09 14:22:44 -08001868type Block interface {
Robert Griesemer130ac742009-12-10 16:43:01 -08001869 BlockSize() int
1870 Encrypt(src, dst []byte)
1871 Decrypt(src, dst []byte)
Rob Pikea9ed30f2009-02-23 19:26:07 -08001872}
1873</pre>
1874
Robert Griesemerfc61b772009-09-28 14:10:20 -07001875<p>
1876The declared type does not inherit any <a href="#Method_declarations">methods</a>
Robert Griesemer2a838d62011-02-08 13:31:01 -08001877bound to the existing type, but the <a href="#Method_sets">method set</a>
Robert Griesemerd4a16192010-04-01 12:48:34 -07001878of an interface type or of elements of a composite type remains unchanged:
Robert Griesemerfc61b772009-09-28 14:10:20 -07001879</p>
1880
1881<pre>
Rob Pikebdbe0de2011-05-25 06:00:07 +10001882// A Mutex is a data type with two methods, Lock and Unlock.
Robert Griesemerfc61b772009-09-28 14:10:20 -07001883type Mutex struct { /* Mutex fields */ }
1884func (m *Mutex) Lock() { /* Lock implementation */ }
1885func (m *Mutex) Unlock() { /* Unlock implementation */ }
1886
1887// NewMutex has the same composition as Mutex but its method set is empty.
1888type NewMutex Mutex
1889
Robert Griesemer2a838d62011-02-08 13:31:01 -08001890// The method set of the <a href="#Pointer_types">base type</a> of PtrMutex remains unchanged,
1891// but the method set of PtrMutex is empty.
1892type PtrMutex *Mutex
1893
Robert Griesemerf5b3c142010-04-27 17:52:44 -07001894// The method set of *PrintableMutex contains the methods
Rob Pike4fe41922009-11-07 22:00:59 -08001895// Lock and Unlock bound to its anonymous field Mutex.
Robert Griesemerfc61b772009-09-28 14:10:20 -07001896type PrintableMutex struct {
Robert Griesemer130ac742009-12-10 16:43:01 -08001897 Mutex
Robert Griesemerfc61b772009-09-28 14:10:20 -07001898}
Robert Griesemer735e00d2010-03-31 16:37:22 -07001899
Rob Pike217408a2011-11-09 14:22:44 -08001900// MyBlock is an interface type that has the same method set as Block.
1901type MyBlock Block
Robert Griesemerfc61b772009-09-28 14:10:20 -07001902</pre>
1903
1904<p>
1905A type declaration may be used to define a different boolean, numeric, or string
1906type and attach methods to it:
1907</p>
1908
1909<pre>
1910type TimeZone int
1911
1912const (
Robert Griesemer130ac742009-12-10 16:43:01 -08001913 EST TimeZone = -(5 + iota)
1914 CST
1915 MST
1916 PST
Robert Griesemerfc61b772009-09-28 14:10:20 -07001917)
1918
1919func (tz TimeZone) String() string {
Robert Griesemer02d74482015-07-31 11:15:11 -07001920 return fmt.Sprintf("GMT%+dh", tz)
Robert Griesemerfc61b772009-09-28 14:10:20 -07001921}
1922</pre>
1923
1924
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001925<h3 id="Variable_declarations">Variable declarations</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001926
1927<p>
Robert Griesemer47094dc2014-09-30 11:44:29 -07001928A variable declaration creates one or more variables, binds corresponding
1929identifiers to them, and gives each a type and an initial value.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001930</p>
Robert Griesemer47094dc2014-09-30 11:44:29 -07001931
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001932<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -08001933VarDecl = "var" ( VarSpec | "(" { VarSpec ";" } ")" ) .
Robert Griesemer4023dce2009-08-14 17:41:52 -07001934VarSpec = IdentifierList ( Type [ "=" ExpressionList ] | "=" ExpressionList ) .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001935</pre>
1936
1937<pre>
1938var i int
Robert Griesemerb94c0d22011-01-19 23:07:21 -05001939var U, V, W float64
Rob Pikea9ed30f2009-02-23 19:26:07 -08001940var k = 0
Robert Griesemerb94c0d22011-01-19 23:07:21 -05001941var x, y float32 = -1, -2
Rob Pikea9ed30f2009-02-23 19:26:07 -08001942var (
David Symonds72a29792011-11-29 15:47:36 -08001943 i int
Rob Pikea9ed30f2009-02-23 19:26:07 -08001944 u, v, s = 2.0, 3.0, "bar"
1945)
Robert Griesemer4e56b332009-09-10 10:14:00 -07001946var re, im = complexSqrt(-1)
Robert Griesemer130ac742009-12-10 16:43:01 -08001947var _, found = entries[name] // map lookup; only interested in "found"
Rob Pikea9ed30f2009-02-23 19:26:07 -08001948</pre>
1949
1950<p>
Robert Griesemeref45e642009-08-21 11:25:00 -07001951If a list of expressions is given, the variables are initialized
Robert Griesemer47094dc2014-09-30 11:44:29 -07001952with the expressions following the rules for <a href="#Assignments">assignments</a>.
Rob Pike4fe41922009-11-07 22:00:59 -08001953Otherwise, each variable is initialized to its <a href="#The_zero_value">zero value</a>.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001954</p>
Robert Griesemeref45e642009-08-21 11:25:00 -07001955
Rob Pikea9ed30f2009-02-23 19:26:07 -08001956<p>
Robert Griesemer47094dc2014-09-30 11:44:29 -07001957If a type is present, each variable is given that type.
1958Otherwise, each variable is given the type of the corresponding
1959initialization value in the assignment.
1960If that value is an untyped constant, it is first
1961<a href="#Conversions">converted</a> to its <a href="#Constants">default type</a>;
1962if it is an untyped boolean value, it is first converted to type <code>bool</code>.
1963The predeclared value <code>nil</code> cannot be used to initialize a variable
1964with no explicit type.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001965</p>
Robert Griesemeref45e642009-08-21 11:25:00 -07001966
Robert Griesemer47094dc2014-09-30 11:44:29 -07001967<pre>
Brad Fitzpatrick5f029de2014-12-22 07:58:26 -08001968var d = math.Sin(0.5) // d is float64
Robert Griesemer47094dc2014-09-30 11:44:29 -07001969var i = 42 // i is int
1970var t, ok = x.(T) // t is T, ok is bool
1971var n = nil // illegal
1972</pre>
Robert Griesemer2c9e1632012-02-28 17:44:24 -08001973
1974<p>
1975Implementation restriction: A compiler may make it illegal to declare a variable
1976inside a <a href="#Function_declarations">function body</a> if the variable is
1977never used.
1978</p>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001979
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001980<h3 id="Short_variable_declarations">Short variable declarations</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001981
Robert Griesemer997851e2009-09-25 15:36:25 -07001982<p>
Robert Griesemeref45e642009-08-21 11:25:00 -07001983A <i>short variable declaration</i> uses the syntax:
Robert Griesemer997851e2009-09-25 15:36:25 -07001984</p>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001985
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001986<pre class="ebnf">
Robert Griesemere1b8cb82009-07-16 20:31:41 -07001987ShortVarDecl = IdentifierList ":=" ExpressionList .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001988</pre>
1989
Robert Griesemer997851e2009-09-25 15:36:25 -07001990<p>
Robert Griesemer2961d222013-07-31 22:25:47 -07001991It is shorthand for a regular <a href="#Variable_declarations">variable declaration</a>
Robert Griesemer2c9e1632012-02-28 17:44:24 -08001992with initializer expressions but no types:
Robert Griesemer997851e2009-09-25 15:36:25 -07001993</p>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001994
1995<pre class="grammar">
1996"var" IdentifierList = ExpressionList .
1997</pre>
1998
1999<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08002000i, j := 0, 10
2001f := func() int { return 7 }
2002ch := make(chan int)
2003r, w := os.Pipe(fd) // os.Pipe() returns two values
2004_, y, _ := coord(p) // coord() returns three values; only interested in y coordinate
Rob Pikea9ed30f2009-02-23 19:26:07 -08002005</pre>
2006
2007<p>
Robert Griesemer85789da2015-08-04 11:29:28 -07002008Unlike regular variable declarations, a short variable declaration may <i>redeclare</i>
2009variables provided they were originally declared earlier in the same block
2010(or the parameter lists if the block is the function body) with the same type,
2011and at least one of the non-<a href="#Blank_identifier">blank</a> variables is new.
2012As a consequence, redeclaration can only appear in a multi-variable short declaration.
2013Redeclaration does not introduce a new variable; it just assigns a new value to the original.
Rob Pike2a1683a2009-04-19 20:04:15 -07002014</p>
2015
2016<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08002017field1, offset := nextField(str, 0)
2018field2, offset := nextField(str, offset) // redeclares offset
Robert Griesemerf1cc0f42013-01-09 11:31:32 -08002019a, a := 1, 2 // illegal: double declaration of a or no new variable if a was declared elsewhere
Rob Pike2a1683a2009-04-19 20:04:15 -07002020</pre>
2021
2022<p>
Rob Pikea9ed30f2009-02-23 19:26:07 -08002023Short variable declarations may appear only inside functions.
Robert Griesemer462a17e2013-03-22 15:36:04 -07002024In some contexts such as the initializers for
2025<a href="#If_statements">"if"</a>,
2026<a href="#For_statements">"for"</a>, or
2027<a href="#Switch_statements">"switch"</a> statements,
2028they can be used to declare local temporary variables.
Rob Pikea9ed30f2009-02-23 19:26:07 -08002029</p>
2030
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002031<h3 id="Function_declarations">Function declarations</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08002032
2033<p>
Robert Griesemerb1d9ae92012-02-12 20:03:30 -08002034A function declaration binds an identifier, the <i>function name</i>,
2035to a function.
Rob Pikea9ed30f2009-02-23 19:26:07 -08002036</p>
2037
Robert Griesemerf7ac31362009-07-10 16:06:40 -07002038<pre class="ebnf">
Robert Griesemer9905cec2013-03-04 13:55:35 -08002039FunctionDecl = "func" FunctionName ( Function | Signature ) .
Robert Griesemerb1d9ae92012-02-12 20:03:30 -08002040FunctionName = identifier .
Robert Griesemer9905cec2013-03-04 13:55:35 -08002041Function = Signature FunctionBody .
2042FunctionBody = Block .
Rob Pikea9ed30f2009-02-23 19:26:07 -08002043</pre>
2044
Robert Griesemer4023dce2009-08-14 17:41:52 -07002045<p>
Robert Griesemer9905cec2013-03-04 13:55:35 -08002046If the function's <a href="#Function_types">signature</a> declares
2047result parameters, the function body's statement list must end in
2048a <a href="#Terminating_statements">terminating statement</a>.
2049</p>
2050
Rob Piked0208912013-03-22 10:03:55 -07002051<pre>
Robert Griesemer02d74482015-07-31 11:15:11 -07002052func IndexRune(s string, r rune) int {
2053 for i, c := range s {
2054 if c == r {
2055 return i
Rob Piked0208912013-03-22 10:03:55 -07002056 }
2057 }
Robert Griesemer02d74482015-07-31 11:15:11 -07002058 // invalid: missing return statement
Rob Piked0208912013-03-22 10:03:55 -07002059}
2060</pre>
2061
Robert Griesemer9905cec2013-03-04 13:55:35 -08002062<p>
Robert Griesemer4023dce2009-08-14 17:41:52 -07002063A function declaration may omit the body. Such a declaration provides the
2064signature for a function implemented outside Go, such as an assembly routine.
2065</p>
2066
Rob Pikea9ed30f2009-02-23 19:26:07 -08002067<pre>
2068func min(x int, y int) int {
2069 if x &lt; y {
Robert Griesemer130ac742009-12-10 16:43:01 -08002070 return x
Rob Pikea9ed30f2009-02-23 19:26:07 -08002071 }
Robert Griesemer130ac742009-12-10 16:43:01 -08002072 return y
Rob Pikea9ed30f2009-02-23 19:26:07 -08002073}
Robert Griesemer4023dce2009-08-14 17:41:52 -07002074
2075func flushICache(begin, end uintptr) // implemented externally
Rob Pikea9ed30f2009-02-23 19:26:07 -08002076</pre>
2077
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002078<h3 id="Method_declarations">Method declarations</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08002079
2080<p>
Robert Griesemer9905cec2013-03-04 13:55:35 -08002081A method is a <a href="#Function_declarations">function</a> with a <i>receiver</i>.
2082A method declaration binds an identifier, the <i>method name</i>, to a method,
2083and associates the method with the receiver's <i>base type</i>.
Rob Pikea9ed30f2009-02-23 19:26:07 -08002084</p>
Robert Griesemerb1d9ae92012-02-12 20:03:30 -08002085
Robert Griesemerf7ac31362009-07-10 16:06:40 -07002086<pre class="ebnf">
Robert Griesemer9905cec2013-03-04 13:55:35 -08002087MethodDecl = "func" Receiver MethodName ( Function | Signature ) .
Robert Griesemer187ee2c2014-06-24 16:25:09 -07002088Receiver = Parameters .
Rob Pikea9ed30f2009-02-23 19:26:07 -08002089</pre>
2090
2091<p>
Paolo Martini35491782015-07-17 17:26:08 +02002092The receiver is specified via an extra parameter section preceding the method
Robert Griesemer57c81ef2015-12-15 13:13:38 -08002093name. That parameter section must declare a single non-variadic parameter, the receiver.
Robert Griesemer187ee2c2014-06-24 16:25:09 -07002094Its type must be of the form <code>T</code> or <code>*T</code> (possibly using
2095parentheses) where <code>T</code> is a type name. The type denoted by <code>T</code> is called
Robert Griesemerb1d9ae92012-02-12 20:03:30 -08002096the receiver <i>base type</i>; it must not be a pointer or interface type and
2097it must be declared in the same package as the method.
2098The method is said to be <i>bound</i> to the base type and the method name
Robert Griesemer05614bf2015-08-04 11:52:01 -07002099is visible only within <a href="#Selectors">selectors</a> for type <code>T</code>
2100or <code>*T</code>.
Robert Griesemerb1d9ae92012-02-12 20:03:30 -08002101</p>
2102
2103<p>
Robert Griesemer85e451e2012-11-29 14:47:47 -08002104A non-<a href="#Blank_identifier">blank</a> receiver identifier must be
2105<a href="#Uniqueness_of_identifiers">unique</a> in the method signature.
2106If the receiver's value is not referenced inside the body of the method,
2107its identifier may be omitted in the declaration. The same applies in
2108general to parameters of functions and methods.
2109</p>
2110
2111<p>
2112For a base type, the non-blank names of methods bound to it must be unique.
Robert Griesemerb1d9ae92012-02-12 20:03:30 -08002113If the base type is a <a href="#Struct_types">struct type</a>,
2114the non-blank method and field names must be distinct.
Rob Pikea9ed30f2009-02-23 19:26:07 -08002115</p>
2116
2117<p>
Rob Pikea9ed30f2009-02-23 19:26:07 -08002118Given type <code>Point</code>, the declarations
2119</p>
2120
2121<pre>
Robert Griesemer777a96a2010-12-02 12:32:14 -08002122func (p *Point) Length() float64 {
2123 return math.Sqrt(p.x * p.x + p.y * p.y)
Rob Pikea9ed30f2009-02-23 19:26:07 -08002124}
2125
Robert Griesemer777a96a2010-12-02 12:32:14 -08002126func (p *Point) Scale(factor float64) {
2127 p.x *= factor
2128 p.y *= factor
Rob Pikea9ed30f2009-02-23 19:26:07 -08002129}
2130</pre>
2131
2132<p>
Rob Pike678625d2009-09-15 09:54:22 -07002133bind the methods <code>Length</code> and <code>Scale</code>,
2134with receiver type <code>*Point</code>,
Rob Pikea9ed30f2009-02-23 19:26:07 -08002135to the base type <code>Point</code>.
2136</p>
2137
2138<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002139The type of a method is the type of a function with the receiver as first
2140argument. For instance, the method <code>Scale</code> has type
2141</p>
2142
2143<pre>
Robert Griesemer777a96a2010-12-02 12:32:14 -08002144func(p *Point, factor float64)
Rob Pikedf3183f2009-02-26 16:37:23 -08002145</pre>
2146
2147<p>
2148However, a function declared this way is not a method.
2149</p>
2150
Rob Pikea9ed30f2009-02-23 19:26:07 -08002151
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002152<h2 id="Expressions">Expressions</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002153
Rob Pikedf3183f2009-02-26 16:37:23 -08002154<p>
2155An expression specifies the computation of a value by applying
Robert Griesemer19b1d352009-09-24 19:36:48 -07002156operators and functions to operands.
Rob Pikedf3183f2009-02-26 16:37:23 -08002157</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002158
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002159<h3 id="Operands">Operands</h3>
Robert Griesemerad711102008-09-11 17:48:20 -07002160
Robert Griesemer997851e2009-09-25 15:36:25 -07002161<p>
Robert Griesemer809e06b2012-06-26 11:49:19 -07002162Operands denote the elementary values in an expression. An operand may be a
Robert Griesemerf57bf7a2013-11-12 21:06:54 -05002163literal, a (possibly <a href="#Qualified_identifiers">qualified</a>)
2164non-<a href="#Blank_identifier">blank</a> identifier denoting a
Robert Griesemer809e06b2012-06-26 11:49:19 -07002165<a href="#Constant_declarations">constant</a>,
2166<a href="#Variable_declarations">variable</a>, or
2167<a href="#Function_declarations">function</a>,
2168a <a href="#Method_expressions">method expression</a> yielding a function,
2169or a parenthesized expression.
Robert Griesemer997851e2009-09-25 15:36:25 -07002170</p>
Robert Griesemerad711102008-09-11 17:48:20 -07002171
Robert Griesemerf57bf7a2013-11-12 21:06:54 -05002172<p>
2173The <a href="#Blank_identifier">blank identifier</a> may appear as an
2174operand only on the left-hand side of an <a href="#Assignments">assignment</a>.
2175</p>
2176
Robert Griesemerf7ac31362009-07-10 16:06:40 -07002177<pre class="ebnf">
Robin Eklindcac006a2014-08-30 10:27:01 -07002178Operand = Literal | OperandName | MethodExpr | "(" Expression ")" .
2179Literal = BasicLit | CompositeLit | FunctionLit .
2180BasicLit = int_lit | float_lit | imaginary_lit | rune_lit | string_lit .
Robert Griesemer809e06b2012-06-26 11:49:19 -07002181OperandName = identifier | QualifiedIdent.
Robert Griesemerc2d55862009-02-19 16:49:10 -08002182</pre>
Robert Griesemerad711102008-09-11 17:48:20 -07002183
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002184<h3 id="Qualified_identifiers">Qualified identifiers</h3>
Robert Griesemerad711102008-09-11 17:48:20 -07002185
Robert Griesemerc2d55862009-02-19 16:49:10 -08002186<p>
Robert Griesemer809e06b2012-06-26 11:49:19 -07002187A qualified identifier is an identifier qualified with a package name prefix.
2188Both the package name and the identifier must not be
2189<a href="#Blank_identifier">blank</a>.
Rob Pikedf3183f2009-02-26 16:37:23 -08002190</p>
Robert Griesemer337af312008-11-17 18:11:36 -08002191
Robert Griesemerf7ac31362009-07-10 16:06:40 -07002192<pre class="ebnf">
Robert Griesemer809e06b2012-06-26 11:49:19 -07002193QualifiedIdent = PackageName "." identifier .
Robert Griesemerc2d55862009-02-19 16:49:10 -08002194</pre>
Robert Griesemerad711102008-09-11 17:48:20 -07002195
Rob Pikedf3183f2009-02-26 16:37:23 -08002196<p>
Robert Griesemerda633712012-02-29 09:06:05 -08002197A qualified identifier accesses an identifier in a different package, which
2198must be <a href="#Import_declarations">imported</a>.
Robert Griesemer103c9db2012-03-01 13:57:49 -08002199The identifier must be <a href="#Exported_identifiers">exported</a> and
2200declared in the <a href="#Blocks">package block</a> of that package.
Rob Pikedf3183f2009-02-26 16:37:23 -08002201</p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002202
2203<pre>
Robert Griesemer103c9db2012-03-01 13:57:49 -08002204math.Sin // denotes the Sin function in package math
Rob Pikedf3183f2009-02-26 16:37:23 -08002205</pre>
Robert Griesemerad711102008-09-11 17:48:20 -07002206
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002207<h3 id="Composite_literals">Composite literals</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002208
Rob Pikedf3183f2009-02-26 16:37:23 -08002209<p>
2210Composite literals construct values for structs, arrays, slices, and maps
2211and create a new value each time they are evaluated.
Robert Griesemerc7208752015-09-22 17:47:38 -07002212They consist of the type of the literal followed by a brace-bound list of elements.
Robert Griesemer3b022422015-09-11 16:20:23 -07002213Each element may optionally be preceded by a corresponding key.
Rob Pikedf3183f2009-02-26 16:37:23 -08002214</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002215
Robert Griesemerf7ac31362009-07-10 16:06:40 -07002216<pre class="ebnf">
Robert Griesemera12141e2010-10-22 08:58:52 -07002217CompositeLit = LiteralType LiteralValue .
Rob Pikedf3183f2009-02-26 16:37:23 -08002218LiteralType = StructType | ArrayType | "[" "..." "]" ElementType |
Robert Griesemer07cc6442010-07-29 18:13:41 -07002219 SliceType | MapType | TypeName .
Robert Griesemera12141e2010-10-22 08:58:52 -07002220LiteralValue = "{" [ ElementList [ "," ] ] "}" .
Robert Griesemer3b022422015-09-11 16:20:23 -07002221ElementList = KeyedElement { "," KeyedElement } .
2222KeyedElement = [ Key ":" ] Element .
Robert Griesemer7727dee2015-01-08 16:01:31 -08002223Key = FieldName | Expression | LiteralValue .
Rob Pike678625d2009-09-15 09:54:22 -07002224FieldName = identifier .
Robert Griesemer3b022422015-09-11 16:20:23 -07002225Element = Expression | LiteralValue .
Robert Griesemerc2d55862009-02-19 16:49:10 -08002226</pre>
Robert Griesemer0976e342008-09-03 13:37:44 -07002227
Rob Pikedf3183f2009-02-26 16:37:23 -08002228<p>
Robert Griesemerc7208752015-09-22 17:47:38 -07002229The LiteralType's underlying type must be a struct, array, slice, or map type
Robert Griesemer838cf122009-05-22 10:25:06 -07002230(the grammar enforces this constraint except when the type is given
2231as a TypeName).
Robert Griesemer3b022422015-09-11 16:20:23 -07002232The types of the elements and keys must be <a href="#Assignability">assignable</a>
Robert Griesemerc7208752015-09-22 17:47:38 -07002233to the respective field, element, and key types of the literal type;
Russ Cox7a5e97b2009-03-03 15:40:30 -08002234there is no additional conversion.
Robert Griesemer838cf122009-05-22 10:25:06 -07002235The key is interpreted as a field name for struct literals,
Robert Griesemer9c9e8112012-12-10 11:55:57 -08002236an index for array and slice literals, and a key for map literals.
Robert Griesemer838cf122009-05-22 10:25:06 -07002237For map literals, all elements must have a key. It is an error
2238to specify multiple elements with the same field name or
2239constant key value.
Rob Pikedf3183f2009-02-26 16:37:23 -08002240</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002241
Robert Griesemer838cf122009-05-22 10:25:06 -07002242<p>
2243For struct literals the following rules apply:
Robert Griesemercfe92112009-06-18 13:29:40 -07002244</p>
Robert Griesemer838cf122009-05-22 10:25:06 -07002245<ul>
Robert Griesemerc7208752015-09-22 17:47:38 -07002246 <li>A key must be a field name declared in the struct type.
Robert Griesemerd3b15652009-11-16 08:58:55 -08002247 </li>
Robert Griesemer369a9742012-10-31 15:07:25 -07002248 <li>An element list that does not contain any keys must
Robert Griesemer838cf122009-05-22 10:25:06 -07002249 list an element for each struct field in the
2250 order in which the fields are declared.
2251 </li>
2252 <li>If any element has a key, every element must have a key.
2253 </li>
Robert Griesemer369a9742012-10-31 15:07:25 -07002254 <li>An element list that contains keys does not need to
Robert Griesemer838cf122009-05-22 10:25:06 -07002255 have an element for each struct field. Omitted fields
2256 get the zero value for that field.
2257 </li>
2258 <li>A literal may omit the element list; such a literal evaluates
Robert Griesemer369a9742012-10-31 15:07:25 -07002259 to the zero value for its type.
Robert Griesemer838cf122009-05-22 10:25:06 -07002260 </li>
2261 <li>It is an error to specify an element for a non-exported
2262 field of a struct belonging to a different package.
2263 </li>
2264</ul>
Robert Griesemer838cf122009-05-22 10:25:06 -07002265
2266<p>
2267Given the declarations
2268</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002269<pre>
Robert Griesemer777a96a2010-12-02 12:32:14 -08002270type Point3D struct { x, y, z float64 }
2271type Line struct { p, q Point3D }
Robert Griesemerc2d55862009-02-19 16:49:10 -08002272</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002273
Rob Pikedf3183f2009-02-26 16:37:23 -08002274<p>
2275one may write
2276</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002277
Robert Griesemerc2d55862009-02-19 16:49:10 -08002278<pre>
Robert Griesemer777a96a2010-12-02 12:32:14 -08002279origin := Point3D{} // zero value for Point3D
2280line := Line{origin, Point3D{y: -4, z: 12.3}} // zero value for line.q.x
Rob Pike37ab8382009-03-18 22:58:36 -07002281</pre>
2282
Robert Griesemercfe92112009-06-18 13:29:40 -07002283<p>
2284For array and slice literals the following rules apply:
2285</p>
Robert Griesemer838cf122009-05-22 10:25:06 -07002286<ul>
2287 <li>Each element has an associated integer index marking
2288 its position in the array.
2289 </li>
2290 <li>An element with a key uses the key as its index; the
2291 key must be a constant integer expression.
2292 </li>
2293 <li>An element without a key uses the previous element's index plus one.
2294 If the first element has no key, its index is zero.
2295 </li>
2296</ul>
Robert Griesemer838cf122009-05-22 10:25:06 -07002297
Rob Pike37ab8382009-03-18 22:58:36 -07002298<p>
Robert Griesemer462a17e2013-03-22 15:36:04 -07002299<a href="#Address_operators">Taking the address</a> of a composite literal
Robert Griesemer6962c152014-10-16 15:08:49 -07002300generates a pointer to a unique <a href="#Variables">variable</a> initialized
2301with the literal's value.
Rob Pike37ab8382009-03-18 22:58:36 -07002302</p>
Rob Pike37ab8382009-03-18 22:58:36 -07002303<pre>
Robert Griesemer777a96a2010-12-02 12:32:14 -08002304var pointer *Point3D = &amp;Point3D{y: 1000}
Robert Griesemerc2d55862009-02-19 16:49:10 -08002305</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002306
Rob Pikedf3183f2009-02-26 16:37:23 -08002307<p>
Robert Griesemerc7208752015-09-22 17:47:38 -07002308The length of an array literal is the length specified in the literal type.
Robert Griesemera3294712009-01-05 11:17:26 -08002309If fewer elements than the length are provided in the literal, the missing
Rob Pike8f2330d2009-02-25 16:20:44 -08002310elements are set to the zero value for the array element type.
Robert Griesemer838cf122009-05-22 10:25:06 -07002311It is an error to provide elements with index values outside the index range
2312of the array. The notation <code>...</code> specifies an array length equal
2313to the maximum element index plus one.
Rob Pikedf3183f2009-02-26 16:37:23 -08002314</p>
Robert Griesemerb90b2132008-09-19 15:49:55 -07002315
Robert Griesemerc2d55862009-02-19 16:49:10 -08002316<pre>
David Symonds72a29792011-11-29 15:47:36 -08002317buffer := [10]string{} // len(buffer) == 10
2318intSet := [6]int{1, 2, 3, 5} // len(intSet) == 6
2319days := [...]string{"Sat", "Sun"} // len(days) == 2
Robert Griesemerc2d55862009-02-19 16:49:10 -08002320</pre>
Robert Griesemer91bbd642009-01-07 09:31:35 -08002321
Rob Pikedf3183f2009-02-26 16:37:23 -08002322<p>
2323A slice literal describes the entire underlying array literal.
Rob Pike678625d2009-09-15 09:54:22 -07002324Thus, the length and capacity of a slice literal are the maximum
Robert Griesemer838cf122009-05-22 10:25:06 -07002325element index plus one. A slice literal has the form
Rob Pikedf3183f2009-02-26 16:37:23 -08002326</p>
Robert Griesemer91bbd642009-01-07 09:31:35 -08002327
Robert Griesemerc2d55862009-02-19 16:49:10 -08002328<pre>
Robert Griesemer3c7271f2011-05-24 14:18:44 -07002329[]T{x1, x2, … xn}
Robert Griesemerc2d55862009-02-19 16:49:10 -08002330</pre>
Robert Griesemer91bbd642009-01-07 09:31:35 -08002331
Rob Pikedf3183f2009-02-26 16:37:23 -08002332<p>
Robert Griesemer2961d222013-07-31 22:25:47 -07002333and is shorthand for a slice operation applied to an array:
Rob Pikedf3183f2009-02-26 16:37:23 -08002334</p>
Robert Griesemer91bbd642009-01-07 09:31:35 -08002335
Robert Griesemerc2d55862009-02-19 16:49:10 -08002336<pre>
Russ Cox4dfe9762011-12-02 12:30:20 -05002337tmp := [n]T{x1, x2, … xn}
2338tmp[0 : n]
Robert Griesemerc2d55862009-02-19 16:49:10 -08002339</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002340
Rob Pikedf3183f2009-02-26 16:37:23 -08002341<p>
Robert Griesemera12141e2010-10-22 08:58:52 -07002342Within a composite literal of array, slice, or map type <code>T</code>,
Robert Griesemer7727dee2015-01-08 16:01:31 -08002343elements or map keys that are themselves composite literals may elide the respective
2344literal type if it is identical to the element or key type of <code>T</code>.
2345Similarly, elements or keys that are addresses of composite literals may elide
2346the <code>&amp;T</code> when the element or key type is <code>*T</code>.
Robert Griesemera12141e2010-10-22 08:58:52 -07002347</p>
2348
2349<pre>
Robert Griesemer7727dee2015-01-08 16:01:31 -08002350[...]Point{{1.5, -3.5}, {0, 0}} // same as [...]Point{Point{1.5, -3.5}, Point{0, 0}}
2351[][]int{{1, 2, 3}, {4, 5}} // same as [][]int{[]int{1, 2, 3}, []int{4, 5}}
2352[][]Point{{{0, 1}, {1, 2}}} // same as [][]Point{[]Point{Point{0, 1}, Point{1, 2}}}
2353map[string]Point{"orig": {0, 0}} // same as map[string]Point{"orig": Point{0, 0}}
Russ Cox5f494562011-12-02 14:12:53 -05002354
Robert Griesemer7727dee2015-01-08 16:01:31 -08002355[...]*Point{{1.5, -3.5}, {0, 0}} // same as [...]*Point{&amp;Point{1.5, -3.5}, &amp;Point{0, 0}}
2356
2357map[Point]string{{0, 0}: "orig"} // same as map[Point]string{Point{0, 0}: "orig"}
Robert Griesemera12141e2010-10-22 08:58:52 -07002358</pre>
2359
2360<p>
Russ Cox7a5e97b2009-03-03 15:40:30 -08002361A parsing ambiguity arises when a composite literal using the
Robert Griesemera36b5b92014-02-27 08:57:30 -08002362TypeName form of the LiteralType appears as an operand between the
2363<a href="#Keywords">keyword</a> and the opening brace of the block
2364of an "if", "for", or "switch" statement, and the composite literal
2365is not enclosed in parentheses, square brackets, or curly braces.
2366In this rare case, the opening brace of the literal is erroneously parsed
2367as the one introducing the block of statements. To resolve the ambiguity,
2368the composite literal must appear within parentheses.
Russ Cox7a5e97b2009-03-03 15:40:30 -08002369</p>
2370
2371<pre>
Robert Griesemer3c7271f2011-05-24 14:18:44 -07002372if x == (T{a,b,c}[i]) { … }
2373if (x == T{a,b,c}[i]) { … }
Russ Cox7a5e97b2009-03-03 15:40:30 -08002374</pre>
2375
Robert Griesemer838cf122009-05-22 10:25:06 -07002376<p>
2377Examples of valid array, slice, and map literals:
2378</p>
2379
2380<pre>
2381// list of prime numbers
Rob Pikefc06cad2012-02-07 06:59:36 +11002382primes := []int{2, 3, 5, 7, 9, 2147483647}
Robert Griesemer838cf122009-05-22 10:25:06 -07002383
2384// vowels[ch] is true if ch is a vowel
Robert Griesemer130ac742009-12-10 16:43:01 -08002385vowels := [128]bool{'a': true, 'e': true, 'i': true, 'o': true, 'u': true, 'y': true}
Robert Griesemer838cf122009-05-22 10:25:06 -07002386
Robert Griesemerb94c0d22011-01-19 23:07:21 -05002387// the array [10]float32{-1, 0, 0, 0, -0.1, -0.1, 0, 0, 0, -1}
2388filter := [10]float32{-1, 4: -0.1, -0.1, 9: -1}
Robert Griesemer838cf122009-05-22 10:25:06 -07002389
2390// frequencies in Hz for equal-tempered scale (A4 = 440Hz)
Robert Griesemerb94c0d22011-01-19 23:07:21 -05002391noteFrequency := map[string]float32{
Robert Griesemer838cf122009-05-22 10:25:06 -07002392 "C0": 16.35, "D0": 18.35, "E0": 20.60, "F0": 21.83,
2393 "G0": 24.50, "A0": 27.50, "B0": 30.87,
2394}
2395</pre>
2396
2397
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002398<h3 id="Function_literals">Function literals</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002399
Rob Pikedf3183f2009-02-26 16:37:23 -08002400<p>
Robert Griesemer9905cec2013-03-04 13:55:35 -08002401A function literal represents an anonymous <a href="#Function_declarations">function</a>.
Rob Pikedf3183f2009-02-26 16:37:23 -08002402</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002403
Robert Griesemerf7ac31362009-07-10 16:06:40 -07002404<pre class="ebnf">
Robert Griesemer9905cec2013-03-04 13:55:35 -08002405FunctionLit = "func" Function .
Robert Griesemerc2d55862009-02-19 16:49:10 -08002406</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002407
Robert Griesemerc2d55862009-02-19 16:49:10 -08002408<pre>
Robert Griesemerb94c0d22011-01-19 23:07:21 -05002409func(a, b int, z float64) bool { return a*b &lt; int(z) }
Robert Griesemerc2d55862009-02-19 16:49:10 -08002410</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002411
Rob Pikedf3183f2009-02-26 16:37:23 -08002412<p>
2413A function literal can be assigned to a variable or invoked directly.
2414</p>
Robert Griesemer7231ceb2008-09-08 15:01:04 -07002415
Robert Griesemerc2d55862009-02-19 16:49:10 -08002416<pre>
Rob Pikedf3183f2009-02-26 16:37:23 -08002417f := func(x, y int) int { return x + y }
Russ Cox9c08d652012-02-21 22:04:30 -05002418func(ch chan int) { ch &lt;- ACK }(replyChan)
Robert Griesemerc2d55862009-02-19 16:49:10 -08002419</pre>
Robert Griesemer7231ceb2008-09-08 15:01:04 -07002420
Rob Pikedf3183f2009-02-26 16:37:23 -08002421<p>
2422Function literals are <i>closures</i>: they may refer to variables
Robert Griesemerd8a764c2009-02-06 17:01:10 -08002423defined in a surrounding function. Those variables are then shared between
2424the surrounding function and the function literal, and they survive as long
Rob Pikedf3183f2009-02-26 16:37:23 -08002425as they are accessible.
2426</p>
Robert Griesemer7231ceb2008-09-08 15:01:04 -07002427
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002428
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002429<h3 id="Primary_expressions">Primary expressions</h3>
Russ Cox5958dd62009-03-04 17:19:21 -08002430
Robert Griesemer164a7bc2009-09-30 12:00:25 -07002431<p>
2432Primary expressions are the operands for unary and binary expressions.
2433</p>
2434
Robert Griesemerf7ac31362009-07-10 16:06:40 -07002435<pre class="ebnf">
Robert Griesemerc2d55862009-02-19 16:49:10 -08002436PrimaryExpr =
2437 Operand |
Robert Griesemer5eb36242009-09-16 11:05:14 -07002438 Conversion |
Robert Griesemerc2d55862009-02-19 16:49:10 -08002439 PrimaryExpr Selector |
2440 PrimaryExpr Index |
2441 PrimaryExpr Slice |
Russ Cox5958dd62009-03-04 17:19:21 -08002442 PrimaryExpr TypeAssertion |
Robert Griesemerccc713c2014-10-27 16:31:15 -07002443 PrimaryExpr Arguments .
Robert Griesemer57b34612008-10-10 12:45:44 -07002444
Russ Cox5958dd62009-03-04 17:19:21 -08002445Selector = "." identifier .
2446Index = "[" Expression "]" .
Robert Griesemere333b962013-09-11 17:18:52 -07002447Slice = "[" ( [ Expression ] ":" [ Expression ] ) |
2448 ( [ Expression ] ":" Expression ":" Expression )
2449 "]" .
Russ Cox5958dd62009-03-04 17:19:21 -08002450TypeAssertion = "." "(" Type ")" .
Robert Griesemerccc713c2014-10-27 16:31:15 -07002451Arguments = "(" [ ( ExpressionList | Type [ "," ExpressionList ] ) [ "..." ] [ "," ] ] ")" .
Robert Griesemerc2d55862009-02-19 16:49:10 -08002452</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002453
2454
Robert Griesemerc2d55862009-02-19 16:49:10 -08002455<pre>
2456x
24572
2458(s + ".txt")
2459f(3.1415, true)
Rob Pike426335f2009-03-02 17:52:52 -08002460Point{1, 2}
Robert Griesemerc2d55862009-02-19 16:49:10 -08002461m["foo"]
2462s[i : j + 1]
2463obj.color
Robert Griesemerc2d55862009-02-19 16:49:10 -08002464f.p[i].x()
2465</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002466
2467
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002468<h3 id="Selectors">Selectors</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002469
Rob Pikedf3183f2009-02-26 16:37:23 -08002470<p>
Robert Griesemer71de83b2012-06-28 12:22:24 -07002471For a <a href="#Primary_expressions">primary expression</a> <code>x</code>
2472that is not a <a href="#Package_clause">package name</a>, the
2473<i>selector expression</i>
Rob Pikedf3183f2009-02-26 16:37:23 -08002474</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002475
Robert Griesemerc2d55862009-02-19 16:49:10 -08002476<pre>
2477x.f
2478</pre>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002479
Robert Griesemerc2d55862009-02-19 16:49:10 -08002480<p>
Robert Griesemer71de83b2012-06-28 12:22:24 -07002481denotes the field or method <code>f</code> of the value <code>x</code>
2482(or sometimes <code>*x</code>; see below).
2483The identifier <code>f</code> is called the (field or method) <i>selector</i>;
2484it must not be the <a href="#Blank_identifier">blank identifier</a>.
2485The type of the selector expression is the type of <code>f</code>.
2486If <code>x</code> is a package name, see the section on
2487<a href="#Qualified_identifiers">qualified identifiers</a>.
Rob Pikedf3183f2009-02-26 16:37:23 -08002488</p>
Robert Griesemer71de83b2012-06-28 12:22:24 -07002489
Robert Griesemerc2d55862009-02-19 16:49:10 -08002490<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002491A selector <code>f</code> may denote a field or method <code>f</code> of
2492a type <code>T</code>, or it may refer
Robert Griesemer71de83b2012-06-28 12:22:24 -07002493to a field or method <code>f</code> of a nested
2494<a href="#Struct_types">anonymous field</a> of <code>T</code>.
Rob Pikedf3183f2009-02-26 16:37:23 -08002495The number of anonymous fields traversed
2496to reach <code>f</code> is called its <i>depth</i> in <code>T</code>.
2497The depth of a field or method <code>f</code>
2498declared in <code>T</code> is zero.
2499The depth of a field or method <code>f</code> declared in
2500an anonymous field <code>A</code> in <code>T</code> is the
2501depth of <code>f</code> in <code>A</code> plus one.
2502</p>
Robert Griesemer71de83b2012-06-28 12:22:24 -07002503
Robert Griesemerc2d55862009-02-19 16:49:10 -08002504<p>
Robert Griesemer071c91b2008-10-23 12:04:45 -07002505The following rules apply to selectors:
Rob Pikedf3183f2009-02-26 16:37:23 -08002506</p>
Robert Griesemer71de83b2012-06-28 12:22:24 -07002507
Rob Pikedf3183f2009-02-26 16:37:23 -08002508<ol>
2509<li>
2510For a value <code>x</code> of type <code>T</code> or <code>*T</code>
Robert Griesemer40818cf2014-11-11 13:19:47 -08002511where <code>T</code> is not a pointer or interface type,
Rob Pikedf3183f2009-02-26 16:37:23 -08002512<code>x.f</code> denotes the field or method at the shallowest depth
2513in <code>T</code> where there
2514is such an <code>f</code>.
Robert Griesemer103c9db2012-03-01 13:57:49 -08002515If there is not exactly <a href="#Uniqueness_of_identifiers">one <code>f</code></a>
2516with shallowest depth, the selector expression is illegal.
Rob Pikedf3183f2009-02-26 16:37:23 -08002517</li>
Robert Griesemer40818cf2014-11-11 13:19:47 -08002518
Rob Pikedf3183f2009-02-26 16:37:23 -08002519<li>
Robert Griesemer40818cf2014-11-11 13:19:47 -08002520For a value <code>x</code> of type <code>I</code> where <code>I</code>
Robert Griesemer71de83b2012-06-28 12:22:24 -07002521is an interface type, <code>x.f</code> denotes the actual method with name
Robert Griesemer40818cf2014-11-11 13:19:47 -08002522<code>f</code> of the dynamic value of <code>x</code>.
Robert Griesemer71de83b2012-06-28 12:22:24 -07002523If there is no method with name <code>f</code> in the
2524<a href="#Method_sets">method set</a> of <code>I</code>, the selector
2525expression is illegal.
Rob Pikedf3183f2009-02-26 16:37:23 -08002526</li>
Robert Griesemer40818cf2014-11-11 13:19:47 -08002527
2528<li>
2529As an exception, if the type of <code>x</code> is a named pointer type
2530and <code>(*x).f</code> is a valid selector expression denoting a field
2531(but not a method), <code>x.f</code> is shorthand for <code>(*x).f</code>.
2532</li>
2533
Rob Pikedf3183f2009-02-26 16:37:23 -08002534<li>
2535In all other cases, <code>x.f</code> is illegal.
Robert Griesemer3af480372010-05-14 13:11:48 -07002536</li>
Robert Griesemer40818cf2014-11-11 13:19:47 -08002537
Robert Griesemer71de83b2012-06-28 12:22:24 -07002538<li>
Russ Cox6e156832013-03-20 16:54:07 -04002539If <code>x</code> is of pointer type and has the value
2540<code>nil</code> and <code>x.f</code> denotes a struct field,
2541assigning to or evaluating <code>x.f</code>
2542causes a <a href="#Run_time_panics">run-time panic</a>.
2543</li>
Robert Griesemer40818cf2014-11-11 13:19:47 -08002544
Russ Cox6e156832013-03-20 16:54:07 -04002545<li>
2546If <code>x</code> is of interface type and has the value
2547<code>nil</code>, <a href="#Calls">calling</a> or
2548<a href="#Method_values">evaluating</a> the method <code>x.f</code>
Robert Griesemer71de83b2012-06-28 12:22:24 -07002549causes a <a href="#Run_time_panics">run-time panic</a>.
Rob Pike9dfc6f62012-08-29 14:46:57 -07002550</li>
Rob Pikedf3183f2009-02-26 16:37:23 -08002551</ol>
Robert Griesemer71de83b2012-06-28 12:22:24 -07002552
Robert Griesemerc2d55862009-02-19 16:49:10 -08002553<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002554For example, given the declarations:
2555</p>
Robert Griesemer071c91b2008-10-23 12:04:45 -07002556
Robert Griesemerc2d55862009-02-19 16:49:10 -08002557<pre>
2558type T0 struct {
Robert Griesemer130ac742009-12-10 16:43:01 -08002559 x int
Robert Griesemerc2d55862009-02-19 16:49:10 -08002560}
Robert Griesemer071c91b2008-10-23 12:04:45 -07002561
Robert Griesemer40818cf2014-11-11 13:19:47 -08002562func (*T0) M0()
Robert Griesemer071c91b2008-10-23 12:04:45 -07002563
Robert Griesemerc2d55862009-02-19 16:49:10 -08002564type T1 struct {
Robert Griesemer130ac742009-12-10 16:43:01 -08002565 y int
Robert Griesemerc2d55862009-02-19 16:49:10 -08002566}
Robert Griesemer071c91b2008-10-23 12:04:45 -07002567
Robert Griesemer40818cf2014-11-11 13:19:47 -08002568func (T1) M1()
Robert Griesemer071c91b2008-10-23 12:04:45 -07002569
Robert Griesemerc2d55862009-02-19 16:49:10 -08002570type T2 struct {
Robert Griesemer130ac742009-12-10 16:43:01 -08002571 z int
2572 T1
2573 *T0
Robert Griesemerc2d55862009-02-19 16:49:10 -08002574}
Robert Griesemer071c91b2008-10-23 12:04:45 -07002575
Robert Griesemer40818cf2014-11-11 13:19:47 -08002576func (*T2) M2()
Robert Griesemer071c91b2008-10-23 12:04:45 -07002577
Robert Griesemer40818cf2014-11-11 13:19:47 -08002578type Q *T2
2579
2580var t T2 // with t.T0 != nil
2581var p *T2 // with p != nil and (*p).T0 != nil
2582var q Q = p
Robert Griesemerc2d55862009-02-19 16:49:10 -08002583</pre>
Robert Griesemer071c91b2008-10-23 12:04:45 -07002584
Rob Pikedf3183f2009-02-26 16:37:23 -08002585<p>
2586one may write:
2587</p>
Robert Griesemer071c91b2008-10-23 12:04:45 -07002588
Robert Griesemerc2d55862009-02-19 16:49:10 -08002589<pre>
Robert Griesemer40818cf2014-11-11 13:19:47 -08002590t.z // t.z
2591t.y // t.T1.y
Robert Griesemerf9ec9292015-05-18 11:18:58 -07002592t.x // (*t.T0).x
Robert Griesemer071c91b2008-10-23 12:04:45 -07002593
Robert Griesemer40818cf2014-11-11 13:19:47 -08002594p.z // (*p).z
2595p.y // (*p).T1.y
2596p.x // (*(*p).T0).x
2597
2598q.x // (*(*q).T0).x (*q).x is a valid field selector
2599
Robert Griesemer02d74482015-07-31 11:15:11 -07002600p.M0() // ((*p).T0).M0() M0 expects *T0 receiver
Robert Griesemer40818cf2014-11-11 13:19:47 -08002601p.M1() // ((*p).T1).M1() M1 expects T1 receiver
Robert Griesemer02d74482015-07-31 11:15:11 -07002602p.M2() // p.M2() M2 expects *T2 receiver
2603t.M2() // (&amp;t).M2() M2 expects *T2 receiver, see section on Calls
Robert Griesemer40818cf2014-11-11 13:19:47 -08002604</pre>
2605
2606<p>
2607but the following is invalid:
2608</p>
2609
2610<pre>
2611q.M0() // (*q).M0 is valid but not a field selector
Robert Griesemerc2d55862009-02-19 16:49:10 -08002612</pre>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002613
2614
Robert Griesemerf8520342014-08-28 08:53:25 -07002615<h3 id="Method_expressions">Method expressions</h3>
2616
2617<p>
2618If <code>M</code> is in the <a href="#Method_sets">method set</a> of type <code>T</code>,
2619<code>T.M</code> is a function that is callable as a regular function
2620with the same arguments as <code>M</code> prefixed by an additional
2621argument that is the receiver of the method.
2622</p>
2623
2624<pre class="ebnf">
2625MethodExpr = ReceiverType "." MethodName .
2626ReceiverType = TypeName | "(" "*" TypeName ")" | "(" ReceiverType ")" .
2627</pre>
2628
2629<p>
2630Consider a struct type <code>T</code> with two methods,
2631<code>Mv</code>, whose receiver is of type <code>T</code>, and
2632<code>Mp</code>, whose receiver is of type <code>*T</code>.
2633</p>
2634
2635<pre>
2636type T struct {
2637 a int
2638}
2639func (tv T) Mv(a int) int { return 0 } // value receiver
2640func (tp *T) Mp(f float32) float32 { return 1 } // pointer receiver
2641
2642var t T
2643</pre>
2644
2645<p>
2646The expression
2647</p>
2648
2649<pre>
2650T.Mv
2651</pre>
2652
2653<p>
2654yields a function equivalent to <code>Mv</code> but
2655with an explicit receiver as its first argument; it has signature
2656</p>
2657
2658<pre>
2659func(tv T, a int) int
2660</pre>
2661
2662<p>
2663That function may be called normally with an explicit receiver, so
2664these five invocations are equivalent:
2665</p>
2666
2667<pre>
2668t.Mv(7)
2669T.Mv(t, 7)
2670(T).Mv(t, 7)
2671f1 := T.Mv; f1(t, 7)
2672f2 := (T).Mv; f2(t, 7)
2673</pre>
2674
2675<p>
2676Similarly, the expression
2677</p>
2678
2679<pre>
2680(*T).Mp
2681</pre>
2682
2683<p>
2684yields a function value representing <code>Mp</code> with signature
2685</p>
2686
2687<pre>
2688func(tp *T, f float32) float32
2689</pre>
2690
2691<p>
2692For a method with a value receiver, one can derive a function
2693with an explicit pointer receiver, so
2694</p>
2695
2696<pre>
2697(*T).Mv
2698</pre>
2699
2700<p>
2701yields a function value representing <code>Mv</code> with signature
2702</p>
2703
2704<pre>
2705func(tv *T, a int) int
2706</pre>
2707
2708<p>
2709Such a function indirects through the receiver to create a value
2710to pass as the receiver to the underlying method;
2711the method does not overwrite the value whose address is passed in
2712the function call.
2713</p>
2714
2715<p>
2716The final case, a value-receiver function for a pointer-receiver method,
2717is illegal because pointer-receiver methods are not in the method set
2718of the value type.
2719</p>
2720
2721<p>
2722Function values derived from methods are called with function call syntax;
2723the receiver is provided as the first argument to the call.
2724That is, given <code>f := T.Mv</code>, <code>f</code> is invoked
2725as <code>f(t, 7)</code> not <code>t.f(7)</code>.
2726To construct a function that binds the receiver, use a
2727<a href="#Function_literals">function literal</a> or
2728<a href="#Method_values">method value</a>.
2729</p>
2730
2731<p>
2732It is legal to derive a function value from a method of an interface type.
2733The resulting function takes an explicit receiver of that interface type.
2734</p>
2735
2736<h3 id="Method_values">Method values</h3>
2737
2738<p>
2739If the expression <code>x</code> has static type <code>T</code> and
2740<code>M</code> is in the <a href="#Method_sets">method set</a> of type <code>T</code>,
2741<code>x.M</code> is called a <i>method value</i>.
2742The method value <code>x.M</code> is a function value that is callable
2743with the same arguments as a method call of <code>x.M</code>.
2744The expression <code>x</code> is evaluated and saved during the evaluation of the
2745method value; the saved copy is then used as the receiver in any calls,
2746which may be executed later.
2747</p>
2748
2749<p>
2750The type <code>T</code> may be an interface or non-interface type.
2751</p>
2752
2753<p>
2754As in the discussion of <a href="#Method_expressions">method expressions</a> above,
2755consider a struct type <code>T</code> with two methods,
2756<code>Mv</code>, whose receiver is of type <code>T</code>, and
2757<code>Mp</code>, whose receiver is of type <code>*T</code>.
2758</p>
2759
2760<pre>
2761type T struct {
2762 a int
2763}
2764func (tv T) Mv(a int) int { return 0 } // value receiver
2765func (tp *T) Mp(f float32) float32 { return 1 } // pointer receiver
2766
2767var t T
2768var pt *T
2769func makeT() T
2770</pre>
2771
2772<p>
2773The expression
2774</p>
2775
2776<pre>
2777t.Mv
2778</pre>
2779
2780<p>
2781yields a function value of type
2782</p>
2783
2784<pre>
2785func(int) int
2786</pre>
2787
2788<p>
2789These two invocations are equivalent:
2790</p>
2791
2792<pre>
2793t.Mv(7)
2794f := t.Mv; f(7)
2795</pre>
2796
2797<p>
2798Similarly, the expression
2799</p>
2800
2801<pre>
2802pt.Mp
2803</pre>
2804
2805<p>
2806yields a function value of type
2807</p>
2808
2809<pre>
2810func(float32) float32
2811</pre>
2812
2813<p>
2814As with <a href="#Selectors">selectors</a>, a reference to a non-interface method with a value receiver
2815using a pointer will automatically dereference that pointer: <code>pt.Mv</code> is equivalent to <code>(*pt).Mv</code>.
2816</p>
2817
2818<p>
2819As with <a href="#Calls">method calls</a>, a reference to a non-interface method with a pointer receiver
2820using an addressable value will automatically take the address of that value: <code>t.Mp</code> is equivalent to <code>(&amp;t).Mp</code>.
2821</p>
2822
2823<pre>
2824f := t.Mv; f(7) // like t.Mv(7)
2825f := pt.Mp; f(7) // like pt.Mp(7)
2826f := pt.Mv; f(7) // like (*pt).Mv(7)
2827f := t.Mp; f(7) // like (&amp;t).Mp(7)
2828f := makeT().Mp // invalid: result of makeT() is not addressable
2829</pre>
2830
2831<p>
2832Although the examples above use non-interface types, it is also legal to create a method value
2833from a value of interface type.
2834</p>
2835
2836<pre>
2837var i interface { M(int) } = myVal
2838f := i.M; f(7) // like i.M(7)
2839</pre>
2840
2841
Robert Griesemer9c9e8112012-12-10 11:55:57 -08002842<h3 id="Index_expressions">Index expressions</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002843
Rob Pikedf3183f2009-02-26 16:37:23 -08002844<p>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002845A primary expression of the form
Rob Pikedf3183f2009-02-26 16:37:23 -08002846</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002847
Robert Griesemerc2d55862009-02-19 16:49:10 -08002848<pre>
2849a[x]
2850</pre>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002851
Rob Pike4501d342009-02-19 17:31:36 -08002852<p>
Robert Griesemer2961d222013-07-31 22:25:47 -07002853denotes the element of the array, pointer to array, slice, string or map <code>a</code> indexed by <code>x</code>.
2854The value <code>x</code> is called the <i>index</i> or <i>map key</i>, respectively.
2855The following rules apply:
Rob Pike4501d342009-02-19 17:31:36 -08002856</p>
Russ Coxeaa92e02009-06-25 14:43:55 -07002857
Robert Griesemerc2d55862009-02-19 16:49:10 -08002858<p>
Robert Griesemer39067062012-12-12 11:06:26 -08002859If <code>a</code> is not a map:
2860</p>
2861<ul>
Robert Griesemerb66df0e2013-03-11 09:20:52 -07002862 <li>the index <code>x</code> must be of integer type or untyped;
2863 it is <i>in range</i> if <code>0 &lt;= x &lt; len(a)</code>,
Robert Griesemer39067062012-12-12 11:06:26 -08002864 otherwise it is <i>out of range</i></li>
2865 <li>a <a href="#Constants">constant</a> index must be non-negative
Oling Cat018e89f2013-01-24 20:46:33 +11002866 and representable by a value of type <code>int</code>
Robert Griesemer39067062012-12-12 11:06:26 -08002867</ul>
2868
2869<p>
Robert Griesemer2961d222013-07-31 22:25:47 -07002870For <code>a</code> of <a href="#Array_types">array type</a> <code>A</code>:
Rob Pike4501d342009-02-19 17:31:36 -08002871</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002872<ul>
Robert Griesemerea7c57a2012-10-17 11:08:42 -07002873 <li>a <a href="#Constants">constant</a> index must be in range</li>
Robert Griesemer2961d222013-07-31 22:25:47 -07002874 <li>if <code>x</code> is out of range at run time,
Robert Griesemerea7c57a2012-10-17 11:08:42 -07002875 a <a href="#Run_time_panics">run-time panic</a> occurs</li>
Rob Pikedf3183f2009-02-26 16:37:23 -08002876 <li><code>a[x]</code> is the array element at index <code>x</code> and the type of
Oling Cat018e89f2013-01-24 20:46:33 +11002877 <code>a[x]</code> is the element type of <code>A</code></li>
Robert Griesemerea7c57a2012-10-17 11:08:42 -07002878</ul>
2879
2880<p>
Robert Griesemer2961d222013-07-31 22:25:47 -07002881For <code>a</code> of <a href="#Pointer_types">pointer</a> to array type:
Robert Griesemerea7c57a2012-10-17 11:08:42 -07002882</p>
2883<ul>
Robert Griesemer2961d222013-07-31 22:25:47 -07002884 <li><code>a[x]</code> is shorthand for <code>(*a)[x]</code></li>
2885</ul>
2886
2887<p>
2888For <code>a</code> of <a href="#Slice_types">slice type</a> <code>S</code>:
2889</p>
2890<ul>
2891 <li>if <code>x</code> is out of range at run time,
Robert Griesemerea7c57a2012-10-17 11:08:42 -07002892 a <a href="#Run_time_panics">run-time panic</a> occurs</li>
2893 <li><code>a[x]</code> is the slice element at index <code>x</code> and the type of
Oling Cat018e89f2013-01-24 20:46:33 +11002894 <code>a[x]</code> is the element type of <code>S</code></li>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002895</ul>
Russ Coxeaa92e02009-06-25 14:43:55 -07002896
Robert Griesemerc2d55862009-02-19 16:49:10 -08002897<p>
Robert Griesemer2961d222013-07-31 22:25:47 -07002898For <code>a</code> of <a href="#String_types">string type</a>:
Russ Coxeaa92e02009-06-25 14:43:55 -07002899</p>
2900<ul>
Robert Griesemer39067062012-12-12 11:06:26 -08002901 <li>a <a href="#Constants">constant</a> index must be in range
Robert Griesemerea7c57a2012-10-17 11:08:42 -07002902 if the string <code>a</code> is also constant</li>
2903 <li>if <code>x</code> is out of range at run time,
2904 a <a href="#Run_time_panics">run-time panic</a> occurs</li>
Robert Griesemer2961d222013-07-31 22:25:47 -07002905 <li><code>a[x]</code> is the non-constant byte value at index <code>x</code> and the type of
Oling Cat018e89f2013-01-24 20:46:33 +11002906 <code>a[x]</code> is <code>byte</code></li>
Robert Griesemer3af480372010-05-14 13:11:48 -07002907 <li><code>a[x]</code> may not be assigned to</li>
Russ Coxeaa92e02009-06-25 14:43:55 -07002908</ul>
2909
2910<p>
Robert Griesemer2961d222013-07-31 22:25:47 -07002911For <code>a</code> of <a href="#Map_types">map type</a> <code>M</code>:
Rob Pike4501d342009-02-19 17:31:36 -08002912</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002913<ul>
Robert Griesemer29f1ca52010-03-23 14:01:51 -07002914 <li><code>x</code>'s type must be
Oling Cat018e89f2013-01-24 20:46:33 +11002915 <a href="#Assignability">assignable</a>
2916 to the key type of <code>M</code></li>
Robert Griesemer29f1ca52010-03-23 14:01:51 -07002917 <li>if the map contains an entry with key <code>x</code>,
Oling Cat018e89f2013-01-24 20:46:33 +11002918 <code>a[x]</code> is the map value with key <code>x</code>
2919 and the type of <code>a[x]</code> is the value type of <code>M</code></li>
Robert Griesemer54731032011-05-12 09:15:59 -07002920 <li>if the map is <code>nil</code> or does not contain such an entry,
Oling Cat018e89f2013-01-24 20:46:33 +11002921 <code>a[x]</code> is the <a href="#The_zero_value">zero value</a>
2922 for the value type of <code>M</code></li>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002923</ul>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002924
Robert Griesemerc2d55862009-02-19 16:49:10 -08002925<p>
Robert Griesemer29f1ca52010-03-23 14:01:51 -07002926Otherwise <code>a[x]</code> is illegal.
Rob Pikedf3183f2009-02-26 16:37:23 -08002927</p>
Robert Griesemer7abfcd92008-10-07 17:14:30 -07002928
Rob Pikedf3183f2009-02-26 16:37:23 -08002929<p>
Robert Griesemer29f1ca52010-03-23 14:01:51 -07002930An index expression on a map <code>a</code> of type <code>map[K]V</code>
Robert Griesemerc0fca132014-08-05 11:31:32 -07002931used in an <a href="#Assignments">assignment</a> or initialization of the special form
Rob Pikedf3183f2009-02-26 16:37:23 -08002932</p>
2933
2934<pre>
Robert Griesemer29f1ca52010-03-23 14:01:51 -07002935v, ok = a[x]
2936v, ok := a[x]
2937var v, ok = a[x]
Rob Pikedf3183f2009-02-26 16:37:23 -08002938</pre>
2939
2940<p>
Robert Griesemerc0fca132014-08-05 11:31:32 -07002941yields an additional untyped boolean value. The value of <code>ok</code> is
Robert Griesemer29f1ca52010-03-23 14:01:51 -07002942<code>true</code> if the key <code>x</code> is present in the map, and
Robert Griesemerc0fca132014-08-05 11:31:32 -07002943<code>false</code> otherwise.
Rob Pikedf3183f2009-02-26 16:37:23 -08002944</p>
2945
2946<p>
Robert Griesemer54731032011-05-12 09:15:59 -07002947Assigning to an element of a <code>nil</code> map causes a
2948<a href="#Run_time_panics">run-time panic</a>.
2949</p>
2950
Robert Griesemer29f1ca52010-03-23 14:01:51 -07002951
Robert Griesemere333b962013-09-11 17:18:52 -07002952<h3 id="Slice_expressions">Slice expressions</h3>
2953
2954<p>
2955Slice expressions construct a substring or slice from a string, array, pointer
2956to array, or slice. There are two variants: a simple form that specifies a low
2957and high bound, and a full form that also specifies a bound on the capacity.
2958</p>
2959
2960<h4>Simple slice expressions</h4>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002961
Rob Pikedf3183f2009-02-26 16:37:23 -08002962<p>
Russ Cox8a8445b2011-12-02 13:11:30 -05002963For a string, array, pointer to array, or slice <code>a</code>, the primary expression
Rob Pikedf3183f2009-02-26 16:37:23 -08002964</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002965
Robert Griesemerc2d55862009-02-19 16:49:10 -08002966<pre>
Robert Griesemer9e5bf272010-09-07 16:32:35 -07002967a[low : high]
Robert Griesemerc2d55862009-02-19 16:49:10 -08002968</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002969
Rob Pikedf3183f2009-02-26 16:37:23 -08002970<p>
Robert Griesemer2961d222013-07-31 22:25:47 -07002971constructs a substring or slice. The <i>indices</i> <code>low</code> and
2972<code>high</code> select which elements of operand <code>a</code> appear
2973in the result. The result has indices starting at 0 and length equal to
Robert Griesemer9e5bf272010-09-07 16:32:35 -07002974<code>high</code>&nbsp;-&nbsp;<code>low</code>.
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08002975After slicing the array <code>a</code>
2976</p>
2977
2978<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08002979a := [5]int{1, 2, 3, 4, 5}
2980s := a[1:4]
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08002981</pre>
2982
2983<p>
2984the slice <code>s</code> has type <code>[]int</code>, length 3, capacity 4, and elements
Rob Pikedf3183f2009-02-26 16:37:23 -08002985</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002986
Robert Griesemerc2d55862009-02-19 16:49:10 -08002987<pre>
2988s[0] == 2
2989s[1] == 3
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08002990s[2] == 4
Robert Griesemerc2d55862009-02-19 16:49:10 -08002991</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002992
Rob Pikedf3183f2009-02-26 16:37:23 -08002993<p>
Robert Griesemer9c9e8112012-12-10 11:55:57 -08002994For convenience, any of the indices may be omitted. A missing <code>low</code>
Robert Griesemer9e5bf272010-09-07 16:32:35 -07002995index defaults to zero; a missing <code>high</code> index defaults to the length of the
2996sliced operand:
Scott Lawrence0c1695b2010-09-07 14:30:17 -07002997</p>
2998
Robert Griesemer9e5bf272010-09-07 16:32:35 -07002999<pre>
Robert Hencke58d18e22013-10-03 12:46:02 -07003000a[2:] // same as a[2 : len(a)]
David Symonds72a29792011-11-29 15:47:36 -08003001a[:3] // same as a[0 : 3]
3002a[:] // same as a[0 : len(a)]
Robert Griesemer9e5bf272010-09-07 16:32:35 -07003003</pre>
3004
Scott Lawrence0c1695b2010-09-07 14:30:17 -07003005<p>
Robert Griesemer2961d222013-07-31 22:25:47 -07003006If <code>a</code> is a pointer to an array, <code>a[low : high]</code> is shorthand for
3007<code>(*a)[low : high]</code>.
3008</p>
3009
3010<p>
3011For arrays or strings, the indices are <i>in range</i> if
3012<code>0</code> &lt;= <code>low</code> &lt;= <code>high</code> &lt;= <code>len(a)</code>,
Robert Griesemerea7c57a2012-10-17 11:08:42 -07003013otherwise they are <i>out of range</i>.
3014For slices, the upper index bound is the slice capacity <code>cap(a)</code> rather than the length.
Robert Griesemer39067062012-12-12 11:06:26 -08003015A <a href="#Constants">constant</a> index must be non-negative and representable by a value of type
Robert Griesemer6ffd2352014-03-06 17:11:13 -08003016<code>int</code>; for arrays or constant strings, constant indices must also be in range.
Robert Griesemer2961d222013-07-31 22:25:47 -07003017If both indices are constant, they must satisfy <code>low &lt;= high</code>.
3018If the indices are out of range at run time, a <a href="#Run_time_panics">run-time panic</a> occurs.
Robert Griesemer19b1d352009-09-24 19:36:48 -07003019</p>
3020
Robert Griesemerc2d55862009-02-19 16:49:10 -08003021<p>
Robert Griesemer2961d222013-07-31 22:25:47 -07003022Except for <a href="#Constants">untyped strings</a>, if the sliced operand is a string or slice,
3023the result of the slice operation is a non-constant value of the same type as the operand.
3024For untyped string operands the result is a non-constant value of type <code>string</code>.
Robert Griesemerc423e952010-09-02 10:16:31 -07003025If the sliced operand is an array, it must be <a href="#Address_operators">addressable</a>
3026and the result of the slice operation is a slice with the same element type as the array.
Rob Pikedf3183f2009-02-26 16:37:23 -08003027</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003028
Robert Griesemer2961d222013-07-31 22:25:47 -07003029<p>
3030If the sliced operand of a valid slice expression is a <code>nil</code> slice, the result
Robert Griesemer15da9972013-10-16 16:16:54 -07003031is a <code>nil</code> slice. Otherwise, the result shares its underlying array with the
3032operand.
Rob Pike15e6ce22013-08-15 13:15:55 +10003033</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003034
Robert Griesemere333b962013-09-11 17:18:52 -07003035<h4>Full slice expressions</h4>
3036
3037<p>
3038For an array, pointer to array, or slice <code>a</code> (but not a string), the primary expression
3039</p>
3040
3041<pre>
3042a[low : high : max]
3043</pre>
3044
3045<p>
3046constructs a slice of the same type, and with the same length and elements as the simple slice
3047expression <code>a[low : high]</code>. Additionally, it controls the resulting slice's capacity
3048by setting it to <code>max - low</code>. Only the first index may be omitted; it defaults to 0.
3049After slicing the array <code>a</code>
3050</p>
3051
3052<pre>
3053a := [5]int{1, 2, 3, 4, 5}
3054t := a[1:3:5]
3055</pre>
3056
3057<p>
3058the slice <code>t</code> has type <code>[]int</code>, length 2, capacity 4, and elements
3059</p>
3060
3061<pre>
3062t[0] == 2
3063t[1] == 3
3064</pre>
3065
3066<p>
3067As for simple slice expressions, if <code>a</code> is a pointer to an array,
3068<code>a[low : high : max]</code> is shorthand for <code>(*a)[low : high : max]</code>.
3069If the sliced operand is an array, it must be <a href="#Address_operators">addressable</a>.
3070</p>
3071
3072<p>
3073The indices are <i>in range</i> if <code>0 &lt;= low &lt;= high &lt;= max &lt;= cap(a)</code>,
3074otherwise they are <i>out of range</i>.
3075A <a href="#Constants">constant</a> index must be non-negative and representable by a value of type
Robert Griesemer6ffd2352014-03-06 17:11:13 -08003076<code>int</code>; for arrays, constant indices must also be in range.
Robert Griesemere333b962013-09-11 17:18:52 -07003077If multiple indices are constant, the constants that are present must be in range relative to each
3078other.
3079If the indices are out of range at run time, a <a href="#Run_time_panics">run-time panic</a> occurs.
3080</p>
3081
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003082<h3 id="Type_assertions">Type assertions</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003083
Rob Pikedf3183f2009-02-26 16:37:23 -08003084<p>
Robert Griesemere9192752009-12-01 16:15:53 -08003085For an expression <code>x</code> of <a href="#Interface_types">interface type</a>
3086and a type <code>T</code>, the primary expression
Rob Pikedf3183f2009-02-26 16:37:23 -08003087</p>
Robert Griesemer434c6052008-11-07 13:34:37 -08003088
Robert Griesemerc2d55862009-02-19 16:49:10 -08003089<pre>
3090x.(T)
3091</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -08003092
Robert Griesemerc2d55862009-02-19 16:49:10 -08003093<p>
Robert Griesemere9192752009-12-01 16:15:53 -08003094asserts that <code>x</code> is not <code>nil</code>
Russ Coxb89a54e2009-05-20 18:16:04 -07003095and that the value stored in <code>x</code> is of type <code>T</code>.
Russ Cox5958dd62009-03-04 17:19:21 -08003096The notation <code>x.(T)</code> is called a <i>type assertion</i>.
Rob Pikedf3183f2009-02-26 16:37:23 -08003097</p>
3098<p>
Russ Cox5958dd62009-03-04 17:19:21 -08003099More precisely, if <code>T</code> is not an interface type, <code>x.(T)</code> asserts
Robert Griesemer7bc03712010-06-07 15:49:39 -07003100that the dynamic type of <code>x</code> is <a href="#Type_identity">identical</a>
3101to the type <code>T</code>.
Robert Griesemer48567312012-12-06 09:17:20 -08003102In this case, <code>T</code> must <a href="#Method_sets">implement</a> the (interface) type of <code>x</code>;
3103otherwise the type assertion is invalid since it is not possible for <code>x</code>
3104to store a value of type <code>T</code>.
Russ Cox5958dd62009-03-04 17:19:21 -08003105If <code>T</code> is an interface type, <code>x.(T)</code> asserts that the dynamic type
Robert Griesemer48567312012-12-06 09:17:20 -08003106of <code>x</code> implements the interface <code>T</code>.
Rob Pikedf3183f2009-02-26 16:37:23 -08003107</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08003108<p>
Russ Cox5958dd62009-03-04 17:19:21 -08003109If the type assertion holds, the value of the expression is the value
Rob Pike5bb29fb2010-03-25 17:59:59 -07003110stored in <code>x</code> and its type is <code>T</code>. If the type assertion is false,
3111a <a href="#Run_time_panics">run-time panic</a> occurs.
3112In other words, even though the dynamic type of <code>x</code>
Robert Griesemerea7c57a2012-10-17 11:08:42 -07003113is known only at run time, the type of <code>x.(T)</code> is
Rob Pikedf3183f2009-02-26 16:37:23 -08003114known to be <code>T</code> in a correct program.
3115</p>
Robert Griesemer48567312012-12-06 09:17:20 -08003116
3117<pre>
3118var x interface{} = 7 // x has dynamic type int and value 7
3119i := x.(int) // i has type int and value 7
3120
3121type I interface { m() }
3122var y I
3123s := y.(string) // illegal: string does not implement I (missing method m)
3124r := y.(io.Reader) // r has type io.Reader and y must implement both I and io.Reader
3125</pre>
3126
Robert Griesemerc2d55862009-02-19 16:49:10 -08003127<p>
Robert Griesemerc0fca132014-08-05 11:31:32 -07003128A type assertion used in an <a href="#Assignments">assignment</a> or initialization of the special form
Rob Pikedf3183f2009-02-26 16:37:23 -08003129</p>
Robert Griesemer434c6052008-11-07 13:34:37 -08003130
Robert Griesemerc2d55862009-02-19 16:49:10 -08003131<pre>
3132v, ok = x.(T)
3133v, ok := x.(T)
Rob Piked5537072009-08-22 00:04:04 -07003134var v, ok = x.(T)
Robert Griesemerc2d55862009-02-19 16:49:10 -08003135</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -08003136
Robert Griesemerc2d55862009-02-19 16:49:10 -08003137<p>
Robert Griesemerc0fca132014-08-05 11:31:32 -07003138yields an additional untyped boolean value. The value of <code>ok</code> is <code>true</code>
3139if the assertion holds. Otherwise it is <code>false</code> and the value of <code>v</code> is
3140the <a href="#The_zero_value">zero value</a> for type <code>T</code>.
Rob Pike5bb29fb2010-03-25 17:59:59 -07003141No run-time panic occurs in this case.
Rob Pikedf3183f2009-02-26 16:37:23 -08003142</p>
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -07003143
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003144
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003145<h3 id="Calls">Calls</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003146
Robert Griesemerc2d55862009-02-19 16:49:10 -08003147<p>
Rob Pike96750f12009-02-27 16:47:48 -08003148Given an expression <code>f</code> of function type
3149<code>F</code>,
Rob Pikedf3183f2009-02-26 16:37:23 -08003150</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003151
Robert Griesemerc2d55862009-02-19 16:49:10 -08003152<pre>
Robert Griesemer3c7271f2011-05-24 14:18:44 -07003153f(a1, a2, … an)
Robert Griesemerc2d55862009-02-19 16:49:10 -08003154</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003155
Robert Griesemerc2d55862009-02-19 16:49:10 -08003156<p>
Robert Griesemer3c7271f2011-05-24 14:18:44 -07003157calls <code>f</code> with arguments <code>a1, a2, … an</code>.
Rob Pike4fe41922009-11-07 22:00:59 -08003158Except for one special case, arguments must be single-valued expressions
Robert Griesemer440cc952010-06-07 17:40:21 -07003159<a href="#Assignability">assignable</a> to the parameter types of
Rob Pikedf3183f2009-02-26 16:37:23 -08003160<code>F</code> and are evaluated before the function is called.
3161The type of the expression is the result type
3162of <code>F</code>.
3163A method invocation is similar but the method itself
3164is specified as a selector upon a value of the receiver type for
3165the method.
3166</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003167
Robert Griesemerc2d55862009-02-19 16:49:10 -08003168<pre>
David Symonds72a29792011-11-29 15:47:36 -08003169math.Atan2(x, y) // function call
Robert Griesemer130ac742009-12-10 16:43:01 -08003170var pt *Point
Robert Griesemerccc713c2014-10-27 16:31:15 -07003171pt.Scale(3.5) // method call with receiver pt
Robert Griesemerc2d55862009-02-19 16:49:10 -08003172</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003173
Rob Pikedf3183f2009-02-26 16:37:23 -08003174<p>
Rob Pike633a2ce2012-01-23 08:40:13 -08003175In a function call, the function value and arguments are evaluated in
3176<a href="#Order_of_evaluation">the usual order</a>.
3177After they are evaluated, the parameters of the call are passed by value to the function
3178and the called function begins execution.
3179The return parameters of the function are passed by value
3180back to the calling function when the function returns.
3181</p>
3182
3183<p>
Hong Ruiqi8374e672012-04-05 22:37:07 +10003184Calling a <code>nil</code> function value
Rob Pike633a2ce2012-01-23 08:40:13 -08003185causes a <a href="#Run_time_panics">run-time panic</a>.
3186</p>
3187
3188<p>
Russ Cox1b3083e2013-02-09 14:46:55 -05003189As a special case, if the return values of a function or method
Robert Griesemer440cc952010-06-07 17:40:21 -07003190<code>g</code> are equal in number and individually
3191assignable to the parameters of another function or method
Rob Pike4fe41922009-11-07 22:00:59 -08003192<code>f</code>, then the call <code>f(g(<i>parameters_of_g</i>))</code>
3193will 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
Russ Cox1b3083e2013-02-09 14:46:55 -05003195of <code>f</code> must contain no parameters other than the call of <code>g</code>,
3196and <code>g</code> must have at least one return value.
Rob Pike4fe41922009-11-07 22:00:59 -08003197If <code>f</code> has a final <code>...</code> parameter, it is
3198assigned the return values of <code>g</code> that remain after
3199assignment of regular parameters.
3200</p>
3201
3202<pre>
3203func Split(s string, pos int) (string, string) {
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08003204 return s[0:pos], s[pos:]
Rob Pike4fe41922009-11-07 22:00:59 -08003205}
3206
3207func Join(s, t string) string {
3208 return s + t
3209}
3210
3211if Join(Split(value, len(value)/2)) != value {
Robert Griesemer7fc4e372011-02-01 12:51:10 -08003212 log.Panic("test fails")
Rob Pike4fe41922009-11-07 22:00:59 -08003213}
3214</pre>
3215
3216<p>
Robert Griesemer2a838d62011-02-08 13:31:01 -08003217A method call <code>x.m()</code> is valid if the <a href="#Method_sets">method set</a>
3218of (the type of) <code>x</code> contains <code>m</code> and the
Robert Griesemer63f01492010-05-28 14:17:30 -07003219argument list can be assigned to the parameter list of <code>m</code>.
Rob Pike678625d2009-09-15 09:54:22 -07003220If <code>x</code> is <a href="#Address_operators">addressable</a> and <code>&amp;x</code>'s method
Robert Griesemer56809d02009-05-20 11:02:48 -07003221set contains <code>m</code>, <code>x.m()</code> is shorthand
3222for <code>(&amp;x).m()</code>:
Rob Pikedf3183f2009-02-26 16:37:23 -08003223</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003224
Robert Griesemerc2d55862009-02-19 16:49:10 -08003225<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08003226var p Point
Rob Pikedf3183f2009-02-26 16:37:23 -08003227p.Scale(3.5)
Robert Griesemerc2d55862009-02-19 16:49:10 -08003228</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003229
Robert Griesemerc2d55862009-02-19 16:49:10 -08003230<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003231There is no distinct method type and there are no method literals.
Rob Pikedf3183f2009-02-26 16:37:23 -08003232</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003233
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003234<h3 id="Passing_arguments_to_..._parameters">Passing arguments to <code>...</code> parameters</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003235
Robert Griesemerc2d55862009-02-19 16:49:10 -08003236<p>
Robert Griesemera7662772014-03-06 10:35:05 -08003237If <code>f</code> is <a href="#Function_types">variadic</a> with a final
3238parameter <code>p</code> of type <code>...T</code>, then within <code>f</code>
3239the type of <code>p</code> is equivalent to type <code>[]T</code>.
3240If <code>f</code> is invoked with no actual arguments for <code>p</code>,
3241the value passed to <code>p</code> is <code>nil</code>.
3242Otherwise, the value passed is a new slice
3243of type <code>[]T</code> with a new underlying array whose successive elements
3244are the actual arguments, which all must be <a href="#Assignability">assignable</a>
3245to <code>T</code>. The length and capacity of the slice is therefore
3246the number of arguments bound to <code>p</code> and may differ for each
3247call site.
Rob Pikeb81065d2010-01-27 13:14:40 -08003248</p>
Robert Griesemer69e26bf2008-11-04 16:46:45 -08003249
Rob Pikedf3183f2009-02-26 16:37:23 -08003250<p>
Robert Griesemera7662772014-03-06 10:35:05 -08003251Given the function and calls
Rob Pikeb81065d2010-01-27 13:14:40 -08003252</p>
3253<pre>
Robert Griesemer3c7271f2011-05-24 14:18:44 -07003254func Greeting(prefix string, who ...string)
Robert Griesemera7662772014-03-06 10:35:05 -08003255Greeting("nobody")
Rob Pikeb81065d2010-01-27 13:14:40 -08003256Greeting("hello:", "Joe", "Anna", "Eileen")
3257</pre>
3258
3259<p>
Robert Griesemerac771a82010-09-24 14:08:28 -07003260within <code>Greeting</code>, <code>who</code> will have the value
Robert Griesemera7662772014-03-06 10:35:05 -08003261<code>nil</code> in the first call, and
3262<code>[]string{"Joe", "Anna", "Eileen"}</code> in the second.
Rob Pikeb81065d2010-01-27 13:14:40 -08003263</p>
3264
Robert Griesemerac771a82010-09-24 14:08:28 -07003265<p>
Robert Griesemer904adfd2010-10-27 10:44:31 -07003266If the final argument is assignable to a slice type <code>[]T</code>, it may be
3267passed unchanged as the value for a <code>...T</code> parameter if the argument
3268is followed by <code>...</code>. In this case no new slice is created.
Robert Griesemerac771a82010-09-24 14:08:28 -07003269</p>
Rob Pikeb81065d2010-01-27 13:14:40 -08003270
3271<p>
Robert Griesemerac771a82010-09-24 14:08:28 -07003272Given the slice <code>s</code> and call
Rob Pikeb81065d2010-01-27 13:14:40 -08003273</p>
Robert Griesemer69e26bf2008-11-04 16:46:45 -08003274
Robert Griesemerac771a82010-09-24 14:08:28 -07003275<pre>
3276s := []string{"James", "Jasmine"}
3277Greeting("goodbye:", s...)
3278</pre>
3279
3280<p>
3281within <code>Greeting</code>, <code>who</code> will have the same value as <code>s</code>
3282with the same underlying array.
3283</p>
3284
3285
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003286<h3 id="Operators">Operators</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003287
Rob Pikedf3183f2009-02-26 16:37:23 -08003288<p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07003289Operators combine operands into expressions.
Rob Pikedf3183f2009-02-26 16:37:23 -08003290</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07003291
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003292<pre class="ebnf">
Matthew Dempskyabb818b2015-04-20 17:17:24 -07003293Expression = UnaryExpr | Expression binary_op Expression .
Rob Pikedf3183f2009-02-26 16:37:23 -08003294UnaryExpr = PrimaryExpr | unary_op UnaryExpr .
Robert Griesemer57b34612008-10-10 12:45:44 -07003295
Robert Griesemerb50ed022011-02-01 12:02:49 -08003296binary_op = "||" | "&amp;&amp;" | rel_op | add_op | mul_op .
Rob Pikedf3183f2009-02-26 16:37:23 -08003297rel_op = "==" | "!=" | "&lt;" | "&lt;=" | ">" | ">=" .
3298add_op = "+" | "-" | "|" | "^" .
Robert Griesemer4ed666e2009-08-27 16:45:42 -07003299mul_op = "*" | "/" | "%" | "&lt;&lt;" | "&gt;&gt;" | "&amp;" | "&amp;^" .
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003300
Rob Pikedf3183f2009-02-26 16:37:23 -08003301unary_op = "+" | "-" | "!" | "^" | "*" | "&amp;" | "&lt;-" .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003302</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003303
Robert Griesemerc2d55862009-02-19 16:49:10 -08003304<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07003305Comparisons are discussed <a href="#Comparison_operators">elsewhere</a>.
Robert Griesemer7bc03712010-06-07 15:49:39 -07003306For other binary operators, the operand types must be <a href="#Type_identity">identical</a>
Robert Griesemer32d12782011-05-23 14:12:42 -07003307unless the operation involves shifts or untyped <a href="#Constants">constants</a>.
Robert Griesemer19b1d352009-09-24 19:36:48 -07003308For operations involving constants only, see the section on
3309<a href="#Constant_expressions">constant expressions</a>.
Rob Pike4501d342009-02-19 17:31:36 -08003310</p>
Robert Griesemerc8e18762008-09-12 12:26:22 -07003311
Rob Pike83cbca52009-08-21 14:18:08 -07003312<p>
Robert Griesemer32d12782011-05-23 14:12:42 -07003313Except for shift operations, if one operand is an untyped <a href="#Constants">constant</a>
Robert Griesemer19b1d352009-09-24 19:36:48 -07003314and the other operand is not, the constant is <a href="#Conversions">converted</a>
3315to the type of the other operand.
Rob Pike83cbca52009-08-21 14:18:08 -07003316</p>
Robert Griesemer7539c852009-07-31 18:05:07 -07003317
Rob Pike83cbca52009-08-21 14:18:08 -07003318<p>
Robert Griesemer32d12782011-05-23 14:12:42 -07003319The right operand in a shift expression must have unsigned integer type
Robert Griesemer19b1d352009-09-24 19:36:48 -07003320or be an untyped constant that can be converted to unsigned integer type.
Robert Griesemer32d12782011-05-23 14:12:42 -07003321If the left operand of a non-constant shift expression is an untyped constant,
Robert Griesemer87c87072015-08-04 10:38:50 -07003322it is first converted to the type it would assume if the shift expression were
Robert Griesemer58e21dd2013-03-15 13:55:50 -07003323replaced by its left operand alone.
Rob Pike83cbca52009-08-21 14:18:08 -07003324</p>
Robert Griesemera6b546f2008-10-20 11:46:40 -07003325
Rob Pike83cbca52009-08-21 14:18:08 -07003326<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08003327var s uint = 33
Robert Griesemer32d12782011-05-23 14:12:42 -07003328var i = 1&lt;&lt;s // 1 has type int
3329var j int32 = 1&lt;&lt;s // 1 has type int32; j == 0
3330var k = uint64(1&lt;&lt;s) // 1 has type uint64; k == 1&lt;&lt;33
Robert Griesemer953f2de2012-03-01 10:35:15 -08003331var m int = 1.0&lt;&lt;s // 1.0 has type int
Robert Griesemer58e21dd2013-03-15 13:55:50 -07003332var n = 1.0&lt;&lt;s != i // 1.0 has type int; n == false if ints are 32bits in size
Robert Griesemer953f2de2012-03-01 10:35:15 -08003333var o = 1&lt;&lt;s == 2&lt;&lt;s // 1 and 2 have type int; o == true if ints are 32bits in size
Robert Griesemer636c5fa2011-06-08 09:11:18 -07003334var 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
Robert Griesemer32d12782011-05-23 14:12:42 -07003335var u = 1.0&lt;&lt;s // illegal: 1.0 has type float64, cannot shift
Robert Griesemer58e21dd2013-03-15 13:55:50 -07003336var u1 = 1.0&lt;&lt;s != 0 // illegal: 1.0 has type float64, cannot shift
3337var u2 = 1&lt;&lt;s != 1.0 // illegal: 1 has type float64, cannot shift
Robert Griesemer32d12782011-05-23 14:12:42 -07003338var v float32 = 1&lt;&lt;s // illegal: 1 has type float32, cannot shift
Robert Griesemer953f2de2012-03-01 10:35:15 -08003339var w int64 = 1.0&lt;&lt;33 // 1.0&lt;&lt;33 is a constant shift expression
Rob Pike83cbca52009-08-21 14:18:08 -07003340</pre>
Robert Griesemer18b05c12009-01-26 09:34:19 -08003341
Robert Griesemer3dd3ab42015-08-04 15:04:39 -07003342
3343<h4 id="Operator_precedence">Operator precedence</h4>
Robert Griesemerc2d55862009-02-19 16:49:10 -08003344<p>
Russ Coxec9b0422009-07-09 16:44:13 -07003345Unary operators have the highest precedence.
3346As the <code>++</code> and <code>--</code> operators form
Rob Pikedf3183f2009-02-26 16:37:23 -08003347statements, not expressions, they fall
Russ Coxec9b0422009-07-09 16:44:13 -07003348outside the operator hierarchy.
Rob Pikedf3183f2009-02-26 16:37:23 -08003349As a consequence, statement <code>*p++</code> is the same as <code>(*p)++</code>.
3350<p>
Robert Griesemerb50ed022011-02-01 12:02:49 -08003351There are five precedence levels for binary operators.
Rob Pikedf3183f2009-02-26 16:37:23 -08003352Multiplication operators bind strongest, followed by addition
Russ Cox1b4e37a2012-09-12 12:05:24 -04003353operators, comparison operators, <code>&amp;&amp;</code> (logical AND),
3354and finally <code>||</code> (logical OR):
Rob Pikedf3183f2009-02-26 16:37:23 -08003355</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003356
Rob Pikeff70f092009-02-20 13:36:14 -08003357<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08003358Precedence Operator
Robert Griesemerb50ed022011-02-01 12:02:49 -08003359 5 * / % &lt;&lt; &gt;&gt; &amp; &amp;^
3360 4 + - | ^
Robert Griesemera1368a62011-02-22 15:31:57 -08003361 3 == != &lt; &lt;= &gt; &gt;=
Robert Griesemerc2d55862009-02-19 16:49:10 -08003362 2 &amp;&amp;
3363 1 ||
3364</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003365
Robert Griesemerc2d55862009-02-19 16:49:10 -08003366<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08003367Binary operators of the same precedence associate from left to right.
Robert Griesemerd36d1912009-09-18 11:58:35 -07003368For instance, <code>x / y * z</code> is the same as <code>(x / y) * z</code>.
Rob Pikedf3183f2009-02-26 16:37:23 -08003369</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003370
Robert Griesemerc2d55862009-02-19 16:49:10 -08003371<pre>
3372+x
337323 + 3*x[i]
3374x &lt;= f()
Robert Griesemer4ed666e2009-08-27 16:45:42 -07003375^a &gt;&gt; b
Robert Griesemerc2d55862009-02-19 16:49:10 -08003376f() || g()
Russ Cox9c08d652012-02-21 22:04:30 -05003377x == y+1 &amp;&amp; &lt;-chanPtr &gt; 0
Robert Griesemerc2d55862009-02-19 16:49:10 -08003378</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003379
3380
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003381<h3 id="Arithmetic_operators">Arithmetic operators</h3>
Robert Griesemerc2d55862009-02-19 16:49:10 -08003382<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07003383Arithmetic operators apply to numeric values and yield a result of the same
Rob Pikedf3183f2009-02-26 16:37:23 -08003384type as the first operand. The four standard arithmetic operators (<code>+</code>,
Robert Griesemer3dd3ab42015-08-04 15:04:39 -07003385<code>-</code>, <code>*</code>, <code>/</code>) apply to integer,
3386floating-point, and complex types; <code>+</code> also applies to strings.
3387The bitwise logical and shift operators apply to integers only.
Rob Pikedf3183f2009-02-26 16:37:23 -08003388</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07003389
Rob Pikeff70f092009-02-20 13:36:14 -08003390<pre class="grammar">
Rob Pike72970872010-03-04 12:35:16 -08003391+ sum integers, floats, complex values, strings
3392- difference integers, floats, complex values
3393* product integers, floats, complex values
3394/ quotient integers, floats, complex values
Rob Pike307ec212009-03-12 15:53:56 -07003395% remainder integers
Robert Griesemerc2d55862009-02-19 16:49:10 -08003396
Russ Cox1b4e37a2012-09-12 12:05:24 -04003397&amp; bitwise AND integers
3398| bitwise OR integers
3399^ bitwise XOR integers
3400&amp;^ bit clear (AND NOT) integers
Robert Griesemerc2d55862009-02-19 16:49:10 -08003401
Robert Griesemer4ed666e2009-08-27 16:45:42 -07003402&lt;&lt; left shift integer &lt;&lt; unsigned integer
3403&gt;&gt; right shift integer &gt;&gt; unsigned integer
Robert Griesemerc2d55862009-02-19 16:49:10 -08003404</pre>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07003405
Robert Griesemer41d65ac2008-09-04 15:17:27 -07003406
Robert Griesemer3dd3ab42015-08-04 15:04:39 -07003407<h4 id="Integer_operators">Integer operators</h4>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07003408
Robert Griesemerc2d55862009-02-19 16:49:10 -08003409<p>
Robert Griesemerbb7eb402011-05-02 17:23:18 -07003410For 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
3412relationships:
Rob Pikedf3183f2009-02-26 16:37:23 -08003413</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003414
Robert Griesemerc2d55862009-02-19 16:49:10 -08003415<pre>
Robert Griesemerbb7eb402011-05-02 17:23:18 -07003416x = q*y + r and |r| &lt; |y|
Robert Griesemerc2d55862009-02-19 16:49:10 -08003417</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003418
Rob Pikedf3183f2009-02-26 16:37:23 -08003419<p>
Robert Griesemerbb7eb402011-05-02 17:23:18 -07003420with <code>x / y</code> truncated towards zero
3421(<a href="http://en.wikipedia.org/wiki/Modulo_operation">"truncated division"</a>).
Rob Pikedf3183f2009-02-26 16:37:23 -08003422</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003423
Robert Griesemerc2d55862009-02-19 16:49:10 -08003424<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>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07003431
Rob Pikedf3183f2009-02-26 16:37:23 -08003432<p>
Robert Griesemerbb7eb402011-05-02 17:23:18 -07003433As an exception to this rule, if the dividend <code>x</code> is the most
3434negative 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
3440int8 -128
3441int16 -32768
3442int32 -2147483648
3443int64 -9223372036854775808
3444</pre>
3445
3446<p>
Robert Griesemerddddd392012-10-19 10:12:09 -07003447If the divisor is a <a href="#Constants">constant</a>, it must not be zero.
3448If the divisor is zero at run time, a <a href="#Run_time_panics">run-time panic</a> occurs.
Matthew Dempsky80a87a92013-01-06 16:56:06 -08003449If the dividend is non-negative and the divisor is a constant power of 2,
Rob Pikef3a33bc2009-09-14 17:39:17 -07003450the division may be replaced by a right shift, and computing the remainder may
Russ Cox1b4e37a2012-09-12 12:05:24 -04003451be replaced by a bitwise AND operation:
Rob Pikedf3183f2009-02-26 16:37:23 -08003452</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07003453
Robert Griesemerc2d55862009-02-19 16:49:10 -08003454<pre>
Robert Griesemer4ed666e2009-08-27 16:45:42 -07003455 x x / 4 x % 4 x &gt;&gt; 2 x &amp; 3
Robert Griesemerc2d55862009-02-19 16:49:10 -08003456 11 2 3 2 3
3457-11 -2 -3 -3 1
3458</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003459
Rob Pikedf3183f2009-02-26 16:37:23 -08003460<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003461The shift operators shift the left operand by the shift count specified by the
3462right operand. They implement arithmetic shifts if the left operand is a signed
Robert Griesemer32d12782011-05-23 14:12:42 -07003463integer and logical shifts if it is an unsigned integer.
3464There is no upper limit on the shift count. Shifts behave
Rob Pikedf3183f2009-02-26 16:37:23 -08003465as if the left operand is shifted <code>n</code> times by 1 for a shift
3466count of <code>n</code>.
Robert Griesemer4ed666e2009-08-27 16:45:42 -07003467As a result, <code>x &lt;&lt; 1</code> is the same as <code>x*2</code>
3468and <code>x &gt;&gt; 1</code> is the same as
Robert Griesemere9192752009-12-01 16:15:53 -08003469<code>x/2</code> but truncated towards negative infinity.
Rob Pikedf3183f2009-02-26 16:37:23 -08003470</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003471
Rob Pikedf3183f2009-02-26 16:37:23 -08003472<p>
3473For integer operands, the unary operators
3474<code>+</code>, <code>-</code>, and <code>^</code> are defined as
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08003475follows:
Rob Pikedf3183f2009-02-26 16:37:23 -08003476</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003477
Rob Pikeff70f092009-02-20 13:36:14 -08003478<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08003479+x is 0 + x
3480-x negation is 0 - x
Russ Coxd83dc4f2009-05-29 16:04:16 -07003481^x bitwise complement is m ^ x with m = "all bits set to 1" for unsigned x
3482 and m = -1 for signed x
Robert Griesemerc2d55862009-02-19 16:49:10 -08003483</pre>
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08003484
3485
Robert Griesemer3dd3ab42015-08-04 15:04:39 -07003486<h4 id="Integer_overflow">Integer overflow</h4>
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08003487
Rob Pikedf3183f2009-02-26 16:37:23 -08003488<p>
3489For unsigned integer values, the operations <code>+</code>,
3490<code>-</code>, <code>*</code>, and <code>&lt;&lt;</code> are
3491computed modulo 2<sup><i>n</i></sup>, where <i>n</i> is the bit width of
Robert Griesemer462a17e2013-03-22 15:36:04 -07003492the <a href="#Numeric_types">unsigned integer</a>'s type.
3493Loosely speaking, these unsigned integer operations
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08003494discard high bits upon overflow, and programs may rely on ``wrap around''.
Rob Pikedf3183f2009-02-26 16:37:23 -08003495</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08003496<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08003497For signed integers, the operations <code>+</code>,
3498<code>-</code>, <code>*</code>, and <code>&lt;&lt;</code> may legally
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08003499overflow and the resulting value exists and is deterministically defined
3500by the signed integer representation, the operation, and its operands.
Rob Pikedf3183f2009-02-26 16:37:23 -08003501No exception is raised as a result of overflow. A
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08003502compiler may not optimize code under the assumption that overflow does
Rob Pikedf3183f2009-02-26 16:37:23 -08003503not occur. For instance, it may not assume that <code>x &lt; x + 1</code> is always true.
3504</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003505
3506
Robert Griesemer3dd3ab42015-08-04 15:04:39 -07003507<h4 id="Floating_point_operators">Floating-point operators</h4>
3508
3509<p>
3510For floating-point and complex numbers,
3511<code>+x</code> is the same as <code>x</code>,
3512while <code>-x</code> is the negation of <code>x</code>.
3513The result of a floating-point or complex division by zero is not specified beyond the
3514IEEE-754 standard; whether a <a href="#Run_time_panics">run-time panic</a>
3515occurs is implementation-specific.
3516</p>
3517
3518
3519<h4 id="String_concatenation">String concatenation</h4>
3520
3521<p>
3522Strings can be concatenated using the <code>+</code> operator
3523or the <code>+=</code> assignment operator:
3524</p>
3525
3526<pre>
3527s := "hi" + string(c)
3528s += " and good bye"
3529</pre>
3530
3531<p>
3532String addition creates a new string by concatenating the operands.
3533</p>
3534
3535
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003536<h3 id="Comparison_operators">Comparison operators</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003537
Rob Pike5af7de32009-02-24 15:17:59 -08003538<p>
Robert Griesemerc729ed62013-03-11 09:16:29 -07003539Comparison operators compare two operands and yield an untyped boolean value.
Rob Pike5af7de32009-02-24 15:17:59 -08003540</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07003541
Rob Pikeff70f092009-02-20 13:36:14 -08003542<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08003543== equal
3544!= not equal
Anthony Martin0122a662011-02-08 14:51:15 -08003545&lt; less
3546&lt;= less or equal
Russ Cox83f648c2011-12-12 22:21:46 -05003547&gt; greater
3548&gt;= greater or equal
Robert Griesemerc2d55862009-02-19 16:49:10 -08003549</pre>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07003550
Rob Pike5af7de32009-02-24 15:17:59 -08003551<p>
Russ Cox83f648c2011-12-12 22:21:46 -05003552In any comparison, the first operand
Robert Griesemer440cc952010-06-07 17:40:21 -07003553must be <a href="#Assignability">assignable</a>
3554to the type of the second operand, or vice versa.
Rob Pike5af7de32009-02-24 15:17:59 -08003555</p>
Robert Griesemer7bc03712010-06-07 15:49:39 -07003556<p>
Russ Cox83f648c2011-12-12 22:21:46 -05003557The equality operators <code>==</code> and <code>!=</code> apply
3558to operands that are <i>comparable</i>.
3559The ordering operators <code>&lt;</code>, <code>&lt;=</code>, <code>&gt;</code>, and <code>&gt;=</code>
3560apply to operands that are <i>ordered</i>.
3561These terms and the result of the comparisons are defined as follows:
Rob Pike5af7de32009-02-24 15:17:59 -08003562</p>
Robert Griesemer1d282a82010-06-03 16:55:50 -07003563
3564<ul>
3565 <li>
Russ Cox83f648c2011-12-12 22:21:46 -05003566 Boolean values are comparable.
3567 Two boolean values are equal if they are either both
3568 <code>true</code> or both <code>false</code>.
Robert Griesemer1d282a82010-06-03 16:55:50 -07003569 </li>
Russ Cox83f648c2011-12-12 22:21:46 -05003570
Robert Griesemer1d282a82010-06-03 16:55:50 -07003571 <li>
Russ Cox83f648c2011-12-12 22:21:46 -05003572 Integer values are comparable and ordered, in the usual way.
Robert Griesemer1d282a82010-06-03 16:55:50 -07003573 </li>
Hong Ruiqi8374e672012-04-05 22:37:07 +10003574
Robert Griesemer1d282a82010-06-03 16:55:50 -07003575 <li>
Russ Cox83f648c2011-12-12 22:21:46 -05003576 Floating point values are comparable and ordered,
3577 as defined by the IEEE-754 standard.
3578 </li>
Hong Ruiqi8374e672012-04-05 22:37:07 +10003579
Russ Cox83f648c2011-12-12 22:21:46 -05003580 <li>
3581 Complex values are comparable.
3582 Two complex values <code>u</code> and <code>v</code> are
Robert Griesemer1d282a82010-06-03 16:55:50 -07003583 equal if both <code>real(u) == real(v)</code> and
3584 <code>imag(u) == imag(v)</code>.
3585 </li>
Hong Ruiqi8374e672012-04-05 22:37:07 +10003586
Robert Griesemer1d282a82010-06-03 16:55:50 -07003587 <li>
Russ Cox83f648c2011-12-12 22:21:46 -05003588 String values are comparable and ordered, lexically byte-wise.
Robert Griesemer1d282a82010-06-03 16:55:50 -07003589 </li>
Hong Ruiqi8374e672012-04-05 22:37:07 +10003590
Robert Griesemer1d282a82010-06-03 16:55:50 -07003591 <li>
Russ Cox83f648c2011-12-12 22:21:46 -05003592 Pointer values are comparable.
Robert Griesemer1320ce02012-01-09 16:54:24 -08003593 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.
Robert Griesemer1d282a82010-06-03 16:55:50 -07003595 </li>
Hong Ruiqi8374e672012-04-05 22:37:07 +10003596
Robert Griesemer1d282a82010-06-03 16:55:50 -07003597 <li>
Russ Cox83f648c2011-12-12 22:21:46 -05003598 Channel values are comparable.
Robert Griesemer462a17e2013-03-22 15:36:04 -07003599 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>
Russ Cox83f648c2011-12-12 22:21:46 -05003601 or if both have value <code>nil</code>.
Robert Griesemer1d282a82010-06-03 16:55:50 -07003602 </li>
Russ Cox83f648c2011-12-12 22:21:46 -05003603
Robert Griesemer1d282a82010-06-03 16:55:50 -07003604 <li>
Russ Cox83f648c2011-12-12 22:21:46 -05003605 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>.
Robert Griesemer1d282a82010-06-03 16:55:50 -07003608 </li>
Hong Ruiqi8374e672012-04-05 22:37:07 +10003609
Robert Griesemer1d282a82010-06-03 16:55:50 -07003610 <li>
Russ Cox83f648c2011-12-12 22:21:46 -05003611 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>.
Robert Griesemer1d282a82010-06-03 16:55:50 -07003617 </li>
Russ Cox83f648c2011-12-12 22:21:46 -05003618
Robert Griesemer1d282a82010-06-03 16:55:50 -07003619 <li>
Robert Griesemer39084672012-02-16 14:13:17 -08003620 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.
Russ Cox83f648c2011-12-12 22:21:46 -05003623 </li>
Hong Ruiqi8374e672012-04-05 22:37:07 +10003624
Russ Cox83f648c2011-12-12 22:21:46 -05003625 <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.
Robert Griesemer1d282a82010-06-03 16:55:50 -07003628 </li>
3629</ul>
Robert Griesemera3294712009-01-05 11:17:26 -08003630
Russ Cox83f648c2011-12-12 22:21:46 -05003631<p>
3632A comparison of two interface values with identical dynamic types
3633causes a <a href="#Run_time_panics">run-time panic</a> if values
3634of that type are not comparable. This behavior applies not only to direct interface
3635value comparisons but also when comparing arrays of interface values
3636or structs with interface-valued fields.
3637</p>
3638
3639<p>
3640Slice, map, and function values are not comparable.
3641However, as a special case, a slice, map, or function value may
3642be compared to the predeclared identifier <code>nil</code>.
3643Comparison of pointer, channel, and interface values to <code>nil</code>
3644is also allowed and follows from the general rules above.
3645</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003646
Russ Cox9c08d652012-02-21 22:04:30 -05003647<pre>
Matthew Dempskyff85f862015-10-20 15:05:22 -07003648const c = 3 &lt; 4 // c is the untyped boolean constant true
Russ Cox9c08d652012-02-21 22:04:30 -05003649
Robert Griesemerc729ed62013-03-11 09:16:29 -07003650type MyBool bool
Russ Cox9c08d652012-02-21 22:04:30 -05003651var x, y int
3652var (
Matthew Dempskyff85f862015-10-20 15:05:22 -07003653 // The result of a comparison is an untyped boolean.
Robert Griesemerc729ed62013-03-11 09:16:29 -07003654 // 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
Russ Cox9c08d652012-02-21 22:04:30 -05003658)
3659</pre>
3660
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003661<h3 id="Logical_operators">Logical operators</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003662
Rob Pikedf3183f2009-02-26 16:37:23 -08003663<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07003664Logical operators apply to <a href="#Boolean_types">boolean</a> values
3665and yield a result of the same type as the operands.
Robert Griesemer41d65ac2008-09-04 15:17:27 -07003666The right operand is evaluated conditionally.
Rob Pikedf3183f2009-02-26 16:37:23 -08003667</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07003668
Rob Pikeff70f092009-02-20 13:36:14 -08003669<pre class="grammar">
Russ Cox1b4e37a2012-09-12 12:05:24 -04003670&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"
Robert Griesemerc2d55862009-02-19 16:49:10 -08003673</pre>
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -07003674
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003675
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003676<h3 id="Address_operators">Address operators</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003677
Rob Pikedf3183f2009-02-26 16:37:23 -08003678<p>
Robert Griesemer0e1d9412011-01-26 11:21:23 -08003679For 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>.
3681The operand must be <i>addressable</i>,
Russ Coxe7561de2010-05-24 14:31:43 -07003682that is, either a variable, pointer indirection, or slice indexing
Robert Griesemer0e1d9412011-01-26 11:21:23 -08003683operation; or a field selector of an addressable struct operand;
Russ Coxe7561de2010-05-24 14:31:43 -07003684or an array indexing operation of an addressable array.
Robert Griesemer0e1d9412011-01-26 11:21:23 -08003685As an exception to the addressability requirement, <code>x</code> may also be a
Robert Griesemer614b02d2013-01-02 18:11:49 -08003686(possibly parenthesized)
Robert Griesemer0e1d9412011-01-26 11:21:23 -08003687<a href="#Composite_literals">composite literal</a>.
Robin Eklindcac006a2014-08-30 10:27:01 -07003688If the evaluation of <code>x</code> would cause a <a href="#Run_time_panics">run-time panic</a>,
Rob Pikecec09542013-09-17 07:41:11 +10003689then the evaluation of <code>&amp;x</code> does too.
Robert Griesemer0e1d9412011-01-26 11:21:23 -08003690</p>
Russ Cox5ce78b72013-08-15 14:33:26 -04003691
Robert Griesemer0e1d9412011-01-26 11:21:23 -08003692<p>
3693For an operand <code>x</code> of pointer type <code>*T</code>, the pointer
Robert Griesemer6962c152014-10-16 15:08:49 -07003694indirection <code>*x</code> denotes the <a href="#Variables">variable</a> of type <code>T</code> pointed
Robert Griesemer0e1d9412011-01-26 11:21:23 -08003695to by <code>x</code>.
Rob Pike633a2ce2012-01-23 08:40:13 -08003696If <code>x</code> is <code>nil</code>, an attempt to evaluate <code>*x</code>
3697will cause a <a href="#Run_time_panics">run-time panic</a>.
Rob Pikeafee1c52009-03-20 17:41:25 -07003698</p>
3699
3700<pre>
3701&amp;x
3702&amp;a[f(2)]
Robert Griesemer614b02d2013-01-02 18:11:49 -08003703&amp;Point{2, 3}
Rob Pikeafee1c52009-03-20 17:41:25 -07003704*p
3705*pf(x)
Russ Cox5ce78b72013-08-15 14:33:26 -04003706
3707var x *int = nil
3708*x // causes a run-time panic
Rob Pikecec09542013-09-17 07:41:11 +10003709&amp;*x // causes a run-time panic
Rob Pikeafee1c52009-03-20 17:41:25 -07003710</pre>
3711
Robert Griesemerb50ed022011-02-01 12:02:49 -08003712
3713<h3 id="Receive_operator">Receive operator</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003714
Robert Griesemerc2d55862009-02-19 16:49:10 -08003715<p>
Robert Griesemerb50ed022011-02-01 12:02:49 -08003716For an operand <code>ch</code> of <a href="#Channel_types">channel type</a>,
3717the value of the receive operation <code>&lt;-ch</code> is the value received
Robert Griesemercc3f21c2012-12-03 14:23:41 -08003718from the channel <code>ch</code>. The channel direction must permit receive operations,
3719and the type of the receive operation is the element type of the channel.
3720The expression blocks until a value is available.
Robert Griesemer54731032011-05-12 09:15:59 -07003721Receiving from a <code>nil</code> channel blocks forever.
Robert Griesemerab5c7622013-05-31 11:21:37 -07003722A receive operation on a <a href="#Close">closed</a> channel can always proceed
Robert Griesemer97aa90d2014-05-07 10:40:39 -07003723immediately, yielding the element type's <a href="#The_zero_value">zero value</a>
3724after any previously sent values have been received.
Rob Pikedf3183f2009-02-26 16:37:23 -08003725</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003726
Robert Griesemerc2d55862009-02-19 16:49:10 -08003727<pre>
Robert Griesemere1e76192009-09-25 14:11:03 -07003728v1 := &lt;-ch
3729v2 = &lt;-ch
3730f(&lt;-ch)
Robert Griesemerb50ed022011-02-01 12:02:49 -08003731&lt;-strobe // wait until clock pulse and discard received value
Robert Griesemerc2d55862009-02-19 16:49:10 -08003732</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003733
Rob Pikedf3183f2009-02-26 16:37:23 -08003734<p>
Robert Griesemerc0fca132014-08-05 11:31:32 -07003735A receive expression used in an <a href="#Assignments">assignment</a> or initialization of the special form
Rob Pikedf3183f2009-02-26 16:37:23 -08003736</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003737
Robert Griesemerc2d55862009-02-19 16:49:10 -08003738<pre>
Robert Griesemere1e76192009-09-25 14:11:03 -07003739x, ok = &lt;-ch
3740x, ok := &lt;-ch
3741var x, ok = &lt;-ch
Robert Griesemerc2d55862009-02-19 16:49:10 -08003742</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003743
Rob Pikedf3183f2009-02-26 16:37:23 -08003744<p>
Robert Griesemerc0fca132014-08-05 11:31:32 -07003745yields an additional untyped boolean result reporting whether the
Robert Griesemer689931c2012-06-25 11:28:24 -07003746communication succeeded. The value of <code>ok</code> is <code>true</code>
3747if the value received was delivered by a successful send operation to the
3748channel, or <code>false</code> if it is a zero value generated because the
3749channel is closed and empty.
Rob Pikedf3183f2009-02-26 16:37:23 -08003750</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003751
Robert Griesemerb50ed022011-02-01 12:02:49 -08003752
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003753<h3 id="Conversions">Conversions</h3>
3754
3755<p>
3756Conversions are expressions of the form <code>T(x)</code>
3757where <code>T</code> is a type and <code>x</code> is an expression
3758that can be converted to type <code>T</code>.
3759</p>
3760
3761<pre class="ebnf">
Robert Griesemer60a6ae82012-09-26 10:31:57 -07003762Conversion = Type "(" Expression [ "," ] ")" .
Robert Griesemer934a5202010-05-24 14:58:26 -07003763</pre>
3764
3765<p>
Robert Griesemer3188ffc2012-10-03 13:46:37 -07003766If the type starts with the operator <code>*</code> or <code>&lt;-</code>,
Russ Cox71c941b2013-02-11 07:48:14 -05003767or if the type starts with the keyword <code>func</code>
3768and has no result list, it must be parenthesized when
3769necessary to avoid ambiguity:
Robert Griesemer934a5202010-05-24 14:58:26 -07003770</p>
3771
3772<pre>
3773*Point(p) // same as *(Point(p))
Russ Cox71c941b2013-02-11 07:48:14 -05003774(*Point)(p) // p is converted to *Point
Robert Griesemer934a5202010-05-24 14:58:26 -07003775&lt;-chan int(c) // same as &lt;-(chan int(c))
Russ Cox71c941b2013-02-11 07:48:14 -05003776(&lt;-chan int)(c) // c is converted to &lt;-chan int
Robert Griesemer3188ffc2012-10-03 13:46:37 -07003777func()(x) // function signature func() x
Russ Cox71c941b2013-02-11 07:48:14 -05003778(func())(x) // x is converted to func()
3779(func() int)(x) // x is converted to func() int
3780func() int(x) // x is converted to func() int (unambiguous)
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003781</pre>
3782
3783<p>
Robert Griesemer27693562011-06-13 16:47:33 -07003784A <a href="#Constants">constant</a> value <code>x</code> can be converted to
3785type <code>T</code> in any of these cases:
3786</p>
3787
3788<ul>
3789 <li>
3790 <code>x</code> is representable by a value of type <code>T</code>.
3791 </li>
3792 <li>
Russ Cox75761792013-02-11 07:47:41 -05003793 <code>x</code> is a floating-point constant,
3794 <code>T</code> is a floating-point type,
3795 and <code>x</code> is representable by a value
3796 of type <code>T</code> after rounding using
Robert Griesemer55ecda42015-09-17 18:10:20 -07003797 IEEE 754 round-to-even rules, but with an IEEE <code>-0.0</code>
3798 further rounded to an unsigned <code>0.0</code>.
Russ Cox75761792013-02-11 07:47:41 -05003799 The constant <code>T(x)</code> is the rounded value.
3800 </li>
3801 <li>
Robert Griesemer27693562011-06-13 16:47:33 -07003802 <code>x</code> is an integer constant and <code>T</code> is a
3803 <a href="#String_types">string type</a>.
Robert Griesemer462a17e2013-03-22 15:36:04 -07003804 The <a href="#Conversions_to_and_from_a_string_type">same rule</a>
3805 as for non-constant <code>x</code> applies in this case.
Robert Griesemer27693562011-06-13 16:47:33 -07003806 </li>
3807</ul>
3808
3809<p>
3810Converting a constant yields a typed constant as result.
3811</p>
3812
3813<pre>
3814uint(iota) // iota value of type uint
3815float32(2.718281828) // 2.718281828 of type float32
3816complex128(1) // 1.0 + 0.0i of type complex128
Russ Cox75761792013-02-11 07:47:41 -05003817float32(0.49999999) // 0.5 of type float32
Robert Griesemer55ecda42015-09-17 18:10:20 -07003818float64(-1e-1000) // 0.0 of type float64
Robert Griesemer27693562011-06-13 16:47:33 -07003819string('x') // "x" of type string
3820string(0x266c) // "♬" of type string
3821MyString("foo" + "bar") // "foobar" of type MyString
3822string([]byte{'a'}) // not a constant: []byte{'a'} is not a constant
3823(*int)(nil) // not a constant: nil is not a constant, *int is not a boolean, numeric, or string type
3824int(1.2) // illegal: 1.2 cannot be represented as an int
3825string(65.0) // illegal: 65.0 is not an integer constant
3826</pre>
3827
3828<p>
3829A non-constant value <code>x</code> can be converted to type <code>T</code>
3830in any of these cases:
Robert Griesemer63f01492010-05-28 14:17:30 -07003831</p>
Robert Griesemer7bc03712010-06-07 15:49:39 -07003832
3833<ul>
3834 <li>
Robert Griesemer440cc952010-06-07 17:40:21 -07003835 <code>x</code> is <a href="#Assignability">assignable</a>
Robert Griesemer7bc03712010-06-07 15:49:39 -07003836 to <code>T</code>.
3837 </li>
3838 <li>
3839 <code>x</code>'s type and <code>T</code> have identical
3840 <a href="#Types">underlying types</a>.
3841 </li>
3842 <li>
3843 <code>x</code>'s type and <code>T</code> are unnamed pointer types
3844 and their pointer base types have identical underlying types.
3845 </li>
3846 <li>
3847 <code>x</code>'s type and <code>T</code> are both integer or floating
3848 point types.
3849 </li>
3850 <li>
3851 <code>x</code>'s type and <code>T</code> are both complex types.
3852 </li>
3853 <li>
Robert Griesemer60a6ae82012-09-26 10:31:57 -07003854 <code>x</code> is an integer or a slice of bytes or runes
3855 and <code>T</code> is a string type.
Robert Griesemer7bc03712010-06-07 15:49:39 -07003856 </li>
3857 <li>
Robert Griesemer60a6ae82012-09-26 10:31:57 -07003858 <code>x</code> is a string and <code>T</code> is a slice of bytes or runes.
Robert Griesemer7bc03712010-06-07 15:49:39 -07003859 </li>
3860</ul>
3861
3862<p>
Robert Griesemer27693562011-06-13 16:47:33 -07003863Specific rules apply to (non-constant) conversions between numeric types or
3864to and from a string type.
Robert Griesemer7bc03712010-06-07 15:49:39 -07003865These conversions may change the representation of <code>x</code>
3866and incur a run-time cost.
3867All other conversions only change the type but not the representation
3868of <code>x</code>.
3869</p>
3870
Robert Griesemer27693562011-06-13 16:47:33 -07003871<p>
3872There is no linguistic mechanism to convert between pointers and integers.
3873The package <a href="#Package_unsafe"><code>unsafe</code></a>
3874implements this functionality under
3875restricted circumstances.
3876</p>
3877
Robert Griesemer7bc03712010-06-07 15:49:39 -07003878<h4>Conversions between numeric types</h4>
Robert Griesemer27693562011-06-13 16:47:33 -07003879
3880<p>
3881For the conversion of non-constant numeric values, the following rules apply:
3882</p>
3883
Robert Griesemer63f01492010-05-28 14:17:30 -07003884<ol>
3885<li>
Robert Griesemer7bc03712010-06-07 15:49:39 -07003886When converting between integer types, if the value is a signed integer, it is
3887sign extended to implicit infinite precision; otherwise it is zero extended.
3888It is then truncated to fit in the result type's size.
3889For example, if <code>v := uint16(0x10F0)</code>, then <code>uint32(int8(v)) == 0xFFFFFFF0</code>.
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003890The conversion always yields a valid value; there is no indication of overflow.
Robert Griesemer7bc03712010-06-07 15:49:39 -07003891</li>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003892<li>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003893When converting a floating-point number to an integer, the fraction is discarded
3894(truncation towards zero).
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003895</li>
3896<li>
Robert Griesemer7bc03712010-06-07 15:49:39 -07003897When converting an integer or floating-point number to a floating-point type,
3898or a complex number to another complex type, the result value is rounded
Rob Pike72970872010-03-04 12:35:16 -08003899to the precision specified by the destination type.
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003900For instance, the value of a variable <code>x</code> of type <code>float32</code>
3901may be stored using additional precision beyond that of an IEEE-754 32-bit number,
3902but float32(x) represents the result of rounding <code>x</code>'s value to
390332-bit precision. Similarly, <code>x + 0.1</code> may use more than 32 bits
Evan Shawcb4e9f82010-05-23 11:21:47 -07003904of precision, but <code>float32(x + 0.1)</code> does not.
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003905</li>
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003906</ol>
3907
3908<p>
Robert Griesemer27693562011-06-13 16:47:33 -07003909In all non-constant conversions involving floating-point or complex values,
Rob Pike72970872010-03-04 12:35:16 -08003910if the result type cannot represent the value the conversion
Robert Griesemer27693562011-06-13 16:47:33 -07003911succeeds but the result value is implementation-dependent.
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003912</p>
3913
Robert Griesemer27693562011-06-13 16:47:33 -07003914<h4 id="Conversions_to_and_from_a_string_type">Conversions to and from a string type</h4>
Rob Pike1811fac2010-02-17 11:26:09 +11003915
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003916<ol>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003917<li>
Rob Pike1811fac2010-02-17 11:26:09 +11003918Converting a signed or unsigned integer value to a string type yields a
Robert Griesemer7bc03712010-06-07 15:49:39 -07003919string containing the UTF-8 representation of the integer. Values outside
3920the range of valid Unicode code points are converted to <code>"\uFFFD"</code>.
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003921
3922<pre>
David Symonds72a29792011-11-29 15:47:36 -08003923string('a') // "a"
Rémy Oudompheng2b4cc6c2012-07-11 20:26:51 +02003924string(-1) // "\ufffd" == "\xef\xbf\xbd"
David Symonds72a29792011-11-29 15:47:36 -08003925string(0xf8) // "\u00f8" == "ø" == "\xc3\xb8"
Rob Pike1811fac2010-02-17 11:26:09 +11003926type MyString string
David Symonds72a29792011-11-29 15:47:36 -08003927MyString(0x65e5) // "\u65e5" == "日" == "\xe6\x97\xa5"
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003928</pre>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003929</li>
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003930
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003931<li>
Russ Cox6e3e3802011-11-22 12:30:02 -05003932Converting a slice of bytes to a string type yields
Robert Griesemerde47f682013-06-21 16:11:13 -07003933a string whose successive bytes are the elements of the slice.
Rob Pike1811fac2010-02-17 11:26:09 +11003934
3935<pre>
Robert Griesemerde47f682013-06-21 16:11:13 -07003936string([]byte{'h', 'e', 'l', 'l', '\xc3', '\xb8'}) // "hellø"
3937string([]byte{}) // ""
3938string([]byte(nil)) // ""
Russ Cox6e3e3802011-11-22 12:30:02 -05003939
3940type MyBytes []byte
3941string(MyBytes{'h', 'e', 'l', 'l', '\xc3', '\xb8'}) // "hellø"
Rob Pike1811fac2010-02-17 11:26:09 +11003942</pre>
3943</li>
3944
3945<li>
Russ Cox6e3e3802011-11-22 12:30:02 -05003946Converting a slice of runes to a string type yields
Robert Griesemerb910a272011-11-01 01:09:22 -04003947a string that is the concatenation of the individual rune values
Robert Griesemerde47f682013-06-21 16:11:13 -07003948converted to strings.
Robert Griesemerb910a272011-11-01 01:09:22 -04003949
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003950<pre>
Robert Griesemerde47f682013-06-21 16:11:13 -07003951string([]rune{0x767d, 0x9d6c, 0x7fd4}) // "\u767d\u9d6c\u7fd4" == "白鵬翔"
3952string([]rune{}) // ""
3953string([]rune(nil)) // ""
Russ Cox6e3e3802011-11-22 12:30:02 -05003954
3955type MyRunes []rune
3956string(MyRunes{0x767d, 0x9d6c, 0x7fd4}) // "\u767d\u9d6c\u7fd4" == "白鵬翔"
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003957</pre>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003958</li>
3959
3960<li>
Russ Cox6e3e3802011-11-22 12:30:02 -05003961Converting a value of a string type to a slice of bytes type
Rob Pike1811fac2010-02-17 11:26:09 +11003962yields a slice whose successive elements are the bytes of the string.
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003963
3964<pre>
David Symonds72a29792011-11-29 15:47:36 -08003965[]byte("hellø") // []byte{'h', 'e', 'l', 'l', '\xc3', '\xb8'}
Robert Griesemerde47f682013-06-21 16:11:13 -07003966[]byte("") // []byte{}
3967
David Symonds72a29792011-11-29 15:47:36 -08003968MyBytes("hellø") // []byte{'h', 'e', 'l', 'l', '\xc3', '\xb8'}
Rob Pike1811fac2010-02-17 11:26:09 +11003969</pre>
3970</li>
3971
3972<li>
Russ Cox6e3e3802011-11-22 12:30:02 -05003973Converting a value of a string type to a slice of runes type
3974yields a slice containing the individual Unicode code points of the string.
Robert Griesemerde47f682013-06-21 16:11:13 -07003975
Rob Pike1811fac2010-02-17 11:26:09 +11003976<pre>
Robert Griesemerb910a272011-11-01 01:09:22 -04003977[]rune(MyString("白鵬翔")) // []rune{0x767d, 0x9d6c, 0x7fd4}
Robert Griesemerde47f682013-06-21 16:11:13 -07003978[]rune("") // []rune{}
3979
Russ Cox6e3e3802011-11-22 12:30:02 -05003980MyRunes("白鵬翔") // []rune{0x767d, 0x9d6c, 0x7fd4}
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003981</pre>
3982</li>
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003983</ol>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003984
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003985
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003986<h3 id="Constant_expressions">Constant expressions</h3>
Robert Griesemerb90b2132008-09-19 15:49:55 -07003987
Rob Pikef27e9f02009-02-23 19:22:05 -08003988<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07003989Constant expressions may contain only <a href="#Constants">constant</a>
Robert Griesemerea7c57a2012-10-17 11:08:42 -07003990operands and are evaluated at compile time.
Rob Pikef27e9f02009-02-23 19:22:05 -08003991</p>
3992
3993<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07003994Untyped boolean, numeric, and string constants may be used as operands
3995wherever it is legal to use an operand of boolean, numeric, or string type,
Russ Coxa9336352011-12-08 21:48:19 -05003996respectively.
3997Except for shift operations, if the operands of a binary operation are
Russ Cox9c08d652012-02-21 22:04:30 -05003998different kinds of untyped constants, the operation and, for non-boolean operations, the result use
Rob Pike9dfc6f62012-08-29 14:46:57 -07003999the kind that appears later in this list: integer, rune, floating-point, complex.
Russ Coxa9336352011-12-08 21:48:19 -05004000For example, an untyped integer constant divided by an
4001untyped complex constant yields an untyped complex constant.
Robert Griesemer19b1d352009-09-24 19:36:48 -07004002</p>
Robert Griesemer997851e2009-09-25 15:36:25 -07004003
4004<p>
Robert Griesemer32d12782011-05-23 14:12:42 -07004005A constant <a href="#Comparison_operators">comparison</a> always yields
Russ Cox9c08d652012-02-21 22:04:30 -05004006an untyped boolean constant. If the left operand of a constant
Robert Griesemer32d12782011-05-23 14:12:42 -07004007<a href="#Operators">shift expression</a> is an untyped constant, the
4008result is an integer constant; otherwise it is a constant of the same
Robert Griesemer462a17e2013-03-22 15:36:04 -07004009type as the left operand, which must be of
4010<a href="#Numeric_types">integer type</a>.
Robert Griesemer32d12782011-05-23 14:12:42 -07004011Applying all other operators to untyped constants results in an untyped
Rob Pike72970872010-03-04 12:35:16 -08004012constant of the same kind (that is, a boolean, integer, floating-point,
Robert Griesemer32d12782011-05-23 14:12:42 -07004013complex, or string constant).
Robert Griesemer19b1d352009-09-24 19:36:48 -07004014</p>
4015
Robert Griesemer32d12782011-05-23 14:12:42 -07004016<pre>
Russ Coxa9336352011-12-08 21:48:19 -05004017const a = 2 + 3.0 // a == 5.0 (untyped floating-point constant)
4018const b = 15 / 4 // b == 3 (untyped integer constant)
4019const c = 15 / 4.0 // c == 3.75 (untyped floating-point constant)
Robert Griesemer2ae61d52012-11-17 11:16:07 -08004020const Θ float64 = 3/2 // Θ == 1.0 (type float64, 3/2 is integer division)
4021const Π float64 = 3/2. // Π == 1.5 (type float64, 3/2. is float division)
Russ Coxa9336352011-12-08 21:48:19 -05004022const d = 1 &lt;&lt; 3.0 // d == 8 (untyped integer constant)
4023const e = 1.0 &lt;&lt; 3 // e == 8 (untyped integer constant)
Robert Griesemer2d846f62013-05-08 10:42:08 -07004024const f = int32(1) &lt;&lt; 33 // illegal (constant 8589934592 overflows int32)
Robert Griesemer32d12782011-05-23 14:12:42 -07004025const g = float64(2) &gt;&gt; 1 // illegal (float64(2) is a typed floating-point constant)
Russ Coxef1c5352011-12-09 00:13:19 -05004026const h = "foo" &gt; "bar" // h == true (untyped boolean constant)
4027const j = true // j == true (untyped boolean constant)
Rob Pike9dfc6f62012-08-29 14:46:57 -07004028const k = 'w' + 1 // k == 'x' (untyped rune constant)
Russ Coxef1c5352011-12-09 00:13:19 -05004029const l = "hi" // l == "hi" (untyped string constant)
4030const m = string(k) // m == "x" (type string)
Robert Hencke1084ab92011-12-10 10:04:33 -08004031const Σ = 1 - 0.707i // (untyped complex constant)
Russ Coxa9336352011-12-08 21:48:19 -05004032const Δ = Σ + 2.0e-4 // (untyped complex constant)
4033const Φ = iota*1i - 1/1i // (untyped complex constant)
Robert Griesemer32d12782011-05-23 14:12:42 -07004034</pre>
4035
Robert Griesemer19b1d352009-09-24 19:36:48 -07004036<p>
Russ Coxa9336352011-12-08 21:48:19 -05004037Applying the built-in function <code>complex</code> to untyped
Rob Pike9dfc6f62012-08-29 14:46:57 -07004038integer, rune, or floating-point constants yields
Russ Coxa9336352011-12-08 21:48:19 -05004039an untyped complex constant.
Rob Pike72970872010-03-04 12:35:16 -08004040</p>
4041
4042<pre>
Robert Griesemer462860b2012-12-12 14:25:40 -08004043const ic = complex(0, c) // ic == 3.75i (untyped complex constant)
Mihai Borobocea8183ed12013-12-30 13:29:56 -08004044const iΘ = complex(0, Θ) // iΘ == 1i (type complex128)
Rob Pike72970872010-03-04 12:35:16 -08004045</pre>
4046
4047<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07004048Constant expressions are always evaluated exactly; intermediate values and the
4049constants themselves may require precision significantly larger than supported
4050by any predeclared type in the language. The following are legal declarations:
Rob Pikef27e9f02009-02-23 19:22:05 -08004051</p>
4052
4053<pre>
Robert Griesemer462860b2012-12-12 14:25:40 -08004054const Huge = 1 &lt;&lt; 100 // Huge == 1267650600228229401496703205376 (untyped integer constant)
4055const Four int8 = Huge &gt;&gt; 98 // Four == 4 (type int8)
Rob Pikef27e9f02009-02-23 19:22:05 -08004056</pre>
4057
4058<p>
Robert Griesemerddddd392012-10-19 10:12:09 -07004059The divisor of a constant division or remainder operation must not be zero:
4060</p>
4061
4062<pre>
40633.14 / 0.0 // illegal: division by zero
4064</pre>
4065
4066<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07004067The values of <i>typed</i> constants must always be accurately representable as values
4068of the constant type. The following constant expressions are illegal:
Rob Pike21d03492009-03-24 19:16:42 -07004069</p>
4070
4071<pre>
David Symonds72a29792011-11-29 15:47:36 -08004072uint(-1) // -1 cannot be represented as a uint
4073int(3.14) // 3.14 cannot be represented as an int
Robert Griesemer462860b2012-12-12 14:25:40 -08004074int64(Huge) // 1267650600228229401496703205376 cannot be represented as an int64
4075Four * 300 // operand 300 cannot be represented as an int8 (type of Four)
4076Four * 100 // product 400 cannot be represented as an int8 (type of Four)
Rob Pike21d03492009-03-24 19:16:42 -07004077</pre>
4078
4079<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07004080The mask used by the unary bitwise complement operator <code>^</code> matches
Rob Pike678625d2009-09-15 09:54:22 -07004081the rule for non-constants: the mask is all 1s for unsigned constants
Robert Griesemer19b1d352009-09-24 19:36:48 -07004082and -1 for signed and untyped constants.
Rob Pike21d03492009-03-24 19:16:42 -07004083</p>
4084
4085<pre>
David Symonds72a29792011-11-29 15:47:36 -08004086^1 // untyped integer constant, equal to -2
Robert Griesemer462860b2012-12-12 14:25:40 -08004087uint8(^1) // illegal: same as uint8(-2), -2 cannot be represented as a uint8
David Symonds72a29792011-11-29 15:47:36 -08004088^uint8(1) // typed uint8 constant, same as 0xFF ^ uint8(1) = uint8(0xFE)
4089int8(^1) // same as int8(-2)
4090^int8(1) // same as -1 ^ int8(1) = -2
Rob Pike21d03492009-03-24 19:16:42 -07004091</pre>
4092
Ian Lance Taylor9126c652012-02-13 11:25:56 -08004093<p>
4094Implementation restriction: A compiler may use rounding while
4095computing untyped floating-point or complex constant expressions; see
4096the implementation restriction in the section
4097on <a href="#Constants">constants</a>. This rounding may cause a
4098floating-point constant expression to be invalid in an integer
4099context, even if it would be integral when calculated using infinite
Robert Griesemerd8c6dac2015-06-23 14:17:59 -07004100precision, and vice versa.
Ian Lance Taylor9126c652012-02-13 11:25:56 -08004101</p>
4102
Robert Griesemer19b1d352009-09-24 19:36:48 -07004103
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004104<h3 id="Order_of_evaluation">Order of evaluation</h3>
Rob Pikec956e902009-04-14 20:10:49 -07004105
4106<p>
Robert Griesemera4366982014-05-20 13:51:39 -07004107At package level, <a href="#Package_initialization">initialization dependencies</a>
Robert Griesemerdbe5f882014-05-07 08:50:52 -07004108determine the evaluation order of individual initialization expressions in
4109<a href="#Variable_declarations">variable declarations</a>.
4110Otherwise, when evaluating the <a href="#Operands">operands</a> of an
4111expression, assignment, or
Robert Griesemerf05a91e2012-08-09 11:50:16 -07004112<a href="#Return_statements">return statement</a>,
4113all function calls, method calls, and
Rob Pikec956e902009-04-14 20:10:49 -07004114communication operations are evaluated in lexical left-to-right
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07004115order.
4116</p>
4117
4118<p>
Robert Griesemerdbe5f882014-05-07 08:50:52 -07004119For example, in the (function-local) assignment
Rob Pikec956e902009-04-14 20:10:49 -07004120</p>
4121<pre>
David Symonds72a29792011-11-29 15:47:36 -08004122y[f()], ok = g(h(), i()+x[j()], &lt;-c), k()
Rob Pikec956e902009-04-14 20:10:49 -07004123</pre>
4124<p>
Robert Griesemer4f185492009-05-01 17:00:16 -07004125the function calls and communication happen in the order
4126<code>f()</code>, <code>h()</code>, <code>i()</code>, <code>j()</code>,
Robert Griesemere1e76192009-09-25 14:11:03 -07004127<code>&lt;-c</code>, <code>g()</code>, and <code>k()</code>.
Robert Griesemer4f185492009-05-01 17:00:16 -07004128However, the order of those events compared to the evaluation
4129and indexing of <code>x</code> and the evaluation
4130of <code>y</code> is not specified.
Rob Pikec956e902009-04-14 20:10:49 -07004131</p>
4132
Robert Griesemerf05a91e2012-08-09 11:50:16 -07004133<pre>
4134a := 1
Shenghou Mabdac9892013-06-11 02:52:07 +08004135f := func() int { a++; return a }
Robert Griesemerdbe5f882014-05-07 08:50:52 -07004136x := []int{a, f()} // x may be [1, 2] or [2, 2]: evaluation order between a and f() is not specified
4137m := 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
4138n := map[int]int{a: f()} // n may be {2: 3} or {3: 3}: evaluation order between the key and the value is not specified
Robert Griesemerf05a91e2012-08-09 11:50:16 -07004139</pre>
4140
Rob Pike4fe41922009-11-07 22:00:59 -08004141<p>
Robert Griesemerdbe5f882014-05-07 08:50:52 -07004142At package level, initialization dependencies override the left-to-right rule
4143for individual initialization expressions, but not for operands within each
Robin Eklindcac006a2014-08-30 10:27:01 -07004144expression:
Robert Griesemerdbe5f882014-05-07 08:50:52 -07004145</p>
4146
4147<pre>
4148var a, b, c = f() + v(), g(), sqr(u()) + v()
4149
4150func f() int { return c }
4151func g() int { return a }
4152func sqr(x int) int { return x*x }
4153
4154// functions u and v are independent of all other variables and functions
4155</pre>
4156
4157<p>
4158The function calls happen in the order
4159<code>u()</code>, <code>sqr()</code>, <code>v()</code>,
4160<code>f()</code>, <code>v()</code>, and <code>g()</code>.
4161</p>
4162
4163<p>
Rob Pike4fe41922009-11-07 22:00:59 -08004164Floating-point operations within a single expression are evaluated according to
4165the associativity of the operators. Explicit parentheses affect the evaluation
4166by overriding the default associativity.
4167In the expression <code>x + (y + z)</code> the addition <code>y + z</code>
4168is performed before adding <code>x</code>.
4169</p>
4170
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004171<h2 id="Statements">Statements</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004172
Rob Pike96750f12009-02-27 16:47:48 -08004173<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004174Statements control execution.
Rob Pike96750f12009-02-27 16:47:48 -08004175</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004176
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004177<pre class="ebnf">
Robert Griesemerdea43942009-03-16 17:36:52 -07004178Statement =
Rob Pikef3a33bc2009-09-14 17:39:17 -07004179 Declaration | LabeledStmt | SimpleStmt |
4180 GoStmt | ReturnStmt | BreakStmt | ContinueStmt | GotoStmt |
Rob Pike11417162009-03-24 17:45:53 -07004181 FallthroughStmt | Block | IfStmt | SwitchStmt | SelectStmt | ForStmt |
4182 DeferStmt .
Robert Griesemer7abfcd92008-10-07 17:14:30 -07004183
Robert Griesemerb50ed022011-02-01 12:02:49 -08004184SimpleStmt = EmptyStmt | ExpressionStmt | SendStmt | IncDecStmt | Assignment | ShortVarDecl .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004185</pre>
Robert Griesemer7271e042008-10-09 20:05:24 -07004186
Robert Griesemer9905cec2013-03-04 13:55:35 -08004187<h3 id="Terminating_statements">Terminating statements</h3>
4188
4189<p>
4190A terminating statement is one of the following:
4191</p>
4192
4193<ol>
4194<li>
4195 A <a href="#Return_statements">"return"</a> or
4196 <a href="#Goto_statements">"goto"</a> statement.
4197 <!-- ul below only for regular layout -->
4198 <ul> </ul>
4199</li>
4200
4201<li>
4202 A call to the built-in function
4203 <a href="#Handling_panics"><code>panic</code></a>.
4204 <!-- ul below only for regular layout -->
4205 <ul> </ul>
4206</li>
4207
4208<li>
4209 A <a href="#Blocks">block</a> in which the statement list ends in a terminating statement.
4210 <!-- ul below only for regular layout -->
4211 <ul> </ul>
4212</li>
4213
4214<li>
4215 An <a href="#If_statements">"if" statement</a> in which:
4216 <ul>
4217 <li>the "else" branch is present, and</li>
4218 <li>both branches are terminating statements.</li>
4219 </ul>
4220</li>
4221
4222<li>
4223 A <a href="#For_statements">"for" statement</a> in which:
4224 <ul>
4225 <li>there are no "break" statements referring to the "for" statement, and</li>
4226 <li>the loop condition is absent.</li>
4227 </ul>
4228</li>
4229
4230<li>
4231 A <a href="#Switch_statements">"switch" statement</a> in which:
4232 <ul>
4233 <li>there are no "break" statements referring to the "switch" statement,</li>
4234 <li>there is a default case, and</li>
4235 <li>the statement lists in each case, including the default, end in a terminating
4236 statement, or a possibly labeled <a href="#Fallthrough_statements">"fallthrough"
4237 statement</a>.</li>
4238 </ul>
4239</li>
4240
4241<li>
4242 A <a href="#Select_statements">"select" statement</a> in which:
4243 <ul>
4244 <li>there are no "break" statements referring to the "select" statement, and</li>
4245 <li>the statement lists in each case, including the default if present,
4246 end in a terminating statement.</li>
4247 </ul>
4248</li>
4249
4250<li>
4251 A <a href="#Labeled_statements">labeled statement</a> labeling
4252 a terminating statement.
4253</li>
4254</ol>
4255
4256<p>
4257All other statements are not terminating.
4258</p>
4259
4260<p>
4261A <a href="#Blocks">statement list</a> ends in a terminating statement if the list
4262is not empty and its final statement is terminating.
4263</p>
4264
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004265
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004266<h3 id="Empty_statements">Empty statements</h3>
Robert Griesemeraed247f2008-10-08 17:05:30 -07004267
Rob Pike96750f12009-02-27 16:47:48 -08004268<p>
Robert Griesemeraed247f2008-10-08 17:05:30 -07004269The empty statement does nothing.
Rob Pike96750f12009-02-27 16:47:48 -08004270</p>
Robert Griesemeraed247f2008-10-08 17:05:30 -07004271
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004272<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07004273EmptyStmt = .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004274</pre>
Robert Griesemeraed247f2008-10-08 17:05:30 -07004275
4276
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004277<h3 id="Labeled_statements">Labeled statements</h3>
Robert Griesemerdea43942009-03-16 17:36:52 -07004278
4279<p>
4280A labeled statement may be the target of a <code>goto</code>,
4281<code>break</code> or <code>continue</code> statement.
4282</p>
4283
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004284<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07004285LabeledStmt = Label ":" Statement .
Robert Griesemerdea43942009-03-16 17:36:52 -07004286Label = identifier .
4287</pre>
4288
4289<pre>
Robert Griesemer7fc4e372011-02-01 12:51:10 -08004290Error: log.Panic("error encountered")
Robert Griesemerdea43942009-03-16 17:36:52 -07004291</pre>
4292
4293
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004294<h3 id="Expression_statements">Expression statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004295
Rob Pike96750f12009-02-27 16:47:48 -08004296<p>
Robert Griesemer8c058b32012-09-18 11:25:53 -07004297With the exception of specific built-in functions,
4298function and method <a href="#Calls">calls</a> and
4299<a href="#Receive_operator">receive operations</a>
Robert Griesemer6af887e2011-05-02 09:16:31 -07004300can appear in statement context. Such statements may be parenthesized.
Rob Pike96750f12009-02-27 16:47:48 -08004301</p>
4302
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004303<pre class="ebnf">
Robert Griesemerc1347182011-04-29 12:20:31 -07004304ExpressionStmt = Expression .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004305</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004306
Robert Griesemer8c058b32012-09-18 11:25:53 -07004307<p>
4308The following built-in functions are not permitted in statement context:
4309</p>
4310
4311<pre>
4312append cap complex imag len make new real
4313unsafe.Alignof unsafe.Offsetof unsafe.Sizeof
4314</pre>
4315
Robert Griesemerc2d55862009-02-19 16:49:10 -08004316<pre>
Robert Griesemerb50ed022011-02-01 12:02:49 -08004317h(x+y)
4318f.Close()
Robert Griesemere1e76192009-09-25 14:11:03 -07004319&lt;-ch
Robert Griesemer6af887e2011-05-02 09:16:31 -07004320(&lt;-ch)
Robert Griesemer8c058b32012-09-18 11:25:53 -07004321len("foo") // illegal if len is the built-in function
Robert Griesemerc2d55862009-02-19 16:49:10 -08004322</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004323
4324
Robert Griesemerb50ed022011-02-01 12:02:49 -08004325<h3 id="Send_statements">Send statements</h3>
4326
4327<p>
4328A send statement sends a value on a channel.
Robert Griesemercc3f21c2012-12-03 14:23:41 -08004329The channel expression must be of <a href="#Channel_types">channel type</a>,
4330the channel direction must permit send operations,
4331and the type of the value to be sent must be <a href="#Assignability">assignable</a>
Robert Griesemerb50ed022011-02-01 12:02:49 -08004332to the channel's element type.
4333</p>
4334
4335<pre class="ebnf">
4336SendStmt = Channel "&lt;-" Expression .
4337Channel = Expression .
4338</pre>
4339
4340<p>
4341Both the channel and the value expression are evaluated before communication
Russ Coxe7a138b2012-02-08 15:24:48 -05004342begins. Communication blocks until the send can proceed.
Robert Griesemerb50ed022011-02-01 12:02:49 -08004343A send on an unbuffered channel can proceed if a receiver is ready.
4344A send on a buffered channel can proceed if there is room in the buffer.
Russ Coxe7a138b2012-02-08 15:24:48 -05004345A send on a closed channel proceeds by causing a <a href="#Run_time_panics">run-time panic</a>.
Robert Griesemer54731032011-05-12 09:15:59 -07004346A send on a <code>nil</code> channel blocks forever.
Robert Griesemerb50ed022011-02-01 12:02:49 -08004347</p>
4348
4349<pre>
Robert Griesemer97aa90d2014-05-07 10:40:39 -07004350ch &lt;- 3 // send value 3 to channel ch
Robert Griesemerb50ed022011-02-01 12:02:49 -08004351</pre>
4352
Robert Griesemerb50ed022011-02-01 12:02:49 -08004353
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004354<h3 id="IncDec_statements">IncDec statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004355
Rob Pike96750f12009-02-27 16:47:48 -08004356<p>
Robert Griesemer52a54802008-09-30 13:02:50 -07004357The "++" and "--" statements increment or decrement their operands
Robert Griesemer19b1d352009-09-24 19:36:48 -07004358by the untyped <a href="#Constants">constant</a> <code>1</code>.
Robert Griesemer5474e162010-09-28 14:44:19 -07004359As with an assignment, the operand must be <a href="#Address_operators">addressable</a>
4360or a map index expression.
Rob Pike96750f12009-02-27 16:47:48 -08004361</p>
Robert Griesemer52a54802008-09-30 13:02:50 -07004362
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004363<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07004364IncDecStmt = Expression ( "++" | "--" ) .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004365</pre>
Russ Cox5958dd62009-03-04 17:19:21 -08004366
Rob Pike96750f12009-02-27 16:47:48 -08004367<p>
Robert Griesemer506c0082009-09-08 15:41:14 -07004368The following <a href="#Assignments">assignment statements</a> are semantically
Robert Griesemer52a54802008-09-30 13:02:50 -07004369equivalent:
Rob Pike96750f12009-02-27 16:47:48 -08004370</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004371
Rob Pikeff70f092009-02-20 13:36:14 -08004372<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08004373IncDec statement Assignment
4374x++ x += 1
4375x-- x -= 1
4376</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004377
Robert Griesemer5474e162010-09-28 14:44:19 -07004378
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004379<h3 id="Assignments">Assignments</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004380
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004381<pre class="ebnf">
Robert Griesemerc2d55862009-02-19 16:49:10 -08004382Assignment = ExpressionList assign_op ExpressionList .
Rob Pikeff70f092009-02-20 13:36:14 -08004383
Robert Griesemerc2d55862009-02-19 16:49:10 -08004384assign_op = [ add_op | mul_op ] "=" .
4385</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004386
Rob Pike96750f12009-02-27 16:47:48 -08004387<p>
Rob Pike678625d2009-09-15 09:54:22 -07004388Each left-hand side operand must be <a href="#Address_operators">addressable</a>,
Robert Griesemerf57bf7a2013-11-12 21:06:54 -05004389a map index expression, or (for <code>=</code> assignments only) the
4390<a href="#Blank_identifier">blank identifier</a>.
Robert Griesemer6af887e2011-05-02 09:16:31 -07004391Operands may be parenthesized.
Rob Pike96750f12009-02-27 16:47:48 -08004392</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004393
Robert Griesemerc2d55862009-02-19 16:49:10 -08004394<pre>
4395x = 1
4396*p = f()
4397a[i] = 23
Robert Griesemer6af887e2011-05-02 09:16:31 -07004398(k) = &lt;-ch // same as: k = &lt;-ch
Robert Griesemerc2d55862009-02-19 16:49:10 -08004399</pre>
Rob Pike96750f12009-02-27 16:47:48 -08004400
4401<p>
4402An <i>assignment operation</i> <code>x</code> <i>op</i><code>=</code>
Robert Griesemer47094dc2014-09-30 11:44:29 -07004403<code>y</code> where <i>op</i> is a binary arithmetic operation is equivalent
Rob Pike96750f12009-02-27 16:47:48 -08004404to <code>x</code> <code>=</code> <code>x</code> <i>op</i>
Robert Griesemer637d5982015-06-11 13:21:46 -07004405<code>(y)</code> but evaluates <code>x</code>
Rob Pike96750f12009-02-27 16:47:48 -08004406only once. The <i>op</i><code>=</code> construct is a single token.
Rob Pike678625d2009-09-15 09:54:22 -07004407In assignment operations, both the left- and right-hand expression lists
Robert Griesemerf57bf7a2013-11-12 21:06:54 -05004408must contain exactly one single-valued expression, and the left-hand
4409expression must not be the blank identifier.
Rob Pike96750f12009-02-27 16:47:48 -08004410</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004411
Robert Griesemerc2d55862009-02-19 16:49:10 -08004412<pre>
Robert Griesemer4ed666e2009-08-27 16:45:42 -07004413a[i] &lt;&lt;= 2
Rob Pike678625d2009-09-15 09:54:22 -07004414i &amp;^= 1&lt;&lt;n
Robert Griesemerc2d55862009-02-19 16:49:10 -08004415</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004416
Rob Pike96750f12009-02-27 16:47:48 -08004417<p>
4418A tuple assignment assigns the individual elements of a multi-valued
4419operation to a list of variables. There are two forms. In the
4420first, the right hand operand is a single multi-valued expression
Robert Griesemer47094dc2014-09-30 11:44:29 -07004421such as a function call, a <a href="#Channel_types">channel</a> or
4422<a href="#Map_types">map</a> operation, or a <a href="#Type_assertions">type assertion</a>.
Russ Cox5958dd62009-03-04 17:19:21 -08004423The number of operands on the left
Robert Griesemer4e56b332009-09-10 10:14:00 -07004424hand side must match the number of values. For instance, if
Rob Pike96750f12009-02-27 16:47:48 -08004425<code>f</code> is a function returning two values,
4426</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004427
Robert Griesemerc2d55862009-02-19 16:49:10 -08004428<pre>
4429x, y = f()
4430</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004431
Rob Pike96750f12009-02-27 16:47:48 -08004432<p>
4433assigns the first value to <code>x</code> and the second to <code>y</code>.
Rob Pike96750f12009-02-27 16:47:48 -08004434In the second form, the number of operands on the left must equal the number
Robert Griesemeref45e642009-08-21 11:25:00 -07004435of expressions on the right, each of which must be single-valued, and the
4436<i>n</i>th expression on the right is assigned to the <i>n</i>th
Robert Griesemerf57bf7a2013-11-12 21:06:54 -05004437operand on the left:
Robert Griesemer2dde4f52012-05-24 10:59:48 -07004438</p>
4439
Robert Griesemerf57bf7a2013-11-12 21:06:54 -05004440<pre>
4441one, two, three = '一', '二', '三'
4442</pre>
4443
4444<p>
4445The <a href="#Blank_identifier">blank identifier</a> provides a way to
4446ignore right-hand side values in an assignment:
4447</p>
4448
4449<pre>
4450_ = x // evaluate x but ignore it
4451x, _ = f() // evaluate f() but ignore second result value
4452</pre>
4453
Robert Griesemer2dde4f52012-05-24 10:59:48 -07004454<p>
4455The assignment proceeds in two phases.
Robert Griesemer9c9e8112012-12-10 11:55:57 -08004456First, the operands of <a href="#Index_expressions">index expressions</a>
Russ Coxfa538112011-10-13 15:44:17 -04004457and <a href="#Address_operators">pointer indirections</a>
4458(including implicit pointer indirections in <a href="#Selectors">selectors</a>)
4459on the left and the expressions on the right are all
4460<a href="#Order_of_evaluation">evaluated in the usual order</a>.
4461Second, the assignments are carried out in left-to-right order.
Rob Pike96750f12009-02-27 16:47:48 -08004462</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004463
Robert Griesemerc2d55862009-02-19 16:49:10 -08004464<pre>
Rob Pike96750f12009-02-27 16:47:48 -08004465a, b = b, a // exchange a and b
Russ Coxfa538112011-10-13 15:44:17 -04004466
4467x := []int{1, 2, 3}
4468i := 0
David Symonds72a29792011-11-29 15:47:36 -08004469i, x[i] = 1, 2 // set i = 1, x[0] = 2
Russ Coxfa538112011-10-13 15:44:17 -04004470
4471i = 0
4472x[i], i = 2, 1 // set x[0] = 2, i = 1
4473
Robert Griesemer2dde4f52012-05-24 10:59:48 -07004474x[0], x[0] = 1, 2 // set x[0] = 1, then x[0] = 2 (so x[0] == 2 at end)
Russ Coxfa538112011-10-13 15:44:17 -04004475
David Symonds72a29792011-11-29 15:47:36 -08004476x[1], x[3] = 4, 5 // set x[1] = 4, then panic setting x[3] = 5.
Russ Coxfa538112011-10-13 15:44:17 -04004477
4478type Point struct { x, y int }
4479var p *Point
4480x[2], p.x = 6, 7 // set x[2] = 6, then panic setting p.x = 7
Robert Griesemer2dde4f52012-05-24 10:59:48 -07004481
4482i = 2
4483x = []int{3, 5, 7}
4484for i, x[i] = range x { // set i, x[2] = 0, x[0]
4485 break
4486}
4487// after this loop, i == 0 and x == []int{3, 5, 3}
Robert Griesemerc2d55862009-02-19 16:49:10 -08004488</pre>
Rob Pike96750f12009-02-27 16:47:48 -08004489
4490<p>
Robert Griesemerf57bf7a2013-11-12 21:06:54 -05004491In assignments, each value must be <a href="#Assignability">assignable</a>
4492to the type of the operand to which it is assigned, with the following special cases:
Rob Pike96750f12009-02-27 16:47:48 -08004493</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004494
Robert Griesemerf57bf7a2013-11-12 21:06:54 -05004495<ol>
Robert Griesemer47094dc2014-09-30 11:44:29 -07004496<li>
4497 Any typed value may be assigned to the blank identifier.
4498</li>
Robert Griesemerf57bf7a2013-11-12 21:06:54 -05004499
Robert Griesemer47094dc2014-09-30 11:44:29 -07004500<li>
4501 If an untyped constant
4502 is assigned to a variable of interface type or the blank identifier,
4503 the constant is first <a href="#Conversions">converted</a> to its
4504 <a href="#Constants">default type</a>.
4505</li>
4506
4507<li>
4508 If an untyped boolean value is assigned to a variable of interface type or
4509 the blank identifier, it is first converted to type <code>bool</code>.
4510</li>
Robert Griesemerf57bf7a2013-11-12 21:06:54 -05004511</ol>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004512
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004513<h3 id="If_statements">If statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004514
Rob Pike96750f12009-02-27 16:47:48 -08004515<p>
4516"If" statements specify the conditional execution of two branches
4517according to the value of a boolean expression. If the expression
4518evaluates to true, the "if" branch is executed, otherwise, if
Robert Griesemera1368a62011-02-22 15:31:57 -08004519present, the "else" branch is executed.
Rob Pike96750f12009-02-27 16:47:48 -08004520</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004521
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004522<pre class="ebnf">
Russ Cox58e19aa2011-07-14 17:15:52 -04004523IfStmt = "if" [ SimpleStmt ";" ] Expression Block [ "else" ( IfStmt | Block ) ] .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004524</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004525
Robert Griesemerc2d55862009-02-19 16:49:10 -08004526<pre>
Robert Griesemera1368a62011-02-22 15:31:57 -08004527if x &gt; max {
4528 x = max
Robert Griesemerc2d55862009-02-19 16:49:10 -08004529}
4530</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004531
Russ Cox5958dd62009-03-04 17:19:21 -08004532<p>
Russ Cox16b95ba2009-08-20 10:22:52 -07004533The expression may be preceded by a simple statement, which
4534executes before the expression is evaluated.
Russ Cox5958dd62009-03-04 17:19:21 -08004535</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004536
Robert Griesemerc2d55862009-02-19 16:49:10 -08004537<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08004538if x := f(); x &lt; y {
4539 return x
Robert Griesemera1368a62011-02-22 15:31:57 -08004540} else if x &gt; z {
Robert Griesemer130ac742009-12-10 16:43:01 -08004541 return z
Robert Griesemerc2d55862009-02-19 16:49:10 -08004542} else {
Robert Griesemer130ac742009-12-10 16:43:01 -08004543 return y
Robert Griesemerc2d55862009-02-19 16:49:10 -08004544}
4545</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004546
4547
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004548<h3 id="Switch_statements">Switch statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004549
Rob Pike96750f12009-02-27 16:47:48 -08004550<p>
4551"Switch" statements provide multi-way execution.
Rob Pike5a578492009-03-17 16:48:35 -07004552An expression or type specifier is compared to the "cases"
4553inside the "switch" to determine which branch
4554to execute.
Rob Pikeafee1c52009-03-20 17:41:25 -07004555</p>
Robert Griesemer091cba82009-03-19 08:39:40 -07004556
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004557<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07004558SwitchStmt = ExprSwitchStmt | TypeSwitchStmt .
Robert Griesemer091cba82009-03-19 08:39:40 -07004559</pre>
4560
Rob Pikeafee1c52009-03-20 17:41:25 -07004561<p>
Rob Pike5a578492009-03-17 16:48:35 -07004562There are two forms: expression switches and type switches.
Rob Pike5a578492009-03-17 16:48:35 -07004563In an expression switch, the cases contain expressions that are compared
4564against the value of the switch expression.
4565In a type switch, the cases contain types that are compared against the
4566type of a specially annotated switch expression.
Robert Griesemer2d9378c2015-07-27 13:30:16 -07004567The switch expression is evaluated exactly once in a switch statement.
Rob Pike96750f12009-02-27 16:47:48 -08004568</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004569
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004570<h4 id="Expression_switches">Expression switches</h4>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004571
Rob Pike96750f12009-02-27 16:47:48 -08004572<p>
Rob Pike5a578492009-03-17 16:48:35 -07004573In an expression switch,
4574the switch expression is evaluated and
4575the case expressions, which need not be constants,
Robert Griesemer4f185492009-05-01 17:00:16 -07004576are evaluated left-to-right and top-to-bottom; the first one that equals the
Rob Pike5a578492009-03-17 16:48:35 -07004577switch expression
Rob Pike96750f12009-02-27 16:47:48 -08004578triggers execution of the statements of the associated case;
4579the other cases are skipped.
Rob Pike5a578492009-03-17 16:48:35 -07004580If no case matches and there is a "default" case,
4581its statements are executed.
Rob Pike96750f12009-02-27 16:47:48 -08004582There can be at most one default case and it may appear anywhere in the
4583"switch" statement.
Robert Griesemerab266232014-02-25 09:13:37 -08004584A missing switch expression is equivalent to the boolean value
4585<code>true</code>.
Rob Pike96750f12009-02-27 16:47:48 -08004586</p>
Rob Pike70c1a102009-03-18 19:23:59 -07004587
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004588<pre class="ebnf">
Rob Pikef3a33bc2009-09-14 17:39:17 -07004589ExprSwitchStmt = "switch" [ SimpleStmt ";" ] [ Expression ] "{" { ExprCaseClause } "}" .
Robert Griesemer9905cec2013-03-04 13:55:35 -08004590ExprCaseClause = ExprSwitchCase ":" StatementList .
Robert Griesemer091cba82009-03-19 08:39:40 -07004591ExprSwitchCase = "case" ExpressionList | "default" .
Rob Pike70c1a102009-03-18 19:23:59 -07004592</pre>
4593
Rob Pike96750f12009-02-27 16:47:48 -08004594<p>
Robert Griesemer2d9378c2015-07-27 13:30:16 -07004595If the switch expression evaluates to an untyped constant, it is first
4596<a href="#Conversions">converted</a> to its <a href="#Constants">default type</a>;
4597if it is an untyped boolean value, it is first converted to type <code>bool</code>.
4598The predeclared untyped value <code>nil</code> cannot be used as a switch expression.
4599</p>
4600
4601<p>
4602If a case expression is untyped, it is first <a href="#Conversions">converted</a>
4603to the type of the switch expression.
4604For each (possibly converted) case expression <code>x</code> and the value <code>t</code>
4605of the switch expression, <code>x == t</code> must be a valid <a href="#Comparison_operators">comparison</a>.
4606</p>
4607
4608<p>
4609In other words, the switch expression is treated as if it were used to declare and
4610initialize a temporary variable <code>t</code> without explicit type; it is that
4611value of <code>t</code> against which each case expression <code>x</code> is tested
4612for equality.
4613</p>
4614
4615<p>
Robert Griesemer67a6b4f2013-03-01 16:45:14 -08004616In a case or default clause, the last non-empty statement
4617may be a (possibly <a href="#Labeled_statements">labeled</a>)
4618<a href="#Fallthrough_statements">"fallthrough" statement</a> to
Rob Pike96750f12009-02-27 16:47:48 -08004619indicate that control should flow from the end of this clause to
Robert Griesemeraed247f2008-10-08 17:05:30 -07004620the first statement of the next clause.
Rob Pike96750f12009-02-27 16:47:48 -08004621Otherwise control flows to the end of the "switch" statement.
Robert Griesemer67a6b4f2013-03-01 16:45:14 -08004622A "fallthrough" statement may appear as the last statement of all
4623but the last clause of an expression switch.
Rob Pike96750f12009-02-27 16:47:48 -08004624</p>
Robert Griesemer0a162a12009-08-19 16:44:04 -07004625
Robert Griesemerc2d55862009-02-19 16:49:10 -08004626<p>
Robert Griesemer2d9378c2015-07-27 13:30:16 -07004627The switch expression may be preceded by a simple statement, which
Russ Cox16b95ba2009-08-20 10:22:52 -07004628executes before the expression is evaluated.
Rob Pike96750f12009-02-27 16:47:48 -08004629</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004630
Robert Griesemerc2d55862009-02-19 16:49:10 -08004631<pre>
4632switch tag {
Rob Pike65ec16b2009-05-29 15:46:03 -07004633default: s3()
4634case 0, 1, 2, 3: s1()
4635case 4, 5, 6, 7: s2()
Robert Griesemerc2d55862009-02-19 16:49:10 -08004636}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004637
Andrew Gerrand10b77f72010-03-29 13:12:08 +11004638switch x := f(); { // missing switch expression means "true"
Rob Pike65ec16b2009-05-29 15:46:03 -07004639case x &lt; 0: return -x
4640default: return x
Robert Griesemerc2d55862009-02-19 16:49:10 -08004641}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004642
Robert Griesemere9192752009-12-01 16:15:53 -08004643switch {
Robert Griesemer130ac742009-12-10 16:43:01 -08004644case x &lt; y: f1()
4645case x &lt; z: f2()
4646case x == 4: f3()
Robert Griesemerc2d55862009-02-19 16:49:10 -08004647}
4648</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004649
Robert Griesemer2d9378c2015-07-27 13:30:16 -07004650<p>
4651Implementation restriction: A compiler may disallow multiple case
4652expressions evaluating to the same constant.
4653For instance, the current compilers disallow duplicate integer,
4654floating point, or string constants in case expressions.
4655</p>
4656
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004657<h4 id="Type_switches">Type switches</h4>
Rob Pike70c1a102009-03-18 19:23:59 -07004658
Rob Pike5a578492009-03-17 16:48:35 -07004659<p>
Rob Pike70c1a102009-03-18 19:23:59 -07004660A type switch compares types rather than values. It is otherwise similar
Robert Griesemer1f95f0d2009-08-27 16:44:17 -07004661to an expression switch. It is marked by a special switch expression that
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07004662has the form of a <a href="#Type_assertions">type assertion</a>
Robert Griesemer48567312012-12-06 09:17:20 -08004663using the reserved word <code>type</code> rather than an actual type:
4664</p>
4665
4666<pre>
4667switch x.(type) {
4668// cases
4669}
4670</pre>
4671
4672<p>
4673Cases then match actual types <code>T</code> against the dynamic type of the
4674expression <code>x</code>. As with type assertions, <code>x</code> must be of
4675<a href="#Interface_types">interface type</a>, and each non-interface type
4676<code>T</code> listed in a case must implement the type of <code>x</code>.
Rob Pike5a578492009-03-17 16:48:35 -07004677</p>
4678
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004679<pre class="ebnf">
Rob Pikef3a33bc2009-09-14 17:39:17 -07004680TypeSwitchStmt = "switch" [ SimpleStmt ";" ] TypeSwitchGuard "{" { TypeCaseClause } "}" .
Russ Coxc1045db2009-12-23 13:48:44 -08004681TypeSwitchGuard = [ identifier ":=" ] PrimaryExpr "." "(" "type" ")" .
Robert Griesemer9905cec2013-03-04 13:55:35 -08004682TypeCaseClause = TypeSwitchCase ":" StatementList .
Rob Pike678625d2009-09-15 09:54:22 -07004683TypeSwitchCase = "case" TypeList | "default" .
4684TypeList = Type { "," Type } .
Rob Pike5a578492009-03-17 16:48:35 -07004685</pre>
4686
4687<p>
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07004688The TypeSwitchGuard may include a
4689<a href="#Short_variable_declarations">short variable declaration</a>.
Rob Pike818e3cd2012-02-22 16:25:55 +11004690When that form is used, the variable is declared at the beginning of
4691the <a href="#Blocks">implicit block</a> in each clause.
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07004692In clauses with a case listing exactly one type, the variable
4693has that type; otherwise, the variable has the type of the expression
4694in the TypeSwitchGuard.
Rob Pike94b67eb2009-03-24 17:40:47 -07004695</p>
4696
4697<p>
Robert Griesemer462a17e2013-03-22 15:36:04 -07004698The type in a case may be <a href="#Predeclared_identifiers"><code>nil</code></a>;
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07004699that case is used when the expression in the TypeSwitchGuard
Robert Griesemer1f95f0d2009-08-27 16:44:17 -07004700is a <code>nil</code> interface value.
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07004701</p>
4702
4703<p>
Robert Griesemere9192752009-12-01 16:15:53 -08004704Given an expression <code>x</code> of type <code>interface{}</code>,
Rob Pike70c1a102009-03-18 19:23:59 -07004705the following type switch:
Rob Pike5a578492009-03-17 16:48:35 -07004706</p>
4707
4708<pre>
Robert Griesemere9192752009-12-01 16:15:53 -08004709switch i := x.(type) {
Rob Pike94b67eb2009-03-24 17:40:47 -07004710case nil:
Robert Griesemer48567312012-12-06 09:17:20 -08004711 printString("x is nil") // type of i is type of x (interface{})
Rob Pike5a578492009-03-17 16:48:35 -07004712case int:
Robert Griesemer48567312012-12-06 09:17:20 -08004713 printInt(i) // type of i is int
Robert Griesemerb94c0d22011-01-19 23:07:21 -05004714case float64:
Robert Griesemer48567312012-12-06 09:17:20 -08004715 printFloat64(i) // type of i is float64
Robert Griesemerb94c0d22011-01-19 23:07:21 -05004716case func(int) float64:
Robert Griesemer48567312012-12-06 09:17:20 -08004717 printFunction(i) // type of i is func(int) float64
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07004718case bool, string:
Robert Griesemer48567312012-12-06 09:17:20 -08004719 printString("type is bool or string") // type of i is type of x (interface{})
Rob Pike5a578492009-03-17 16:48:35 -07004720default:
Robert Griesemer48567312012-12-06 09:17:20 -08004721 printString("don't know the type") // type of i is type of x (interface{})
Rob Pike5a578492009-03-17 16:48:35 -07004722}
4723</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004724
Rob Pike70c1a102009-03-18 19:23:59 -07004725<p>
4726could be rewritten:
4727</p>
4728
4729<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08004730v := x // x is evaluated exactly once
Rob Pike94b67eb2009-03-24 17:40:47 -07004731if v == nil {
Robert Griesemer48567312012-12-06 09:17:20 -08004732 i := v // type of i is type of x (interface{})
Robert Griesemer130ac742009-12-10 16:43:01 -08004733 printString("x is nil")
Russ Cox9c08d652012-02-21 22:04:30 -05004734} else if i, isInt := v.(int); isInt {
Robert Griesemer48567312012-12-06 09:17:20 -08004735 printInt(i) // type of i is int
Russ Cox9c08d652012-02-21 22:04:30 -05004736} else if i, isFloat64 := v.(float64); isFloat64 {
Robert Griesemer48567312012-12-06 09:17:20 -08004737 printFloat64(i) // type of i is float64
Russ Cox9c08d652012-02-21 22:04:30 -05004738} else if i, isFunc := v.(func(int) float64); isFunc {
Robert Griesemer48567312012-12-06 09:17:20 -08004739 printFunction(i) // type of i is func(int) float64
Rob Pike70c1a102009-03-18 19:23:59 -07004740} else {
Robert Griesemer48567312012-12-06 09:17:20 -08004741 _, isBool := v.(bool)
4742 _, isString := v.(string)
Russ Cox9c08d652012-02-21 22:04:30 -05004743 if isBool || isString {
Robert Griesemer48567312012-12-06 09:17:20 -08004744 i := v // type of i is type of x (interface{})
4745 printString("type is bool or string")
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07004746 } else {
Robert Griesemer48567312012-12-06 09:17:20 -08004747 i := v // type of i is type of x (interface{})
4748 printString("don't know the type")
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07004749 }
Rob Pike70c1a102009-03-18 19:23:59 -07004750}
4751</pre>
4752
Robert Griesemeraeaab592009-08-31 17:30:55 -07004753<p>
Russ Cox16b95ba2009-08-20 10:22:52 -07004754The type switch guard may be preceded by a simple statement, which
4755executes before the guard is evaluated.
Robert Griesemer1f95f0d2009-08-27 16:44:17 -07004756</p>
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07004757
4758<p>
4759The "fallthrough" statement is not permitted in a type switch.
Russ Cox16b95ba2009-08-20 10:22:52 -07004760</p>
4761
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004762<h3 id="For_statements">For statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004763
Rob Pike96750f12009-02-27 16:47:48 -08004764<p>
4765A "for" statement specifies repeated execution of a block. The iteration is
4766controlled by a condition, a "for" clause, or a "range" clause.
4767</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004768
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004769<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07004770ForStmt = "for" [ Condition | ForClause | RangeClause ] Block .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004771Condition = Expression .
4772</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004773
Rob Pike96750f12009-02-27 16:47:48 -08004774<p>
4775In its simplest form, a "for" statement specifies the repeated execution of
4776a block as long as a boolean condition evaluates to true.
4777The condition is evaluated before each iteration.
Robert Griesemerab266232014-02-25 09:13:37 -08004778If the condition is absent, it is equivalent to the boolean value
4779<code>true</code>.
Rob Pike96750f12009-02-27 16:47:48 -08004780</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004781
Robert Griesemerc2d55862009-02-19 16:49:10 -08004782<pre>
4783for a &lt; b {
4784 a *= 2
4785}
4786</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004787
Rob Pike96750f12009-02-27 16:47:48 -08004788<p>
Rob Pike678625d2009-09-15 09:54:22 -07004789A "for" statement with a ForClause is also controlled by its condition, but
Rob Pike96750f12009-02-27 16:47:48 -08004790additionally it may specify an <i>init</i>
4791and a <i>post</i> statement, such as an assignment,
Russ Cox16b95ba2009-08-20 10:22:52 -07004792an increment or decrement statement. The init statement may be a
Robert Griesemer4ed666e2009-08-27 16:45:42 -07004793<a href="#Short_variable_declarations">short variable declaration</a>, but the post statement must not.
Robert Griesemer2fa3e43f2014-09-25 12:52:05 -07004794Variables declared by the init statement are re-used in each iteration.
Rob Pike96750f12009-02-27 16:47:48 -08004795</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004796
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004797<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -08004798ForClause = [ InitStmt ] ";" [ Condition ] ";" [ PostStmt ] .
Rob Pike11417162009-03-24 17:45:53 -07004799InitStmt = SimpleStmt .
4800PostStmt = SimpleStmt .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004801</pre>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08004802
Robert Griesemerc2d55862009-02-19 16:49:10 -08004803<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08004804for i := 0; i &lt; 10; i++ {
Robert Griesemerc2d55862009-02-19 16:49:10 -08004805 f(i)
4806}
4807</pre>
Russ Cox5958dd62009-03-04 17:19:21 -08004808
Robert Griesemerc2d55862009-02-19 16:49:10 -08004809<p>
Rob Pike96750f12009-02-27 16:47:48 -08004810If non-empty, the init statement is executed once before evaluating the
4811condition for the first iteration;
4812the post statement is executed after each execution of the block (and
4813only if the block was executed).
Robert Griesemer130ac742009-12-10 16:43:01 -08004814Any element of the ForClause may be empty but the
4815<a href="#Semicolons">semicolons</a> are
Rob Pike96750f12009-02-27 16:47:48 -08004816required unless there is only a condition.
Robert Griesemerab266232014-02-25 09:13:37 -08004817If the condition is absent, it is equivalent to the boolean value
4818<code>true</code>.
Rob Pike96750f12009-02-27 16:47:48 -08004819</p>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08004820
Robert Griesemerc2d55862009-02-19 16:49:10 -08004821<pre>
Rob Pike678625d2009-09-15 09:54:22 -07004822for cond { S() } is the same as for ; cond ; { S() }
4823for { S() } is the same as for true { S() }
Robert Griesemerc2d55862009-02-19 16:49:10 -08004824</pre>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08004825
Rob Pike96750f12009-02-27 16:47:48 -08004826<p>
4827A "for" statement with a "range" clause
Rob Pike7aee71b2009-04-15 20:28:25 -07004828iterates through all entries of an array, slice, string or map,
Robert Griesemer5474e162010-09-28 14:44:19 -07004829or values received on a channel. For each entry it assigns <i>iteration values</i>
Robert Griesemer20ae6d92014-07-14 15:08:09 -07004830to corresponding <i>iteration variables</i> if present and then executes the block.
Rob Pike96750f12009-02-27 16:47:48 -08004831</p>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08004832
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004833<pre class="ebnf">
Robert Griesemer20ae6d92014-07-14 15:08:09 -07004834RangeClause = [ ExpressionList "=" | IdentifierList ":=" ] "range" Expression .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004835</pre>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08004836
Robert Griesemerc2d55862009-02-19 16:49:10 -08004837<p>
Robert Griesemer5474e162010-09-28 14:44:19 -07004838The expression on the right in the "range" clause is called the <i>range expression</i>,
Robert Griesemercc3f21c2012-12-03 14:23:41 -08004839which may be an array, pointer to an array, slice, string, map, or channel permitting
4840<a href="#Receive_operator">receive operations</a>.
Robert Griesemer20ae6d92014-07-14 15:08:09 -07004841As with an assignment, if present the operands on the left must be
Robert Griesemer5474e162010-09-28 14:44:19 -07004842<a href="#Address_operators">addressable</a> or map index expressions; they
Robert Griesemer20ae6d92014-07-14 15:08:09 -07004843denote the iteration variables. If the range expression is a channel, at most
4844one iteration variable is permitted, otherwise there may be up to two.
4845If the last iteration variable is the <a href="#Blank_identifier">blank identifier</a>,
4846the range clause is equivalent to the same clause without that identifier.
Robert Griesemer5474e162010-09-28 14:44:19 -07004847</p>
Rob Pike29d0f022011-01-05 11:39:57 -08004848
4849<p>
Russ Cox61e02ee2013-02-15 14:39:28 -05004850The range expression is evaluated once before beginning the loop,
Robert Griesemer20ae6d92014-07-14 15:08:09 -07004851with one exception: if the range expression is an array or a pointer to an array
4852and at most one iteration variable is present, only the range expression's
4853length is evaluated; if that length is constant,
4854<a href="#Length_and_capacity">by definition</a>
Russ Cox61e02ee2013-02-15 14:39:28 -05004855the range expression itself will not be evaluated.
4856</p>
4857
4858<p>
Robert Griesemer5474e162010-09-28 14:44:19 -07004859Function calls on the left are evaluated once per iteration.
Robert Griesemer20ae6d92014-07-14 15:08:09 -07004860For each iteration, iteration values are produced as follows
4861if the respective iteration variables are present:
Robert Griesemer5474e162010-09-28 14:44:19 -07004862</p>
4863
4864<pre class="grammar">
Robert Griesemer20ae6d92014-07-14 15:08:09 -07004865Range expression 1st value 2nd value
Robert Griesemer5474e162010-09-28 14:44:19 -07004866
4867array or slice a [n]E, *[n]E, or []E index i int a[i] E
Robert Griesemerb910a272011-11-01 01:09:22 -04004868string s string type index i int see below rune
Robert Griesemer5474e162010-09-28 14:44:19 -07004869map m map[K]V key k K m[k] V
Shenghou Maced57152013-01-17 23:11:25 +08004870channel c chan E, &lt;-chan E element e E
Robert Griesemer5474e162010-09-28 14:44:19 -07004871</pre>
4872
4873<ol>
4874<li>
Robert Griesemer54731032011-05-12 09:15:59 -07004875For an array, pointer to array, or slice value <code>a</code>, the index iteration
Russ Cox61e02ee2013-02-15 14:39:28 -05004876values are produced in increasing order, starting at element index 0.
Robert Griesemer20ae6d92014-07-14 15:08:09 -07004877If at most one iteration variable is present, the range loop produces
Robert Griesemerbb3a32e2013-05-20 13:27:53 -07004878iteration values from 0 up to <code>len(a)-1</code> and does not index into the array
Robert Griesemer54731032011-05-12 09:15:59 -07004879or slice itself. For a <code>nil</code> slice, the number of iterations is 0.
Robert Griesemer5474e162010-09-28 14:44:19 -07004880</li>
4881
4882<li>
4883For a string value, the "range" clause iterates over the Unicode code points
4884in the string starting at byte index 0. On successive iterations, the index value will be the
4885index of the first byte of successive UTF-8-encoded code points in the string,
Robert Griesemerb910a272011-11-01 01:09:22 -04004886and the second value, of type <code>rune</code>, will be the value of
Rob Pike7aee71b2009-04-15 20:28:25 -07004887the corresponding code point. If the iteration encounters an invalid
Robert Griesemer5474e162010-09-28 14:44:19 -07004888UTF-8 sequence, the second value will be <code>0xFFFD</code>,
Rob Pike7aee71b2009-04-15 20:28:25 -07004889the Unicode replacement character, and the next iteration will advance
4890a single byte in the string.
Robert Griesemer5474e162010-09-28 14:44:19 -07004891</li>
4892
4893<li>
Russ Coxe40d6e02011-10-17 18:49:02 -04004894The iteration order over maps is not specified
4895and is not guaranteed to be the same from one iteration to the next.
Shenghou Maced57152013-01-17 23:11:25 +08004896If map entries that have not yet been reached are removed during iteration,
Robert Griesemer5474e162010-09-28 14:44:19 -07004897the corresponding iteration values will not be produced. If map entries are
Shenghou Maced57152013-01-17 23:11:25 +08004898created during iteration, that entry may be produced during the iteration or
4899may be skipped. The choice may vary for each entry created and from one
4900iteration to the next.
4901If the map is <code>nil</code>, the number of iterations is 0.
Robert Griesemer5474e162010-09-28 14:44:19 -07004902</li>
4903
4904<li>
4905For channels, the iteration values produced are the successive values sent on
Robert Griesemer54731032011-05-12 09:15:59 -07004906the channel until the channel is <a href="#Close">closed</a>. If the channel
4907is <code>nil</code>, the range expression blocks forever.
Robert Griesemer5474e162010-09-28 14:44:19 -07004908</li>
Anthony Martin0122a662011-02-08 14:51:15 -08004909</ol>
Robert Griesemer5474e162010-09-28 14:44:19 -07004910
Rob Pike7aee71b2009-04-15 20:28:25 -07004911<p>
Robert Griesemer5474e162010-09-28 14:44:19 -07004912The iteration values are assigned to the respective
4913iteration variables as in an <a href="#Assignments">assignment statement</a>.
Rob Pike94b67eb2009-03-24 17:40:47 -07004914</p>
Robert Griesemer5474e162010-09-28 14:44:19 -07004915
Rob Pike94b67eb2009-03-24 17:40:47 -07004916<p>
Robert Griesemerda633712012-02-29 09:06:05 -08004917The iteration variables may be declared by the "range" clause using a form of
Robert Griesemer2c9e1632012-02-28 17:44:24 -08004918<a href="#Short_variable_declarations">short variable declaration</a>
4919(<code>:=</code>).
Robert Griesemer5474e162010-09-28 14:44:19 -07004920In this case their types are set to the types of the respective iteration values
Robert Griesemer2fa3e43f2014-09-25 12:52:05 -07004921and their <a href="#Declarations_and_scope">scope</a> is the block of the "for"
Robert Griesemer5474e162010-09-28 14:44:19 -07004922statement; they are re-used in each iteration.
Rob Pike96750f12009-02-27 16:47:48 -08004923If the iteration variables are declared outside the "for" statement,
4924after execution their values will be those of the last iteration.
4925</p>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08004926
Robert Griesemerc2d55862009-02-19 16:49:10 -08004927<pre>
Robert Griesemer54731032011-05-12 09:15:59 -07004928var testdata *struct {
4929 a *[7]int
4930}
4931for i, _ := range testdata.a {
4932 // testdata.a is never evaluated; len(testdata.a) is constant
4933 // i ranges from 0 to 6
4934 f(i)
4935}
4936
Robert Griesemer130ac742009-12-10 16:43:01 -08004937var a [10]string
Robert Griesemerc2d55862009-02-19 16:49:10 -08004938for i, s := range a {
4939 // type of i is int
4940 // type of s is string
4941 // s == a[i]
4942 g(i, s)
4943}
4944
Robert Griesemer130ac742009-12-10 16:43:01 -08004945var key string
Robert Griesemer440cc952010-06-07 17:40:21 -07004946var val interface {} // value type of m is assignable to val
Robert Griesemer63f54ae2013-07-11 14:41:46 -07004947m := map[string]int{"mon":0, "tue":1, "wed":2, "thu":3, "fri":4, "sat":5, "sun":6}
Rob Pike678625d2009-09-15 09:54:22 -07004948for key, val = range m {
4949 h(key, val)
Robert Griesemerc2d55862009-02-19 16:49:10 -08004950}
4951// key == last map key encountered in iteration
4952// val == map[key]
Robert Griesemer54731032011-05-12 09:15:59 -07004953
4954var ch chan Work = producer()
4955for w := range ch {
4956 doWork(w)
4957}
Robert Griesemer20ae6d92014-07-14 15:08:09 -07004958
4959// empty a channel
4960for range ch {}
Robert Griesemerc2d55862009-02-19 16:49:10 -08004961</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004962
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004963
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004964<h3 id="Go_statements">Go statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004965
Rob Pike96750f12009-02-27 16:47:48 -08004966<p>
Robert Griesemer25dd0022012-11-29 11:46:25 -08004967A "go" statement starts the execution of a function call
Rob Pike96750f12009-02-27 16:47:48 -08004968as an independent concurrent thread of control, or <i>goroutine</i>,
4969within the same address space.
4970</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004971
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004972<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07004973GoStmt = "go" Expression .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004974</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004975
Rob Pike96750f12009-02-27 16:47:48 -08004976<p>
Robert Griesemer25dd0022012-11-29 11:46:25 -08004977The expression must be a function or method call; it cannot be parenthesized.
4978Calls of built-in functions are restricted as for
4979<a href="#Expression_statements">expression statements</a>.
4980</p>
4981
4982<p>
Rob Pike633a2ce2012-01-23 08:40:13 -08004983The function value and parameters are
4984<a href="#Calls">evaluated as usual</a>
4985in the calling goroutine, but
Rob Pike96750f12009-02-27 16:47:48 -08004986unlike with a regular call, program execution does not wait
Robert Griesemerb9f8b9c2008-09-26 13:38:38 -07004987for the invoked function to complete.
Rob Pike633a2ce2012-01-23 08:40:13 -08004988Instead, the function begins executing independently
4989in a new goroutine.
4990When the function terminates, its goroutine also terminates.
4991If the function has any return values, they are discarded when the
4992function completes.
Rob Pike96750f12009-02-27 16:47:48 -08004993</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004994
Robert Griesemerc2d55862009-02-19 16:49:10 -08004995<pre>
4996go Server()
Robert Griesemere1e76192009-09-25 14:11:03 -07004997go func(ch chan&lt;- bool) { for { sleep(10); ch &lt;- true; }} (c)
Robert Griesemerc2d55862009-02-19 16:49:10 -08004998</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004999
5000
Russ Cox7c4f7cc2009-08-20 11:11:03 -07005001<h3 id="Select_statements">Select statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005002
Rob Pike96750f12009-02-27 16:47:48 -08005003<p>
Robert Griesemer61d8a332014-05-14 11:47:19 -07005004A "select" statement chooses which of a set of possible
5005<a href="#Send_statements">send</a> or
5006<a href="#Receive_operator">receive</a>
5007operations will proceed.
5008It looks similar to a
5009<a href="#Switch_statements">"switch"</a> statement but with the
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005010cases all referring to communication operations.
Rob Pike96750f12009-02-27 16:47:48 -08005011</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005012
Robert Griesemerf7ac31362009-07-10 16:06:40 -07005013<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07005014SelectStmt = "select" "{" { CommClause } "}" .
Robert Griesemer9905cec2013-03-04 13:55:35 -08005015CommClause = CommCase ":" StatementList .
Robert Griesemerb50ed022011-02-01 12:02:49 -08005016CommCase = "case" ( SendStmt | RecvStmt ) | "default" .
Robert Griesemerd3679722013-01-18 13:59:25 -08005017RecvStmt = [ ExpressionList "=" | IdentifierList ":=" ] RecvExpr .
Robert Griesemerc1347182011-04-29 12:20:31 -07005018RecvExpr = Expression .
Russ Cox61439182011-01-31 17:42:10 -05005019</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005020
Rob Pike96750f12009-02-27 16:47:48 -08005021<p>
Robert Griesemer61d8a332014-05-14 11:47:19 -07005022A case with a RecvStmt may assign the result of a RecvExpr to one or
5023two variables, which may be declared using a
Robert Griesemer4ed666e2009-08-27 16:45:42 -07005024<a href="#Short_variable_declarations">short variable declaration</a>.
Robert Griesemer61d8a332014-05-14 11:47:19 -07005025The RecvExpr must be a (possibly parenthesized) receive operation.
5026There can be at most one default case and it may appear anywhere
5027in the list of cases.
5028</p>
5029
5030<p>
5031Execution of a "select" statement proceeds in several steps:
5032</p>
5033
5034<ol>
5035<li>
5036For all the cases in the statement, the channel operands of receive operations
5037and the channel and right-hand-side expressions of send statements are
5038evaluated exactly once, in source order, upon entering the "select" statement.
5039The result is a set of channels to receive from or send to,
5040and the corresponding values to send.
5041Any side effects in that evaluation will occur irrespective of which (if any)
5042communication operation is selected to proceed.
5043Expressions on the left-hand side of a RecvStmt with a short variable declaration
5044or assignment are not yet evaluated.
5045</li>
5046
5047<li>
5048If one or more of the communications can proceed,
5049a single one that can proceed is chosen via a uniform pseudo-random selection.
5050Otherwise, if there is a default case, that case is chosen.
5051If there is no default case, the "select" statement blocks until
5052at least one of the communications can proceed.
5053</li>
5054
5055<li>
5056Unless the selected case is the default case, the respective communication
5057operation is executed.
5058</li>
5059
5060<li>
5061If the selected case is a RecvStmt with a short variable declaration or
5062an assignment, the left-hand side expressions are evaluated and the
5063received value (or values) are assigned.
5064</li>
5065
5066<li>
5067The statement list of the selected case is executed.
5068</li>
5069</ol>
5070
5071<p>
5072Since communication on <code>nil</code> channels can never proceed,
5073a select with only <code>nil</code> channels and no default case blocks forever.
Rob Pike96750f12009-02-27 16:47:48 -08005074</p>
Robert Griesemer2902a822008-09-17 13:57:11 -07005075
Robert Griesemerc2d55862009-02-19 16:49:10 -08005076<pre>
Robert Griesemer61d8a332014-05-14 11:47:19 -07005077var a []int
5078var c, c1, c2, c3, c4 chan int
Robert Griesemer130ac742009-12-10 16:43:01 -08005079var i1, i2 int
Robert Griesemerc2d55862009-02-19 16:49:10 -08005080select {
5081case i1 = &lt;-c1:
Robert Griesemer130ac742009-12-10 16:43:01 -08005082 print("received ", i1, " from c1\n")
Robert Griesemerc2d55862009-02-19 16:49:10 -08005083case c2 &lt;- i2:
Robert Griesemer130ac742009-12-10 16:43:01 -08005084 print("sent ", i2, " to c2\n")
Robert Griesemer6af887e2011-05-02 09:16:31 -07005085case i3, ok := (&lt;-c3): // same as: i3, ok := &lt;-c3
Russ Cox19d9a402011-01-27 15:34:28 -05005086 if ok {
5087 print("received ", i3, " from c3\n")
5088 } else {
5089 print("c3 is closed\n")
5090 }
Robert Griesemer61d8a332014-05-14 11:47:19 -07005091case a[f()] = &lt;-c4:
5092 // same as:
5093 // case t := &lt;-c4
5094 // a[f()] = t
Robert Griesemerc2d55862009-02-19 16:49:10 -08005095default:
Robert Griesemer130ac742009-12-10 16:43:01 -08005096 print("no communication\n")
Robert Griesemerc2d55862009-02-19 16:49:10 -08005097}
5098
5099for { // send random sequence of bits to c
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005100 select {
Robert Griesemerc2d55862009-02-19 16:49:10 -08005101 case c &lt;- 0: // note: no statement, no fallthrough, no folding of cases
5102 case c &lt;- 1:
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005103 }
Robert Griesemerc2d55862009-02-19 16:49:10 -08005104}
Rob Pike041d1162010-07-13 16:23:54 -07005105
David Symonds72a29792011-11-29 15:47:36 -08005106select {} // block forever
Robert Griesemerc2d55862009-02-19 16:49:10 -08005107</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005108
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005109
Russ Cox7c4f7cc2009-08-20 11:11:03 -07005110<h3 id="Return_statements">Return statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005111
Rob Pike96750f12009-02-27 16:47:48 -08005112<p>
Robert Griesemer1e8e14c2012-11-01 10:13:48 -07005113A "return" statement in a function <code>F</code> terminates the execution
5114of <code>F</code>, and optionally provides one or more result values.
5115Any functions <a href="#Defer_statements">deferred</a> by <code>F</code>
5116are executed before <code>F</code> returns to its caller.
Rob Pike96750f12009-02-27 16:47:48 -08005117</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005118
Robert Griesemerf7ac31362009-07-10 16:06:40 -07005119<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07005120ReturnStmt = "return" [ ExpressionList ] .
Robert Griesemerc2d55862009-02-19 16:49:10 -08005121</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005122
Robert Griesemer4b908332009-08-07 17:05:41 -07005123<p>
5124In a function without a result type, a "return" statement must not
5125specify any result values.
5126</p>
Rob Pike96750f12009-02-27 16:47:48 -08005127<pre>
Russ Cox9c08d652012-02-21 22:04:30 -05005128func noResult() {
Rob Pike96750f12009-02-27 16:47:48 -08005129 return
5130}
5131</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005132
Rob Pike96750f12009-02-27 16:47:48 -08005133<p>
Robert Griesemer4b908332009-08-07 17:05:41 -07005134There are three ways to return values from a function with a result
5135type:
Rob Pike96750f12009-02-27 16:47:48 -08005136</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005137
Robert Griesemer4b908332009-08-07 17:05:41 -07005138<ol>
5139 <li>The return value or values may be explicitly listed
5140 in the "return" statement. Each expression must be single-valued
Robert Griesemer440cc952010-06-07 17:40:21 -07005141 and <a href="#Assignability">assignable</a>
5142 to the corresponding element of the function's result type.
Robert Griesemerc2d55862009-02-19 16:49:10 -08005143<pre>
Russ Cox9c08d652012-02-21 22:04:30 -05005144func simpleF() int {
Rob Pike96750f12009-02-27 16:47:48 -08005145 return 2
Robert Griesemerc2d55862009-02-19 16:49:10 -08005146}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005147
Russ Cox9c08d652012-02-21 22:04:30 -05005148func complexF1() (re float64, im float64) {
Rob Pike96750f12009-02-27 16:47:48 -08005149 return -7.0, -4.0
Robert Griesemerc2d55862009-02-19 16:49:10 -08005150}
5151</pre>
Robert Griesemer4b908332009-08-07 17:05:41 -07005152 </li>
5153 <li>The expression list in the "return" statement may be a single
5154 call to a multi-valued function. The effect is as if each value
5155 returned from that function were assigned to a temporary
5156 variable with the type of the respective value, followed by a
5157 "return" statement listing these variables, at which point the
5158 rules of the previous case apply.
Robert Griesemerc2d55862009-02-19 16:49:10 -08005159<pre>
Russ Cox9c08d652012-02-21 22:04:30 -05005160func complexF2() (re float64, im float64) {
5161 return complexF1()
Rob Pike96750f12009-02-27 16:47:48 -08005162}
5163</pre>
Robert Griesemer4b908332009-08-07 17:05:41 -07005164 </li>
Peter Mundy5928e1d2010-11-09 10:10:57 -08005165 <li>The expression list may be empty if the function's result
Robert Griesemer462a17e2013-03-22 15:36:04 -07005166 type specifies names for its <a href="#Function_types">result parameters</a>.
Rob Pikedb8c2b182010-06-11 21:30:03 -07005167 The result parameters act as ordinary local variables
Robert Griesemer4b908332009-08-07 17:05:41 -07005168 and the function may assign values to them as necessary.
5169 The "return" statement returns the values of these variables.
Rob Pike96750f12009-02-27 16:47:48 -08005170<pre>
Russ Cox9c08d652012-02-21 22:04:30 -05005171func complexF3() (re float64, im float64) {
Robert Griesemer130ac742009-12-10 16:43:01 -08005172 re = 7.0
5173 im = 4.0
5174 return
Robert Griesemerc2d55862009-02-19 16:49:10 -08005175}
Robert Griesemerfb64e0d2011-03-07 16:29:07 -08005176
Russ Coxd9877e22011-11-01 21:45:02 -04005177func (devnull) Write(p []byte) (n int, _ error) {
Robert Griesemerfb64e0d2011-03-07 16:29:07 -08005178 n = len(p)
5179 return
Charles L. Dorian44262d12011-11-01 15:13:33 +09005180}
Robert Griesemerc2d55862009-02-19 16:49:10 -08005181</pre>
Robert Griesemer4b908332009-08-07 17:05:41 -07005182 </li>
5183</ol>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005184
Rob Pikedb8c2b182010-06-11 21:30:03 -07005185<p>
Robert Griesemer462a17e2013-03-22 15:36:04 -07005186Regardless of how they are declared, all the result values are initialized to
5187the <a href="#The_zero_value">zero values</a> for their type upon entry to the
Robert Griesemer1e8e14c2012-11-01 10:13:48 -07005188function. A "return" statement that specifies results sets the result parameters before
5189any deferred functions are executed.
Rob Pikedb8c2b182010-06-11 21:30:03 -07005190</p>
5191
Robert Griesemerc97778f2014-03-05 11:59:53 -08005192<p>
5193Implementation restriction: A compiler may disallow an empty expression list
5194in a "return" statement if a different entity (constant, type, or variable)
5195with the same name as a result parameter is in
5196<a href="#Declarations_and_scope">scope</a> at the place of the return.
5197</p>
5198
5199<pre>
5200func f(n int) (res int, err error) {
5201 if _, err := f(n-1); err != nil {
5202 return // invalid return statement: err is shadowed
5203 }
5204 return
5205}
5206</pre>
Russ Cox5958dd62009-03-04 17:19:21 -08005207
Russ Cox7c4f7cc2009-08-20 11:11:03 -07005208<h3 id="Break_statements">Break statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005209
Rob Pike96750f12009-02-27 16:47:48 -08005210<p>
5211A "break" statement terminates execution of the innermost
Robert Griesemer462a17e2013-03-22 15:36:04 -07005212<a href="#For_statements">"for"</a>,
5213<a href="#Switch_statements">"switch"</a>, or
Robert Griesemer94849d52014-05-28 08:43:47 -07005214<a href="#Select_statements">"select"</a> statement
5215within the same function.
Rob Pike96750f12009-02-27 16:47:48 -08005216</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005217
Robert Griesemerf7ac31362009-07-10 16:06:40 -07005218<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -08005219BreakStmt = "break" [ Label ] .
Robert Griesemerc2d55862009-02-19 16:49:10 -08005220</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005221
Rob Pike96750f12009-02-27 16:47:48 -08005222<p>
5223If there is a label, it must be that of an enclosing
Robert Griesemer462a17e2013-03-22 15:36:04 -07005224"for", "switch", or "select" statement,
5225and that is the one whose execution terminates.
Rob Pike96750f12009-02-27 16:47:48 -08005226</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005227
Robert Griesemerc2d55862009-02-19 16:49:10 -08005228<pre>
Rob Pikecec09542013-09-17 07:41:11 +10005229OuterLoop:
5230 for i = 0; i &lt; n; i++ {
5231 for j = 0; j &lt; m; j++ {
5232 switch a[i][j] {
5233 case nil:
5234 state = Error
5235 break OuterLoop
5236 case item:
5237 state = Found
5238 break OuterLoop
5239 }
Russ Cox108564d2011-03-15 13:51:24 -04005240 }
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005241 }
Robert Griesemerc2d55862009-02-19 16:49:10 -08005242</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005243
Russ Cox7c4f7cc2009-08-20 11:11:03 -07005244<h3 id="Continue_statements">Continue statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005245
Rob Pike96750f12009-02-27 16:47:48 -08005246<p>
5247A "continue" statement begins the next iteration of the
Robert Griesemer462a17e2013-03-22 15:36:04 -07005248innermost <a href="#For_statements">"for" loop</a> at its post statement.
Robert Griesemer94849d52014-05-28 08:43:47 -07005249The "for" loop must be within the same function.
Rob Pike96750f12009-02-27 16:47:48 -08005250</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005251
Robert Griesemerf7ac31362009-07-10 16:06:40 -07005252<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -08005253ContinueStmt = "continue" [ Label ] .
Robert Griesemerc2d55862009-02-19 16:49:10 -08005254</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005255
Rob Pike96750f12009-02-27 16:47:48 -08005256<p>
Rob Pikede9219962010-04-28 13:18:40 -07005257If there is a label, it must be that of an enclosing
5258"for" statement, and that is the one whose execution
Robert Griesemer462a17e2013-03-22 15:36:04 -07005259advances.
Rob Pike96750f12009-02-27 16:47:48 -08005260</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005261
Rob Pikecec09542013-09-17 07:41:11 +10005262<pre>
5263RowLoop:
5264 for y, row := range rows {
5265 for x, data := range row {
5266 if data == endOfRow {
5267 continue RowLoop
5268 }
5269 row[x] = data + bias(x, y)
5270 }
5271 }
5272</pre>
5273
Russ Cox7c4f7cc2009-08-20 11:11:03 -07005274<h3 id="Goto_statements">Goto statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005275
Rob Pike96750f12009-02-27 16:47:48 -08005276<p>
Robert Griesemer94849d52014-05-28 08:43:47 -07005277A "goto" statement transfers control to the statement with the corresponding label
5278within the same function.
Rob Pike96750f12009-02-27 16:47:48 -08005279</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005280
Robert Griesemerf7ac31362009-07-10 16:06:40 -07005281<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07005282GotoStmt = "goto" Label .
Robert Griesemerc2d55862009-02-19 16:49:10 -08005283</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005284
Robert Griesemerc2d55862009-02-19 16:49:10 -08005285<pre>
5286goto Error
5287</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005288
Rob Pike96750f12009-02-27 16:47:48 -08005289<p>
5290Executing the "goto" statement must not cause any variables to come into
Russ Coxf4c7db02011-06-17 12:49:04 -04005291<a href="#Declarations_and_scope">scope</a> that were not already in scope at the point of the goto.
5292For instance, this example:
Rob Pike96750f12009-02-27 16:47:48 -08005293</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005294
Robert Griesemerc2d55862009-02-19 16:49:10 -08005295<pre>
Russ Cox108564d2011-03-15 13:51:24 -04005296 goto L // BAD
5297 v := 3
Robert Griesemerc2d55862009-02-19 16:49:10 -08005298L:
5299</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005300
Rob Pike96750f12009-02-27 16:47:48 -08005301<p>
5302is erroneous because the jump to label <code>L</code> skips
5303the creation of <code>v</code>.
Russ Coxf4c7db02011-06-17 12:49:04 -04005304</p>
5305
5306<p>
5307A "goto" statement outside a <a href="#Blocks">block</a> cannot jump to a label inside that block.
5308For instance, this example:
5309</p>
5310
5311<pre>
5312if n%2 == 1 {
5313 goto L1
5314}
5315for n &gt; 0 {
5316 f()
5317 n--
5318L1:
5319 f()
5320 n--
5321}
5322</pre>
5323
5324<p>
Charles L. Dorian44262d12011-11-01 15:13:33 +09005325is erroneous because the label <code>L1</code> is inside
Russ Coxf4c7db02011-06-17 12:49:04 -04005326the "for" statement's block but the <code>goto</code> is not.
Rob Pike96750f12009-02-27 16:47:48 -08005327</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005328
Russ Cox7c4f7cc2009-08-20 11:11:03 -07005329<h3 id="Fallthrough_statements">Fallthrough statements</h3>
Robert Griesemeraed247f2008-10-08 17:05:30 -07005330
Rob Pike96750f12009-02-27 16:47:48 -08005331<p>
5332A "fallthrough" statement transfers control to the first statement of the
Shenghou Maca9876d2015-12-31 11:25:51 -05005333next case clause in an <a href="#Expression_switches">expression "switch" statement</a>.
Robert Griesemer67a6b4f2013-03-01 16:45:14 -08005334It may be used only as the final non-empty statement in such a clause.
Rob Pike96750f12009-02-27 16:47:48 -08005335</p>
Robert Griesemeraed247f2008-10-08 17:05:30 -07005336
Robert Griesemerf7ac31362009-07-10 16:06:40 -07005337<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07005338FallthroughStmt = "fallthrough" .
Robert Griesemerc2d55862009-02-19 16:49:10 -08005339</pre>
Robert Griesemeraed247f2008-10-08 17:05:30 -07005340
5341
Russ Cox7c4f7cc2009-08-20 11:11:03 -07005342<h3 id="Defer_statements">Defer statements</h3>
Robert Griesemer4a903e02009-01-27 09:29:40 -08005343
Rob Pike96750f12009-02-27 16:47:48 -08005344<p>
Robert Griesemer1e8e14c2012-11-01 10:13:48 -07005345A "defer" statement invokes a function whose execution is deferred
5346to the moment the surrounding function returns, either because the
5347surrounding function executed a <a href="#Return_statements">return statement</a>,
5348reached the end of its <a href="#Function_declarations">function body</a>,
5349or because the corresponding goroutine is <a href="#Handling_panics">panicking</a>.
Rob Pike96750f12009-02-27 16:47:48 -08005350</p>
Robert Griesemer4a903e02009-01-27 09:29:40 -08005351
Robert Griesemerf7ac31362009-07-10 16:06:40 -07005352<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07005353DeferStmt = "defer" Expression .
Robert Griesemerc2d55862009-02-19 16:49:10 -08005354</pre>
Robert Griesemer4a903e02009-01-27 09:29:40 -08005355
Rob Pike96750f12009-02-27 16:47:48 -08005356<p>
Robert Griesemer25dd0022012-11-29 11:46:25 -08005357The expression must be a function or method call; it cannot be parenthesized.
5358Calls of built-in functions are restricted as for
5359<a href="#Expression_statements">expression statements</a>.
5360</p>
5361
5362<p>
Robert Griesemerb4eb22d2014-09-19 13:32:07 -07005363Each time a "defer" statement
Rob Pike633a2ce2012-01-23 08:40:13 -08005364executes, the function value and parameters to the call are
5365<a href="#Calls">evaluated as usual</a>
Robert Griesemerb4eb22d2014-09-19 13:32:07 -07005366and saved anew but the actual function is not invoked.
5367Instead, deferred functions are invoked immediately before
Robert Griesemer1e8e14c2012-11-01 10:13:48 -07005368the surrounding function returns, in the reverse order
5369they were deferred.
Robert Griesemerb4eb22d2014-09-19 13:32:07 -07005370If a deferred function value evaluates
5371to <code>nil</code>, execution <a href="#Handling_panics">panics</a>
Rob Pike651bb8e2014-09-19 14:13:51 -07005372when the function is invoked, not when the "defer" statement is executed.
Robert Griesemer1e8e14c2012-11-01 10:13:48 -07005373</p>
5374
5375<p>
5376For instance, if the deferred function is
Rob Pike5bb29fb2010-03-25 17:59:59 -07005377a <a href="#Function_literals">function literal</a> and the surrounding
Robert Griesemer48f0cd22010-03-23 17:30:14 -07005378function has <a href="#Function_types">named result parameters</a> that
5379are in scope within the literal, the deferred function may access and modify
5380the result parameters before they are returned.
Rob Pike633a2ce2012-01-23 08:40:13 -08005381If the deferred function has any return values, they are discarded when
5382the function completes.
Rob Pike15970c82012-10-16 11:27:20 +11005383(See also the section on <a href="#Handling_panics">handling panics</a>.)
5384</p>
5385
Robert Griesemerc2d55862009-02-19 16:49:10 -08005386<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08005387lock(l)
5388defer unlock(l) // unlocking happens before surrounding function returns
Robert Griesemer4a903e02009-01-27 09:29:40 -08005389
Robert Griesemerc2d55862009-02-19 16:49:10 -08005390// prints 3 2 1 0 before surrounding function returns
5391for i := 0; i &lt;= 3; i++ {
Robert Griesemer130ac742009-12-10 16:43:01 -08005392 defer fmt.Print(i)
Robert Griesemerc2d55862009-02-19 16:49:10 -08005393}
Robert Griesemer48f0cd22010-03-23 17:30:14 -07005394
5395// f returns 1
5396func f() (result int) {
5397 defer func() {
5398 result++
5399 }()
5400 return 0
5401}
Robert Griesemerc2d55862009-02-19 16:49:10 -08005402</pre>
Robert Griesemer4a903e02009-01-27 09:29:40 -08005403
Robert Griesemer164a7bc2009-09-30 12:00:25 -07005404<h2 id="Built-in_functions">Built-in functions</h2>
5405
5406<p>
Rob Pike72970872010-03-04 12:35:16 -08005407Built-in functions are
Robert Griesemer164a7bc2009-09-30 12:00:25 -07005408<a href="#Predeclared_identifiers">predeclared</a>.
5409They are called like any other function but some of them
5410accept a type instead of an expression as the first argument.
5411</p>
5412
Russ Cox2a5f0c62009-12-04 10:23:12 -08005413<p>
5414The built-in functions do not have standard Go types,
5415so they can only appear in <a href="#Calls">call expressions</a>;
5416they cannot be used as function values.
5417</p>
5418
Russ Cox9f2cb862011-03-11 14:47:02 -05005419<h3 id="Close">Close</h3>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07005420
5421<p>
Robert Griesemerdc60c5a2010-07-14 16:09:22 -07005422For a channel <code>c</code>, the built-in function <code>close(c)</code>
Russ Coxf58ed4e2011-10-13 16:58:04 -04005423records that no more values will be sent on the channel.
5424It is an error if <code>c</code> is a receive-only channel.
5425Sending to or closing a closed channel causes a <a href="#Run_time_panics">run-time panic</a>.
5426Closing the nil channel also causes a <a href="#Run_time_panics">run-time panic</a>.
Robert Griesemerdc60c5a2010-07-14 16:09:22 -07005427After calling <code>close</code>, and after any previously
Robert Griesemer164a7bc2009-09-30 12:00:25 -07005428sent values have been received, receive operations will return
Robert Griesemerdc60c5a2010-07-14 16:09:22 -07005429the zero value for the channel's type without blocking.
Russ Cox9f2cb862011-03-11 14:47:02 -05005430The multi-valued <a href="#Receive_operator">receive operation</a>
5431returns a received value along with an indication of whether the channel is closed.
Robert Griesemer164a7bc2009-09-30 12:00:25 -07005432</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005433
Robert Griesemerdc60c5a2010-07-14 16:09:22 -07005434
Russ Cox7c4f7cc2009-08-20 11:11:03 -07005435<h3 id="Length_and_capacity">Length and capacity</h3>
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -07005436
Robert Griesemer164a7bc2009-09-30 12:00:25 -07005437<p>
5438The built-in functions <code>len</code> and <code>cap</code> take arguments
5439of various types and return a result of type <code>int</code>.
5440The implementation guarantees that the result always fits into an <code>int</code>.
5441</p>
5442
Rob Pikeff70f092009-02-20 13:36:14 -08005443<pre class="grammar">
Robert Griesemer54731032011-05-12 09:15:59 -07005444Call Argument type Result
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -07005445
Robert Griesemer54731032011-05-12 09:15:59 -07005446len(s) string type string length in bytes
5447 [n]T, *[n]T array length (== n)
5448 []T slice length
5449 map[K]T map length (number of defined keys)
5450 chan T number of elements queued in channel buffer
Robert Griesemer4dc25282008-09-09 10:37:19 -07005451
Robert Griesemer54731032011-05-12 09:15:59 -07005452cap(s) [n]T, *[n]T array length (== n)
5453 []T slice capacity
5454 chan T channel buffer capacity
Robert Griesemerc2d55862009-02-19 16:49:10 -08005455</pre>
Robert Griesemer4dc25282008-09-09 10:37:19 -07005456
Robert Griesemerc2d55862009-02-19 16:49:10 -08005457<p>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07005458The capacity of a slice is the number of elements for which there is
5459space allocated in the underlying array.
5460At any time the following relationship holds:
5461</p>
Robert Griesemer633957b2009-01-06 13:23:20 -08005462
Robert Griesemerc2d55862009-02-19 16:49:10 -08005463<pre>
Anthony Martin0122a662011-02-08 14:51:15 -080054640 &lt;= len(s) &lt;= cap(s)
Robert Griesemerc2d55862009-02-19 16:49:10 -08005465</pre>
Robert Griesemer667ef6c2008-09-10 13:00:32 -07005466
Russ Coxf4429182010-07-01 17:49:47 -07005467<p>
Shenghou Maa0b5b462013-01-22 03:18:20 +08005468The length of a <code>nil</code> slice, map or channel is 0.
Rob Pike82e2db72014-01-04 10:52:59 -08005469The capacity of a <code>nil</code> slice or channel is 0.
Robert Griesemer0c2e6b32010-07-13 11:54:57 -07005470</p>
5471
5472<p>
Robert Griesemer54731032011-05-12 09:15:59 -07005473The expression <code>len(s)</code> is <a href="#Constants">constant</a> if
5474<code>s</code> is a string constant. The expressions <code>len(s)</code> and
5475<code>cap(s)</code> are constants if the type of <code>s</code> is an array
5476or pointer to an array and the expression <code>s</code> does not contain
Robert Griesemer87169812014-03-03 20:07:34 -08005477<a href="#Receive_operator">channel receives</a> or (non-constant)
Robert Griesemer54731032011-05-12 09:15:59 -07005478<a href="#Calls">function calls</a>; in this case <code>s</code> is not evaluated.
5479Otherwise, invocations of <code>len</code> and <code>cap</code> are not
5480constant and <code>s</code> is evaluated.
Russ Coxf4429182010-07-01 17:49:47 -07005481</p>
Robert Griesemer4dc25282008-09-09 10:37:19 -07005482
Robert Griesemer87169812014-03-03 20:07:34 -08005483<pre>
5484const (
5485 c1 = imag(2i) // imag(2i) = 2.0 is a constant
5486 c2 = len([10]float64{2}) // [10]float64{2} contains no function calls
5487 c3 = len([10]float64{c1}) // [10]float64{c1} contains no function calls
5488 c4 = len([10]float64{imag(2i)}) // imag(2i) is a constant and no function call is issued
Shenghou Mac0abdd92014-12-26 02:50:33 -05005489 c5 = len([10]float64{imag(z)}) // invalid: imag(z) is a (non-constant) function call
Robert Griesemer87169812014-03-03 20:07:34 -08005490)
5491var z complex128
5492</pre>
Robert Griesemer54731032011-05-12 09:15:59 -07005493
Russ Cox7c4f7cc2009-08-20 11:11:03 -07005494<h3 id="Allocation">Allocation</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005495
Rob Pike96750f12009-02-27 16:47:48 -08005496<p>
Robert Griesemer6962c152014-10-16 15:08:49 -07005497The built-in function <code>new</code> takes a type <code>T</code>,
5498allocates storage for a <a href="#Variables">variable</a> of that type
5499at run time, and returns a value of type <code>*T</code>
5500<a href="#Pointer_types">pointing</a> to it.
5501The variable is initialized as described in the section on
Robert Griesemer462a17e2013-03-22 15:36:04 -07005502<a href="#The_zero_value">initial values</a>.
Rob Pike96750f12009-02-27 16:47:48 -08005503</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005504
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08005505<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08005506new(T)
5507</pre>
Robert Griesemer4dc25282008-09-09 10:37:19 -07005508
Rob Pike96750f12009-02-27 16:47:48 -08005509<p>
Robert Griesemer4dc25282008-09-09 10:37:19 -07005510For instance
Rob Pike96750f12009-02-27 16:47:48 -08005511</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005512
Robert Griesemerc2d55862009-02-19 16:49:10 -08005513<pre>
Robert Griesemerb94c0d22011-01-19 23:07:21 -05005514type S struct { a int; b float64 }
Robert Griesemerc2d55862009-02-19 16:49:10 -08005515new(S)
5516</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005517
Rob Pike96750f12009-02-27 16:47:48 -08005518<p>
Robert Griesemer6962c152014-10-16 15:08:49 -07005519allocates storage for a variable of type <code>S</code>,
Rob Pike96750f12009-02-27 16:47:48 -08005520initializes it (<code>a=0</code>, <code>b=0.0</code>),
5521and returns a value of type <code>*S</code> containing the address
Robert Griesemer6962c152014-10-16 15:08:49 -07005522of the location.
Rob Pike96750f12009-02-27 16:47:48 -08005523</p>
5524
Russ Cox7c4f7cc2009-08-20 11:11:03 -07005525<h3 id="Making_slices_maps_and_channels">Making slices, maps and channels</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005526
Robert Griesemerc2d55862009-02-19 16:49:10 -08005527<p>
Rob Pike96750f12009-02-27 16:47:48 -08005528The built-in function <code>make</code> takes a type <code>T</code>,
5529which must be a slice, map or channel type,
5530optionally followed by a type-specific list of expressions.
5531It returns a value of type <code>T</code> (not <code>*T</code>).
Robert Griesemer462a17e2013-03-22 15:36:04 -07005532The memory is initialized as described in the section on
5533<a href="#The_zero_value">initial values</a>.
Rob Pike96750f12009-02-27 16:47:48 -08005534</p>
Robert Griesemer633957b2009-01-06 13:23:20 -08005535
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08005536<pre class="grammar">
Robert Griesemerdf674ff2010-05-04 17:31:40 -07005537Call Type T Result
5538
5539make(T, n) slice slice of type T with length n and capacity n
5540make(T, n, m) slice slice of type T with length n and capacity m
5541
5542make(T) map map of type T
5543make(T, n) map map of type T with initial space for n elements
5544
Robert Griesemer97aa90d2014-05-07 10:40:39 -07005545make(T) channel unbuffered channel of type T
5546make(T, n) channel buffered channel of type T, buffer size n
Robert Griesemerc2d55862009-02-19 16:49:10 -08005547</pre>
Robert Griesemer633957b2009-01-06 13:23:20 -08005548
Robert Griesemer633957b2009-01-06 13:23:20 -08005549
Rob Pike96750f12009-02-27 16:47:48 -08005550<p>
Robert Griesemerb66df0e2013-03-11 09:20:52 -07005551The size arguments <code>n</code> and <code>m</code> must be of integer type or untyped.
Robert Griesemer39067062012-12-12 11:06:26 -08005552A <a href="#Constants">constant</a> size argument must be non-negative and
5553representable by a value of type <code>int</code>.
5554If both <code>n</code> and <code>m</code> are provided and are constant, then
Robert Griesemer3bde0002012-10-19 10:11:06 -07005555<code>n</code> must be no larger than <code>m</code>.
5556If <code>n</code> is negative or larger than <code>m</code> at run time,
5557a <a href="#Run_time_panics">run-time panic</a> occurs.
Rob Pike96750f12009-02-27 16:47:48 -08005558</p>
Robert Griesemer633957b2009-01-06 13:23:20 -08005559
Robert Griesemerc2d55862009-02-19 16:49:10 -08005560<pre>
David Symonds72a29792011-11-29 15:47:36 -08005561s := make([]int, 10, 100) // slice with len(s) == 10, cap(s) == 100
Robert Griesemer3bde0002012-10-19 10:11:06 -07005562s := make([]int, 1e3) // slice with len(s) == cap(s) == 1000
Robert Griesemer39067062012-12-12 11:06:26 -08005563s := make([]int, 1&lt;&lt;63) // illegal: len(s) is not representable by a value of type int
Oling Cat018e89f2013-01-24 20:46:33 +11005564s := make([]int, 10, 0) // illegal: len(s) > cap(s)
David Symonds72a29792011-11-29 15:47:36 -08005565c := make(chan int, 10) // channel with a buffer size of 10
5566m := make(map[string]int, 100) // map with initial space for 100 elements
Robert Griesemerc2d55862009-02-19 16:49:10 -08005567</pre>
Robert Griesemer633957b2009-01-06 13:23:20 -08005568
Robert Griesemer164a7bc2009-09-30 12:00:25 -07005569
Robert Griesemer07e983a2010-10-25 16:50:31 -07005570<h3 id="Appending_and_copying_slices">Appending to and copying slices</h3>
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08005571
5572<p>
Robert Griesemer0c494712012-09-28 15:55:38 -07005573The built-in functions <code>append</code> and <code>copy</code> assist in
5574common slice operations.
5575For both functions, the result is independent of whether the memory referenced
5576by the arguments overlaps.
Robert Griesemer07e983a2010-10-25 16:50:31 -07005577</p>
5578
5579<p>
Robert Griesemer95b81372011-06-12 12:09:50 -07005580The <a href="#Function_types">variadic</a> function <code>append</code>
5581appends zero or more values <code>x</code>
Robert Griesemer0f7acf12011-04-19 14:38:49 -07005582to <code>s</code> of type <code>S</code>, which must be a slice type, and
5583returns the resulting slice, also of type <code>S</code>.
Robert Griesemer95b81372011-06-12 12:09:50 -07005584The values <code>x</code> are passed to a parameter of type <code>...T</code>
5585where <code>T</code> is the <a href="#Slice_types">element type</a> of
5586<code>S</code> and the respective
5587<a href="#Passing_arguments_to_..._parameters">parameter passing rules</a> apply.
Luuk van Dijk77fac212011-10-12 15:59:23 +02005588As a special case, <code>append</code> also accepts a first argument
5589assignable to type <code>[]byte</code> with a second argument of
5590string type followed by <code>...</code>. This form appends the
5591bytes of the string.
Robert Griesemer07e983a2010-10-25 16:50:31 -07005592</p>
5593
5594<pre class="grammar">
Robert Griesemer0f7acf12011-04-19 14:38:49 -07005595append(s S, x ...T) S // T is the element type of S
Robert Griesemer07e983a2010-10-25 16:50:31 -07005596</pre>
5597
5598<p>
5599If the capacity of <code>s</code> is not large enough to fit the additional
Robert Griesemer15da9972013-10-16 16:16:54 -07005600values, <code>append</code> allocates a new, sufficiently large underlying
5601array that fits both the existing slice elements and the additional values.
5602Otherwise, <code>append</code> re-uses the underlying array.
Robert Griesemer07e983a2010-10-25 16:50:31 -07005603</p>
5604
5605<pre>
5606s0 := []int{0, 0}
Robert Griesemer0c494712012-09-28 15:55:38 -07005607s1 := append(s0, 2) // append a single element s1 == []int{0, 0, 2}
5608s2 := append(s1, 3, 5, 7) // append multiple elements s2 == []int{0, 0, 2, 3, 5, 7}
5609s3 := append(s2, s0...) // append a slice s3 == []int{0, 0, 2, 3, 5, 7, 0, 0}
5610s4 := append(s3[3:6], s3[2:]...) // append overlapping slice s4 == []int{3, 5, 7, 2, 3, 5, 7, 0, 0}
Robert Griesemer0f7acf12011-04-19 14:38:49 -07005611
5612var t []interface{}
David Symonds583b29c2014-12-04 09:29:29 +11005613t = append(t, 42, 3.1415, "foo") // t == []interface{}{42, 3.1415, "foo"}
Luuk van Dijk77fac212011-10-12 15:59:23 +02005614
5615var b []byte
Robert Griesemer0c494712012-09-28 15:55:38 -07005616b = append(b, "bar"...) // append string contents b == []byte{'b', 'a', 'r' }
Robert Griesemer07e983a2010-10-25 16:50:31 -07005617</pre>
5618
5619<p>
5620The function <code>copy</code> copies slice elements from
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08005621a source <code>src</code> to a destination <code>dst</code> and returns the
Robert Griesemer0c494712012-09-28 15:55:38 -07005622number of elements copied.
Robert Griesemer7bc03712010-06-07 15:49:39 -07005623Both arguments must have <a href="#Type_identity">identical</a> element type <code>T</code> and must be
Robert Griesemer425bbad2010-10-25 16:41:06 -07005624<a href="#Assignability">assignable</a> to a slice of type <code>[]T</code>.
Nigel Tao703b0922011-05-15 16:04:37 -07005625The number of elements copied is the minimum of
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08005626<code>len(src)</code> and <code>len(dst)</code>.
Robert Griesemer425bbad2010-10-25 16:41:06 -07005627As a special case, <code>copy</code> also accepts a destination argument assignable
5628to type <code>[]byte</code> with a source argument of a string type.
5629This form copies the bytes from the string into the byte slice.
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08005630</p>
5631
5632<pre class="grammar">
5633copy(dst, src []T) int
Robert Griesemer425bbad2010-10-25 16:41:06 -07005634copy(dst []byte, src string) int
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08005635</pre>
5636
5637<p>
5638Examples:
5639</p>
5640
5641<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08005642var a = [...]int{0, 1, 2, 3, 4, 5, 6, 7}
5643var s = make([]int, 6)
Robert Griesemer425bbad2010-10-25 16:41:06 -07005644var b = make([]byte, 5)
5645n1 := copy(s, a[0:]) // n1 == 6, s == []int{0, 1, 2, 3, 4, 5}
5646n2 := copy(s, s[2:]) // n2 == 4, s == []int{2, 3, 4, 5, 4, 5}
5647n3 := copy(b, "Hello, World!") // n3 == 5, b == []byte("Hello")
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08005648</pre>
5649
Robert Griesemer3e0c0a82011-10-17 12:53:10 -07005650
5651<h3 id="Deletion_of_map_elements">Deletion of map elements</h3>
5652
5653<p>
5654The built-in function <code>delete</code> removes the element with key
5655<code>k</code> from a <a href="#Map_types">map</a> <code>m</code>. The
5656type of <code>k</code> must be <a href="#Assignability">assignable</a>
5657to the key type of <code>m</code>.
5658</p>
5659
5660<pre class="grammar">
5661delete(m, k) // remove element m[k] from map m
5662</pre>
5663
5664<p>
Robert Griesemera9a49fe2012-12-12 13:08:35 -08005665If the map <code>m</code> is <code>nil</code> or the element <code>m[k]</code>
5666does not exist, <code>delete</code> is a no-op.
Robert Griesemer3e0c0a82011-10-17 12:53:10 -07005667</p>
5668
5669
Russ Cox0201e372012-02-29 15:20:11 -05005670<h3 id="Complex_numbers">Manipulating complex numbers</h3>
Rob Pike72970872010-03-04 12:35:16 -08005671
5672<p>
5673Three functions assemble and disassemble complex numbers.
Robert Griesemerb94c0d22011-01-19 23:07:21 -05005674The built-in function <code>complex</code> constructs a complex
Rob Pike72970872010-03-04 12:35:16 -08005675value from a floating-point real and imaginary part, while
5676<code>real</code> and <code>imag</code>
5677extract the real and imaginary parts of a complex value.
5678</p>
5679
5680<pre class="grammar">
Robert Griesemerb94c0d22011-01-19 23:07:21 -05005681complex(realPart, imaginaryPart floatT) complexT
Rob Pike72970872010-03-04 12:35:16 -08005682real(complexT) floatT
5683imag(complexT) floatT
5684</pre>
5685
5686<p>
5687The type of the arguments and return value correspond.
Robert Griesemerb94c0d22011-01-19 23:07:21 -05005688For <code>complex</code>, the two arguments must be of the same
Rob Pike72970872010-03-04 12:35:16 -08005689floating-point type and the return type is the complex type
5690with the corresponding floating-point constituents:
Robert Griesemer98aa8222015-07-29 15:42:04 -07005691<code>complex64</code> for <code>float32</code> arguments, and
5692<code>complex128</code> for <code>float64</code> arguments.
5693If one of the arguments evaluates to an untyped constant, it is first
5694<a href="#Conversions">converted</a> to the type of the other argument.
5695If both arguments evaluate to untyped constants, they must be non-complex
5696numbers or their imaginary parts must be zero, and the return value of
5697the function is an untyped complex constant.
5698</p>
5699
5700<p>
5701For <code>real</code> and <code>imag</code>, the argument must be
5702of complex type, and the return type is the corresponding floating-point
5703type: <code>float32</code> for a <code>complex64</code> argument, and
5704<code>float64</code> for a <code>complex128</code> argument.
5705If the argument evaluates to an untyped constant, it must be a number,
5706and the return value of the function is an untyped floating-point constant.
5707</p>
5708
5709<p>
5710The <code>real</code> and <code>imag</code> functions together form the inverse of
5711<code>complex</code>, so for a value <code>z</code> of a complex type <code>Z</code>,
5712<code>z&nbsp;==&nbsp;Z(complex(real(z),&nbsp;imag(z)))</code>.
Rob Pike72970872010-03-04 12:35:16 -08005713</p>
5714
5715<p>
5716If the operands of these functions are all constants, the return
5717value is a constant.
5718</p>
5719
5720<pre>
Robert Griesemerb94c0d22011-01-19 23:07:21 -05005721var a = complex(2, -2) // complex128
Robert Griesemer98aa8222015-07-29 15:42:04 -07005722const b = complex(1.0, -1.4) // untyped complex constant 1 - 1.4i
Robert Griesemerb94c0d22011-01-19 23:07:21 -05005723x := float32(math.Cos(math.Pi/2)) // float32
5724var c64 = complex(5, -x) // complex64
Robert Griesemer98aa8222015-07-29 15:42:04 -07005725const s uint = complex(1, 0) // untyped complex constant 1 + 0i can be converted to uint
5726_ = complex(1, 2&lt;&lt;s) // illegal: 2 has floating-point type, cannot shift
Robert Griesemerb94c0d22011-01-19 23:07:21 -05005727var rl = real(c64) // float32
Robert Griesemer98aa8222015-07-29 15:42:04 -07005728var im = imag(a) // float64
5729const c = imag(b) // untyped constant -1.4
5730_ = imag(3 &lt;&lt; s) // illegal: 3 has complex type, cannot shift
Rob Pike72970872010-03-04 12:35:16 -08005731</pre>
5732
Rob Pike5bb29fb2010-03-25 17:59:59 -07005733<h3 id="Handling_panics">Handling panics</h3>
5734
5735<p> Two built-in functions, <code>panic</code> and <code>recover</code>,
5736assist in reporting and handling <a href="#Run_time_panics">run-time panics</a>
Charles L. Dorian44262d12011-11-01 15:13:33 +09005737and program-defined error conditions.
Rob Pike5bb29fb2010-03-25 17:59:59 -07005738</p>
5739
5740<pre class="grammar">
5741func panic(interface{})
5742func recover() interface{}
5743</pre>
5744
5745<p>
Rob Pikec34050f2013-03-12 14:28:16 -07005746While executing a function <code>F</code>,
5747an explicit call to <code>panic</code> or a <a href="#Run_time_panics">run-time panic</a>
5748terminates the execution of <code>F</code>.
Robert Griesemer1e8e14c2012-11-01 10:13:48 -07005749Any functions <a href="#Defer_statements">deferred</a> by <code>F</code>
Rob Pikec34050f2013-03-12 14:28:16 -07005750are then executed as usual.
5751Next, any deferred functions run by <code>F's</code> caller are run,
5752and so on up to any deferred by the top-level function in the executing goroutine.
5753At that point, the program is terminated and the error
Robert Griesemer1e8e14c2012-11-01 10:13:48 -07005754condition is reported, including the value of the argument to <code>panic</code>.
5755This termination sequence is called <i>panicking</i>.
Rob Pike5bb29fb2010-03-25 17:59:59 -07005756</p>
5757
Robert Griesemer76f32282011-02-04 08:43:21 -08005758<pre>
5759panic(42)
5760panic("unreachable")
5761panic(Error("cannot parse"))
5762</pre>
5763
Rob Pike5bb29fb2010-03-25 17:59:59 -07005764<p>
5765The <code>recover</code> function allows a program to manage behavior
Rob Pikec34050f2013-03-12 14:28:16 -07005766of a panicking goroutine.
5767Suppose a function <code>G</code> defers a function <code>D</code> that calls
5768<code>recover</code> and a panic occurs in a function on the same goroutine in which <code>G</code>
5769is executing.
5770When the running of deferred functions reaches <code>D</code>,
5771the return value of <code>D</code>'s call to <code>recover</code> will be the value passed to the call of <code>panic</code>.
5772If <code>D</code> returns normally, without starting a new
5773<code>panic</code>, the panicking sequence stops. In that case,
5774the state of functions called between <code>G</code> and the call to <code>panic</code>
5775is discarded, and normal execution resumes.
5776Any functions deferred by <code>G</code> before <code>D</code> are then run and <code>G</code>'s
5777execution terminates by returning to its caller.
Rob Pike5bb29fb2010-03-25 17:59:59 -07005778</p>
5779
5780<p>
Rob Pikec34050f2013-03-12 14:28:16 -07005781The return value of <code>recover</code> is <code>nil</code> if any of the following conditions holds:
5782</p>
5783<ul>
5784<li>
5785<code>panic</code>'s argument was <code>nil</code>;
5786</li>
5787<li>
5788the goroutine is not panicking;
5789</li>
5790<li>
5791<code>recover</code> was not called directly by a deferred function.
5792</li>
5793</ul>
5794
5795<p>
Robert Griesemer76f32282011-02-04 08:43:21 -08005796The <code>protect</code> function in the example below invokes
5797the function argument <code>g</code> and protects callers from
5798run-time panics raised by <code>g</code>.
Rob Pike5bb29fb2010-03-25 17:59:59 -07005799</p>
5800
5801<pre>
Robert Griesemer76f32282011-02-04 08:43:21 -08005802func protect(g func()) {
Rob Pike5bb29fb2010-03-25 17:59:59 -07005803 defer func() {
Charles L. Dorian44262d12011-11-01 15:13:33 +09005804 log.Println("done") // Println executes normally even if there is a panic
Rob Pike5bb29fb2010-03-25 17:59:59 -07005805 if x := recover(); x != nil {
Rob Pike966bf712011-03-01 13:54:22 -08005806 log.Printf("run time panic: %v", x)
Rob Pike5bb29fb2010-03-25 17:59:59 -07005807 }
Rob Pikee6b1d422011-04-05 11:01:25 -07005808 }()
Robert Griesemer76f32282011-02-04 08:43:21 -08005809 log.Println("start")
5810 g()
Rob Pike5bb29fb2010-03-25 17:59:59 -07005811}
5812</pre>
5813
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08005814
Robert Griesemer164a7bc2009-09-30 12:00:25 -07005815<h3 id="Bootstrapping">Bootstrapping</h3>
5816
Robert Griesemer5eb36242009-09-16 11:05:14 -07005817<p>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07005818Current implementations provide several built-in functions useful during
5819bootstrapping. These functions are documented for completeness but are not
5820guaranteed to stay in the language. They do not return a result.
Robert Griesemer5eb36242009-09-16 11:05:14 -07005821</p>
5822
Robert Griesemer164a7bc2009-09-30 12:00:25 -07005823<pre class="grammar">
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07005824Function Behavior
Robert Griesemer164a7bc2009-09-30 12:00:25 -07005825
5826print prints all arguments; formatting of arguments is implementation-specific
5827println like print but prints spaces between arguments and a newline at the end
Robert Griesemer164a7bc2009-09-30 12:00:25 -07005828</pre>
5829
5830
Russ Cox7c4f7cc2009-08-20 11:11:03 -07005831<h2 id="Packages">Packages</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005832
Rob Pike46596852009-03-02 16:17:29 -08005833<p>
5834Go programs are constructed by linking together <i>packages</i>.
Robert Griesemer326ef132009-09-28 19:21:15 -07005835A package in turn is constructed from one or more source files
5836that together declare constants, types, variables and functions
5837belonging to the package and which are accessible in all files
5838of the same package. Those elements may be
5839<a href="#Exported_identifiers">exported</a> and used in another package.
Rob Pike46596852009-03-02 16:17:29 -08005840</p>
5841
Russ Cox7c4f7cc2009-08-20 11:11:03 -07005842<h3 id="Source_file_organization">Source file organization</h3>
Rob Pike46596852009-03-02 16:17:29 -08005843
5844<p>
5845Each source file consists of a package clause defining the package
5846to which it belongs, followed by a possibly empty set of import
5847declarations that declare packages whose contents it wishes to use,
5848followed by a possibly empty set of declarations of functions,
Robert Griesemer0a162a12009-08-19 16:44:04 -07005849types, variables, and constants.
Rob Pike46596852009-03-02 16:17:29 -08005850</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005851
Robert Griesemerf7ac31362009-07-10 16:06:40 -07005852<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -08005853SourceFile = PackageClause ";" { ImportDecl ";" } { TopLevelDecl ";" } .
Robert Griesemerc2d55862009-02-19 16:49:10 -08005854</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005855
Russ Cox7c4f7cc2009-08-20 11:11:03 -07005856<h3 id="Package_clause">Package clause</h3>
Rob Pike46596852009-03-02 16:17:29 -08005857
Robert Griesemerc2d55862009-02-19 16:49:10 -08005858<p>
Rob Pike46596852009-03-02 16:17:29 -08005859A package clause begins each source file and defines the package
5860to which the file belongs.
5861</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005862
Robert Griesemerf7ac31362009-07-10 16:06:40 -07005863<pre class="ebnf">
Robert Griesemer4e56b332009-09-10 10:14:00 -07005864PackageClause = "package" PackageName .
5865PackageName = identifier .
Robert Griesemerc2d55862009-02-19 16:49:10 -08005866</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005867
Robert Griesemer4e56b332009-09-10 10:14:00 -07005868<p>
5869The PackageName must not be the <a href="#Blank_identifier">blank identifier</a>.
5870</p>
5871
Rob Pike46596852009-03-02 16:17:29 -08005872<pre>
5873package math
5874</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005875
Rob Pike46596852009-03-02 16:17:29 -08005876<p>
5877A set of files sharing the same PackageName form the implementation of a package.
5878An implementation may require that all source files for a package inhabit the same directory.
5879</p>
5880
Russ Cox7c4f7cc2009-08-20 11:11:03 -07005881<h3 id="Import_declarations">Import declarations</h3>
Rob Pike46596852009-03-02 16:17:29 -08005882
5883<p>
Robert Griesemer103c9db2012-03-01 13:57:49 -08005884An import declaration states that the source file containing the declaration
5885depends on functionality of the <i>imported</i> package
5886(<a href="#Program_initialization_and_execution">§Program initialization and execution</a>)
Rob Pikeb51ad9c2012-09-23 05:03:43 +10005887and enables access to <a href="#Exported_identifiers">exported</a> identifiers
Robert Griesemer103c9db2012-03-01 13:57:49 -08005888of that package.
5889The import names an identifier (PackageName) to be used for access and an ImportPath
Rob Pike3aec2e42009-09-25 17:00:22 -07005890that specifies the package to be imported.
Rob Pike46596852009-03-02 16:17:29 -08005891</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005892
Robert Griesemerf7ac31362009-07-10 16:06:40 -07005893<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -08005894ImportDecl = "import" ( ImportSpec | "(" { ImportSpec ";" } ")" ) .
Robert Griesemer997851e2009-09-25 15:36:25 -07005895ImportSpec = [ "." | PackageName ] ImportPath .
Robert Griesemer130ac742009-12-10 16:43:01 -08005896ImportPath = string_lit .
Robert Griesemerc2d55862009-02-19 16:49:10 -08005897</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005898
Robert Griesemerc2d55862009-02-19 16:49:10 -08005899<p>
Rob Pike3aec2e42009-09-25 17:00:22 -07005900The PackageName is used in <a href="#Qualified_identifiers">qualified identifiers</a>
Robert Griesemer103c9db2012-03-01 13:57:49 -08005901to access exported identifiers of the package within the importing source file.
Rob Pike3aec2e42009-09-25 17:00:22 -07005902It is declared in the <a href="#Blocks">file block</a>.
5903If the PackageName is omitted, it defaults to the identifier specified in the
Robert Griesemer556506e2011-02-22 09:34:13 -08005904<a href="#Package_clause">package clause</a> of the imported package.
Rob Pike3aec2e42009-09-25 17:00:22 -07005905If an explicit period (<code>.</code>) appears instead of a name, all the
Robert Griesemer103c9db2012-03-01 13:57:49 -08005906package's exported identifiers declared in that package's
5907<a href="#Blocks">package block</a> will be declared in the importing source
Rob Pike227fe5f2014-01-14 15:16:01 -08005908file's file block and must be accessed without a qualifier.
Rob Pike46596852009-03-02 16:17:29 -08005909</p>
Russ Cox16b95ba2009-08-20 10:22:52 -07005910
Robert Griesemerc2d55862009-02-19 16:49:10 -08005911<p>
Rob Pike3aec2e42009-09-25 17:00:22 -07005912The interpretation of the ImportPath is implementation-dependent but
5913it is typically a substring of the full file name of the compiled
5914package and may be relative to a repository of installed packages.
5915</p>
5916
5917<p>
Robert Griesemerac4055b2012-02-22 23:51:25 -08005918Implementation restriction: A compiler may restrict ImportPaths to
5919non-empty strings using only characters belonging to
Rob Pikee6863e72014-02-25 14:15:49 -08005920<a href="http://www.unicode.org/versions/Unicode6.3.0/">Unicode's</a>
Robert Griesemerac4055b2012-02-22 23:51:25 -08005921L, M, N, P, and S general categories (the Graphic characters without
Russ Coxfad10f92012-02-23 22:46:04 -05005922spaces) and may also exclude the characters
5923<code>!"#$%&amp;'()*,:;&lt;=&gt;?[\]^`{|}</code>
5924and the Unicode replacement character U+FFFD.
Robert Griesemerac4055b2012-02-22 23:51:25 -08005925</p>
5926
5927<p>
Rob Pike3aec2e42009-09-25 17:00:22 -07005928Assume we have compiled a package containing the package clause
5929<code>package math</code>, which exports function <code>Sin</code>, and
5930installed the compiled package in the file identified by
Rob Pike46596852009-03-02 16:17:29 -08005931<code>"lib/math"</code>.
Rob Pike227fe5f2014-01-14 15:16:01 -08005932This table illustrates how <code>Sin</code> is accessed in files
Rob Pike3aec2e42009-09-25 17:00:22 -07005933that import the package after the
5934various types of import declaration.
Rob Pike46596852009-03-02 16:17:29 -08005935</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005936
Rob Pike46596852009-03-02 16:17:29 -08005937<pre class="grammar">
Robert Griesemer997851e2009-09-25 15:36:25 -07005938Import declaration Local name of Sin
Rob Pike46596852009-03-02 16:17:29 -08005939
Rob Pike46596852009-03-02 16:17:29 -08005940import "lib/math" math.Sin
Rob Pikeb51ad9c2012-09-23 05:03:43 +10005941import m "lib/math" m.Sin
Rob Pike46596852009-03-02 16:17:29 -08005942import . "lib/math" Sin
5943</pre>
5944
Robert Griesemer997851e2009-09-25 15:36:25 -07005945<p>
Rob Pike3aec2e42009-09-25 17:00:22 -07005946An import declaration declares a dependency relation between
5947the importing and imported package.
Robert Griesemer4be38dd2013-03-04 12:59:40 -08005948It is illegal for a package to import itself, directly or indirectly,
5949or to directly import a package without
Robert Griesemer997851e2009-09-25 15:36:25 -07005950referring to any of its exported identifiers. To import a package solely for
5951its side-effects (initialization), use the <a href="#Blank_identifier">blank</a>
5952identifier as explicit package name:
5953</p>
5954
5955<pre>
5956import _ "lib/math"
5957</pre>
5958
5959
Russ Cox7c4f7cc2009-08-20 11:11:03 -07005960<h3 id="An_example_package">An example package</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005961
Robert Griesemerc2d55862009-02-19 16:49:10 -08005962<p>
Rob Pike46596852009-03-02 16:17:29 -08005963Here is a complete Go package that implements a concurrent prime sieve.
5964</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005965
Robert Griesemerc2d55862009-02-19 16:49:10 -08005966<pre>
5967package main
5968
Rob Pike46596852009-03-02 16:17:29 -08005969import "fmt"
5970
Robert Griesemer3c7271f2011-05-24 14:18:44 -07005971// Send the sequence 2, 3, 4, … to channel 'ch'.
Robert Griesemere1e76192009-09-25 14:11:03 -07005972func generate(ch chan&lt;- int) {
Robert Griesemerc2d55862009-02-19 16:49:10 -08005973 for i := 2; ; i++ {
Robert Griesemer130ac742009-12-10 16:43:01 -08005974 ch &lt;- i // Send 'i' to channel 'ch'.
Robert Griesemerc2d55862009-02-19 16:49:10 -08005975 }
5976}
5977
Fazlul Shahriar330139e2009-11-30 21:23:58 -08005978// Copy the values from channel 'src' to channel 'dst',
Robert Griesemerc2d55862009-02-19 16:49:10 -08005979// removing those divisible by 'prime'.
Robert Griesemere1e76192009-09-25 14:11:03 -07005980func filter(src &lt;-chan int, dst chan&lt;- int, prime int) {
David Symonds72a29792011-11-29 15:47:36 -08005981 for i := range src { // Loop over values received from 'src'.
Robert Griesemere1e76192009-09-25 14:11:03 -07005982 if i%prime != 0 {
Robert Griesemer130ac742009-12-10 16:43:01 -08005983 dst &lt;- i // Send 'i' to channel 'dst'.
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005984 }
5985 }
Robert Griesemerc2d55862009-02-19 16:49:10 -08005986}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005987
Robert Griesemerc2d55862009-02-19 16:49:10 -08005988// The prime sieve: Daisy-chain filter processes together.
5989func sieve() {
Robert Griesemer130ac742009-12-10 16:43:01 -08005990 ch := make(chan int) // Create a new channel.
5991 go generate(ch) // Start generate() as a subprocess.
Robert Griesemerc2d55862009-02-19 16:49:10 -08005992 for {
Robert Griesemer130ac742009-12-10 16:43:01 -08005993 prime := &lt;-ch
5994 fmt.Print(prime, "\n")
5995 ch1 := make(chan int)
5996 go filter(ch, ch1, prime)
5997 ch = ch1
Robert Griesemerc2d55862009-02-19 16:49:10 -08005998 }
5999}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07006000
Robert Griesemerc2d55862009-02-19 16:49:10 -08006001func main() {
Robert Griesemer130ac742009-12-10 16:43:01 -08006002 sieve()
Robert Griesemerc2d55862009-02-19 16:49:10 -08006003}
6004</pre>
Robert Griesemer67153582008-12-16 14:45:09 -08006005
Russ Cox7c4f7cc2009-08-20 11:11:03 -07006006<h2 id="Program_initialization_and_execution">Program initialization and execution</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07006007
Russ Cox7c4f7cc2009-08-20 11:11:03 -07006008<h3 id="The_zero_value">The zero value</h3>
Rob Pike8f2330d2009-02-25 16:20:44 -08006009<p>
Robert Griesemer6962c152014-10-16 15:08:49 -07006010When storage is allocated for a <a href="#Variables">variable</a>,
6011either through a declaration or a call of <code>new</code>, or when
6012a new value is created, either through a composite literal or a call
6013of <code>make</code>,
6014and no explicit initialization is provided, the variable or value is
6015given a default value. Each element of such a variable or value is
Robert Griesemeref45e642009-08-21 11:25:00 -07006016set to the <i>zero value</i> for its type: <code>false</code> for booleans,
Rob Pike8f2330d2009-02-25 16:20:44 -08006017<code>0</code> for integers, <code>0.0</code> for floats, <code>""</code>
Robert Griesemer19b1d352009-09-24 19:36:48 -07006018for strings, and <code>nil</code> for pointers, functions, interfaces, slices, channels, and maps.
Rob Pikeff70f092009-02-20 13:36:14 -08006019This initialization is done recursively, so for instance each element of an
Rob Pike8f2330d2009-02-25 16:20:44 -08006020array of structs will have its fields zeroed if no value is specified.
6021</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08006022<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07006023These two simple declarations are equivalent:
Rob Pike8f2330d2009-02-25 16:20:44 -08006024</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07006025
Robert Griesemerc2d55862009-02-19 16:49:10 -08006026<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08006027var i int
6028var i int = 0
Robert Griesemerc2d55862009-02-19 16:49:10 -08006029</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07006030
Rob Pike8f2330d2009-02-25 16:20:44 -08006031<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07006032After
Rob Pike8f2330d2009-02-25 16:20:44 -08006033</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07006034
Robert Griesemerc2d55862009-02-19 16:49:10 -08006035<pre>
Robert Griesemerb94c0d22011-01-19 23:07:21 -05006036type T struct { i int; f float64; next *T }
Robert Griesemer130ac742009-12-10 16:43:01 -08006037t := new(T)
Robert Griesemerc2d55862009-02-19 16:49:10 -08006038</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07006039
Rob Pike8f2330d2009-02-25 16:20:44 -08006040<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07006041the following holds:
Rob Pike8f2330d2009-02-25 16:20:44 -08006042</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07006043
Robert Griesemerc2d55862009-02-19 16:49:10 -08006044<pre>
6045t.i == 0
6046t.f == 0.0
6047t.next == nil
6048</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07006049
Rob Pike96750f12009-02-27 16:47:48 -08006050<p>
6051The same would also be true after
6052</p>
6053
6054<pre>
6055var t T
6056</pre>
6057
Robert Griesemera4366982014-05-20 13:51:39 -07006058<h3 id="Package_initialization">Package initialization</h3>
Robert Griesemer259f0ff2014-09-29 12:44:50 -07006059
Rob Pike8f2330d2009-02-25 16:20:44 -08006060<p>
Robert Griesemer259f0ff2014-09-29 12:44:50 -07006061Within a package, package-level variables are initialized in
6062<i>declaration order</i> but after any of the variables
6063they <i>depend</i> on.
6064</p>
6065
6066<p>
6067More precisely, a package-level variable is considered <i>ready for
6068initialization</i> if it is not yet initialized and either has
6069no <a href="#Variable_declarations">initialization expression</a> or
6070its initialization expression has no dependencies on uninitialized variables.
6071Initialization proceeds by repeatedly initializing the next package-level
6072variable that is earliest in declaration order and ready for initialization,
6073until there are no variables ready for initialization.
6074</p>
6075
6076<p>
6077If any variables are still uninitialized when this
6078process ends, those variables are part of one or more initialization cycles,
6079and the program is not valid.
6080</p>
6081
6082<p>
6083The declaration order of variables declared in multiple files is determined
6084by the order in which the files are presented to the compiler: Variables
6085declared in the first file are declared before any of the variables declared
6086in the second file, and so on.
Rob Pike96750f12009-02-27 16:47:48 -08006087</p>
Robert Griesemera4366982014-05-20 13:51:39 -07006088
6089<p>
6090Dependency analysis does not rely on the actual values of the
6091variables, only on lexical <i>references</i> to them in the source,
Robert Griesemer259f0ff2014-09-29 12:44:50 -07006092analyzed transitively. For instance, if a variable <code>x</code>'s
6093initialization expression refers to a function whose body refers to
6094variable <code>y</code> then <code>x</code> depends on <code>y</code>.
Robert Griesemera4366982014-05-20 13:51:39 -07006095Specifically:
6096</p>
6097
6098<ul>
6099<li>
6100A reference to a variable or function is an identifier denoting that
6101variable or function.
6102</li>
6103
6104<li>
6105A reference to a method <code>m</code> is a
6106<a href="#Method_values">method value</a> or
Robin Eklindcac006a2014-08-30 10:27:01 -07006107<a href="#Method_expressions">method expression</a> of the form
Robert Griesemera4366982014-05-20 13:51:39 -07006108<code>t.m</code>, where the (static) type of <code>t</code> is
6109not an interface type, and the method <code>m</code> is in the
6110<a href="#Method_sets">method set</a> of <code>t</code>.
6111It is immaterial whether the resulting function value
6112<code>t.m</code> is invoked.
6113</li>
6114
6115<li>
Robin Eklindcac006a2014-08-30 10:27:01 -07006116A variable, function, or method <code>x</code> depends on a variable
Robert Griesemera4366982014-05-20 13:51:39 -07006117<code>y</code> if <code>x</code>'s initialization expression or body
6118(for functions and methods) contains a reference to <code>y</code>
6119or to a function or method that depends on <code>y</code>.
6120</li>
6121</ul>
6122
6123<p>
6124Dependency analysis is performed per package; only references referring
6125to variables, functions, and methods declared in the current package
6126are considered.
Robert Griesemera4366982014-05-20 13:51:39 -07006127</p>
6128
6129<p>
6130For example, given the declarations
6131</p>
6132
Rob Pike96750f12009-02-27 16:47:48 -08006133<pre>
Robert Griesemera4366982014-05-20 13:51:39 -07006134var (
6135 a = c + b
6136 b = f()
6137 c = f()
6138 d = 3
6139)
6140
6141func f() int {
6142 d++
6143 return d
6144}
Rob Pike96750f12009-02-27 16:47:48 -08006145</pre>
Robert Griesemera4366982014-05-20 13:51:39 -07006146
Rob Pike96750f12009-02-27 16:47:48 -08006147<p>
Robert Griesemera4366982014-05-20 13:51:39 -07006148the initialization order is <code>d</code>, <code>b</code>, <code>c</code>, <code>a</code>.
Rob Pike8f2330d2009-02-25 16:20:44 -08006149</p>
Robert Griesemera4366982014-05-20 13:51:39 -07006150
Robert Griesemerc2d55862009-02-19 16:49:10 -08006151<p>
Robert Griesemera4366982014-05-20 13:51:39 -07006152Variables may also be initialized using functions named <code>init</code>
6153declared in the package block, with no arguments and no result parameters.
Rob Pike8cb91842009-09-15 11:56:39 -07006154</p>
Robert Griesemera4366982014-05-20 13:51:39 -07006155
6156<pre>
6157func init() { … }
6158</pre>
6159
Rob Pike8cb91842009-09-15 11:56:39 -07006160<p>
Robin Eklindcac006a2014-08-30 10:27:01 -07006161Multiple such functions may be defined, even within a single
Robert Griesemera4366982014-05-20 13:51:39 -07006162source file. The <code>init</code> identifier is not
6163<a href="#Declarations_and_scope">declared</a> and thus
6164<code>init</code> functions cannot be referred to from anywhere
6165in a program.
Rob Pike8f2330d2009-02-25 16:20:44 -08006166</p>
Robert Griesemera4366982014-05-20 13:51:39 -07006167
Robert Griesemerc2d55862009-02-19 16:49:10 -08006168<p>
Robert Griesemera4366982014-05-20 13:51:39 -07006169A package with no imports is initialized by assigning initial values
6170to all its package-level variables followed by calling all <code>init</code>
Robert Griesemerc00043b2014-05-20 17:46:08 -07006171functions in the order they appear in the source, possibly in multiple files,
6172as presented to the compiler.
Robert Griesemerdf49fb32008-08-28 17:47:53 -07006173If a package has imports, the imported packages are initialized
Robert Griesemer566e3b22008-09-26 16:41:50 -07006174before initializing the package itself. If multiple packages import
Robert Griesemera4366982014-05-20 13:51:39 -07006175a package, the imported package will be initialized only once.
6176The importing of packages, by construction, guarantees that there
6177can be no cyclic initialization dependencies.
Rob Pike8f2330d2009-02-25 16:20:44 -08006178</p>
Robert Griesemera4366982014-05-20 13:51:39 -07006179
Robert Griesemerc2d55862009-02-19 16:49:10 -08006180<p>
Robert Griesemera4366982014-05-20 13:51:39 -07006181Package initialization&mdash;variable initialization and the invocation of
6182<code>init</code> functions&mdash;happens in a single goroutine,
6183sequentially, one package at a time.
6184An <code>init</code> function may launch other goroutines, which can run
6185concurrently with the initialization code. However, initialization
6186always sequences
6187the <code>init</code> functions: it will not invoke the next one
6188until the previous one has returned.
Rob Pike8f2330d2009-02-25 16:20:44 -08006189</p>
Robert Griesemera4366982014-05-20 13:51:39 -07006190
Robert Griesemer259f0ff2014-09-29 12:44:50 -07006191<p>
6192To ensure reproducible initialization behavior, build systems are encouraged
6193to present multiple files belonging to the same package in lexical file name
6194order to a compiler.
6195</p>
6196
Robert Griesemera4366982014-05-20 13:51:39 -07006197
6198<h3 id="Program_execution">Program execution</h3>
Robert Griesemerc2d55862009-02-19 16:49:10 -08006199<p>
Russ Coxa6736ca2011-02-03 13:40:51 -05006200A complete program is created by linking a single, unimported package
6201called the <i>main package</i> with all the packages it imports, transitively.
6202The main package must
6203have package name <code>main</code> and
Charles L. Dorian44262d12011-11-01 15:13:33 +09006204declare a function <code>main</code> that takes no
Russ Coxa6736ca2011-02-03 13:40:51 -05006205arguments and returns no value.
Rob Pike8f2330d2009-02-25 16:20:44 -08006206</p>
Robert Griesemer4dc25282008-09-09 10:37:19 -07006207
Robert Griesemerc2d55862009-02-19 16:49:10 -08006208<pre>
Robert Griesemer3c7271f2011-05-24 14:18:44 -07006209func main() { … }
Robert Griesemerc2d55862009-02-19 16:49:10 -08006210</pre>
Robert Griesemer4dc25282008-09-09 10:37:19 -07006211
Rob Pike8f2330d2009-02-25 16:20:44 -08006212<p>
Russ Coxa6736ca2011-02-03 13:40:51 -05006213Program execution begins by initializing the main package and then
6214invoking the function <code>main</code>.
Robert Griesemer7f1d62d2014-05-19 08:54:19 -07006215When that function invocation returns, the program exits.
Russ Coxa6736ca2011-02-03 13:40:51 -05006216It does not wait for other (non-<code>main</code>) goroutines to complete.
Rob Pike811dd252009-03-04 20:39:39 -08006217</p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08006218
Russ Coxd9877e22011-11-01 21:45:02 -04006219<h2 id="Errors">Errors</h2>
6220
6221<p>
6222The predeclared type <code>error</code> is defined as
6223</p>
6224
6225<pre>
6226type error interface {
6227 Error() string
6228}
6229</pre>
6230
6231<p>
6232It is the conventional interface for representing an error condition,
6233with the nil value representing no error.
6234For instance, a function to read data from a file might be defined:
6235</p>
6236
6237<pre>
6238func Read(f *File, b []byte) (n int, err error)
6239</pre>
6240
Evan Shaw21110c72010-04-22 10:14:53 -07006241<h2 id="Run_time_panics">Run-time panics</h2>
Rob Pike5bb29fb2010-03-25 17:59:59 -07006242
6243<p>
6244Execution errors such as attempting to index an array out
6245of bounds trigger a <i>run-time panic</i> equivalent to a call of
6246the built-in function <a href="#Handling_panics"><code>panic</code></a>
6247with a value of the implementation-defined interface type <code>runtime.Error</code>.
Charles L. Dorian9a358df2011-12-08 22:27:14 -05006248That type satisfies the predeclared interface type
Russ Coxd9877e22011-11-01 21:45:02 -04006249<a href="#Errors"><code>error</code></a>.
6250The exact error values that
6251represent distinct run-time error conditions are unspecified.
Rob Pike5bb29fb2010-03-25 17:59:59 -07006252</p>
6253
6254<pre>
6255package runtime
6256
6257type Error interface {
Russ Coxd9877e22011-11-01 21:45:02 -04006258 error
6259 // and perhaps other methods
Rob Pike5bb29fb2010-03-25 17:59:59 -07006260}
6261</pre>
6262
Russ Cox7c4f7cc2009-08-20 11:11:03 -07006263<h2 id="System_considerations">System considerations</h2>
Robert Griesemer52c02c22009-02-11 13:46:30 -08006264
Russ Cox7c4f7cc2009-08-20 11:11:03 -07006265<h3 id="Package_unsafe">Package <code>unsafe</code></h3>
Robert Griesemer52c02c22009-02-11 13:46:30 -08006266
Robert Griesemerc2d55862009-02-19 16:49:10 -08006267<p>
Rob Pike96750f12009-02-27 16:47:48 -08006268The built-in package <code>unsafe</code>, known to the compiler,
6269provides facilities for low-level programming including operations
6270that violate the type system. A package using <code>unsafe</code>
Robert Griesemer5361b742014-10-23 09:45:11 -07006271must be vetted manually for type safety and may not be portable.
6272The package provides the following interface:
Rob Pikef27e9f02009-02-23 19:22:05 -08006273</p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08006274
Rob Pikeff70f092009-02-20 13:36:14 -08006275<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08006276package unsafe
Robert Griesemer52c02c22009-02-11 13:46:30 -08006277
Robert Griesemer98b4f6a2009-05-12 21:37:46 -07006278type ArbitraryType int // shorthand for an arbitrary Go type; it is not a real type
6279type Pointer *ArbitraryType
Robert Griesemer52c02c22009-02-11 13:46:30 -08006280
Robert Griesemereee70b02011-06-13 16:46:42 -07006281func Alignof(variable ArbitraryType) uintptr
Hong Ruiqi8374e672012-04-05 22:37:07 +10006282func Offsetof(selector ArbitraryType) uintptr
Robert Griesemereee70b02011-06-13 16:46:42 -07006283func Sizeof(variable ArbitraryType) uintptr
Robert Griesemerc2d55862009-02-19 16:49:10 -08006284</pre>
Robert Griesemer52c02c22009-02-11 13:46:30 -08006285
Robert Griesemerc2d55862009-02-19 16:49:10 -08006286<p>
Robert Griesemere121de22013-10-07 10:43:28 -07006287A <code>Pointer</code> is a <a href="#Pointer_types">pointer type</a> but a <code>Pointer</code>
6288value may not be <a href="#Address_operators">dereferenced</a>.
Robert Griesemer5361b742014-10-23 09:45:11 -07006289Any pointer or value of <a href="#Types">underlying type</a> <code>uintptr</code> can be converted to
6290a <code>Pointer</code> type and vice versa.
6291The effect of converting between <code>Pointer</code> and <code>uintptr</code> is implementation-defined.
Rob Pikef27e9f02009-02-23 19:22:05 -08006292</p>
Russ Cox81eb9302013-02-09 17:36:31 -05006293
6294<pre>
6295var f float64
6296bits = *(*uint64)(unsafe.Pointer(&amp;f))
6297
6298type ptr unsafe.Pointer
6299bits = *(*uint64)(ptr(&amp;f))
Robert Griesemere121de22013-10-07 10:43:28 -07006300
6301var p ptr = nil
Russ Cox81eb9302013-02-09 17:36:31 -05006302</pre>
6303
Robert Griesemerc2d55862009-02-19 16:49:10 -08006304<p>
Robert Griesemerc7631f52012-09-17 12:23:41 -07006305The functions <code>Alignof</code> and <code>Sizeof</code> take an expression <code>x</code>
6306of any type and return the alignment or size, respectively, of a hypothetical variable <code>v</code>
6307as if <code>v</code> was declared via <code>var v = x</code>.
Rob Pikef27e9f02009-02-23 19:22:05 -08006308</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08006309<p>
Robert Griesemer8c058b32012-09-18 11:25:53 -07006310The function <code>Offsetof</code> takes a (possibly parenthesized) <a href="#Selectors">selector</a>
Robert Griesemer51338092013-03-07 20:11:37 -08006311<code>s.f</code>, denoting a field <code>f</code> of the struct denoted by <code>s</code>
6312or <code>*s</code>, and returns the field offset in bytes relative to the struct's address.
6313If <code>f</code> is an <a href="#Struct_types">embedded field</a>, it must be reachable
6314without pointer indirections through fields of the struct.
Rob Pike678625d2009-09-15 09:54:22 -07006315For a struct <code>s</code> with field <code>f</code>:
Rob Pikef27e9f02009-02-23 19:22:05 -08006316</p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08006317
Robert Griesemerc2d55862009-02-19 16:49:10 -08006318<pre>
Robert Griesemereee70b02011-06-13 16:46:42 -07006319uintptr(unsafe.Pointer(&amp;s)) + unsafe.Offsetof(s.f) == uintptr(unsafe.Pointer(&amp;s.f))
Robert Griesemerc2d55862009-02-19 16:49:10 -08006320</pre>
Robert Griesemer52c02c22009-02-11 13:46:30 -08006321
Robert Griesemerc2d55862009-02-19 16:49:10 -08006322<p>
Rob Pikef27e9f02009-02-23 19:22:05 -08006323Computer architectures may require memory addresses to be <i>aligned</i>;
6324that is, for addresses of a variable to be a multiple of a factor,
6325the variable's type's <i>alignment</i>. The function <code>Alignof</code>
6326takes an expression denoting a variable of any type and returns the
6327alignment of the (type of the) variable in bytes. For a variable
6328<code>x</code>:
6329</p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08006330
Robert Griesemerc2d55862009-02-19 16:49:10 -08006331<pre>
Robert Griesemereee70b02011-06-13 16:46:42 -07006332uintptr(unsafe.Pointer(&amp;x)) % unsafe.Alignof(x) == 0
Robert Griesemerc2d55862009-02-19 16:49:10 -08006333</pre>
Robert Griesemer533dfd62009-05-13 16:56:00 -07006334
Rob Pikef27e9f02009-02-23 19:22:05 -08006335<p>
6336Calls to <code>Alignof</code>, <code>Offsetof</code>, and
Robert Griesemereee70b02011-06-13 16:46:42 -07006337<code>Sizeof</code> are compile-time constant expressions of type <code>uintptr</code>.
Rob Pikef27e9f02009-02-23 19:22:05 -08006338</p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08006339
Russ Cox7c4f7cc2009-08-20 11:11:03 -07006340<h3 id="Size_and_alignment_guarantees">Size and alignment guarantees</h3>
Robert Griesemer52c02c22009-02-11 13:46:30 -08006341
Robert Griesemer997851e2009-09-25 15:36:25 -07006342<p>
Robert Griesemer462a17e2013-03-22 15:36:04 -07006343For the <a href="#Numeric_types">numeric types</a>, the following sizes are guaranteed:
Robert Griesemer997851e2009-09-25 15:36:25 -07006344</p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08006345
Rob Pikeff70f092009-02-20 13:36:14 -08006346<pre class="grammar">
Robert Griesemer777a96a2010-12-02 12:32:14 -08006347type size in bytes
Robert Griesemer52c02c22009-02-11 13:46:30 -08006348
Robert Griesemer777a96a2010-12-02 12:32:14 -08006349byte, uint8, int8 1
6350uint16, int16 2
6351uint32, int32, float32 4
6352uint64, int64, float64, complex64 8
6353complex128 16
Robert Griesemerc2d55862009-02-19 16:49:10 -08006354</pre>
Robert Griesemer52c02c22009-02-11 13:46:30 -08006355
Robert Griesemerc2d55862009-02-19 16:49:10 -08006356<p>
Rob Pikef27e9f02009-02-23 19:22:05 -08006357The following minimal alignment properties are guaranteed:
Rob Pike4501d342009-02-19 17:31:36 -08006358</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08006359<ol>
Robert Griesemerdd916be2011-01-10 14:25:17 -08006360<li>For a variable <code>x</code> of any type: <code>unsafe.Alignof(x)</code> is at least 1.
Robert Griesemer3af480372010-05-14 13:11:48 -07006361</li>
Robert Griesemer52c02c22009-02-11 13:46:30 -08006362
Rob Pikef27e9f02009-02-23 19:22:05 -08006363<li>For a variable <code>x</code> of struct type: <code>unsafe.Alignof(x)</code> is the largest of
Robert Griesemerdd916be2011-01-10 14:25:17 -08006364 all the values <code>unsafe.Alignof(x.f)</code> for each field <code>f</code> of <code>x</code>, but at least 1.
Robert Griesemer3af480372010-05-14 13:11:48 -07006365</li>
Robert Griesemer52c02c22009-02-11 13:46:30 -08006366
Rob Pikef27e9f02009-02-23 19:22:05 -08006367<li>For a variable <code>x</code> of array type: <code>unsafe.Alignof(x)</code> is the same as
6368 <code>unsafe.Alignof(x[0])</code>, but at least 1.
Robert Griesemer3af480372010-05-14 13:11:48 -07006369</li>
Robert Griesemerc2d55862009-02-19 16:49:10 -08006370</ol>
Robert Griesemer52c02c22009-02-11 13:46:30 -08006371
Robert Griesemer1320ce02012-01-09 16:54:24 -08006372<p>
6373A 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.
6374</p>