blob: 82c7ed419c6ded838c4aacd3d510ec8652ceb0c0 [file] [log] [blame]
Robert Griesemere8e49872010-03-30 17:37:42 -07001<!-- title The Go Programming Language Specification -->
Robert Griesemer38a53c72011-07-14 15:58:37 -07002<!-- subtitle Version of July 14, 2011 -->
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003
Robert Griesemerc59d2f12008-09-09 10:48:14 -07004<!--
Robert Griesemer1d282a82010-06-03 16:55:50 -07005TODO
Robert Griesemer4b908332009-08-07 17:05:41 -07006[ ] need language about function/method calls and parameter passing rules
Rob Pike72970872010-03-04 12:35:16 -08007[ ] last paragraph of #Assignments (constant promotion) should be elsewhere
8 and mention assignment to empty interface.
Robert Griesemer0a162a12009-08-19 16:44:04 -07009[ ] need to say something about "scope" of selectors?
Robert Griesemer4b908332009-08-07 17:05:41 -070010[ ] clarify what a field name is in struct declarations
11 (struct{T} vs struct {T T} vs struct {t T})
Robert Griesemer7539c852009-07-31 18:05:07 -070012[ ] need explicit language about the result type of operations
Robert Griesemer40d6bb52009-04-20 15:32:20 -070013[ ] should probably write something about evaluation order of statements even
14 though obvious
Robert Griesemere1b8cb82009-07-16 20:31:41 -070015[ ] review language on implicit dereferencing
Robert Griesemer1d282a82010-06-03 16:55:50 -070016[ ] clarify what it means for two functions to be "the same" when comparing them
Robert Griesemerc59d2f12008-09-09 10:48:14 -070017-->
18
Robert Griesemer40d6bb52009-04-20 15:32:20 -070019
Russ Cox7c4f7cc2009-08-20 11:11:03 -070020<h2 id="Introduction">Introduction</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -070021
Robert Griesemerc2d55862009-02-19 16:49:10 -080022<p>
Rob Pike4501d342009-02-19 17:31:36 -080023This is a reference manual for the Go programming language. For
Rob Pike9339e072009-11-02 15:28:41 -080024more information and other documents, see <a href="http://golang.org/">http://golang.org</a>.
Rob Pike4501d342009-02-19 17:31:36 -080025</p>
Robert Griesemer67153582008-12-16 14:45:09 -080026
Robert Griesemerc2d55862009-02-19 16:49:10 -080027<p>
Rob Pike4501d342009-02-19 17:31:36 -080028Go is a general-purpose language designed with systems programming
Rob Pike678625d2009-09-15 09:54:22 -070029in mind. It is strongly typed and garbage-collected and has explicit
Rob Pike4501d342009-02-19 17:31:36 -080030support for concurrent programming. Programs are constructed from
31<i>packages</i>, whose properties allow efficient management of
32dependencies. The existing implementations use a traditional
33compile/link model to generate executable binaries.
34</p>
35
Robert Griesemerc2d55862009-02-19 16:49:10 -080036<p>
Rob Pike4501d342009-02-19 17:31:36 -080037The grammar is compact and regular, allowing for easy analysis by
38automatic tools such as integrated development environments.
39</p>
Larry Hosken698c6c02009-09-17 08:05:12 -070040
Russ Cox7c4f7cc2009-08-20 11:11:03 -070041<h2 id="Notation">Notation</h2>
Rob Pike4501d342009-02-19 17:31:36 -080042<p>
Robert Griesemer4d230302008-12-17 15:39:15 -080043The syntax is specified using Extended Backus-Naur Form (EBNF):
Rob Pike4501d342009-02-19 17:31:36 -080044</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -070045
Rob Pikeff70f092009-02-20 13:36:14 -080046<pre class="grammar">
Robert Griesemer32b822f2011-05-13 12:54:51 -070047Production = production_name "=" [ Expression ] "." .
Rob Pike4501d342009-02-19 17:31:36 -080048Expression = Alternative { "|" Alternative } .
Robert Griesemerc2d55862009-02-19 16:49:10 -080049Alternative = Term { Term } .
Robert Griesemer3c7271f2011-05-24 14:18:44 -070050Term = production_name | token [ "…" token ] | Group | Option | Repetition .
Rob Pike4501d342009-02-19 17:31:36 -080051Group = "(" Expression ")" .
Rob Pikec956e902009-04-14 20:10:49 -070052Option = "[" Expression "]" .
Rob Pike4501d342009-02-19 17:31:36 -080053Repetition = "{" Expression "}" .
Robert Griesemerc2d55862009-02-19 16:49:10 -080054</pre>
Robert Griesemerbbfe3122008-10-09 17:12:09 -070055
Rob Pike4501d342009-02-19 17:31:36 -080056<p>
57Productions are expressions constructed from terms and the following
58operators, in increasing precedence:
59</p>
Rob Pikeff70f092009-02-20 13:36:14 -080060<pre class="grammar">
Rob Pike4501d342009-02-19 17:31:36 -080061| alternation
62() grouping
63[] option (0 or 1 times)
64{} repetition (0 to n times)
Robert Griesemerc2d55862009-02-19 16:49:10 -080065</pre>
Robert Griesemer4d230302008-12-17 15:39:15 -080066
Robert Griesemerc2d55862009-02-19 16:49:10 -080067<p>
Rob Pike4501d342009-02-19 17:31:36 -080068Lower-case production names are used to identify lexical tokens.
69Non-terminals are in CamelCase. Lexical symbols are enclosed in
Rob Pike678625d2009-09-15 09:54:22 -070070double quotes <code>""</code> or back quotes <code>``</code>.
Rob Pike4501d342009-02-19 17:31:36 -080071</p>
72
Robert Griesemerc2d55862009-02-19 16:49:10 -080073<p>
Robert Griesemer3c7271f2011-05-24 14:18:44 -070074The form <code>a … b</code> represents the set of characters from
75<code>a</code> through <code>b</code> as alternatives. The horizontal
76ellipis … is also used elsewhere in the spec to informally denote various
77enumerations or code snippets that are not further specified. The character …
78(as opposed to the three characters <code>...</code>) is not a token of the Go
79language.
Rob Pike4501d342009-02-19 17:31:36 -080080</p>
81
Russ Cox7c4f7cc2009-08-20 11:11:03 -070082<h2 id="Source_code_representation">Source code representation</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -070083
Robert Griesemerc2d55862009-02-19 16:49:10 -080084<p>
Robert Griesemere9192752009-12-01 16:15:53 -080085Source code is Unicode text encoded in
86<a href="http://en.wikipedia.org/wiki/UTF-8">UTF-8</a>. The text is not
Rob Pikeff70f092009-02-20 13:36:14 -080087canonicalized, so a single accented code point is distinct from the
88same character constructed from combining an accent and a letter;
89those are treated as two code points. For simplicity, this document
90will use the term <i>character</i> to refer to a Unicode code point.
91</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -080092<p>
Rob Pikeff70f092009-02-20 13:36:14 -080093Each code point is distinct; for instance, upper and lower case letters
94are different characters.
95</p>
Russ Coxb7d9ffe2010-02-16 16:47:18 -080096<p>
Robert Griesemerf42e8832010-02-17 15:50:34 -080097Implementation restriction: For compatibility with other tools, a
98compiler may disallow the NUL character (U+0000) in the source text.
Russ Coxb7d9ffe2010-02-16 16:47:18 -080099</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700100
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700101<h3 id="Characters">Characters</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700102
Robert Griesemerc2d55862009-02-19 16:49:10 -0800103<p>
Rob Pike4501d342009-02-19 17:31:36 -0800104The following terms are used to denote specific Unicode character classes:
105</p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700106<pre class="ebnf">
Robert Griesemer0e8032c2011-05-05 09:03:00 -0700107newline = /* the Unicode code point U+000A */ .
108unicode_char = /* an arbitrary Unicode code point except newline */ .
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700109unicode_letter = /* a Unicode code point classified as "Letter" */ .
Robert Griesemer838b5ad2011-02-03 12:27:41 -0800110unicode_digit = /* a Unicode code point classified as "Decimal Digit" */ .
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700111</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700112
Rob Pike678625d2009-09-15 09:54:22 -0700113<p>
Robert Griesemer838b5ad2011-02-03 12:27:41 -0800114In <a href="http://www.unicode.org/versions/Unicode6.0.0/">The Unicode Standard 6.0</a>,
115Section 4.5 "General Category"
Rob Pike678625d2009-09-15 09:54:22 -0700116defines a set of character categories. Go treats
117those characters in category Lu, Ll, Lt, Lm, or Lo as Unicode letters,
118and those in category Nd as Unicode digits.
119</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700120
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700121<h3 id="Letters_and_digits">Letters and digits</h3>
Rob Pikeff70f092009-02-20 13:36:14 -0800122
123<p>
Rob Pikef27e9f02009-02-23 19:22:05 -0800124The underscore character <code>_</code> (U+005F) is considered a letter.
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700125</p>
126<pre class="ebnf">
Robert Griesemerc2d55862009-02-19 16:49:10 -0800127letter = unicode_letter | "_" .
Robert Griesemer3c7271f2011-05-24 14:18:44 -0700128decimal_digit = "0" … "9" .
129octal_digit = "0" … "7" .
130hex_digit = "0" … "9" | "A" … "F" | "a" … "f" .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800131</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700132
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700133<h2 id="Lexical_elements">Lexical elements</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700134
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700135<h3 id="Comments">Comments</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700136
Rob Pikeff70f092009-02-20 13:36:14 -0800137<p>
Robert Griesemer130ac742009-12-10 16:43:01 -0800138There are two forms of comments:
Rob Pikeff70f092009-02-20 13:36:14 -0800139</p>
140
Robert Griesemer130ac742009-12-10 16:43:01 -0800141<ol>
142<li>
143<i>Line comments</i> start with the character sequence <code>//</code>
Robert Griesemerd73d1c52010-11-04 13:48:32 -0700144and stop at the end of the line. A line comment acts like a newline.
Robert Griesemer130ac742009-12-10 16:43:01 -0800145</li>
146<li>
147<i>General comments</i> start with the character sequence <code>/*</code>
148and continue through the character sequence <code>*/</code>. A general
149comment that spans multiple lines acts like a newline, otherwise it acts
150like a space.
151</li>
152</ol>
153
154<p>
155Comments do not nest.
156</p>
157
158
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700159<h3 id="Tokens">Tokens</h3>
Rob Pikeff70f092009-02-20 13:36:14 -0800160
161<p>
162Tokens form the vocabulary of the Go language.
Robert Griesemereb109a72009-12-28 14:40:42 -0800163There are four classes: <i>identifiers</i>, <i>keywords</i>, <i>operators
164and delimiters</i>, and <i>literals</i>. <i>White space</i>, formed from
Rob Pike4fe41922009-11-07 22:00:59 -0800165spaces (U+0020), horizontal tabs (U+0009),
166carriage returns (U+000D), and newlines (U+000A),
167is ignored except as it separates tokens
Robert Griesemer0e66a132010-09-27 18:59:11 -0700168that would otherwise combine into a single token. Also, a newline or end of file
Robert Griesemereb109a72009-12-28 14:40:42 -0800169may trigger the insertion of a <a href="#Semicolons">semicolon</a>.
Robert Griesemer130ac742009-12-10 16:43:01 -0800170While breaking the input into tokens,
Rob Pikeff70f092009-02-20 13:36:14 -0800171the next token is the longest sequence of characters that form a
172valid token.
173</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700174
Robert Griesemer130ac742009-12-10 16:43:01 -0800175<h3 id="Semicolons">Semicolons</h3>
176
177<p>
178The formal grammar uses semicolons <code>";"</code> as terminators in
179a number of productions. Go programs may omit most of these semicolons
180using the following two rules:
181</p>
182
183<ol>
184<li>
185<p>
186When the input is broken into tokens, a semicolon is automatically inserted
187into the token stream at the end of a non-blank line if the line's final
188token is
189</p>
190<ul>
Robert Griesemer3af480372010-05-14 13:11:48 -0700191 <li>an
192 <a href="#Identifiers">identifier</a>
Robert Griesemer130ac742009-12-10 16:43:01 -0800193 </li>
Robert Griesemer3af480372010-05-14 13:11:48 -0700194
195 <li>an
196 <a href="#Integer_literals">integer</a>,
197 <a href="#Floating-point_literals">floating-point</a>,
198 <a href="#Imaginary_literals">imaginary</a>,
199 <a href="#Character_literals">character</a>, or
200 <a href="#String_literals">string</a> literal
201 </li>
202
203 <li>one of the <a href="#Keywords">keywords</a>
204 <code>break</code>,
205 <code>continue</code>,
206 <code>fallthrough</code>, or
207 <code>return</code>
208 </li>
209
210 <li>one of the <a href="#Operators_and_Delimiters">operators and delimiters</a>
211 <code>++</code>,
212 <code>--</code>,
213 <code>)</code>,
214 <code>]</code>, or
215 <code>}</code>
Robert Griesemer130ac742009-12-10 16:43:01 -0800216 </li>
217</ul>
218</li>
219
220<li>
221To allow complex statements to occupy a single line, a semicolon
222may be omitted before a closing <code>")"</code> or <code>"}"</code>.
223</li>
224</ol>
225
226<p>
227To reflect idiomatic use, code examples in this document elide semicolons
228using these rules.
229</p>
230
231
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700232<h3 id="Identifiers">Identifiers</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700233
Rob Pikeff70f092009-02-20 13:36:14 -0800234<p>
235Identifiers name program entities such as variables and types.
236An identifier is a sequence of one or more letters and digits.
237The first character in an identifier must be a letter.
238</p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700239<pre class="ebnf">
Robert Griesemerd36d1912009-09-18 11:58:35 -0700240identifier = letter { letter | unicode_digit } .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800241</pre>
Robert Griesemerc2d55862009-02-19 16:49:10 -0800242<pre>
243a
244_x9
245ThisVariableIsExported
246αβ
247</pre>
Robert Griesemer130ac742009-12-10 16:43:01 -0800248
249<p>
Robert Griesemer4e56b332009-09-10 10:14:00 -0700250Some identifiers are <a href="#Predeclared_identifiers">predeclared</a>.
Robert Griesemer130ac742009-12-10 16:43:01 -0800251</p>
252
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -0700253
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700254<h3 id="Keywords">Keywords</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700255
Rob Pikeff70f092009-02-20 13:36:14 -0800256<p>
257The following keywords are reserved and may not be used as identifiers.
258</p>
259<pre class="grammar">
260break default func interface select
261case defer go map struct
262chan else goto package switch
263const fallthrough if range type
264continue for import return var
265</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700266
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700267<h3 id="Operators_and_Delimiters">Operators and Delimiters</h3>
Rob Pikeff70f092009-02-20 13:36:14 -0800268
269<p>
Robert Griesemerd36d1912009-09-18 11:58:35 -0700270The following character sequences represent <a href="#Operators">operators</a>, delimiters, and other special tokens:
Rob Pikeff70f092009-02-20 13:36:14 -0800271</p>
272<pre class="grammar">
273+ &amp; += &amp;= &amp;&amp; == != ( )
274- | -= |= || &lt; &lt;= [ ]
275* ^ *= ^= &lt;- &gt; &gt;= { }
Robert Griesemer4ed666e2009-08-27 16:45:42 -0700276/ &lt;&lt; /= &lt;&lt;= ++ = := , ;
277% &gt;&gt; %= &gt;&gt;= -- ! ... . :
Rob Pikecd04ec92009-03-11 21:59:05 -0700278 &amp;^ &amp;^=
Rob Pikeff70f092009-02-20 13:36:14 -0800279</pre>
280
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700281<h3 id="Integer_literals">Integer literals</h3>
Rob Pikeff70f092009-02-20 13:36:14 -0800282
283<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700284An integer literal is a sequence of digits representing an
285<a href="#Constants">integer constant</a>.
286An optional prefix sets a non-decimal base: <code>0</code> for octal, <code>0x</code> or
Rob Pikef27e9f02009-02-23 19:22:05 -0800287<code>0X</code> for hexadecimal. In hexadecimal literals, letters
288<code>a-f</code> and <code>A-F</code> represent values 10 through 15.
Rob Pikeff70f092009-02-20 13:36:14 -0800289</p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700290<pre class="ebnf">
Robert Griesemerd36d1912009-09-18 11:58:35 -0700291int_lit = decimal_lit | octal_lit | hex_lit .
Robert Griesemer3c7271f2011-05-24 14:18:44 -0700292decimal_lit = ( "1" … "9" ) { decimal_digit } .
Robert Griesemerd36d1912009-09-18 11:58:35 -0700293octal_lit = "0" { octal_digit } .
294hex_lit = "0" ( "x" | "X" ) hex_digit { hex_digit } .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800295</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700296
Robert Griesemerc2d55862009-02-19 16:49:10 -0800297<pre>
29842
2990600
3000xBadFace
301170141183460469231731687303715884105727
302</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700303
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700304<h3 id="Floating-point_literals">Floating-point literals</h3>
Rob Pikeff70f092009-02-20 13:36:14 -0800305<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700306A floating-point literal is a decimal representation of a
307<a href="#Constants">floating-point constant</a>.
308It has an integer part, a decimal point, a fractional part,
Rob Pikeff70f092009-02-20 13:36:14 -0800309and an exponent part. The integer and fractional part comprise
Rob Pikef27e9f02009-02-23 19:22:05 -0800310decimal digits; the exponent part is an <code>e</code> or <code>E</code>
Rob Pikeff70f092009-02-20 13:36:14 -0800311followed by an optionally signed decimal exponent. One of the
312integer part or the fractional part may be elided; one of the decimal
313point or the exponent may be elided.
314</p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700315<pre class="ebnf">
Robert Griesemerd36d1912009-09-18 11:58:35 -0700316float_lit = decimals "." [ decimals ] [ exponent ] |
317 decimals exponent |
318 "." decimals [ exponent ] .
319decimals = decimal_digit { decimal_digit } .
320exponent = ( "e" | "E" ) [ "+" | "-" ] decimals .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800321</pre>
Robert Griesemerad711102008-09-11 17:48:20 -0700322
Robert Griesemerc2d55862009-02-19 16:49:10 -0800323<pre>
3240.
Rob Pike72970872010-03-04 12:35:16 -080032572.40
326072.40 // == 72.40
Robert Griesemerc2d55862009-02-19 16:49:10 -08003272.71828
3281.e+0
3296.67428e-11
3301E6
331.25
332.12345E+5
333</pre>
Robert Griesemerad711102008-09-11 17:48:20 -0700334
Rob Pike72970872010-03-04 12:35:16 -0800335<h3 id="Imaginary_literals">Imaginary literals</h3>
336<p>
337An imaginary literal is a decimal representation of the imaginary part of a
338<a href="#Constants">complex constant</a>.
339It consists of a
340<a href="#Floating-point_literals">floating-point literal</a>
341or decimal integer followed
342by the lower-case letter <code>i</code>.
343</p>
344<pre class="ebnf">
345imaginary_lit = (decimals | float_lit) "i" .
346</pre>
347
348<pre>
3490i
350011i // == 11i
3510.i
3522.71828i
3531.e+0i
3546.67428e-11i
3551E6i
356.25i
357.12345E+5i
358</pre>
359
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700360
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700361<h3 id="Character_literals">Character literals</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700362
Rob Pike4501d342009-02-19 17:31:36 -0800363<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700364A character literal represents an <a href="#Constants">integer constant</a>,
365typically a Unicode code point, as one or more characters enclosed in single
Rob Pikeff70f092009-02-20 13:36:14 -0800366quotes. Within the quotes, any character may appear except single
367quote and newline. A single quoted character represents itself,
368while multi-character sequences beginning with a backslash encode
369values in various formats.
Rob Pike4501d342009-02-19 17:31:36 -0800370</p>
Rob Pikeff70f092009-02-20 13:36:14 -0800371<p>
372The simplest form represents the single character within the quotes;
373since Go source text is Unicode characters encoded in UTF-8, multiple
374UTF-8-encoded bytes may represent a single integer value. For
Rob Pikef27e9f02009-02-23 19:22:05 -0800375instance, the literal <code>'a'</code> holds a single byte representing
376a literal <code>a</code>, Unicode U+0061, value <code>0x61</code>, while
377<code>'ä'</code> holds two bytes (<code>0xc3</code> <code>0xa4</code>) representing
378a literal <code>a</code>-dieresis, U+00E4, value <code>0xe4</code>.
Rob Pikeff70f092009-02-20 13:36:14 -0800379</p>
380<p>
381Several backslash escapes allow arbitrary values to be represented
382as ASCII text. There are four ways to represent the integer value
Rob Pikef27e9f02009-02-23 19:22:05 -0800383as a numeric constant: <code>\x</code> followed by exactly two hexadecimal
384digits; <code>\u</code> followed by exactly four hexadecimal digits;
385<code>\U</code> followed by exactly eight hexadecimal digits, and a
386plain backslash <code>\</code> followed by exactly three octal digits.
Rob Pikeff70f092009-02-20 13:36:14 -0800387In each case the value of the literal is the value represented by
388the digits in the corresponding base.
389</p>
390<p>
391Although these representations all result in an integer, they have
392different valid ranges. Octal escapes must represent a value between
Rob Pike678625d2009-09-15 09:54:22 -07003930 and 255 inclusive. Hexadecimal escapes satisfy this condition
394by construction. The escapes <code>\u</code> and <code>\U</code>
Rob Pikeff70f092009-02-20 13:36:14 -0800395represent Unicode code points so within them some values are illegal,
Rob Pikef27e9f02009-02-23 19:22:05 -0800396in particular those above <code>0x10FFFF</code> and surrogate halves.
Rob Pikeff70f092009-02-20 13:36:14 -0800397</p>
398<p>
399After a backslash, certain single-character escapes represent special values:
400</p>
401<pre class="grammar">
402\a U+0007 alert or bell
403\b U+0008 backspace
404\f U+000C form feed
405\n U+000A line feed or newline
406\r U+000D carriage return
407\t U+0009 horizontal tab
408\v U+000b vertical tab
409\\ U+005c backslash
410\' U+0027 single quote (valid escape only within character literals)
411\" U+0022 double quote (valid escape only within string literals)
Robert Griesemerc2d55862009-02-19 16:49:10 -0800412</pre>
Robert Griesemerc2d55862009-02-19 16:49:10 -0800413<p>
Rob Pike4fe41922009-11-07 22:00:59 -0800414All other sequences starting with a backslash are illegal inside character literals.
Rob Pike4501d342009-02-19 17:31:36 -0800415</p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700416<pre class="ebnf">
Rob Pikeff70f092009-02-20 13:36:14 -0800417char_lit = "'" ( unicode_value | byte_value ) "'" .
418unicode_value = unicode_char | little_u_value | big_u_value | escaped_char .
419byte_value = octal_byte_value | hex_byte_value .
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700420octal_byte_value = `\` octal_digit octal_digit octal_digit .
421hex_byte_value = `\` "x" hex_digit hex_digit .
422little_u_value = `\` "u" hex_digit hex_digit hex_digit hex_digit .
423big_u_value = `\` "U" hex_digit hex_digit hex_digit hex_digit
Rob Pikeff70f092009-02-20 13:36:14 -0800424 hex_digit hex_digit hex_digit hex_digit .
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700425escaped_char = `\` ( "a" | "b" | "f" | "n" | "r" | "t" | "v" | `\` | "'" | `"` ) .
Rob Pikeff70f092009-02-20 13:36:14 -0800426</pre>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700427
Robert Griesemerc2d55862009-02-19 16:49:10 -0800428<pre>
429'a'
430'ä'
431'本'
432'\t'
433'\000'
434'\007'
435'\377'
436'\x07'
437'\xff'
438'\u12e4'
439'\U00101234'
440</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>
451Raw string literals are character sequences between back quotes
Rob Pikef27e9f02009-02-23 19:22:05 -0800452<code>``</code>. Within the quotes, any character is legal except
Robert Griesemerdb7a6222009-06-18 13:51:14 -0700453back quote. The value of a raw string literal is the
454string composed of the uninterpreted characters between the quotes;
455in particular, backslashes have no special meaning and the string may
456span multiple lines.
Rob Pikeff70f092009-02-20 13:36:14 -0800457</p>
458<p>
459Interpreted string literals are character sequences between double
Rob Pike4fe41922009-11-07 22:00:59 -0800460quotes <code>&quot;&quot;</code>. The text between the quotes,
461which may not span multiple lines, forms the
Rob Pikeff70f092009-02-20 13:36:14 -0800462value of the literal, with backslash escapes interpreted as they
Rob Pikef27e9f02009-02-23 19:22:05 -0800463are in character literals (except that <code>\'</code> is illegal and
Robert Griesemere9192752009-12-01 16:15:53 -0800464<code>\"</code> is legal). The three-digit octal (<code>\</code><i>nnn</i>)
465and two-digit hexadecimal (<code>\x</code><i>nn</i>) escapes represent individual
Rob Pikeff70f092009-02-20 13:36:14 -0800466<i>bytes</i> of the resulting string; all other escapes represent
467the (possibly multi-byte) UTF-8 encoding of individual <i>characters</i>.
Rob Pikef27e9f02009-02-23 19:22:05 -0800468Thus inside a string literal <code>\377</code> and <code>\xFF</code> represent
469a single byte of value <code>0xFF</code>=255, while <code>ÿ</code>,
470<code>\u00FF</code>, <code>\U000000FF</code> and <code>\xc3\xbf</code> represent
Robert Griesemere1e76192009-09-25 14:11:03 -0700471the two bytes <code>0xc3</code> <code>0xbf</code> of the UTF-8 encoding of character
Rob Pikeff70f092009-02-20 13:36:14 -0800472U+00FF.
473</p>
474
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700475<pre class="ebnf">
Rob Pikeff70f092009-02-20 13:36:14 -0800476string_lit = raw_string_lit | interpreted_string_lit .
Robert Griesemer0e8032c2011-05-05 09:03:00 -0700477raw_string_lit = "`" { unicode_char | newline } "`" .
Robert Griesemerd4d4ff02009-10-19 13:13:59 -0700478interpreted_string_lit = `"` { unicode_value | byte_value } `"` .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800479</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700480
Robert Griesemerc2d55862009-02-19 16:49:10 -0800481<pre>
Robert Griesemerdb7a6222009-06-18 13:51:14 -0700482`abc` // same as "abc"
483`\n
484\n` // same as "\\n\n\\n"
Robert Griesemerc2d55862009-02-19 16:49:10 -0800485"\n"
486""
487"Hello, world!\n"
488"日本語"
489"\u65e5本\U00008a9e"
490"\xff\u00FF"
491</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700492
Rob Pikeff70f092009-02-20 13:36:14 -0800493<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700494These examples all represent the same string:
Rob Pikeff70f092009-02-20 13:36:14 -0800495</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700496
Robert Griesemerc2d55862009-02-19 16:49:10 -0800497<pre>
Rob Pikeff70f092009-02-20 13:36:14 -0800498"日本語" // UTF-8 input text
499`日本語` // UTF-8 input text as a raw literal
500"\u65e5\u672c\u8a9e" // The explicit Unicode code points
501"\U000065e5\U0000672c\U00008a9e" // The explicit Unicode code points
Robert Griesemerc2d55862009-02-19 16:49:10 -0800502"\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e" // The explicit UTF-8 bytes
503</pre>
Robert Griesemercd499272008-09-29 12:09:00 -0700504
Robert Griesemerc2d55862009-02-19 16:49:10 -0800505<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700506If the source code represents a character as two code points, such as
507a combining form involving an accent and a letter, the result will be
508an error if placed in a character literal (it is not a single code
509point), and will appear as two code points if placed in a string
510literal.
Rob Pikeff70f092009-02-20 13:36:14 -0800511</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700512
Robert Griesemer19b1d352009-09-24 19:36:48 -0700513
514<h2 id="Constants">Constants</h2>
515
Rob Pike72970872010-03-04 12:35:16 -0800516<p>There are <i>boolean constants</i>, <i>integer constants</i>,
517<i>floating-point constants</i>, <i>complex constants</i>,
518and <i>string constants</i>. Integer, floating-point,
519and complex constants are
Robert Griesemer19b1d352009-09-24 19:36:48 -0700520collectively called <i>numeric constants</i>.
521</p>
Rob Pike678625d2009-09-15 09:54:22 -0700522
523<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700524A constant value is represented by an
525<a href="#Integer_literals">integer</a>,
526<a href="#Floating-point_literals">floating-point</a>,
Rob Pike72970872010-03-04 12:35:16 -0800527<a href="#Imaginary_literals">imaginary</a>,
Robert Griesemer19b1d352009-09-24 19:36:48 -0700528<a href="#Character_literals">character</a>, or
529<a href="#String_literals">string</a> literal,
530an identifier denoting a constant,
Robert Griesemer27693562011-06-13 16:47:33 -0700531a <a href="#Constant_expressions">constant expression</a>,
532a <a href="#Conversions">conversion</a> with a result that is a constant, or
Russ Coxf4429182010-07-01 17:49:47 -0700533the result value of some built-in functions such as
534<code>unsafe.Sizeof</code> applied to any value,
535<code>cap</code> or <code>len</code> applied to
536<a href="#Length_and_capacity">some expressions</a>,
Rob Pike72970872010-03-04 12:35:16 -0800537<code>real</code> and <code>imag</code> applied to a complex constant
Robert Griesemerb94c0d22011-01-19 23:07:21 -0500538and <code>complex</code> applied to numeric constants.
Robert Griesemer19b1d352009-09-24 19:36:48 -0700539The boolean truth values are represented by the predeclared constants
540<code>true</code> and <code>false</code>. The predeclared identifier
541<a href="#Iota">iota</a> denotes an integer constant.
Rob Pike678625d2009-09-15 09:54:22 -0700542</p>
543
Robert Griesemer19b1d352009-09-24 19:36:48 -0700544<p>
Rob Pike72970872010-03-04 12:35:16 -0800545In general, complex constants are a form of
546<a href="#Constant_expressions">constant expression</a>
547and are discussed in that section.
548</p>
549
550<p>
Robert Griesemere9192752009-12-01 16:15:53 -0800551Numeric constants represent values of arbitrary precision and do not overflow.
Robert Griesemer19b1d352009-09-24 19:36:48 -0700552</p>
553
554<p>
555Constants may be <a href="#Types">typed</a> or untyped.
556Literal constants, <code>true</code>, <code>false</code>, <code>iota</code>,
557and certain <a href="#Constant_expressions">constant expressions</a>
558containing only untyped constant operands are untyped.
559</p>
560
561<p>
562A constant may be given a type explicitly by a <a href="#Constant_declarations">constant declaration</a>
563or <a href="#Conversions">conversion</a>, or implicitly when used in a
564<a href="#Variable_declarations">variable declaration</a> or an
565<a href="#Assignments">assignment</a> or as an
566operand in an <a href="#Expressions">expression</a>.
567It is an error if the constant value
Robert Griesemerdfc5bb52011-01-19 10:33:41 -0800568cannot be represented as a value of the respective type.
Robert Griesemere9192752009-12-01 16:15:53 -0800569For instance, <code>3.0</code> can be given any integer or any
Rob Pike4fe41922009-11-07 22:00:59 -0800570floating-point type, while <code>2147483648.0</code> (equal to <code>1&lt;&lt;31</code>)
571can be given the types <code>float32</code>, <code>float64</code>, or <code>uint32</code> but
572not <code>int32</code> or <code>string</code>.
Robert Griesemer19b1d352009-09-24 19:36:48 -0700573</p>
574
575<p>
Russ Cox16205a32010-01-18 15:59:14 -0800576There are no constants denoting the IEEE-754 infinity and not-a-number values,
577but the <a href="/pkg/math/"><code>math</code> package</a>'s
578<a href="/pkg/math/#Inf">Inf</a>,
579<a href="/pkg/math/#NaN">NaN</a>,
580<a href="/pkg/math/#IsInf">IsInf</a>, and
581<a href="/pkg/math/#IsNaN">IsNaN</a>
582functions return and test for those values at run time.
583</p>
584
585<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700586Implementation restriction: A compiler may implement numeric constants by choosing
587an internal representation with at least twice as many bits as any machine type;
588for floating-point values, both the mantissa and exponent must be twice as large.
589</p>
590
591
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700592<h2 id="Types">Types</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700593
Robert Griesemerc2d55862009-02-19 16:49:10 -0800594<p>
Robert Griesemer56809d02009-05-20 11:02:48 -0700595A type determines the set of values and operations specific to values of that
596type. A type may be specified by a (possibly qualified) <i>type name</i>
Robert Griesemer947e2182010-09-07 11:14:36 -0700597<a href="#Qualified_identifiers">Qualified identifier</a>, §<a href="#Type_declarations">Type declarations</a>) or a <i>type literal</i>,
Robert Griesemer56809d02009-05-20 11:02:48 -0700598which composes a new type from previously declared types.
Rob Pike5af7de32009-02-24 15:17:59 -0800599</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700600
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700601<pre class="ebnf">
Rob Pike8f2330d2009-02-25 16:20:44 -0800602Type = TypeName | TypeLit | "(" Type ")" .
Anthony Martin11a01612010-12-13 22:19:41 -0800603TypeName = QualifiedIdent .
Rob Pike8f2330d2009-02-25 16:20:44 -0800604TypeLit = ArrayType | StructType | PointerType | FunctionType | InterfaceType |
605 SliceType | MapType | ChannelType .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800606</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -0800607
Robert Griesemerc2d55862009-02-19 16:49:10 -0800608<p>
Robert Griesemerfc61b772009-09-28 14:10:20 -0700609Named instances of the boolean, numeric, and string types are
610<a href="#Predeclared_identifiers">predeclared</a>.
611<i>Composite types</i>&mdash;array, struct, pointer, function,
612interface, slice, map, and channel types&mdash;may be constructed using
613type literals.
Rob Pike5af7de32009-02-24 15:17:59 -0800614</p>
Robert Griesemer7abfcd92008-10-07 17:14:30 -0700615
Rob Pike5af7de32009-02-24 15:17:59 -0800616<p>
Robert Griesemer2a838d62011-02-08 13:31:01 -0800617The <i>static type</i> (or just <i>type</i>) of a variable is the
618type defined by its declaration. Variables of interface type
619also have a distinct <i>dynamic type</i>, which
620is the actual type of the value stored in the variable at run-time.
621The dynamic type may vary during execution but is always
622<a href="#Assignability">assignable</a>
623to the static type of the interface variable. For non-interface
624types, the dynamic type is always the static type.
625</p>
626
627<p>
Robert Griesemer7bc03712010-06-07 15:49:39 -0700628Each type <code>T</code> has an <i>underlying type</i>: If <code>T</code>
629is a predeclared type or a type literal, the corresponding underlying
630type is <code>T</code> itself. Otherwise, <code>T</code>'s underlying type
631is the underlying type of the type to which <code>T</code> refers in its
632<a href="#Type_declarations">type declaration</a>.
633</p>
634
635<pre>
636 type T1 string
637 type T2 T1
638 type T3 []T1
639 type T4 T3
640</pre>
641
642<p>
643The underlying type of <code>string</code>, <code>T1</code>, and <code>T2</code>
644is <code>string</code>. The underlying type of <code>[]T1</code>, <code>T3</code>,
645and <code>T4</code> is <code>[]T1</code>.
646</p>
647
Robert Griesemer2a838d62011-02-08 13:31:01 -0800648<h3 id="Method_sets">Method sets</h3>
Robert Griesemer7bc03712010-06-07 15:49:39 -0700649<p>
Robert Griesemer3b576a72009-06-17 14:31:33 -0700650A type may have a <i>method set</i> associated with it
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700651<a href="#Interface_types">Interface types</a>, §<a href="#Method_declarations">Method declarations</a>).
Robert Griesemer19b1d352009-09-24 19:36:48 -0700652The method set of an <a href="#Interface_types">interface type</a> is its interface.
Robert Griesemer56809d02009-05-20 11:02:48 -0700653The method set of any other named type <code>T</code>
Robert Griesemerfc61b772009-09-28 14:10:20 -0700654consists of all methods with receiver type <code>T</code>.
Robert Griesemer56809d02009-05-20 11:02:48 -0700655The method set of the corresponding pointer type <code>*T</code>
656is the set of all methods with receiver <code>*T</code> or <code>T</code>
657(that is, it also contains the method set of <code>T</code>).
658Any other type has an empty method set.
Robert Griesemerd4d4ff02009-10-19 13:13:59 -0700659In a method set, each method must have a unique name.
Rob Pike5af7de32009-02-24 15:17:59 -0800660</p>
Robert Griesemer1f3e8422008-09-29 18:41:30 -0700661
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700662
Robert Griesemer19b1d352009-09-24 19:36:48 -0700663<h3 id="Boolean_types">Boolean types</h3>
664
665A <i>boolean type</i> represents the set of Boolean truth values
666denoted by the predeclared constants <code>true</code>
667and <code>false</code>. The predeclared boolean type is <code>bool</code>.
668
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700669
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700670<h3 id="Numeric_types">Numeric types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700671
Rob Pike5af7de32009-02-24 15:17:59 -0800672<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700673A <i>numeric type</i> represents sets of integer or floating-point values.
674The predeclared architecture-independent numeric types are:
Rob Pike5af7de32009-02-24 15:17:59 -0800675</p>
Robert Griesemerebf14c62008-10-30 14:50:23 -0700676
Rob Pikeff70f092009-02-20 13:36:14 -0800677<pre class="grammar">
Rob Pike72970872010-03-04 12:35:16 -0800678uint8 the set of all unsigned 8-bit integers (0 to 255)
679uint16 the set of all unsigned 16-bit integers (0 to 65535)
680uint32 the set of all unsigned 32-bit integers (0 to 4294967295)
681uint64 the set of all unsigned 64-bit integers (0 to 18446744073709551615)
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700682
Rob Pike72970872010-03-04 12:35:16 -0800683int8 the set of all signed 8-bit integers (-128 to 127)
684int16 the set of all signed 16-bit integers (-32768 to 32767)
685int32 the set of all signed 32-bit integers (-2147483648 to 2147483647)
686int64 the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700687
Rob Pike72970872010-03-04 12:35:16 -0800688float32 the set of all IEEE-754 32-bit floating-point numbers
689float64 the set of all IEEE-754 64-bit floating-point numbers
690
691complex64 the set of all complex numbers with float32 real and imaginary parts
692complex128 the set of all complex numbers with float64 real and imaginary parts
Rob Pike5af7de32009-02-24 15:17:59 -0800693
Robert Griesemeref4c2b82010-03-10 15:29:36 -0800694byte familiar alias for uint8
Robert Griesemerc2d55862009-02-19 16:49:10 -0800695</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700696
Rob Pike5af7de32009-02-24 15:17:59 -0800697<p>
Robert Griesemere9192752009-12-01 16:15:53 -0800698The value of an <i>n</i>-bit integer is <i>n</i> bits wide and represented using
699<a href="http://en.wikipedia.org/wiki/Two's_complement">two's complement arithmetic</a>.
Rob Pike5af7de32009-02-24 15:17:59 -0800700</p>
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -0800701
Rob Pike5af7de32009-02-24 15:17:59 -0800702<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700703There is also a set of predeclared numeric types with implementation-specific sizes:
Rob Pike5af7de32009-02-24 15:17:59 -0800704</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700705
Rob Pikeff70f092009-02-20 13:36:14 -0800706<pre class="grammar">
Robert Griesemercfe92112009-06-18 13:29:40 -0700707uint either 32 or 64 bits
Robert Griesemer97025eb2011-01-13 10:24:04 -0800708int same size as uint
Robert Griesemercfe92112009-06-18 13:29:40 -0700709uintptr an unsigned integer large enough to store the uninterpreted bits of a pointer value
Robert Griesemerc2d55862009-02-19 16:49:10 -0800710</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700711
Robert Griesemerc2d55862009-02-19 16:49:10 -0800712<p>
Rob Pikeda389742009-03-02 19:13:40 -0800713To avoid portability issues all numeric types are distinct except
714<code>byte</code>, which is an alias for <code>uint8</code>.
715Conversions
Robert Griesemer7bc03712010-06-07 15:49:39 -0700716are required when different numeric types are mixed in an expression
Rob Pike5af7de32009-02-24 15:17:59 -0800717or assignment. For instance, <code>int32</code> and <code>int</code>
Russ Cox5958dd62009-03-04 17:19:21 -0800718are not the same type even though they may have the same size on a
Rob Pike5af7de32009-02-24 15:17:59 -0800719particular architecture.
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700720
721
Robert Griesemer19b1d352009-09-24 19:36:48 -0700722<h3 id="String_types">String types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700723
Rob Pike4501d342009-02-19 17:31:36 -0800724<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700725A <i>string type</i> represents the set of string values.
Rob Pike5af7de32009-02-24 15:17:59 -0800726Strings behave like arrays of bytes but are immutable: once created,
727it is impossible to change the contents of a string.
Robert Griesemer19b1d352009-09-24 19:36:48 -0700728The predeclared string type is <code>string</code>.
Rob Pike5af7de32009-02-24 15:17:59 -0800729
730<p>
731The elements of strings have type <code>byte</code> and may be
Robert Griesemerfc61b772009-09-28 14:10:20 -0700732accessed using the usual <a href="#Indexes">indexing operations</a>. It is
Rob Pike678625d2009-09-15 09:54:22 -0700733illegal to take the address of such an element; if
734<code>s[i]</code> is the <i>i</i>th byte of a
Robert Griesemercfe92112009-06-18 13:29:40 -0700735string, <code>&amp;s[i]</code> is invalid. The length of string
736<code>s</code> can be discovered using the built-in function
Robert Griesemer19b1d352009-09-24 19:36:48 -0700737<code>len</code>. The length is a compile-time constant if <code>s</code>
Robert Griesemercfe92112009-06-18 13:29:40 -0700738is a string literal.
Rob Pike4501d342009-02-19 17:31:36 -0800739</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700740
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700741
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700742<h3 id="Array_types">Array types</h3>
Russ Cox461dd912009-03-04 14:44:51 -0800743
744<p>
745An array is a numbered sequence of elements of a single
Robert Griesemer4023dce2009-08-14 17:41:52 -0700746type, called the element type.
747The number of elements is called the length and is never
Russ Cox461dd912009-03-04 14:44:51 -0800748negative.
749</p>
750
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700751<pre class="ebnf">
Russ Cox461dd912009-03-04 14:44:51 -0800752ArrayType = "[" ArrayLength "]" ElementType .
753ArrayLength = Expression .
Robert Griesemer4023dce2009-08-14 17:41:52 -0700754ElementType = Type .
Russ Cox461dd912009-03-04 14:44:51 -0800755</pre>
756
757<p>
Robert Griesemere9192752009-12-01 16:15:53 -0800758The length is part of the array's type and must be a
Robert Griesemer4ed666e2009-08-27 16:45:42 -0700759<a href="#Constant_expressions">constant expression</a> that evaluates to a non-negative
Russ Cox461dd912009-03-04 14:44:51 -0800760integer value. The length of array <code>a</code> can be discovered
Robert Griesemer0c2e6b32010-07-13 11:54:57 -0700761using the built-in function <a href="#Length_and_capacity"><code>len(a)</code></a>.
762The elements can be indexed by integer
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700763indices 0 through the <code>len(a)-1</code><a href="#Indexes">Indexes</a>).
Rob Pikeff6a8fd2009-11-20 15:47:15 -0800764Array types are always one-dimensional but may be composed to form
765multi-dimensional types.
Russ Cox461dd912009-03-04 14:44:51 -0800766</p>
767
768<pre>
769[32]byte
770[2*N] struct { x, y int32 }
771[1000]*float64
Rob Pikeff6a8fd2009-11-20 15:47:15 -0800772[3][5]int
773[2][2][2]float64 // same as [2]([2]([2]float64))
Russ Cox461dd912009-03-04 14:44:51 -0800774</pre>
775
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700776<h3 id="Slice_types">Slice types</h3>
Russ Cox461dd912009-03-04 14:44:51 -0800777
778<p>
779A slice is a reference to a contiguous segment of an array and
780contains a numbered sequence of elements from that array. A slice
781type denotes the set of all slices of arrays of its element type.
Robert Griesemer7bc03712010-06-07 15:49:39 -0700782The value of an uninitialized slice is <code>nil</code>.
Russ Cox461dd912009-03-04 14:44:51 -0800783</p>
784
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700785<pre class="ebnf">
Russ Cox461dd912009-03-04 14:44:51 -0800786SliceType = "[" "]" ElementType .
787</pre>
788
789<p>
790Like arrays, slices are indexable and have a length. The length of a
791slice <code>s</code> can be discovered by the built-in function
Robert Griesemer0c2e6b32010-07-13 11:54:57 -0700792<a href="#Length_and_capacity"><code>len(s)</code></a>; unlike with arrays it may change during
Russ Cox461dd912009-03-04 14:44:51 -0800793execution. The elements can be addressed by integer indices 0
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700794through <code>len(s)-1</code><a href="#Indexes">Indexes</a>). The slice index of a
Russ Cox461dd912009-03-04 14:44:51 -0800795given element may be less than the index of the same element in the
796underlying array.
797</p>
798<p>
799A slice, once initialized, is always associated with an underlying
Rob Pike7ec08562010-01-09 07:32:26 +1100800array that holds its elements. A slice therefore shares storage
Russ Cox461dd912009-03-04 14:44:51 -0800801with its array and with other slices of the same array; by contrast,
802distinct arrays always represent distinct storage.
803</p>
804<p>
805The array underlying a slice may extend past the end of the slice.
Russ Cox5958dd62009-03-04 17:19:21 -0800806The <i>capacity</i> is a measure of that extent: it is the sum of
Russ Cox461dd912009-03-04 14:44:51 -0800807the length of the slice and the length of the array beyond the slice;
808a slice of length up to that capacity can be created by `slicing' a new
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700809one from the original slice (§<a href="#Slices">Slices</a>).
Russ Cox461dd912009-03-04 14:44:51 -0800810The capacity of a slice <code>a</code> can be discovered using the
Robert Griesemer0c2e6b32010-07-13 11:54:57 -0700811built-in function <a href="#Length_and_capacity"><code>cap(a)</code></a>.
Russ Cox461dd912009-03-04 14:44:51 -0800812</p>
813
Russ Cox461dd912009-03-04 14:44:51 -0800814<p>
Robert Griesemer0c2e6b32010-07-13 11:54:57 -0700815A new, initialized slice value for a given element type <code>T</code> is
816made using the built-in function
817<a href="#Making_slices_maps_and_channels"><code>make</code></a>,
818which takes a slice type
Russ Cox461dd912009-03-04 14:44:51 -0800819and parameters specifying the length and optionally the capacity:
820</p>
821
822<pre>
823make([]T, length)
824make([]T, length, capacity)
825</pre>
Russ Cox5958dd62009-03-04 17:19:21 -0800826
Russ Cox461dd912009-03-04 14:44:51 -0800827<p>
Rob Pike46f482a2011-05-25 06:44:09 +1000828A call to <code>make</code> allocates a new, hidden array to which the returned
Rob Pike678625d2009-09-15 09:54:22 -0700829slice value refers. That is, executing
Russ Cox461dd912009-03-04 14:44:51 -0800830</p>
831
832<pre>
833make([]T, length, capacity)
834</pre>
835
836<p>
837produces the same slice as allocating an array and slicing it, so these two examples
838result in the same slice:
839</p>
840
841<pre>
842make([]int, 50, 100)
843new([100]int)[0:50]
844</pre>
845
Rob Pikeff6a8fd2009-11-20 15:47:15 -0800846<p>
847Like arrays, slices are always one-dimensional but may be composed to construct
848higher-dimensional objects.
849With arrays of arrays, the inner arrays are, by construction, always the same length;
850however with slices of slices (or arrays of slices), the lengths may vary dynamically.
851Moreover, the inner slices must be allocated individually (with <code>make</code>).
852</p>
Russ Cox461dd912009-03-04 14:44:51 -0800853
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700854<h3 id="Struct_types">Struct types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700855
Rob Pike5af7de32009-02-24 15:17:59 -0800856<p>
Robert Griesemerd3b15652009-11-16 08:58:55 -0800857A struct is a sequence of named elements, called fields, each of which has a
858name and a type. Field names may be specified explicitly (IdentifierList) or
859implicitly (AnonymousField).
860Within a struct, non-<a href="#Blank_identifier">blank</a> field names must
861be unique.
Rob Pike5af7de32009-02-24 15:17:59 -0800862</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700863
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700864<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -0800865StructType = "struct" "{" { FieldDecl ";" } "}" .
Robert Griesemerd3b15652009-11-16 08:58:55 -0800866FieldDecl = (IdentifierList Type | AnonymousField) [ Tag ] .
867AnonymousField = [ "*" ] TypeName .
Robert Griesemer130ac742009-12-10 16:43:01 -0800868Tag = string_lit .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800869</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700870
Robert Griesemerc2d55862009-02-19 16:49:10 -0800871<pre>
872// An empty struct.
873struct {}
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700874
Robert Griesemer4e56b332009-09-10 10:14:00 -0700875// A struct with 6 fields.
Robert Griesemerc2d55862009-02-19 16:49:10 -0800876struct {
Robert Griesemer130ac742009-12-10 16:43:01 -0800877 x, y int
Robert Griesemerb94c0d22011-01-19 23:07:21 -0500878 u float32
879 _ float32 // padding
Robert Griesemer130ac742009-12-10 16:43:01 -0800880 A *[]int
881 F func()
Robert Griesemerc2d55862009-02-19 16:49:10 -0800882}
883</pre>
Robert Griesemer1f3e8422008-09-29 18:41:30 -0700884
Rob Pike5af7de32009-02-24 15:17:59 -0800885<p>
Russ Coxbee2d5b2010-09-30 14:59:41 -0400886A field declared with a type but no explicit field name is an <i>anonymous field</i>
887(colloquially called an embedded field).
Rob Pike5af7de32009-02-24 15:17:59 -0800888Such a field type must be specified as
Russ Coxbee2d5b2010-09-30 14:59:41 -0400889a 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 -0700890and <code>T</code> itself may not be
Robert Griesemerd3b15652009-11-16 08:58:55 -0800891a pointer type. The unqualified type name acts as the field name.
Rob Pike5af7de32009-02-24 15:17:59 -0800892</p>
Robert Griesemer1f3e8422008-09-29 18:41:30 -0700893
Robert Griesemerc2d55862009-02-19 16:49:10 -0800894<pre>
895// A struct with four anonymous fields of type T1, *T2, P.T3 and *P.T4
896struct {
Robert Griesemer130ac742009-12-10 16:43:01 -0800897 T1 // field name is T1
898 *T2 // field name is T2
899 P.T3 // field name is T3
900 *P.T4 // field name is T4
901 x, y int // field names are x and y
Robert Griesemerc2d55862009-02-19 16:49:10 -0800902}
903</pre>
Robert Griesemer1f3e8422008-09-29 18:41:30 -0700904
Rob Pike5af7de32009-02-24 15:17:59 -0800905<p>
Robert Griesemerd3b15652009-11-16 08:58:55 -0800906The following declaration is illegal because field names must be unique
907in a struct type:
Rob Pike5af7de32009-02-24 15:17:59 -0800908</p>
Robert Griesemer071c91b2008-10-23 12:04:45 -0700909
Robert Griesemerc2d55862009-02-19 16:49:10 -0800910<pre>
911struct {
Robert Griesemer130ac742009-12-10 16:43:01 -0800912 T // conflicts with anonymous field *T and *P.T
913 *T // conflicts with anonymous field T and *P.T
914 *P.T // conflicts with anonymous field T and *T
Robert Griesemerc2d55862009-02-19 16:49:10 -0800915}
916</pre>
Robert Griesemer1f3e8422008-09-29 18:41:30 -0700917
Robert Griesemerc2d55862009-02-19 16:49:10 -0800918<p>
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700919Fields and methods (§<a href="#Method_declarations">Method declarations</a>) of an anonymous field are
920promoted to be ordinary fields and methods of the struct (§<a href="#Selectors">Selectors</a>).
Robert Griesemer56809d02009-05-20 11:02:48 -0700921The following rules apply for a struct type named <code>S</code> and
922a type named <code>T</code>:
Rob Pike5af7de32009-02-24 15:17:59 -0800923</p>
Robert Griesemer56809d02009-05-20 11:02:48 -0700924<ul>
925 <li>If <code>S</code> contains an anonymous field <code>T</code>, the
Robert Griesemer2a838d62011-02-08 13:31:01 -0800926 <a href="#Method_sets">method set</a> of <code>S</code> includes the
927 method set of <code>T</code>.
Robert Griesemer56809d02009-05-20 11:02:48 -0700928 </li>
929
930 <li>If <code>S</code> contains an anonymous field <code>*T</code>, the
931 method set of <code>S</code> includes the method set of <code>*T</code>
932 (which itself includes the method set of <code>T</code>).
933 </li>
934
935 <li>If <code>S</code> contains an anonymous field <code>T</code> or
936 <code>*T</code>, the method set of <code>*S</code> includes the
937 method set of <code>*T</code> (which itself includes the method
938 set of <code>T</code>).
939 </li>
940</ul>
Rob Pike5af7de32009-02-24 15:17:59 -0800941<p>
Robert Griesemer56809d02009-05-20 11:02:48 -0700942A field declaration may be followed by an optional string literal <i>tag</i>,
Robert Griesemerd3b15652009-11-16 08:58:55 -0800943which becomes an attribute for all the fields in the corresponding
Rob Pike5af7de32009-02-24 15:17:59 -0800944field declaration. The tags are made
Rob Pike8cb91842009-09-15 11:56:39 -0700945visible through a <a href="#Package_unsafe">reflection interface</a>
Rob Pike5af7de32009-02-24 15:17:59 -0800946but are otherwise ignored.
947</p>
Robert Griesemer2e90e542008-10-30 15:52:37 -0700948
Robert Griesemerc2d55862009-02-19 16:49:10 -0800949<pre>
Rob Pike678625d2009-09-15 09:54:22 -0700950// A struct corresponding to the TimeStamp protocol buffer.
Rob Pikecdbf6192009-02-24 17:47:45 -0800951// The tag strings define the protocol buffer field numbers.
Robert Griesemerc2d55862009-02-19 16:49:10 -0800952struct {
Robert Griesemer130ac742009-12-10 16:43:01 -0800953 microsec uint64 "field 1"
954 serverIP6 uint64 "field 2"
955 process string "field 3"
Robert Griesemerc2d55862009-02-19 16:49:10 -0800956}
957</pre>
Robert Griesemer2e90e542008-10-30 15:52:37 -0700958
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700959<h3 id="Pointer_types">Pointer types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700960
Rob Pike5af7de32009-02-24 15:17:59 -0800961<p>
Robert Griesemer4dc25282008-09-09 10:37:19 -0700962A pointer type denotes the set of all pointers to variables of a given
Rob Pikeda389742009-03-02 19:13:40 -0800963type, called the <i>base type</i> of the pointer.
Peter Mundy5928e1d2010-11-09 10:10:57 -0800964The value of an uninitialized pointer is <code>nil</code>.
Rob Pike5af7de32009-02-24 15:17:59 -0800965</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700966
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700967<pre class="ebnf">
Robert Griesemerc2d55862009-02-19 16:49:10 -0800968PointerType = "*" BaseType .
969BaseType = Type .
970</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700971
Robert Griesemerc2d55862009-02-19 16:49:10 -0800972<pre>
973*int
Rob Pike8f2330d2009-02-25 16:20:44 -0800974*map[string] *chan int
Robert Griesemerc2d55862009-02-19 16:49:10 -0800975</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700976
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700977<h3 id="Function_types">Function types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700978
Rob Pike8f2330d2009-02-25 16:20:44 -0800979<p>
Robert Griesemer7231ceb2008-09-08 15:01:04 -0700980A function type denotes the set of all functions with the same parameter
Peter Mundy5928e1d2010-11-09 10:10:57 -0800981and result types. The value of an uninitialized variable of function type
Robert Griesemer7bc03712010-06-07 15:49:39 -0700982is <code>nil</code>.
Rob Pike8f2330d2009-02-25 16:20:44 -0800983</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700984
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700985<pre class="ebnf">
Rob Pike8f2330d2009-02-25 16:20:44 -0800986FunctionType = "func" Signature .
987Signature = Parameters [ Result ] .
Robert Griesemer4023dce2009-08-14 17:41:52 -0700988Result = Parameters | Type .
Robert Griesemer130ac742009-12-10 16:43:01 -0800989Parameters = "(" [ ParameterList [ "," ] ] ")" .
Rob Pike8f2330d2009-02-25 16:20:44 -0800990ParameterList = ParameterDecl { "," ParameterDecl } .
Russ Cox95625922010-06-12 11:37:13 -0700991ParameterDecl = [ IdentifierList ] [ "..." ] Type .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800992</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700993
Robert Griesemerc2d55862009-02-19 16:49:10 -0800994<p>
Rob Pike8f2330d2009-02-25 16:20:44 -0800995Within a list of parameters or results, the names (IdentifierList)
996must either all be present or all be absent. If present, each name
997stands for one item (parameter or result) of the specified type; if absent, each
998type stands for one item of that type. Parameter and result
999lists are always parenthesized except that if there is exactly
Robert Griesemer73ca1272010-07-09 13:02:54 -07001000one unnamed result it may be written as an unparenthesized type.
Rob Pike8f2330d2009-02-25 16:20:44 -08001001</p>
Russ Cox95625922010-06-12 11:37:13 -07001002
Robert Griesemerac771a82010-09-24 14:08:28 -07001003<p>
1004The final parameter in a function signature may have
1005a type prefixed with <code>...</code>.
1006A function with such a parameter is called <i>variadic</i> and
1007may be invoked with zero or more arguments for that parameter.
Rob Pike8f2330d2009-02-25 16:20:44 -08001008</p>
Robert Griesemer2bfa9572008-10-24 13:13:12 -07001009
Robert Griesemerc2d55862009-02-19 16:49:10 -08001010<pre>
Russ Cox46871692010-01-26 10:25:56 -08001011func()
1012func(x int)
1013func() int
Russ Cox95625922010-06-12 11:37:13 -07001014func(prefix string, values ...int)
Robert Griesemerb94c0d22011-01-19 23:07:21 -05001015func(a, b int, z float32) bool
1016func(a, b int, z float32) (bool)
1017func(a, b int, z float64, opt ...interface{}) (success bool)
1018func(int, int, float64) (float64, *[]int)
Russ Cox46871692010-01-26 10:25:56 -08001019func(n int) func(p *T)
Robert Griesemerc2d55862009-02-19 16:49:10 -08001020</pre>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08001021
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001022
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001023<h3 id="Interface_types">Interface types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001024
Rob Pike8f2330d2009-02-25 16:20:44 -08001025<p>
Robert Griesemer2a838d62011-02-08 13:31:01 -08001026An interface type specifies a <a href="#Method_sets">method set</a> called its <i>interface</i>.
Robert Griesemer56809d02009-05-20 11:02:48 -07001027A variable of interface type can store a value of any type with a method set
1028that is any superset of the interface. Such a type is said to
Robert Griesemer7bc03712010-06-07 15:49:39 -07001029<i>implement the interface</i>.
Peter Mundy5928e1d2010-11-09 10:10:57 -08001030The value of an uninitialized variable of interface type is <code>nil</code>.
Rob Pike8f2330d2009-02-25 16:20:44 -08001031</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001032
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001033<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -08001034InterfaceType = "interface" "{" { MethodSpec ";" } "}" .
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07001035MethodSpec = MethodName Signature | InterfaceTypeName .
1036MethodName = identifier .
Rob Pike8f2330d2009-02-25 16:20:44 -08001037InterfaceTypeName = TypeName .
Robert Griesemerc2d55862009-02-19 16:49:10 -08001038</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001039
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07001040<p>
1041As with all method sets, in an interface type, each method must have a unique name.
1042</p>
1043
Robert Griesemerc2d55862009-02-19 16:49:10 -08001044<pre>
Rob Pike8f2330d2009-02-25 16:20:44 -08001045// A simple File interface
Robert Griesemerc2d55862009-02-19 16:49:10 -08001046interface {
Robert Griesemer130ac742009-12-10 16:43:01 -08001047 Read(b Buffer) bool
1048 Write(b Buffer) bool
1049 Close()
Robert Griesemerc2d55862009-02-19 16:49:10 -08001050}
1051</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001052
Rob Pike8f2330d2009-02-25 16:20:44 -08001053<p>
Robert Griesemer56809d02009-05-20 11:02:48 -07001054More than one type may implement an interface.
Rob Pike8f2330d2009-02-25 16:20:44 -08001055For instance, if two types <code>S1</code> and <code>S2</code>
Robert Griesemer56809d02009-05-20 11:02:48 -07001056have the method set
Rob Pike8f2330d2009-02-25 16:20:44 -08001057</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001058
Robert Griesemerc2d55862009-02-19 16:49:10 -08001059<pre>
Robert Griesemer3c7271f2011-05-24 14:18:44 -07001060func (p T) Read(b Buffer) bool { return … }
1061func (p T) Write(b Buffer) bool { return … }
1062func (p T) Close() { … }
Robert Griesemerc2d55862009-02-19 16:49:10 -08001063</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001064
Rob Pike8f2330d2009-02-25 16:20:44 -08001065<p>
1066(where <code>T</code> stands for either <code>S1</code> or <code>S2</code>)
1067then the <code>File</code> interface is implemented by both <code>S1</code> and
1068<code>S2</code>, regardless of what other methods
1069<code>S1</code> and <code>S2</code> may have or share.
1070</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001071
Rob Pike8f2330d2009-02-25 16:20:44 -08001072<p>
1073A type implements any interface comprising any subset of its methods
1074and may therefore implement several distinct interfaces. For
1075instance, all types implement the <i>empty interface</i>:
1076</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001077
Robert Griesemerc2d55862009-02-19 16:49:10 -08001078<pre>
Robert Griesemer19b1d352009-09-24 19:36:48 -07001079interface{}
Robert Griesemerc2d55862009-02-19 16:49:10 -08001080</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001081
Rob Pike8f2330d2009-02-25 16:20:44 -08001082<p>
1083Similarly, consider this interface specification,
Robert Griesemer4ed666e2009-08-27 16:45:42 -07001084which appears within a <a href="#Type_declarations">type declaration</a>
Rob Pike8f2330d2009-02-25 16:20:44 -08001085to define an interface called <code>Lock</code>:
1086</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001087
Robert Griesemerc2d55862009-02-19 16:49:10 -08001088<pre>
1089type Lock interface {
Robert Griesemer130ac742009-12-10 16:43:01 -08001090 Lock()
1091 Unlock()
Robert Griesemerc2d55862009-02-19 16:49:10 -08001092}
1093</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001094
Rob Pike8f2330d2009-02-25 16:20:44 -08001095<p>
1096If <code>S1</code> and <code>S2</code> also implement
1097</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001098
Robert Griesemerc2d55862009-02-19 16:49:10 -08001099<pre>
Robert Griesemer3c7271f2011-05-24 14:18:44 -07001100func (p T) Lock() { … }
1101func (p T) Unlock() { … }
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 -08001105they implement the <code>Lock</code> interface as well
1106as the <code>File</code> interface.
1107</p>
1108<p>
1109An interface may contain an interface type name <code>T</code>
1110in place of a method specification.
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07001111The effect is equivalent to enumerating the methods of <code>T</code> explicitly
Rob Pike8f2330d2009-02-25 16:20:44 -08001112in the interface.
1113</p>
Robert Griesemer38c232f2009-02-11 15:09:15 -08001114
Robert Griesemerc2d55862009-02-19 16:49:10 -08001115<pre>
1116type ReadWrite interface {
Robert Griesemer130ac742009-12-10 16:43:01 -08001117 Read(b Buffer) bool
1118 Write(b Buffer) bool
Robert Griesemerc2d55862009-02-19 16:49:10 -08001119}
Robert Griesemer38c232f2009-02-11 15:09:15 -08001120
Robert Griesemerc2d55862009-02-19 16:49:10 -08001121type File interface {
Robert Griesemer130ac742009-12-10 16:43:01 -08001122 ReadWrite // same as enumerating the methods in ReadWrite
1123 Lock // same as enumerating the methods in Lock
1124 Close()
Robert Griesemerc2d55862009-02-19 16:49:10 -08001125}
1126</pre>
Robert Griesemer38c232f2009-02-11 15:09:15 -08001127
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001128<h3 id="Map_types">Map types</h3>
Robert Griesemera3294712009-01-05 11:17:26 -08001129
Rob Pike8f2330d2009-02-25 16:20:44 -08001130<p>
1131A map is an unordered group of elements of one type, called the
Robert Griesemer0660d242009-11-15 17:42:27 -08001132element type, indexed by a set of unique <i>keys</i> of another type,
Robert Griesemer4023dce2009-08-14 17:41:52 -07001133called the key type.
Robert Griesemer7bc03712010-06-07 15:49:39 -07001134The value of an uninitialized map is <code>nil</code>.
Rob Pike8f2330d2009-02-25 16:20:44 -08001135</p>
Robert Griesemera3294712009-01-05 11:17:26 -08001136
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001137<pre class="ebnf">
Robert Griesemer19b1d352009-09-24 19:36:48 -07001138MapType = "map" "[" KeyType "]" ElementType .
Robert Griesemer4023dce2009-08-14 17:41:52 -07001139KeyType = Type .
Robert Griesemerc2d55862009-02-19 16:49:10 -08001140</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001141
Robert Griesemerc2d55862009-02-19 16:49:10 -08001142<p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001143The comparison operators <code>==</code> and <code>!=</code>
Robert Griesemer1d282a82010-06-03 16:55:50 -07001144<a href="#Comparison_operators">Comparison operators</a>) must be fully defined
1145for operands of the key type; thus the key type must not be a struct, array or slice.
1146If the key type is an interface type, these
Rob Pike8f2330d2009-02-25 16:20:44 -08001147comparison operators must be defined for the dynamic key values;
Rob Pike5bb29fb2010-03-25 17:59:59 -07001148failure will cause a <a href="#Run_time_panics">run-time panic</a>.
Rob Pike8f2330d2009-02-25 16:20:44 -08001149
1150</p>
Robert Griesemera3294712009-01-05 11:17:26 -08001151
Robert Griesemerc2d55862009-02-19 16:49:10 -08001152<pre>
1153map [string] int
Robert Griesemerb94c0d22011-01-19 23:07:21 -05001154map [*T] struct { x, y float64 }
Robert Griesemerc2d55862009-02-19 16:49:10 -08001155map [string] interface {}
1156</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001157
Rob Pike5af7de32009-02-24 15:17:59 -08001158<p>
Robert Griesemer0c2e6b32010-07-13 11:54:57 -07001159The number of map elements is called its length.
1160For a map <code>m</code>, it can be discovered using the
1161built-in function <a href="#Length_and_capacity"><code>len(m)</code></a>
Robert Griesemer1bdb1802011-04-22 16:26:51 -07001162and may change during execution. Elements may be added and removed
1163during execution using special forms of <a href="#Assignments">assignment</a>;
1164and they may be accessed with <a href="#Indexes">index</a> expressions.
Rob Pike5af7de32009-02-24 15:17:59 -08001165</p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001166<p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001167A new, empty map value is made using the built-in
Robert Griesemer0c2e6b32010-07-13 11:54:57 -07001168function <a href="#Making_slices_maps_and_channels"><code>make</code></a>,
1169which takes the map type and an optional capacity hint as arguments:
Rob Pike8f2330d2009-02-25 16:20:44 -08001170</p>
1171
1172<pre>
Rob Pikeda389742009-03-02 19:13:40 -08001173make(map[string] int)
1174make(map[string] int, 100)
Rob Pike8f2330d2009-02-25 16:20:44 -08001175</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001176
Rob Pikeda389742009-03-02 19:13:40 -08001177<p>
1178The initial capacity does not bound its size:
1179maps grow to accommodate the number of items
Robert Griesemer54731032011-05-12 09:15:59 -07001180stored in them, with the exception of <code>nil</code> maps.
1181A <code>nil</code> map is equivalent to an empty map except that no elements
1182may be added.
Rob Pikeda389742009-03-02 19:13:40 -08001183
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001184<h3 id="Channel_types">Channel types</h3>
Robert Griesemera3294712009-01-05 11:17:26 -08001185
Rob Pike8f2330d2009-02-25 16:20:44 -08001186<p>
Robert Griesemera3294712009-01-05 11:17:26 -08001187A channel provides a mechanism for two concurrently executing functions
Rob Pike8f2330d2009-02-25 16:20:44 -08001188to synchronize execution and communicate by passing a value of a
Robert Griesemer4023dce2009-08-14 17:41:52 -07001189specified element type.
Robert Griesemer7bc03712010-06-07 15:49:39 -07001190The value of an uninitialized channel is <code>nil</code>.
Rob Pike8f2330d2009-02-25 16:20:44 -08001191</p>
Robert Griesemera3294712009-01-05 11:17:26 -08001192
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001193<pre class="ebnf">
Robert Griesemer56ca6972010-05-07 18:22:40 -07001194ChannelType = ( "chan" [ "&lt;-" ] | "&lt;-" "chan" ) ElementType .
Robert Griesemerc2d55862009-02-19 16:49:10 -08001195</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001196
Rob Pike8f2330d2009-02-25 16:20:44 -08001197<p>
Robert Griesemer56ca6972010-05-07 18:22:40 -07001198The <code>&lt;-</code> operator specifies the channel <i>direction</i>,
1199<i>send</i> or <i>receive</i>. If no direction is given, the channel is
1200<i>bi-directional</i>.
1201A channel may be constrained only to send or only to receive by
1202<a href="#Conversions">conversion</a> or <a href="#Assignments">assignment</a>.
1203</p>
1204
1205<pre>
1206chan T // can be used to send and receive values of type T
Robert Griesemerb94c0d22011-01-19 23:07:21 -05001207chan&lt;- float64 // can only be used to send float64s
Robert Griesemer56ca6972010-05-07 18:22:40 -07001208&lt;-chan int // can only be used to receive ints
1209</pre>
1210
1211<p>
1212The <code>&lt;-</code> operator associates with the leftmost <code>chan</code>
1213possible:
Robert Griesemer1c369bd2010-01-27 09:35:39 -08001214</p>
1215
1216<pre>
1217chan&lt;- chan int // same as chan&lt;- (chan int)
1218chan&lt;- &lt;-chan int // same as chan&lt;- (&lt;-chan int)
1219&lt;-chan &lt;-chan int // same as &lt;-chan (&lt;-chan int)
1220chan (&lt;-chan int)
1221</pre>
1222
1223<p>
Robert Griesemer7bc03712010-06-07 15:49:39 -07001224A new, initialized channel
Robert Griesemer56ca6972010-05-07 18:22:40 -07001225value can be made using the built-in function
1226<a href="#Making_slices_maps_and_channels"><code>make</code></a>,
Robert Griesemer633957b2009-01-06 13:23:20 -08001227which takes the channel type and an optional capacity as arguments:
Rob Pike8f2330d2009-02-25 16:20:44 -08001228</p>
Robert Griesemera3294712009-01-05 11:17:26 -08001229
Robert Griesemerc2d55862009-02-19 16:49:10 -08001230<pre>
Rob Pikeda389742009-03-02 19:13:40 -08001231make(chan int, 100)
Robert Griesemerc2d55862009-02-19 16:49:10 -08001232</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001233
Rob Pike8f2330d2009-02-25 16:20:44 -08001234<p>
1235The capacity, in number of elements, sets the size of the buffer in the channel. If the
Robert Griesemerf14c29a2011-02-15 11:33:12 -08001236capacity is greater than zero, the channel is asynchronous: communication operations
1237succeed without blocking if the buffer is not full (sends) or not empty (receives),
1238and elements are received in the order they are sent.
1239If the capacity is zero or absent, the communication succeeds only when both a sender and
1240receiver are ready.
Robert Griesemer54731032011-05-12 09:15:59 -07001241A <code>nil</code> channel is never ready for communication.
Rob Pike8f2330d2009-02-25 16:20:44 -08001242</p>
Robert Griesemera3294712009-01-05 11:17:26 -08001243
Rob Pike94b67eb2009-03-24 17:40:47 -07001244<p>
Russ Cox9f2cb862011-03-11 14:47:02 -05001245A channel may be closed with the built-in function
1246<a href="#Close"><code>close</code></a>; the
1247multi-valued assignment form of the
1248<a href="#Receive_operator">receive operator</a>
1249tests whether a channel has been closed.
Rob Pike94b67eb2009-03-24 17:40:47 -07001250</p>
1251
Rob Pike83cbca52009-08-21 14:18:08 -07001252<h2 id="Properties_of_types_and_values">Properties of types and values</h2>
Robert Griesemer434c6052008-11-07 13:34:37 -08001253
Robert Griesemer7bc03712010-06-07 15:49:39 -07001254<h3 id="Type_identity">Type identity</h3>
1255
Rob Pike4501d342009-02-19 17:31:36 -08001256<p>
Robert Griesemer7bc03712010-06-07 15:49:39 -07001257Two types are either <i>identical</i> or <i>different</i>.
Robert Griesemer19b1d352009-09-24 19:36:48 -07001258</p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001259
Robert Griesemerc2d55862009-02-19 16:49:10 -08001260<p>
Robert Griesemer533dfd62009-05-13 16:56:00 -07001261Two named types are identical if their type names originate in the same
Robert Griesemer7bc03712010-06-07 15:49:39 -07001262type <a href="#Declarations_and_scope">declaration</a>.
Robert Griesemer63f01492010-05-28 14:17:30 -07001263A named and an unnamed type are always different. Two unnamed types are identical
Robert Griesemer7bc03712010-06-07 15:49:39 -07001264if the corresponding type literals are identical, that is, if they have the same
Robert Griesemer63f01492010-05-28 14:17:30 -07001265literal structure and corresponding components have identical types. In detail:
Rob Pike4501d342009-02-19 17:31:36 -08001266</p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001267
Robert Griesemerc2d55862009-02-19 16:49:10 -08001268<ul>
Robert Griesemer533dfd62009-05-13 16:56:00 -07001269 <li>Two array types are identical if they have identical element types and
1270 the same array length.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001271
Robert Griesemer533dfd62009-05-13 16:56:00 -07001272 <li>Two slice types are identical if they have identical element types.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001273
Robert Griesemer533dfd62009-05-13 16:56:00 -07001274 <li>Two struct types are identical if they have the same sequence of fields,
Russ Coxe4953512010-06-21 12:42:33 -07001275 and if corresponding fields have the same names, and identical types,
1276 and identical tags.
Robert Griesemer63f01492010-05-28 14:17:30 -07001277 Two anonymous fields are considered to have the same name. Lower-case field
1278 names from different packages are always different.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001279
Robert Griesemer533dfd62009-05-13 16:56:00 -07001280 <li>Two pointer types are identical if they have identical base types.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001281
Robert Griesemer533dfd62009-05-13 16:56:00 -07001282 <li>Two function types are identical if they have the same number of parameters
Russ Cox95625922010-06-12 11:37:13 -07001283 and result values, corresponding parameter and result types are
1284 identical, and either both functions are variadic or neither is.
Robert Griesemer533dfd62009-05-13 16:56:00 -07001285 Parameter and result names are not required to match.</li>
Robert Griesemera3294712009-01-05 11:17:26 -08001286
Robert Griesemer533dfd62009-05-13 16:56:00 -07001287 <li>Two interface types are identical if they have the same set of methods
Robert Griesemer63f01492010-05-28 14:17:30 -07001288 with the same names and identical function types. Lower-case method names from
1289 different packages are always different. The order of the methods is irrelevant.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001290
Robert Griesemer533dfd62009-05-13 16:56:00 -07001291 <li>Two map types are identical if they have identical key and value types.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001292
Robert Griesemer533dfd62009-05-13 16:56:00 -07001293 <li>Two channel types are identical if they have identical value types and
1294 the same direction.</li>
Robert Griesemerc2d55862009-02-19 16:49:10 -08001295</ul>
Robert Griesemer434c6052008-11-07 13:34:37 -08001296
Robert Griesemerc2d55862009-02-19 16:49:10 -08001297<p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001298Given the declarations
1299</p>
Robert Griesemer434c6052008-11-07 13:34:37 -08001300
Robert Griesemerc2d55862009-02-19 16:49:10 -08001301<pre>
1302type (
Robert Griesemer130ac742009-12-10 16:43:01 -08001303 T0 []string
1304 T1 []string
1305 T2 struct { a, b int }
1306 T3 struct { a, c int }
Robert Griesemerb94c0d22011-01-19 23:07:21 -05001307 T4 func(int, float64) *T0
1308 T5 func(x int, y float64) *[]string
Robert Griesemerc2d55862009-02-19 16:49:10 -08001309)
1310</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -08001311
Rob Pike8f2330d2009-02-25 16:20:44 -08001312<p>
Robert Griesemer533dfd62009-05-13 16:56:00 -07001313these types are identical:
Rob Pike8f2330d2009-02-25 16:20:44 -08001314</p>
Robert Griesemer434c6052008-11-07 13:34:37 -08001315
Robert Griesemerc2d55862009-02-19 16:49:10 -08001316<pre>
1317T0 and T0
1318[]int and []int
1319struct { a, b *T5 } and struct { a, b *T5 }
Robert Griesemerb94c0d22011-01-19 23:07:21 -05001320func(x int, y float64) *[]string and func(int, float64) (result *[]string)
Robert Griesemerc2d55862009-02-19 16:49:10 -08001321</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -08001322
Rob Pike8f2330d2009-02-25 16:20:44 -08001323<p>
Robert Griesemer7bc03712010-06-07 15:49:39 -07001324<code>T0</code> and <code>T1</code> are different because they are named types
Robert Griesemerb94c0d22011-01-19 23:07:21 -05001325with distinct declarations; <code>func(int, float64) *T0</code> and
1326<code>func(x int, y float64) *[]string</code> are different because <code>T0</code>
Robert Griesemer7bc03712010-06-07 15:49:39 -07001327is different from <code>[]string</code>.
Robert Griesemer533dfd62009-05-13 16:56:00 -07001328</p>
1329
Robert Griesemer434c6052008-11-07 13:34:37 -08001330
Robert Griesemer440cc952010-06-07 17:40:21 -07001331<h3 id="Assignability">Assignability</h3>
Rob Pike5af7de32009-02-24 15:17:59 -08001332
Rob Pike5af7de32009-02-24 15:17:59 -08001333<p>
Robert Griesemer440cc952010-06-07 17:40:21 -07001334A value <code>x</code> is <i>assignable</i> to a variable of type <code>T</code>
1335("<code>x</code> is assignable to <code>T</code>") in any of these cases:
Rob Pike5af7de32009-02-24 15:17:59 -08001336</p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07001337
Rob Pike5af7de32009-02-24 15:17:59 -08001338<ul>
1339<li>
Robert Griesemer7bc03712010-06-07 15:49:39 -07001340<code>x</code>'s type is identical to <code>T</code>.
1341</li>
1342<li>
Rob Pike68f16092010-09-01 10:40:50 +10001343<code>x</code>'s type <code>V</code> and <code>T</code> have identical
1344<a href="#Types">underlying types</a> and at least one of <code>V</code>
1345or <code>T</code> is not a named type.
Rob Pike5af7de32009-02-24 15:17:59 -08001346</li>
1347<li>
Robert Griesemer19b1d352009-09-24 19:36:48 -07001348<code>T</code> is an interface type and
Robert Griesemer7bc03712010-06-07 15:49:39 -07001349<code>x</code> <a href="#Interface_types">implements</a> <code>T</code>.
Robert Griesemere2cb60b2009-06-19 13:03:01 -07001350</li>
1351<li>
Robert Griesemer7bc03712010-06-07 15:49:39 -07001352<code>x</code> is a bidirectional channel value, <code>T</code> is a channel type,
1353<code>x</code>'s type <code>V</code> and <code>T</code> have identical element types,
Rob Pike68f16092010-09-01 10:40:50 +10001354and at least one of <code>V</code> or <code>T</code> is not a named type.
Robert Griesemer7bc03712010-06-07 15:49:39 -07001355</li>
1356<li>
1357<code>x</code> is the predeclared identifier <code>nil</code> and <code>T</code>
1358is a pointer, function, slice, map, channel, or interface type.
1359</li>
1360<li>
1361<code>x</code> is an untyped <a href="#Constants">constant</a> representable
1362by a value of type <code>T</code>.
Robert Griesemer4e56b332009-09-10 10:14:00 -07001363</li>
Rob Pike5af7de32009-02-24 15:17:59 -08001364</ul>
1365
Robert Griesemer19b1d352009-09-24 19:36:48 -07001366<p>
Robert Griesemerfa3d0d72011-02-03 10:53:31 -08001367If <code>T</code> is a struct type with non-<a href="#Exported_identifiers">exported</a>
1368fields, the assignment must be in the same package in which <code>T</code> is declared,
1369or <code>x</code> must be the receiver of a method call.
Robert Griesemer326ef132009-09-28 19:21:15 -07001370In other words, a struct value can be assigned to a struct variable only if
Robert Griesemerfa3d0d72011-02-03 10:53:31 -08001371every field of the struct may be legally assigned individually by the program,
1372or if the assignment is initializing the receiver of a method of the struct type.
Robert Griesemer326ef132009-09-28 19:21:15 -07001373</p>
1374
1375<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07001376Any value may be assigned to the <a href="#Blank_identifier">blank identifier</a>.
1377</p>
1378
Rob Pikea9ed30f2009-02-23 19:26:07 -08001379
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001380<h2 id="Blocks">Blocks</h2>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001381
1382<p>
Robert Griesemer0a162a12009-08-19 16:44:04 -07001383A <i>block</i> is a sequence of declarations and statements within matching
1384brace brackets.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001385</p>
1386
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001387<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -08001388Block = "{" { Statement ";" } "}" .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001389</pre>
Russ Cox5958dd62009-03-04 17:19:21 -08001390
Rob Pikea9ed30f2009-02-23 19:26:07 -08001391<p>
Robert Griesemer0a162a12009-08-19 16:44:04 -07001392In addition to explicit blocks in the source code, there are implicit blocks:
Rob Pikea9ed30f2009-02-23 19:26:07 -08001393</p>
1394
1395<ol>
Robert Griesemer0a162a12009-08-19 16:44:04 -07001396 <li>The <i>universe block</i> encompasses all Go source text.</li>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001397
Robert Griesemer4e56b332009-09-10 10:14:00 -07001398 <li>Each <a href="#Packages">package</a> has a <i>package block</i> containing all
Robert Griesemer0a162a12009-08-19 16:44:04 -07001399 Go source text for that package.</li>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001400
Robert Griesemer0a162a12009-08-19 16:44:04 -07001401 <li>Each file has a <i>file block</i> containing all Go source text
1402 in that file.</li>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001403
Robert Griesemer0a162a12009-08-19 16:44:04 -07001404 <li>Each <code>if</code>, <code>for</code>, and <code>switch</code>
1405 statement is considered to be in its own implicit block.</li>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001406
Russ Cox16b95ba2009-08-20 10:22:52 -07001407 <li>Each clause in a <code>switch</code> or <code>select</code> statement
Robert Griesemer0a162a12009-08-19 16:44:04 -07001408 acts as an implicit block.</li>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001409</ol>
1410
Robert Griesemer0a162a12009-08-19 16:44:04 -07001411<p>
Robert Griesemer4e56b332009-09-10 10:14:00 -07001412Blocks nest and influence <a href="#Declarations_and_scope">scoping</a>.
Robert Griesemer0a162a12009-08-19 16:44:04 -07001413</p>
1414
1415
Robert Griesemeraeaab592009-08-31 17:30:55 -07001416<h2 id="Declarations_and_scope">Declarations and scope</h2>
Robert Griesemer0a162a12009-08-19 16:44:04 -07001417
1418<p>
Robert Griesemer4e56b332009-09-10 10:14:00 -07001419A declaration binds a non-<a href="#Blank_identifier">blank</a>
1420identifier to a constant, type, variable, function, or package.
Robert Griesemer0a162a12009-08-19 16:44:04 -07001421Every identifier in a program must be declared.
1422No identifier may be declared twice in the same block, and
1423no identifier may be declared in both the file and package block.
1424</p>
1425
1426<pre class="ebnf">
1427Declaration = ConstDecl | TypeDecl | VarDecl .
1428TopLevelDecl = Declaration | FunctionDecl | MethodDecl .
1429</pre>
1430
1431<p>
1432The <i>scope</i> of a declared identifier is the extent of source text in which
1433the identifier denotes the specified constant, type, variable, function, or package.
1434</p>
1435
1436<p>
1437Go is lexically scoped using blocks:
1438</p>
1439
1440<ol>
1441 <li>The scope of a predeclared identifier is the universe block.</li>
1442
1443 <li>The scope of an identifier denoting a constant, type, variable,
Robert Griesemer967a2b32011-03-03 15:24:28 -08001444 or function (but not method) declared at top level (outside any
1445 function) is the package block.</li>
Robert Griesemer0a162a12009-08-19 16:44:04 -07001446
1447 <li>The scope of an imported package identifier is the file block
1448 of the file containing the import declaration.</li>
1449
1450 <li>The scope of an identifier denoting a function parameter or
1451 result variable is the function body.</li>
1452
1453 <li>The scope of a constant or variable identifier declared
1454 inside a function begins at the end of the ConstSpec or VarSpec
Robert Griesemer95b81372011-06-12 12:09:50 -07001455 (ShortVarDecl for short variable declarations)
Robert Griesemer0a162a12009-08-19 16:44:04 -07001456 and ends at the end of the innermost containing block.</li>
1457
1458 <li>The scope of a type identifier declared inside a function
Russ Cox16b95ba2009-08-20 10:22:52 -07001459 begins at the identifier in the TypeSpec
Robert Griesemer0a162a12009-08-19 16:44:04 -07001460 and ends at the end of the innermost containing block.</li>
1461</ol>
1462
1463<p>
1464An identifier declared in a block may be redeclared in an inner block.
1465While the identifier of the inner declaration is in scope, it denotes
1466the entity declared by the inner declaration.
1467</p>
1468
1469<p>
Robert Griesemer506c0082009-09-08 15:41:14 -07001470The <a href="#Package_clause">package clause</a> is not a declaration; the package name
Robert Griesemer0a162a12009-08-19 16:44:04 -07001471does not appear in any scope. Its purpose is to identify the files belonging
Robert Griesemer997851e2009-09-25 15:36:25 -07001472to the same <a href="#Packages">package</a> and to specify the default package name for import
Robert Griesemer0a162a12009-08-19 16:44:04 -07001473declarations.
1474</p>
1475
1476
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001477<h3 id="Label_scopes">Label scopes</h3>
Robert Griesemer0a162a12009-08-19 16:44:04 -07001478
1479<p>
Robert Griesemer506c0082009-09-08 15:41:14 -07001480Labels are declared by <a href="#Labeled_statements">labeled statements</a> and are
Robert Griesemer0a162a12009-08-19 16:44:04 -07001481used in the <code>break</code>, <code>continue</code>, and <code>goto</code>
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001482statements (§<a href="#Break_statements">Break statements</a>, §<a href="#Continue_statements">Continue statements</a>, §<a href="#Goto_statements">Goto statements</a>).
Russ Cox108564d2011-03-15 13:51:24 -04001483It is illegal to define a label that is never used.
Robert Griesemer0a162a12009-08-19 16:44:04 -07001484In contrast to other identifiers, labels are not block scoped and do
1485not conflict with identifiers that are not labels. The scope of a label
1486is the body of the function in which it is declared and excludes
1487the body of any nested function.
1488</p>
1489
1490
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001491<h3 id="Predeclared_identifiers">Predeclared identifiers</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001492
1493<p>
Robert Griesemer0a162a12009-08-19 16:44:04 -07001494The following identifiers are implicitly declared in the universe block:
Rob Pikea9ed30f2009-02-23 19:26:07 -08001495</p>
1496<pre class="grammar">
1497Basic types:
Robert Griesemer7bc03712010-06-07 15:49:39 -07001498 bool byte complex64 complex128 float32 float64
1499 int8 int16 int32 int64 string uint8 uint16 uint32 uint64
Rob Pikea9ed30f2009-02-23 19:26:07 -08001500
Rob Pike5af7de32009-02-24 15:17:59 -08001501Architecture-specific convenience types:
Robert Griesemerb94c0d22011-01-19 23:07:21 -05001502 int uint uintptr
Rob Pikea9ed30f2009-02-23 19:26:07 -08001503
1504Constants:
Robert Griesemer19b1d352009-09-24 19:36:48 -07001505 true false iota
1506
1507Zero value:
1508 nil
Rob Pikea9ed30f2009-02-23 19:26:07 -08001509
1510Functions:
Russ Cox9f2cb862011-03-11 14:47:02 -05001511 append cap close complex copy imag len
Robert Griesemer07e983a2010-10-25 16:50:31 -07001512 make new panic print println real recover
Rob Pikea9ed30f2009-02-23 19:26:07 -08001513</pre>
1514
Robert Griesemeraeaab592009-08-31 17:30:55 -07001515
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001516<h3 id="Exported_identifiers">Exported identifiers</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001517
1518<p>
Robert Griesemeraeaab592009-08-31 17:30:55 -07001519An identifier may be <i>exported</i> to permit access to it from another package
1520using a <a href="#Qualified_identifiers">qualified identifier</a>. An identifier
1521is exported if both:
Rob Pikea9ed30f2009-02-23 19:26:07 -08001522</p>
1523<ol>
Robert Griesemer3af480372010-05-14 13:11:48 -07001524 <li>the first character of the identifier's name is a Unicode upper case letter (Unicode class "Lu"); and</li>
Rob Pike678625d2009-09-15 09:54:22 -07001525 <li>the identifier is declared in the <a href="#Blocks">package block</a> or denotes a field or method of a type
Robert Griesemer3af480372010-05-14 13:11:48 -07001526 declared in that block.</li>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001527</ol>
1528<p>
Robert Griesemeraeaab592009-08-31 17:30:55 -07001529All other identifiers are not exported.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001530</p>
1531
Robert Griesemeraeaab592009-08-31 17:30:55 -07001532
Robert Griesemer4e56b332009-09-10 10:14:00 -07001533<h3 id="Blank_identifier">Blank identifier</h3>
1534
1535<p>
1536The <i>blank identifier</i>, represented by the underscore character <code>_</code>, may be used in a declaration like
1537any other identifier but the declaration does not introduce a new binding.
1538</p>
1539
1540
Robert Griesemer19b1d352009-09-24 19:36:48 -07001541<h3 id="Constant_declarations">Constant declarations</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001542
1543<p>
1544A constant declaration binds a list of identifiers (the names of
Robert Griesemer506c0082009-09-08 15:41:14 -07001545the constants) to the values of a list of <a href="#Constant_expressions">constant expressions</a>.
1546The number of identifiers must be equal
1547to the number of expressions, and the <i>n</i>th identifier on
1548the left is bound to the value of the <i>n</i>th expression on the
Rob Pikea9ed30f2009-02-23 19:26:07 -08001549right.
1550</p>
1551
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001552<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -08001553ConstDecl = "const" ( ConstSpec | "(" { ConstSpec ";" } ")" ) .
Robert Griesemer4023dce2009-08-14 17:41:52 -07001554ConstSpec = IdentifierList [ [ Type ] "=" ExpressionList ] .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001555
1556IdentifierList = identifier { "," identifier } .
1557ExpressionList = Expression { "," Expression } .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001558</pre>
1559
1560<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07001561If the type is present, all constants take the type specified, and
Robert Griesemer440cc952010-06-07 17:40:21 -07001562the expressions must be <a href="#Assignability">assignable</a> to that type.
Robert Griesemer4023dce2009-08-14 17:41:52 -07001563If the type is omitted, the constants take the
Robert Griesemer19b1d352009-09-24 19:36:48 -07001564individual types of the corresponding expressions.
1565If the expression values are untyped <a href="#Constants">constants</a>,
1566the declared constants remain untyped and the constant identifiers
1567denote the constant values. For instance, if the expression is a
1568floating-point literal, the constant identifier denotes a floating-point
1569constant, even if the literal's fractional part is zero.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001570</p>
1571
1572<pre>
1573const Pi float64 = 3.14159265358979323846
Robert Griesemer19b1d352009-09-24 19:36:48 -07001574const zero = 0.0 // untyped floating-point constant
Rob Pikea9ed30f2009-02-23 19:26:07 -08001575const (
Robert Griesemer130ac742009-12-10 16:43:01 -08001576 size int64 = 1024
1577 eof = -1 // untyped integer constant
Rob Pikea9ed30f2009-02-23 19:26:07 -08001578)
Robert Griesemer19b1d352009-09-24 19:36:48 -07001579const a, b, c = 3, 4, "foo" // a = 3, b = 4, c = "foo", untyped integer and string constants
Robert Griesemerb94c0d22011-01-19 23:07:21 -05001580const u, v float32 = 0, 3 // u = 0.0, v = 3.0
Rob Pikea9ed30f2009-02-23 19:26:07 -08001581</pre>
1582
1583<p>
1584Within a parenthesized <code>const</code> declaration list the
1585expression list may be omitted from any but the first declaration.
1586Such an empty list is equivalent to the textual substitution of the
Rob Pike678625d2009-09-15 09:54:22 -07001587first preceding non-empty expression list and its type if any.
Russ Cox5958dd62009-03-04 17:19:21 -08001588Omitting the list of expressions is therefore equivalent to
1589repeating the previous list. The number of identifiers must be equal
1590to the number of expressions in the previous list.
Robert Griesemer506c0082009-09-08 15:41:14 -07001591Together with the <a href="#Iota"><code>iota</code> constant generator</a>
1592this mechanism permits light-weight declaration of sequential values:
Rob Pikea9ed30f2009-02-23 19:26:07 -08001593</p>
1594
1595<pre>
1596const (
Robert Griesemer130ac742009-12-10 16:43:01 -08001597 Sunday = iota
1598 Monday
1599 Tuesday
1600 Wednesday
1601 Thursday
1602 Friday
1603 Partyday
1604 numberOfDays // this constant is not exported
Rob Pikea9ed30f2009-02-23 19:26:07 -08001605)
1606</pre>
1607
1608
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001609<h3 id="Iota">Iota</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001610
1611<p>
Robert Griesemer39f009c2010-04-29 10:57:27 -07001612Within a <a href="#Constant_declarations">constant declaration</a>, the predeclared identifier
Robert Griesemer19b1d352009-09-24 19:36:48 -07001613<code>iota</code> represents successive untyped integer <a href="#Constants">
1614constants</a>. It is reset to 0 whenever the reserved word <code>const</code>
Robert Griesemer39f009c2010-04-29 10:57:27 -07001615appears in the source and increments after each <a href="#ConstSpec">ConstSpec</a>.
1616It can be used to construct a set of related constants:
Rob Pikea9ed30f2009-02-23 19:26:07 -08001617</p>
1618
1619<pre>
Robert Griesemer4e56b332009-09-10 10:14:00 -07001620const ( // iota is reset to 0
Robert Griesemer130ac742009-12-10 16:43:01 -08001621 c0 = iota // c0 == 0
1622 c1 = iota // c1 == 1
1623 c2 = iota // c2 == 2
Rob Pikea9ed30f2009-02-23 19:26:07 -08001624)
1625
1626const (
Robert Griesemer130ac742009-12-10 16:43:01 -08001627 a = 1 &lt;&lt; iota // a == 1 (iota has been reset)
1628 b = 1 &lt;&lt; iota // b == 2
1629 c = 1 &lt;&lt; iota // c == 4
Rob Pikea9ed30f2009-02-23 19:26:07 -08001630)
1631
1632const (
Robert Griesemerb94c0d22011-01-19 23:07:21 -05001633 u = iota * 42 // u == 0 (untyped integer constant)
1634 v float64 = iota * 42 // v == 42.0 (float64 constant)
1635 w = iota * 42 // w == 84 (untyped integer constant)
Rob Pikea9ed30f2009-02-23 19:26:07 -08001636)
1637
Robert Griesemer130ac742009-12-10 16:43:01 -08001638const x = iota // x == 0 (iota has been reset)
1639const y = iota // y == 0 (iota has been reset)
Rob Pikea9ed30f2009-02-23 19:26:07 -08001640</pre>
1641
1642<p>
1643Within an ExpressionList, the value of each <code>iota</code> is the same because
Robert Griesemer39f009c2010-04-29 10:57:27 -07001644it is only incremented after each ConstSpec:
Rob Pikea9ed30f2009-02-23 19:26:07 -08001645</p>
1646
1647<pre>
1648const (
Robert Griesemer130ac742009-12-10 16:43:01 -08001649 bit0, mask0 = 1 &lt;&lt; iota, 1 &lt;&lt; iota - 1 // bit0 == 1, mask0 == 0
1650 bit1, mask1 // bit1 == 2, mask1 == 1
1651 _, _ // skips iota == 2
1652 bit3, mask3 // bit3 == 8, mask3 == 7
Rob Pikea9ed30f2009-02-23 19:26:07 -08001653)
1654</pre>
1655
1656<p>
1657This last example exploits the implicit repetition of the
1658last non-empty expression list.
1659</p>
1660
1661
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001662<h3 id="Type_declarations">Type declarations</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001663
1664<p>
Robert Griesemerfc61b772009-09-28 14:10:20 -07001665A type declaration binds an identifier, the <i>type name</i>, to a new type
Robert Griesemer7bc03712010-06-07 15:49:39 -07001666that has the same <a href="#Types">underlying type</a> as
1667an existing type. The new type is <a href="#Type_identity">different</a> from
1668the existing type.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001669</p>
1670
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001671<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -08001672TypeDecl = "type" ( TypeSpec | "(" { TypeSpec ";" } ")" ) .
Robert Griesemerbdec3302009-08-31 17:57:14 -07001673TypeSpec = identifier Type .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001674</pre>
1675
1676<pre>
Robert Griesemerbdec3302009-08-31 17:57:14 -07001677type IntArray [16]int
Rob Pikea9ed30f2009-02-23 19:26:07 -08001678
1679type (
Robert Griesemer777a96a2010-12-02 12:32:14 -08001680 Point struct { x, y float64 }
Rob Pikea9ed30f2009-02-23 19:26:07 -08001681 Polar Point
1682)
1683
1684type TreeNode struct {
Robert Griesemer130ac742009-12-10 16:43:01 -08001685 left, right *TreeNode
1686 value *Comparable
Rob Pikea9ed30f2009-02-23 19:26:07 -08001687}
1688
Rob Pike678625d2009-09-15 09:54:22 -07001689type Cipher interface {
Robert Griesemer130ac742009-12-10 16:43:01 -08001690 BlockSize() int
1691 Encrypt(src, dst []byte)
1692 Decrypt(src, dst []byte)
Rob Pikea9ed30f2009-02-23 19:26:07 -08001693}
1694</pre>
1695
Robert Griesemerfc61b772009-09-28 14:10:20 -07001696<p>
1697The declared type does not inherit any <a href="#Method_declarations">methods</a>
Robert Griesemer2a838d62011-02-08 13:31:01 -08001698bound to the existing type, but the <a href="#Method_sets">method set</a>
Robert Griesemerd4a16192010-04-01 12:48:34 -07001699of an interface type or of elements of a composite type remains unchanged:
Robert Griesemerfc61b772009-09-28 14:10:20 -07001700</p>
1701
1702<pre>
Rob Pikebdbe0de2011-05-25 06:00:07 +10001703// A Mutex is a data type with two methods, Lock and Unlock.
Robert Griesemerfc61b772009-09-28 14:10:20 -07001704type Mutex struct { /* Mutex fields */ }
1705func (m *Mutex) Lock() { /* Lock implementation */ }
1706func (m *Mutex) Unlock() { /* Unlock implementation */ }
1707
1708// NewMutex has the same composition as Mutex but its method set is empty.
1709type NewMutex Mutex
1710
Robert Griesemer2a838d62011-02-08 13:31:01 -08001711// The method set of the <a href="#Pointer_types">base type</a> of PtrMutex remains unchanged,
1712// but the method set of PtrMutex is empty.
1713type PtrMutex *Mutex
1714
Robert Griesemerf5b3c142010-04-27 17:52:44 -07001715// The method set of *PrintableMutex contains the methods
Rob Pike4fe41922009-11-07 22:00:59 -08001716// Lock and Unlock bound to its anonymous field Mutex.
Robert Griesemerfc61b772009-09-28 14:10:20 -07001717type PrintableMutex struct {
Robert Griesemer130ac742009-12-10 16:43:01 -08001718 Mutex
Robert Griesemerfc61b772009-09-28 14:10:20 -07001719}
Robert Griesemer735e00d2010-03-31 16:37:22 -07001720
Robert Griesemerd4a16192010-04-01 12:48:34 -07001721// MyCipher is an interface type that has the same method set as Cipher.
Robert Griesemer735e00d2010-03-31 16:37:22 -07001722type MyCipher Cipher
Robert Griesemerfc61b772009-09-28 14:10:20 -07001723</pre>
1724
1725<p>
1726A type declaration may be used to define a different boolean, numeric, or string
1727type and attach methods to it:
1728</p>
1729
1730<pre>
1731type TimeZone int
1732
1733const (
Robert Griesemer130ac742009-12-10 16:43:01 -08001734 EST TimeZone = -(5 + iota)
1735 CST
1736 MST
1737 PST
Robert Griesemerfc61b772009-09-28 14:10:20 -07001738)
1739
1740func (tz TimeZone) String() string {
Robert Griesemer130ac742009-12-10 16:43:01 -08001741 return fmt.Sprintf("GMT+%dh", tz)
Robert Griesemerfc61b772009-09-28 14:10:20 -07001742}
1743</pre>
1744
1745
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001746<h3 id="Variable_declarations">Variable declarations</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001747
1748<p>
1749A variable declaration creates a variable, binds an identifier to it and
1750gives it a type and optionally an initial value.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001751</p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001752<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -08001753VarDecl = "var" ( VarSpec | "(" { VarSpec ";" } ")" ) .
Robert Griesemer4023dce2009-08-14 17:41:52 -07001754VarSpec = IdentifierList ( Type [ "=" ExpressionList ] | "=" ExpressionList ) .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001755</pre>
1756
1757<pre>
1758var i int
Robert Griesemerb94c0d22011-01-19 23:07:21 -05001759var U, V, W float64
Rob Pikea9ed30f2009-02-23 19:26:07 -08001760var k = 0
Robert Griesemerb94c0d22011-01-19 23:07:21 -05001761var x, y float32 = -1, -2
Rob Pikea9ed30f2009-02-23 19:26:07 -08001762var (
Robert Griesemer130ac742009-12-10 16:43:01 -08001763 i int
Rob Pikea9ed30f2009-02-23 19:26:07 -08001764 u, v, s = 2.0, 3.0, "bar"
1765)
Robert Griesemer4e56b332009-09-10 10:14:00 -07001766var re, im = complexSqrt(-1)
Robert Griesemer130ac742009-12-10 16:43:01 -08001767var _, found = entries[name] // map lookup; only interested in "found"
Rob Pikea9ed30f2009-02-23 19:26:07 -08001768</pre>
1769
1770<p>
Robert Griesemeref45e642009-08-21 11:25:00 -07001771If a list of expressions is given, the variables are initialized
Rob Pike678625d2009-09-15 09:54:22 -07001772by assigning the expressions to the variables (§<a href="#Assignments">Assignments</a>)
1773in order; all expressions must be consumed and all variables initialized from them.
Rob Pike4fe41922009-11-07 22:00:59 -08001774Otherwise, each variable is initialized to its <a href="#The_zero_value">zero value</a>.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001775</p>
Robert Griesemeref45e642009-08-21 11:25:00 -07001776
Rob Pikea9ed30f2009-02-23 19:26:07 -08001777<p>
Robert Griesemeref45e642009-08-21 11:25:00 -07001778If the type is present, each variable is given that type.
1779Otherwise, the types are deduced from the assignment
1780of the expression list.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001781</p>
Robert Griesemeref45e642009-08-21 11:25:00 -07001782
Rob Pikea9ed30f2009-02-23 19:26:07 -08001783<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07001784If the type is absent and the corresponding expression evaluates to an
1785untyped <a href="#Constants">constant</a>, the type of the declared variable
Robert Griesemerb94c0d22011-01-19 23:07:21 -05001786is <code>bool</code>, <code>int</code>, <code>float64</code>, or <code>string</code>
Robert Griesemer19b1d352009-09-24 19:36:48 -07001787respectively, depending on whether the value is a boolean, integer,
1788floating-point, or string constant:
Rob Pikea9ed30f2009-02-23 19:26:07 -08001789</p>
1790
1791<pre>
Robert Griesemer19b1d352009-09-24 19:36:48 -07001792var b = true // t has type bool
Rob Pikea9ed30f2009-02-23 19:26:07 -08001793var i = 0 // i has type int
Robert Griesemerb94c0d22011-01-19 23:07:21 -05001794var f = 3.0 // f has type float64
Robert Griesemeref45e642009-08-21 11:25:00 -07001795var s = "OMDB" // s has type string
Rob Pikea9ed30f2009-02-23 19:26:07 -08001796</pre>
1797
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001798<h3 id="Short_variable_declarations">Short variable declarations</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001799
Robert Griesemer997851e2009-09-25 15:36:25 -07001800<p>
Robert Griesemeref45e642009-08-21 11:25:00 -07001801A <i>short variable declaration</i> uses the syntax:
Robert Griesemer997851e2009-09-25 15:36:25 -07001802</p>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001803
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001804<pre class="ebnf">
Robert Griesemere1b8cb82009-07-16 20:31:41 -07001805ShortVarDecl = IdentifierList ":=" ExpressionList .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001806</pre>
1807
Robert Griesemer997851e2009-09-25 15:36:25 -07001808<p>
Robert Griesemeref45e642009-08-21 11:25:00 -07001809It is a shorthand for a regular variable declaration with
1810initializer expressions but no types:
Robert Griesemer997851e2009-09-25 15:36:25 -07001811</p>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001812
1813<pre class="grammar">
1814"var" IdentifierList = ExpressionList .
1815</pre>
1816
1817<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08001818i, j := 0, 10
1819f := func() int { return 7 }
1820ch := make(chan int)
1821r, w := os.Pipe(fd) // os.Pipe() returns two values
1822_, y, _ := coord(p) // coord() returns three values; only interested in y coordinate
Rob Pikea9ed30f2009-02-23 19:26:07 -08001823</pre>
1824
1825<p>
Robert Griesemeref45e642009-08-21 11:25:00 -07001826Unlike regular variable declarations, a short variable declaration may redeclare variables provided they
Rob Pike2a1683a2009-04-19 20:04:15 -07001827were originally declared in the same block with the same type, and at
Robert Griesemer4e56b332009-09-10 10:14:00 -07001828least one of the non-<a href="#Blank_identifier">blank</a> variables is new. As a consequence, redeclaration
Rob Pike2a1683a2009-04-19 20:04:15 -07001829can only appear in a multi-variable short declaration.
1830Redeclaration does not introduce a new
1831variable; it just assigns a new value to the original.
1832</p>
1833
1834<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08001835field1, offset := nextField(str, 0)
1836field2, offset := nextField(str, offset) // redeclares offset
Rob Pike2a1683a2009-04-19 20:04:15 -07001837</pre>
1838
1839<p>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001840Short variable declarations may appear only inside functions.
1841In some contexts such as the initializers for <code>if</code>,
1842<code>for</code>, or <code>switch</code> statements,
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001843they can be used to declare local temporary variables (§<a href="#Statements">Statements</a>).
Rob Pikea9ed30f2009-02-23 19:26:07 -08001844</p>
1845
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001846<h3 id="Function_declarations">Function declarations</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001847
1848<p>
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001849A function declaration binds an identifier to a function (§<a href="#Function_types">Function types</a>).
Rob Pikea9ed30f2009-02-23 19:26:07 -08001850</p>
1851
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001852<pre class="ebnf">
Robert Griesemer0a162a12009-08-19 16:44:04 -07001853FunctionDecl = "func" identifier Signature [ Body ] .
Anthony Martin11a01612010-12-13 22:19:41 -08001854Body = Block .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001855</pre>
1856
Robert Griesemer4023dce2009-08-14 17:41:52 -07001857<p>
1858A function declaration may omit the body. Such a declaration provides the
1859signature for a function implemented outside Go, such as an assembly routine.
1860</p>
1861
Rob Pikea9ed30f2009-02-23 19:26:07 -08001862<pre>
1863func min(x int, y int) int {
1864 if x &lt; y {
Robert Griesemer130ac742009-12-10 16:43:01 -08001865 return x
Rob Pikea9ed30f2009-02-23 19:26:07 -08001866 }
Robert Griesemer130ac742009-12-10 16:43:01 -08001867 return y
Rob Pikea9ed30f2009-02-23 19:26:07 -08001868}
Robert Griesemer4023dce2009-08-14 17:41:52 -07001869
1870func flushICache(begin, end uintptr) // implemented externally
Rob Pikea9ed30f2009-02-23 19:26:07 -08001871</pre>
1872
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001873<h3 id="Method_declarations">Method declarations</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001874
1875<p>
Robert Griesemere9192752009-12-01 16:15:53 -08001876A method is a function with a <i>receiver</i>.
1877A method declaration binds an identifier to a method.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001878</p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001879<pre class="ebnf">
Rob Pike4fe41922009-11-07 22:00:59 -08001880MethodDecl = "func" Receiver MethodName Signature [ Body ] .
1881Receiver = "(" [ identifier ] [ "*" ] BaseTypeName ")" .
Robert Griesemerfc61b772009-09-28 14:10:20 -07001882BaseTypeName = identifier .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001883</pre>
1884
1885<p>
Robert Griesemer56809d02009-05-20 11:02:48 -07001886The receiver type must be of the form <code>T</code> or <code>*T</code> where
1887<code>T</code> is a type name. <code>T</code> is called the
1888<i>receiver base type</i> or just <i>base type</i>.
1889The base type must not be a pointer or interface type and must be
Stephen Ma5db1d382009-09-02 20:09:25 -07001890declared in the same package as the method.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001891The method is said to be <i>bound</i> to the base type
1892and is visible only within selectors for that type
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001893<a href="#Type_declarations">Type declarations</a>, §<a href="#Selectors">Selectors</a>).
Rob Pikea9ed30f2009-02-23 19:26:07 -08001894</p>
1895
1896<p>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001897Given type <code>Point</code>, the declarations
1898</p>
1899
1900<pre>
Robert Griesemer777a96a2010-12-02 12:32:14 -08001901func (p *Point) Length() float64 {
1902 return math.Sqrt(p.x * p.x + p.y * p.y)
Rob Pikea9ed30f2009-02-23 19:26:07 -08001903}
1904
Robert Griesemer777a96a2010-12-02 12:32:14 -08001905func (p *Point) Scale(factor float64) {
1906 p.x *= factor
1907 p.y *= factor
Rob Pikea9ed30f2009-02-23 19:26:07 -08001908}
1909</pre>
1910
1911<p>
Rob Pike678625d2009-09-15 09:54:22 -07001912bind the methods <code>Length</code> and <code>Scale</code>,
1913with receiver type <code>*Point</code>,
Rob Pikea9ed30f2009-02-23 19:26:07 -08001914to the base type <code>Point</code>.
1915</p>
1916
1917<p>
Robert Griesemere9192752009-12-01 16:15:53 -08001918If the receiver's value is not referenced inside the body of the method,
Rob Pikea9ed30f2009-02-23 19:26:07 -08001919its identifier may be omitted in the declaration. The same applies in
1920general to parameters of functions and methods.
1921</p>
1922
1923<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08001924The type of a method is the type of a function with the receiver as first
1925argument. For instance, the method <code>Scale</code> has type
1926</p>
1927
1928<pre>
Robert Griesemer777a96a2010-12-02 12:32:14 -08001929func(p *Point, factor float64)
Rob Pikedf3183f2009-02-26 16:37:23 -08001930</pre>
1931
1932<p>
1933However, a function declared this way is not a method.
1934</p>
1935
Rob Pikea9ed30f2009-02-23 19:26:07 -08001936
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001937<h2 id="Expressions">Expressions</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001938
Rob Pikedf3183f2009-02-26 16:37:23 -08001939<p>
1940An expression specifies the computation of a value by applying
Robert Griesemer19b1d352009-09-24 19:36:48 -07001941operators and functions to operands.
Rob Pikedf3183f2009-02-26 16:37:23 -08001942</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001943
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001944<h3 id="Operands">Operands</h3>
Robert Griesemerad711102008-09-11 17:48:20 -07001945
Robert Griesemer997851e2009-09-25 15:36:25 -07001946<p>
Robert Griesemerad711102008-09-11 17:48:20 -07001947Operands denote the elementary values in an expression.
Robert Griesemer997851e2009-09-25 15:36:25 -07001948</p>
Robert Griesemerad711102008-09-11 17:48:20 -07001949
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001950<pre class="ebnf">
Rob Pike01cadde2009-09-15 15:56:44 -07001951Operand = Literal | QualifiedIdent | MethodExpr | "(" Expression ")" .
Rob Pikedf3183f2009-02-26 16:37:23 -08001952Literal = BasicLit | CompositeLit | FunctionLit .
Rob Pike72970872010-03-04 12:35:16 -08001953BasicLit = int_lit | float_lit | imaginary_lit | char_lit | string_lit .
Robert Griesemerc2d55862009-02-19 16:49:10 -08001954</pre>
Robert Griesemerad711102008-09-11 17:48:20 -07001955
1956
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001957<h3 id="Qualified_identifiers">Qualified identifiers</h3>
Robert Griesemerad711102008-09-11 17:48:20 -07001958
Robert Griesemerc2d55862009-02-19 16:49:10 -08001959<p>
Robert Griesemer4e56b332009-09-10 10:14:00 -07001960A qualified identifier is a non-<a href="#Blank_identifier">blank</a> identifier qualified by a package name prefix.
Rob Pikedf3183f2009-02-26 16:37:23 -08001961</p>
Robert Griesemer337af312008-11-17 18:11:36 -08001962
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001963<pre class="ebnf">
Russ Cox16b95ba2009-08-20 10:22:52 -07001964QualifiedIdent = [ PackageName "." ] identifier .
Robert Griesemerc2d55862009-02-19 16:49:10 -08001965</pre>
Robert Griesemerad711102008-09-11 17:48:20 -07001966
Rob Pikedf3183f2009-02-26 16:37:23 -08001967<p>
Robert Griesemeraeaab592009-08-31 17:30:55 -07001968A qualified identifier accesses an identifier in a separate package.
1969The identifier must be <a href="#Exported_identifiers">exported</a> by that
1970package, which means that it must begin with a Unicode upper case letter.
Rob Pikedf3183f2009-02-26 16:37:23 -08001971</p>
Rob Pikedf3183f2009-02-26 16:37:23 -08001972
1973<pre>
Rob Pike678625d2009-09-15 09:54:22 -07001974math.Sin
Rob Pikedf3183f2009-02-26 16:37:23 -08001975</pre>
Robert Griesemerad711102008-09-11 17:48:20 -07001976
Robert Griesemer54731032011-05-12 09:15:59 -07001977<!--
Robert Griesemer4e56b332009-09-10 10:14:00 -07001978<p>
Robert Griesemer90cc4a52009-10-22 09:41:38 -07001979<span class="alert">TODO: Unify this section with Selectors - it's the same syntax.</span>
Robert Griesemer4e56b332009-09-10 10:14:00 -07001980</p>
Rob Pike38d7bcf2011-05-08 14:05:18 -07001981-->
Robert Griesemer4e56b332009-09-10 10:14:00 -07001982
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001983<h3 id="Composite_literals">Composite literals</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001984
Rob Pikedf3183f2009-02-26 16:37:23 -08001985<p>
1986Composite literals construct values for structs, arrays, slices, and maps
1987and create a new value each time they are evaluated.
1988They consist of the type of the value
Robert Griesemer838cf122009-05-22 10:25:06 -07001989followed by a brace-bound list of composite elements. An element may be
1990a single expression or a key-value pair.
Rob Pikedf3183f2009-02-26 16:37:23 -08001991</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001992
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001993<pre class="ebnf">
Robert Griesemera12141e2010-10-22 08:58:52 -07001994CompositeLit = LiteralType LiteralValue .
Rob Pikedf3183f2009-02-26 16:37:23 -08001995LiteralType = StructType | ArrayType | "[" "..." "]" ElementType |
Robert Griesemer07cc6442010-07-29 18:13:41 -07001996 SliceType | MapType | TypeName .
Robert Griesemera12141e2010-10-22 08:58:52 -07001997LiteralValue = "{" [ ElementList [ "," ] ] "}" .
Robert Griesemer130ac742009-12-10 16:43:01 -08001998ElementList = Element { "," Element } .
Robert Griesemer838cf122009-05-22 10:25:06 -07001999Element = [ Key ":" ] Value .
Robert Griesemerfb5fce52009-11-09 12:35:56 -08002000Key = FieldName | ElementIndex .
Rob Pike678625d2009-09-15 09:54:22 -07002001FieldName = identifier .
Robert Griesemerfb5fce52009-11-09 12:35:56 -08002002ElementIndex = Expression .
Robert Griesemera12141e2010-10-22 08:58:52 -07002003Value = Expression | LiteralValue .
Robert Griesemerc2d55862009-02-19 16:49:10 -08002004</pre>
Robert Griesemer0976e342008-09-03 13:37:44 -07002005
Rob Pikedf3183f2009-02-26 16:37:23 -08002006<p>
Robert Griesemer838cf122009-05-22 10:25:06 -07002007The LiteralType must be a struct, array, slice, or map type
2008(the grammar enforces this constraint except when the type is given
2009as a TypeName).
Robert Griesemer440cc952010-06-07 17:40:21 -07002010The types of the expressions must be <a href="#Assignability">assignable</a>
2011to the respective field, element, and key types of the LiteralType;
Russ Cox7a5e97b2009-03-03 15:40:30 -08002012there is no additional conversion.
Robert Griesemer838cf122009-05-22 10:25:06 -07002013The key is interpreted as a field name for struct literals,
Rob Pike678625d2009-09-15 09:54:22 -07002014an index expression for array and slice literals, and a key for map literals.
Robert Griesemer838cf122009-05-22 10:25:06 -07002015For map literals, all elements must have a key. It is an error
2016to specify multiple elements with the same field name or
2017constant key value.
Rob Pikedf3183f2009-02-26 16:37:23 -08002018</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002019
Robert Griesemer838cf122009-05-22 10:25:06 -07002020<p>
2021For struct literals the following rules apply:
Robert Griesemercfe92112009-06-18 13:29:40 -07002022</p>
Robert Griesemer838cf122009-05-22 10:25:06 -07002023<ul>
Robert Griesemerd3b15652009-11-16 08:58:55 -08002024 <li>A key must be a field name declared in the LiteralType.
2025 </li>
Rob Pike678625d2009-09-15 09:54:22 -07002026 <li>A literal that does not contain any keys must
Robert Griesemer838cf122009-05-22 10:25:06 -07002027 list an element for each struct field in the
2028 order in which the fields are declared.
2029 </li>
2030 <li>If any element has a key, every element must have a key.
2031 </li>
Rob Pike678625d2009-09-15 09:54:22 -07002032 <li>A literal that contains keys does not need to
Robert Griesemer838cf122009-05-22 10:25:06 -07002033 have an element for each struct field. Omitted fields
2034 get the zero value for that field.
2035 </li>
2036 <li>A literal may omit the element list; such a literal evaluates
2037 to the zero value for its type.
2038 </li>
2039 <li>It is an error to specify an element for a non-exported
2040 field of a struct belonging to a different package.
2041 </li>
2042</ul>
Robert Griesemer838cf122009-05-22 10:25:06 -07002043
2044<p>
2045Given the declarations
2046</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002047<pre>
Robert Griesemer777a96a2010-12-02 12:32:14 -08002048type Point3D struct { x, y, z float64 }
2049type Line struct { p, q Point3D }
Robert Griesemerc2d55862009-02-19 16:49:10 -08002050</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002051
Rob Pikedf3183f2009-02-26 16:37:23 -08002052<p>
2053one may write
2054</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002055
Robert Griesemerc2d55862009-02-19 16:49:10 -08002056<pre>
Robert Griesemer777a96a2010-12-02 12:32:14 -08002057origin := Point3D{} // zero value for Point3D
2058line := Line{origin, Point3D{y: -4, z: 12.3}} // zero value for line.q.x
Rob Pike37ab8382009-03-18 22:58:36 -07002059</pre>
2060
Robert Griesemercfe92112009-06-18 13:29:40 -07002061<p>
2062For array and slice literals the following rules apply:
2063</p>
Robert Griesemer838cf122009-05-22 10:25:06 -07002064<ul>
2065 <li>Each element has an associated integer index marking
2066 its position in the array.
2067 </li>
2068 <li>An element with a key uses the key as its index; the
2069 key must be a constant integer expression.
2070 </li>
2071 <li>An element without a key uses the previous element's index plus one.
2072 If the first element has no key, its index is zero.
2073 </li>
2074</ul>
Robert Griesemer838cf122009-05-22 10:25:06 -07002075
Rob Pike37ab8382009-03-18 22:58:36 -07002076<p>
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002077Taking the address of a composite literal (§<a href="#Address_operators">Address operators</a>)
Robert Griesemer0e1d9412011-01-26 11:21:23 -08002078generates a pointer to a unique instance of the literal's value.
Rob Pike37ab8382009-03-18 22:58:36 -07002079</p>
Rob Pike37ab8382009-03-18 22:58:36 -07002080<pre>
Robert Griesemer777a96a2010-12-02 12:32:14 -08002081var pointer *Point3D = &amp;Point3D{y: 1000}
Robert Griesemerc2d55862009-02-19 16:49:10 -08002082</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002083
Rob Pikedf3183f2009-02-26 16:37:23 -08002084<p>
Robert Griesemera3294712009-01-05 11:17:26 -08002085The length of an array literal is the length specified in the LiteralType.
2086If fewer elements than the length are provided in the literal, the missing
Rob Pike8f2330d2009-02-25 16:20:44 -08002087elements are set to the zero value for the array element type.
Robert Griesemer838cf122009-05-22 10:25:06 -07002088It is an error to provide elements with index values outside the index range
2089of the array. The notation <code>...</code> specifies an array length equal
2090to the maximum element index plus one.
Rob Pikedf3183f2009-02-26 16:37:23 -08002091</p>
Robert Griesemerb90b2132008-09-19 15:49:55 -07002092
Robert Griesemerc2d55862009-02-19 16:49:10 -08002093<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08002094buffer := [10]string{} // len(buffer) == 10
2095intSet := [6]int{1, 2, 3, 5} // len(intSet) == 6
2096days := [...]string{"Sat", "Sun"} // len(days) == 2
Robert Griesemerc2d55862009-02-19 16:49:10 -08002097</pre>
Robert Griesemer91bbd642009-01-07 09:31:35 -08002098
Rob Pikedf3183f2009-02-26 16:37:23 -08002099<p>
2100A slice literal describes the entire underlying array literal.
Rob Pike678625d2009-09-15 09:54:22 -07002101Thus, the length and capacity of a slice literal are the maximum
Robert Griesemer838cf122009-05-22 10:25:06 -07002102element index plus one. A slice literal has the form
Rob Pikedf3183f2009-02-26 16:37:23 -08002103</p>
Robert Griesemer91bbd642009-01-07 09:31:35 -08002104
Robert Griesemerc2d55862009-02-19 16:49:10 -08002105<pre>
Robert Griesemer3c7271f2011-05-24 14:18:44 -07002106[]T{x1, x2, … xn}
Robert Griesemerc2d55862009-02-19 16:49:10 -08002107</pre>
Robert Griesemer91bbd642009-01-07 09:31:35 -08002108
Rob Pikedf3183f2009-02-26 16:37:23 -08002109<p>
2110and is a shortcut for a slice operation applied to an array literal:
2111</p>
Robert Griesemer91bbd642009-01-07 09:31:35 -08002112
Robert Griesemerc2d55862009-02-19 16:49:10 -08002113<pre>
Robert Griesemer3c7271f2011-05-24 14:18:44 -07002114[n]T{x1, x2, … xn}[0 : n]
Robert Griesemerc2d55862009-02-19 16:49:10 -08002115</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002116
Rob Pikedf3183f2009-02-26 16:37:23 -08002117<p>
Robert Griesemera12141e2010-10-22 08:58:52 -07002118Within a composite literal of array, slice, or map type <code>T</code>,
2119elements that are themselves composite literals may elide the respective
2120literal type if it is identical to the element type of <code>T</code>.
2121</p>
2122
2123<pre>
2124[...]Point{{1.5, -3.5}, {0, 0}} // same as [...]Point{Point{1.5, -3.5}, Point{0, 0}}
2125[][]int{{1, 2, 3}, {4, 5}} // same as [][]int{[]int{1, 2, 3}, []int{4, 5}}
2126</pre>
2127
2128<p>
Russ Cox7a5e97b2009-03-03 15:40:30 -08002129A parsing ambiguity arises when a composite literal using the
Robert Griesemer07cc6442010-07-29 18:13:41 -07002130TypeName form of the LiteralType appears between the
2131<a href="#Keywords">keyword</a> and the opening brace of the block of an
Russ Cox7a5e97b2009-03-03 15:40:30 -08002132"if", "for", or "switch" statement, because the braces surrounding
2133the expressions in the literal are confused with those introducing
Robert Griesemer07cc6442010-07-29 18:13:41 -07002134the block of statements. To resolve the ambiguity in this rare case,
Russ Cox7a5e97b2009-03-03 15:40:30 -08002135the composite literal must appear within
2136parentheses.
2137</p>
2138
2139<pre>
Robert Griesemer3c7271f2011-05-24 14:18:44 -07002140if x == (T{a,b,c}[i]) { … }
2141if (x == T{a,b,c}[i]) { … }
Russ Cox7a5e97b2009-03-03 15:40:30 -08002142</pre>
2143
Robert Griesemer838cf122009-05-22 10:25:06 -07002144<p>
2145Examples of valid array, slice, and map literals:
2146</p>
2147
2148<pre>
2149// list of prime numbers
Robert Griesemer130ac742009-12-10 16:43:01 -08002150primes := []int{2, 3, 5, 7, 9, 11, 13, 17, 19, 991}
Robert Griesemer838cf122009-05-22 10:25:06 -07002151
2152// vowels[ch] is true if ch is a vowel
Robert Griesemer130ac742009-12-10 16:43:01 -08002153vowels := [128]bool{'a': true, 'e': true, 'i': true, 'o': true, 'u': true, 'y': true}
Robert Griesemer838cf122009-05-22 10:25:06 -07002154
Robert Griesemerb94c0d22011-01-19 23:07:21 -05002155// the array [10]float32{-1, 0, 0, 0, -0.1, -0.1, 0, 0, 0, -1}
2156filter := [10]float32{-1, 4: -0.1, -0.1, 9: -1}
Robert Griesemer838cf122009-05-22 10:25:06 -07002157
2158// frequencies in Hz for equal-tempered scale (A4 = 440Hz)
Robert Griesemerb94c0d22011-01-19 23:07:21 -05002159noteFrequency := map[string]float32{
Robert Griesemer838cf122009-05-22 10:25:06 -07002160 "C0": 16.35, "D0": 18.35, "E0": 20.60, "F0": 21.83,
2161 "G0": 24.50, "A0": 27.50, "B0": 30.87,
2162}
2163</pre>
2164
2165
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002166<h3 id="Function_literals">Function literals</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002167
Rob Pikedf3183f2009-02-26 16:37:23 -08002168<p>
2169A function literal represents an anonymous function.
2170It consists of a specification of the function type and a function body.
2171</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002172
Robert Griesemerf7ac31362009-07-10 16:06:40 -07002173<pre class="ebnf">
Robert Griesemer0a162a12009-08-19 16:44:04 -07002174FunctionLit = FunctionType Body .
Robert Griesemerc2d55862009-02-19 16:49:10 -08002175</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002176
Robert Griesemerc2d55862009-02-19 16:49:10 -08002177<pre>
Robert Griesemerb94c0d22011-01-19 23:07:21 -05002178func(a, b int, z float64) bool { return a*b &lt; int(z) }
Robert Griesemerc2d55862009-02-19 16:49:10 -08002179</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002180
Rob Pikedf3183f2009-02-26 16:37:23 -08002181<p>
2182A function literal can be assigned to a variable or invoked directly.
2183</p>
Robert Griesemer7231ceb2008-09-08 15:01:04 -07002184
Robert Griesemerc2d55862009-02-19 16:49:10 -08002185<pre>
Rob Pikedf3183f2009-02-26 16:37:23 -08002186f := func(x, y int) int { return x + y }
2187func(ch chan int) { ch &lt;- ACK } (reply_chan)
Robert Griesemerc2d55862009-02-19 16:49:10 -08002188</pre>
Robert Griesemer7231ceb2008-09-08 15:01:04 -07002189
Rob Pikedf3183f2009-02-26 16:37:23 -08002190<p>
2191Function literals are <i>closures</i>: they may refer to variables
Robert Griesemerd8a764c2009-02-06 17:01:10 -08002192defined in a surrounding function. Those variables are then shared between
2193the surrounding function and the function literal, and they survive as long
Rob Pikedf3183f2009-02-26 16:37:23 -08002194as they are accessible.
2195</p>
Robert Griesemer7231ceb2008-09-08 15:01:04 -07002196
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002197
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002198<h3 id="Primary_expressions">Primary expressions</h3>
Russ Cox5958dd62009-03-04 17:19:21 -08002199
Robert Griesemer164a7bc2009-09-30 12:00:25 -07002200<p>
2201Primary expressions are the operands for unary and binary expressions.
2202</p>
2203
Robert Griesemerf7ac31362009-07-10 16:06:40 -07002204<pre class="ebnf">
Robert Griesemerc2d55862009-02-19 16:49:10 -08002205PrimaryExpr =
2206 Operand |
Robert Griesemer5eb36242009-09-16 11:05:14 -07002207 Conversion |
Robert Griesemer164a7bc2009-09-30 12:00:25 -07002208 BuiltinCall |
Robert Griesemerc2d55862009-02-19 16:49:10 -08002209 PrimaryExpr Selector |
2210 PrimaryExpr Index |
2211 PrimaryExpr Slice |
Russ Cox5958dd62009-03-04 17:19:21 -08002212 PrimaryExpr TypeAssertion |
Robert Griesemerc2d55862009-02-19 16:49:10 -08002213 PrimaryExpr Call .
Robert Griesemer57b34612008-10-10 12:45:44 -07002214
Russ Cox5958dd62009-03-04 17:19:21 -08002215Selector = "." identifier .
2216Index = "[" Expression "]" .
Scott Lawrence0c1695b2010-09-07 14:30:17 -07002217Slice = "[" [ Expression ] ":" [ Expression ] "]" .
Russ Cox5958dd62009-03-04 17:19:21 -08002218TypeAssertion = "." "(" Type ")" .
Robert Griesemerac771a82010-09-24 14:08:28 -07002219Call = "(" [ ArgumentList [ "," ] ] ")" .
2220ArgumentList = ExpressionList [ "..." ] .
Robert Griesemerc2d55862009-02-19 16:49:10 -08002221</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002222
2223
Robert Griesemerc2d55862009-02-19 16:49:10 -08002224<pre>
2225x
22262
2227(s + ".txt")
2228f(3.1415, true)
Rob Pike426335f2009-03-02 17:52:52 -08002229Point{1, 2}
Robert Griesemerc2d55862009-02-19 16:49:10 -08002230m["foo"]
2231s[i : j + 1]
2232obj.color
Robert Griesemer777a96a2010-12-02 12:32:14 -08002233math.Sin
Robert Griesemerc2d55862009-02-19 16:49:10 -08002234f.p[i].x()
2235</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002236
2237
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002238<h3 id="Selectors">Selectors</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002239
Rob Pikedf3183f2009-02-26 16:37:23 -08002240<p>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002241A primary expression of the form
Rob Pikedf3183f2009-02-26 16:37:23 -08002242</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002243
Robert Griesemerc2d55862009-02-19 16:49:10 -08002244<pre>
2245x.f
2246</pre>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002247
Robert Griesemerc2d55862009-02-19 16:49:10 -08002248<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002249denotes the field or method <code>f</code> of the value denoted by <code>x</code>
Russ Coxbee2d5b2010-09-30 14:59:41 -04002250(or sometimes <code>*x</code>; see below). The identifier <code>f</code>
Rob Pikedf3183f2009-02-26 16:37:23 -08002251is called the (field or method)
Robert Griesemer4e56b332009-09-10 10:14:00 -07002252<i>selector</i>; it must not be the <a href="#Blank_identifier">blank identifier</a>.
Rob Pikedf3183f2009-02-26 16:37:23 -08002253The type of the expression is the type of <code>f</code>.
2254</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002255<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002256A selector <code>f</code> may denote a field or method <code>f</code> of
2257a type <code>T</code>, or it may refer
2258to a field or method <code>f</code> of a nested anonymous field of
2259<code>T</code>.
2260The number of anonymous fields traversed
2261to reach <code>f</code> is called its <i>depth</i> in <code>T</code>.
2262The depth of a field or method <code>f</code>
2263declared in <code>T</code> is zero.
2264The depth of a field or method <code>f</code> declared in
2265an anonymous field <code>A</code> in <code>T</code> is the
2266depth of <code>f</code> in <code>A</code> plus one.
2267</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002268<p>
Robert Griesemer071c91b2008-10-23 12:04:45 -07002269The following rules apply to selectors:
Rob Pikedf3183f2009-02-26 16:37:23 -08002270</p>
2271<ol>
2272<li>
2273For a value <code>x</code> of type <code>T</code> or <code>*T</code>
2274where <code>T</code> is not an interface type,
2275<code>x.f</code> denotes the field or method at the shallowest depth
2276in <code>T</code> where there
2277is such an <code>f</code>.
2278If there is not exactly one <code>f</code> with shallowest depth, the selector
Robert Griesemer071c91b2008-10-23 12:04:45 -07002279expression is illegal.
Rob Pikedf3183f2009-02-26 16:37:23 -08002280</li>
2281<li>
Rob Pikee041b992011-02-19 15:04:56 -08002282For a variable <code>x</code> of type <code>I</code>
Rob Pikedf3183f2009-02-26 16:37:23 -08002283where <code>I</code> is an interface type,
2284<code>x.f</code> denotes the actual method with name <code>f</code> of the value assigned
2285to <code>x</code> if there is such a method.
2286If no value or <code>nil</code> was assigned to <code>x</code>, <code>x.f</code> is illegal.
2287</li>
2288<li>
2289In all other cases, <code>x.f</code> is illegal.
Robert Griesemer3af480372010-05-14 13:11:48 -07002290</li>
Rob Pikedf3183f2009-02-26 16:37:23 -08002291</ol>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002292<p>
Russ Coxbee2d5b2010-09-30 14:59:41 -04002293Selectors automatically dereference pointers to structs.
2294If <code>x</code> is a pointer to a struct, <code>x.y</code>
2295is shorthand for <code>(*x).y</code>; if the field <code>y</code>
2296is also a pointer to a struct, <code>x.y.z</code> is shorthand
Rob Pikedf3183f2009-02-26 16:37:23 -08002297for <code>(*(*x).y).z</code>, and so on.
Russ Coxbee2d5b2010-09-30 14:59:41 -04002298If <code>x</code> contains an anonymous field of type <code>*A</code>,
2299where <code>A</code> is also a struct type,
Rob Pikedf3183f2009-02-26 16:37:23 -08002300<code>x.f</code> is a shortcut for <code>(*x.A).f</code>.
2301</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002302<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002303For example, given the declarations:
2304</p>
Robert Griesemer071c91b2008-10-23 12:04:45 -07002305
Robert Griesemerc2d55862009-02-19 16:49:10 -08002306<pre>
2307type T0 struct {
Robert Griesemer130ac742009-12-10 16:43:01 -08002308 x int
Robert Griesemerc2d55862009-02-19 16:49:10 -08002309}
Robert Griesemer071c91b2008-10-23 12:04:45 -07002310
Robert Griesemerc2d55862009-02-19 16:49:10 -08002311func (recv *T0) M0()
Robert Griesemer071c91b2008-10-23 12:04:45 -07002312
Robert Griesemerc2d55862009-02-19 16:49:10 -08002313type T1 struct {
Robert Griesemer130ac742009-12-10 16:43:01 -08002314 y int
Robert Griesemerc2d55862009-02-19 16:49:10 -08002315}
Robert Griesemer071c91b2008-10-23 12:04:45 -07002316
Robert Griesemerc2d55862009-02-19 16:49:10 -08002317func (recv T1) M1()
Robert Griesemer071c91b2008-10-23 12:04:45 -07002318
Robert Griesemerc2d55862009-02-19 16:49:10 -08002319type T2 struct {
Robert Griesemer130ac742009-12-10 16:43:01 -08002320 z int
2321 T1
2322 *T0
Robert Griesemerc2d55862009-02-19 16:49:10 -08002323}
Robert Griesemer071c91b2008-10-23 12:04:45 -07002324
Robert Griesemerc2d55862009-02-19 16:49:10 -08002325func (recv *T2) M2()
Robert Griesemer071c91b2008-10-23 12:04:45 -07002326
Robert Griesemer130ac742009-12-10 16:43:01 -08002327var p *T2 // with p != nil and p.T1 != nil
Robert Griesemerc2d55862009-02-19 16:49:10 -08002328</pre>
Robert Griesemer071c91b2008-10-23 12:04:45 -07002329
Rob Pikedf3183f2009-02-26 16:37:23 -08002330<p>
2331one may write:
2332</p>
Robert Griesemer071c91b2008-10-23 12:04:45 -07002333
Robert Griesemerc2d55862009-02-19 16:49:10 -08002334<pre>
2335p.z // (*p).z
2336p.y // ((*p).T1).y
2337p.x // (*(*p).T0).x
Robert Griesemer071c91b2008-10-23 12:04:45 -07002338
Robert Griesemerc2d55862009-02-19 16:49:10 -08002339p.M2 // (*p).M2
2340p.M1 // ((*p).T1).M1
2341p.M0 // ((*p).T0).M0
2342</pre>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002343
2344
Robert Griesemer54731032011-05-12 09:15:59 -07002345<!--
Robert Griesemer90cc4a52009-10-22 09:41:38 -07002346<span class="alert">
Robert Griesemer071c91b2008-10-23 12:04:45 -07002347TODO: Specify what happens to receivers.
Robert Griesemer90cc4a52009-10-22 09:41:38 -07002348</span>
Rob Pike38d7bcf2011-05-08 14:05:18 -07002349-->
Robert Griesemer7abfcd92008-10-07 17:14:30 -07002350
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002351
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002352<h3 id="Indexes">Indexes</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002353
Rob Pikedf3183f2009-02-26 16:37:23 -08002354<p>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002355A primary expression of the form
Rob Pikedf3183f2009-02-26 16:37:23 -08002356</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002357
Robert Griesemerc2d55862009-02-19 16:49:10 -08002358<pre>
2359a[x]
2360</pre>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002361
Rob Pike4501d342009-02-19 17:31:36 -08002362<p>
Rob Pike678625d2009-09-15 09:54:22 -07002363denotes the element of the array, slice, string or map <code>a</code> indexed by <code>x</code>.
Rob Pikedf3183f2009-02-26 16:37:23 -08002364The value <code>x</code> is called the
Rob Pike678625d2009-09-15 09:54:22 -07002365<i>index</i> or <i>map key</i>, respectively. The following
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002366rules apply:
Rob Pike4501d342009-02-19 17:31:36 -08002367</p>
Russ Coxeaa92e02009-06-25 14:43:55 -07002368
Robert Griesemerc2d55862009-02-19 16:49:10 -08002369<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002370For <code>a</code> of type <code>A</code> or <code>*A</code>
Robert Griesemer506c0082009-09-08 15:41:14 -07002371where <code>A</code> is an <a href="#Array_types">array type</a>,
2372or for <code>a</code> of type <code>S</code> where <code>S</code> is a <a href="#Slice_types">slice type</a>:
Rob Pike4501d342009-02-19 17:31:36 -08002373</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002374<ul>
Robert Griesemer29f1ca52010-03-23 14:01:51 -07002375 <li><code>x</code> must be an integer value and <code>0 &lt;= x &lt; len(a)</code></li>
Rob Pikedf3183f2009-02-26 16:37:23 -08002376 <li><code>a[x]</code> is the array element at index <code>x</code> and the type of
Robert Griesemer29f1ca52010-03-23 14:01:51 -07002377 <code>a[x]</code> is the element type of <code>A</code></li>
Robert Griesemer54731032011-05-12 09:15:59 -07002378 <li>if <code>a</code> is <code>nil</code> or if the index <code>x</code> is out of range,
Rob Pike5bb29fb2010-03-25 17:59:59 -07002379 a <a href="#Run_time_panics">run-time panic</a> occurs</li>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002380</ul>
Russ Coxeaa92e02009-06-25 14:43:55 -07002381
Robert Griesemerc2d55862009-02-19 16:49:10 -08002382<p>
Russ Coxeaa92e02009-06-25 14:43:55 -07002383For <code>a</code> of type <code>T</code>
Robert Griesemer19b1d352009-09-24 19:36:48 -07002384where <code>T</code> is a <a href="#String_types">string type</a>:
Russ Coxeaa92e02009-06-25 14:43:55 -07002385</p>
2386<ul>
Robert Griesemer29f1ca52010-03-23 14:01:51 -07002387 <li><code>x</code> must be an integer value and <code>0 &lt;= x &lt; len(a)</code></li>
Russ Coxeaa92e02009-06-25 14:43:55 -07002388 <li><code>a[x]</code> is the byte at index <code>x</code> and the type of
Robert Griesemer29f1ca52010-03-23 14:01:51 -07002389 <code>a[x]</code> is <code>byte</code></li>
Robert Griesemer3af480372010-05-14 13:11:48 -07002390 <li><code>a[x]</code> may not be assigned to</li>
Rob Pike5bb29fb2010-03-25 17:59:59 -07002391 <li>if the index <code>x</code> is out of range,
2392 a <a href="#Run_time_panics">run-time panic</a> occurs</li>
Russ Coxeaa92e02009-06-25 14:43:55 -07002393</ul>
2394
2395<p>
2396For <code>a</code> of type <code>M</code>
Robert Griesemer506c0082009-09-08 15:41:14 -07002397where <code>M</code> is a <a href="#Map_types">map type</a>:
Rob Pike4501d342009-02-19 17:31:36 -08002398</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002399<ul>
Robert Griesemer29f1ca52010-03-23 14:01:51 -07002400 <li><code>x</code>'s type must be
Robert Griesemer440cc952010-06-07 17:40:21 -07002401 <a href="#Assignability">assignable</a>
2402 to the key type of <code>M</code></li>
Robert Griesemer29f1ca52010-03-23 14:01:51 -07002403 <li>if the map contains an entry with key <code>x</code>,
2404 <code>a[x]</code> is the map value with key <code>x</code>
2405 and the type of <code>a[x]</code> is the value type of <code>M</code></li>
Robert Griesemer54731032011-05-12 09:15:59 -07002406 <li>if the map is <code>nil</code> or does not contain such an entry,
Robert Griesemer29f1ca52010-03-23 14:01:51 -07002407 <code>a[x]</code> is the <a href="#The_zero_value">zero value</a>
2408 for the value type of <code>M</code></li>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002409</ul>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002410
Robert Griesemerc2d55862009-02-19 16:49:10 -08002411<p>
Robert Griesemer29f1ca52010-03-23 14:01:51 -07002412Otherwise <code>a[x]</code> is illegal.
Rob Pikedf3183f2009-02-26 16:37:23 -08002413</p>
Robert Griesemer7abfcd92008-10-07 17:14:30 -07002414
Rob Pikedf3183f2009-02-26 16:37:23 -08002415<p>
Robert Griesemer29f1ca52010-03-23 14:01:51 -07002416An index expression on a map <code>a</code> of type <code>map[K]V</code>
2417may be used in an assignment or initialization of the special form
Rob Pikedf3183f2009-02-26 16:37:23 -08002418</p>
2419
2420<pre>
Robert Griesemer29f1ca52010-03-23 14:01:51 -07002421v, ok = a[x]
2422v, ok := a[x]
2423var v, ok = a[x]
Rob Pikedf3183f2009-02-26 16:37:23 -08002424</pre>
2425
2426<p>
Robert Griesemer29f1ca52010-03-23 14:01:51 -07002427where the result of the index expression is a pair of values with types
2428<code>(V, bool)</code>. In this form, the value of <code>ok</code> is
2429<code>true</code> if the key <code>x</code> is present in the map, and
2430<code>false</code> otherwise. The value of <code>v</code> is the value
2431<code>a[x]</code> as in the single-result form.
Rob Pikedf3183f2009-02-26 16:37:23 -08002432</p>
2433
2434<p>
Robert Griesemer54731032011-05-12 09:15:59 -07002435Similarly, if an assignment to a map element has the special form
Rob Pikedf3183f2009-02-26 16:37:23 -08002436</p>
2437
2438<pre>
Robert Griesemer29f1ca52010-03-23 14:01:51 -07002439a[x] = v, ok
Rob Pikedf3183f2009-02-26 16:37:23 -08002440</pre>
2441
2442<p>
2443and boolean <code>ok</code> has the value <code>false</code>,
2444the entry for key <code>x</code> is deleted from the map; if
2445<code>ok</code> is <code>true</code>, the construct acts like
2446a regular assignment to an element of the map.
2447</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002448
Robert Griesemer54731032011-05-12 09:15:59 -07002449<p>
2450Assigning to an element of a <code>nil</code> map causes a
2451<a href="#Run_time_panics">run-time panic</a>.
2452</p>
2453
Robert Griesemer29f1ca52010-03-23 14:01:51 -07002454
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002455<h3 id="Slices">Slices</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002456
Rob Pikedf3183f2009-02-26 16:37:23 -08002457<p>
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08002458For a string, array, or slice <code>a</code>, the primary expression
Rob Pikedf3183f2009-02-26 16:37:23 -08002459</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002460
Robert Griesemerc2d55862009-02-19 16:49:10 -08002461<pre>
Robert Griesemer9e5bf272010-09-07 16:32:35 -07002462a[low : high]
Robert Griesemerc2d55862009-02-19 16:49:10 -08002463</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002464
Rob Pikedf3183f2009-02-26 16:37:23 -08002465<p>
Robert Griesemer9e5bf272010-09-07 16:32:35 -07002466constructs a substring or slice. The index expressions <code>low</code> and
2467<code>high</code> select which elements appear in the result. The result has
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08002468indexes starting at 0 and length equal to
Robert Griesemer9e5bf272010-09-07 16:32:35 -07002469<code>high</code>&nbsp;-&nbsp;<code>low</code>.
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08002470After slicing the array <code>a</code>
2471</p>
2472
2473<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08002474a := [5]int{1, 2, 3, 4, 5}
2475s := a[1:4]
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08002476</pre>
2477
2478<p>
2479the slice <code>s</code> has type <code>[]int</code>, length 3, capacity 4, and elements
Rob Pikedf3183f2009-02-26 16:37:23 -08002480</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002481
Robert Griesemerc2d55862009-02-19 16:49:10 -08002482<pre>
2483s[0] == 2
2484s[1] == 3
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08002485s[2] == 4
Robert Griesemerc2d55862009-02-19 16:49:10 -08002486</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002487
Rob Pikedf3183f2009-02-26 16:37:23 -08002488<p>
Robert Griesemer9e5bf272010-09-07 16:32:35 -07002489For convenience, any of the index expressions may be omitted. A missing <code>low</code>
2490index defaults to zero; a missing <code>high</code> index defaults to the length of the
2491sliced operand:
Scott Lawrence0c1695b2010-09-07 14:30:17 -07002492</p>
2493
Robert Griesemer9e5bf272010-09-07 16:32:35 -07002494<pre>
2495a[2:] // same a[2 : len(a)]
2496a[:3] // same as a[0 : 3]
2497a[:] // same as a[0 : len(a)]
2498</pre>
2499
Scott Lawrence0c1695b2010-09-07 14:30:17 -07002500<p>
2501For arrays or strings, the indexes <code>low</code> and <code>high</code> must
2502satisfy 0 &lt;= <code>low</code> &lt;= <code>high</code> &lt;= length; for
2503slices, the upper bound is the capacity rather than the length.
Robert Griesemer19b1d352009-09-24 19:36:48 -07002504</p>
2505
Robert Griesemerc2d55862009-02-19 16:49:10 -08002506<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07002507If the sliced operand is a string or slice, the result of the slice operation
2508is a string or slice of the same type.
Robert Griesemerc423e952010-09-02 10:16:31 -07002509If the sliced operand is an array, it must be <a href="#Address_operators">addressable</a>
2510and the result of the slice operation is a slice with the same element type as the array.
Rob Pikedf3183f2009-02-26 16:37:23 -08002511</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002512
2513
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002514<h3 id="Type_assertions">Type assertions</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002515
Rob Pikedf3183f2009-02-26 16:37:23 -08002516<p>
Robert Griesemere9192752009-12-01 16:15:53 -08002517For an expression <code>x</code> of <a href="#Interface_types">interface type</a>
2518and a type <code>T</code>, the primary expression
Rob Pikedf3183f2009-02-26 16:37:23 -08002519</p>
Robert Griesemer434c6052008-11-07 13:34:37 -08002520
Robert Griesemerc2d55862009-02-19 16:49:10 -08002521<pre>
2522x.(T)
2523</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -08002524
Robert Griesemerc2d55862009-02-19 16:49:10 -08002525<p>
Robert Griesemere9192752009-12-01 16:15:53 -08002526asserts that <code>x</code> is not <code>nil</code>
Russ Coxb89a54e2009-05-20 18:16:04 -07002527and that the value stored in <code>x</code> is of type <code>T</code>.
Russ Cox5958dd62009-03-04 17:19:21 -08002528The notation <code>x.(T)</code> is called a <i>type assertion</i>.
Rob Pikedf3183f2009-02-26 16:37:23 -08002529</p>
2530<p>
Russ Cox5958dd62009-03-04 17:19:21 -08002531More precisely, if <code>T</code> is not an interface type, <code>x.(T)</code> asserts
Robert Griesemer7bc03712010-06-07 15:49:39 -07002532that the dynamic type of <code>x</code> is <a href="#Type_identity">identical</a>
2533to the type <code>T</code>.
Russ Cox5958dd62009-03-04 17:19:21 -08002534If <code>T</code> is an interface type, <code>x.(T)</code> asserts that the dynamic type
Rob Pike632a9852010-01-13 12:06:33 +11002535of <code>x</code> implements the interface <code>T</code><a href="#Interface_types">Interface types</a>).
Rob Pikedf3183f2009-02-26 16:37:23 -08002536</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002537<p>
Russ Cox5958dd62009-03-04 17:19:21 -08002538If the type assertion holds, the value of the expression is the value
Rob Pike5bb29fb2010-03-25 17:59:59 -07002539stored in <code>x</code> and its type is <code>T</code>. If the type assertion is false,
2540a <a href="#Run_time_panics">run-time panic</a> occurs.
2541In other words, even though the dynamic type of <code>x</code>
Russ Cox5958dd62009-03-04 17:19:21 -08002542is known only at run-time, the type of <code>x.(T)</code> is
Rob Pikedf3183f2009-02-26 16:37:23 -08002543known to be <code>T</code> in a correct program.
2544</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002545<p>
Rob Piked5537072009-08-22 00:04:04 -07002546If a type assertion is used in an assignment or initialization of the form
Rob Pikedf3183f2009-02-26 16:37:23 -08002547</p>
Robert Griesemer434c6052008-11-07 13:34:37 -08002548
Robert Griesemerc2d55862009-02-19 16:49:10 -08002549<pre>
2550v, ok = x.(T)
2551v, ok := x.(T)
Rob Piked5537072009-08-22 00:04:04 -07002552var v, ok = x.(T)
Robert Griesemerc2d55862009-02-19 16:49:10 -08002553</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -08002554
Robert Griesemerc2d55862009-02-19 16:49:10 -08002555<p>
Russ Cox5958dd62009-03-04 17:19:21 -08002556the result of the assertion is a pair of values with types <code>(T, bool)</code>.
2557If the assertion holds, the expression returns the pair <code>(x.(T), true)</code>;
Rob Pikedf3183f2009-02-26 16:37:23 -08002558otherwise, the expression returns <code>(Z, false)</code> where <code>Z</code>
Robert Griesemer506c0082009-09-08 15:41:14 -07002559is the <a href="#The_zero_value">zero value</a> for type <code>T</code>.
Rob Pike5bb29fb2010-03-25 17:59:59 -07002560No run-time panic occurs in this case.
Russ Cox5958dd62009-03-04 17:19:21 -08002561The type assertion in this construct thus acts like a function call
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002562returning a value and a boolean indicating success. (§<a href="#Assignments">Assignments</a>)
Rob Pikedf3183f2009-02-26 16:37:23 -08002563</p>
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -07002564
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002565
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002566<h3 id="Calls">Calls</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002567
Robert Griesemerc2d55862009-02-19 16:49:10 -08002568<p>
Rob Pike96750f12009-02-27 16:47:48 -08002569Given an expression <code>f</code> of function type
2570<code>F</code>,
Rob Pikedf3183f2009-02-26 16:37:23 -08002571</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002572
Robert Griesemerc2d55862009-02-19 16:49:10 -08002573<pre>
Robert Griesemer3c7271f2011-05-24 14:18:44 -07002574f(a1, a2, … an)
Robert Griesemerc2d55862009-02-19 16:49:10 -08002575</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002576
Robert Griesemerc2d55862009-02-19 16:49:10 -08002577<p>
Robert Griesemer3c7271f2011-05-24 14:18:44 -07002578calls <code>f</code> with arguments <code>a1, a2, … an</code>.
Rob Pike4fe41922009-11-07 22:00:59 -08002579Except for one special case, arguments must be single-valued expressions
Robert Griesemer440cc952010-06-07 17:40:21 -07002580<a href="#Assignability">assignable</a> to the parameter types of
Rob Pikedf3183f2009-02-26 16:37:23 -08002581<code>F</code> and are evaluated before the function is called.
2582The type of the expression is the result type
2583of <code>F</code>.
2584A method invocation is similar but the method itself
2585is specified as a selector upon a value of the receiver type for
2586the method.
2587</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002588
Robert Griesemerc2d55862009-02-19 16:49:10 -08002589<pre>
Robert Griesemere9192752009-12-01 16:15:53 -08002590math.Atan2(x, y) // function call
Robert Griesemer130ac742009-12-10 16:43:01 -08002591var pt *Point
Rob Pikedf3183f2009-02-26 16:37:23 -08002592pt.Scale(3.5) // method call with receiver pt
Robert Griesemerc2d55862009-02-19 16:49:10 -08002593</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002594
Rob Pikedf3183f2009-02-26 16:37:23 -08002595<p>
Rob Pike4fe41922009-11-07 22:00:59 -08002596As a special case, if the return parameters of a function or method
Robert Griesemer440cc952010-06-07 17:40:21 -07002597<code>g</code> are equal in number and individually
2598assignable to the parameters of another function or method
Rob Pike4fe41922009-11-07 22:00:59 -08002599<code>f</code>, then the call <code>f(g(<i>parameters_of_g</i>))</code>
2600will invoke <code>f</code> after binding the return values of
2601<code>g</code> to the parameters of <code>f</code> in order. The call
2602of <code>f</code> must contain no parameters other than the call of <code>g</code>.
2603If <code>f</code> has a final <code>...</code> parameter, it is
2604assigned the return values of <code>g</code> that remain after
2605assignment of regular parameters.
2606</p>
2607
2608<pre>
2609func Split(s string, pos int) (string, string) {
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08002610 return s[0:pos], s[pos:]
Rob Pike4fe41922009-11-07 22:00:59 -08002611}
2612
2613func Join(s, t string) string {
2614 return s + t
2615}
2616
2617if Join(Split(value, len(value)/2)) != value {
Robert Griesemer7fc4e372011-02-01 12:51:10 -08002618 log.Panic("test fails")
Rob Pike4fe41922009-11-07 22:00:59 -08002619}
2620</pre>
2621
2622<p>
Robert Griesemer2a838d62011-02-08 13:31:01 -08002623A method call <code>x.m()</code> is valid if the <a href="#Method_sets">method set</a>
2624of (the type of) <code>x</code> contains <code>m</code> and the
Robert Griesemer63f01492010-05-28 14:17:30 -07002625argument list can be assigned to the parameter list of <code>m</code>.
Rob Pike678625d2009-09-15 09:54:22 -07002626If <code>x</code> is <a href="#Address_operators">addressable</a> and <code>&amp;x</code>'s method
Robert Griesemer56809d02009-05-20 11:02:48 -07002627set contains <code>m</code>, <code>x.m()</code> is shorthand
2628for <code>(&amp;x).m()</code>:
Rob Pikedf3183f2009-02-26 16:37:23 -08002629</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002630
Robert Griesemerc2d55862009-02-19 16:49:10 -08002631<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08002632var p Point
Rob Pikedf3183f2009-02-26 16:37:23 -08002633p.Scale(3.5)
Robert Griesemerc2d55862009-02-19 16:49:10 -08002634</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002635
Robert Griesemerc2d55862009-02-19 16:49:10 -08002636<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002637There is no distinct method type and there are no method literals.
Rob Pikedf3183f2009-02-26 16:37:23 -08002638</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002639
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002640<h3 id="Passing_arguments_to_..._parameters">Passing arguments to <code>...</code> parameters</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002641
Robert Griesemerc2d55862009-02-19 16:49:10 -08002642<p>
Russ Cox95625922010-06-12 11:37:13 -07002643If <code>f</code> is variadic with final parameter type <code>...T</code>,
2644then within the function the argument is equivalent to a parameter of type
2645<code>[]T</code>. At each call of <code>f</code>, the argument
2646passed to the final parameter is
2647a new slice of type <code>[]T</code> whose successive elements are
Robert Griesemerac771a82010-09-24 14:08:28 -07002648the actual arguments, which all must be <a href="#Assignability">assignable</a>
2649to the type <code>T</code>. The length of the slice is therefore the number of
2650arguments bound to the final parameter and may differ for each call site.
Rob Pikeb81065d2010-01-27 13:14:40 -08002651</p>
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002652
Rob Pikedf3183f2009-02-26 16:37:23 -08002653<p>
Rob Pikeb81065d2010-01-27 13:14:40 -08002654Given the function and call
2655</p>
2656<pre>
Robert Griesemer3c7271f2011-05-24 14:18:44 -07002657func Greeting(prefix string, who ...string)
Rob Pikeb81065d2010-01-27 13:14:40 -08002658Greeting("hello:", "Joe", "Anna", "Eileen")
2659</pre>
2660
2661<p>
Robert Griesemerac771a82010-09-24 14:08:28 -07002662within <code>Greeting</code>, <code>who</code> will have the value
Joe Poirierd4c8a542010-09-20 10:51:05 -07002663<code>[]string{"Joe", "Anna", "Eileen"}</code>
Rob Pikeb81065d2010-01-27 13:14:40 -08002664</p>
2665
Robert Griesemerac771a82010-09-24 14:08:28 -07002666<p>
Robert Griesemer904adfd2010-10-27 10:44:31 -07002667If the final argument is assignable to a slice type <code>[]T</code>, it may be
2668passed unchanged as the value for a <code>...T</code> parameter if the argument
2669is followed by <code>...</code>. In this case no new slice is created.
Robert Griesemerac771a82010-09-24 14:08:28 -07002670</p>
Rob Pikeb81065d2010-01-27 13:14:40 -08002671
2672<p>
Robert Griesemerac771a82010-09-24 14:08:28 -07002673Given the slice <code>s</code> and call
Rob Pikeb81065d2010-01-27 13:14:40 -08002674</p>
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002675
Robert Griesemerac771a82010-09-24 14:08:28 -07002676<pre>
2677s := []string{"James", "Jasmine"}
2678Greeting("goodbye:", s...)
2679</pre>
2680
2681<p>
2682within <code>Greeting</code>, <code>who</code> will have the same value as <code>s</code>
2683with the same underlying array.
2684</p>
2685
2686
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002687<h3 id="Operators">Operators</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002688
Rob Pikedf3183f2009-02-26 16:37:23 -08002689<p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002690Operators combine operands into expressions.
Rob Pikedf3183f2009-02-26 16:37:23 -08002691</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002692
Robert Griesemerf7ac31362009-07-10 16:06:40 -07002693<pre class="ebnf">
Russ Cox5958dd62009-03-04 17:19:21 -08002694Expression = UnaryExpr | Expression binary_op UnaryExpr .
Rob Pikedf3183f2009-02-26 16:37:23 -08002695UnaryExpr = PrimaryExpr | unary_op UnaryExpr .
Robert Griesemer57b34612008-10-10 12:45:44 -07002696
Robert Griesemerb50ed022011-02-01 12:02:49 -08002697binary_op = "||" | "&amp;&amp;" | rel_op | add_op | mul_op .
Rob Pikedf3183f2009-02-26 16:37:23 -08002698rel_op = "==" | "!=" | "&lt;" | "&lt;=" | ">" | ">=" .
2699add_op = "+" | "-" | "|" | "^" .
Robert Griesemer4ed666e2009-08-27 16:45:42 -07002700mul_op = "*" | "/" | "%" | "&lt;&lt;" | "&gt;&gt;" | "&amp;" | "&amp;^" .
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002701
Rob Pikedf3183f2009-02-26 16:37:23 -08002702unary_op = "+" | "-" | "!" | "^" | "*" | "&amp;" | "&lt;-" .
Robert Griesemerc2d55862009-02-19 16:49:10 -08002703</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002704
Robert Griesemerc2d55862009-02-19 16:49:10 -08002705<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07002706Comparisons are discussed <a href="#Comparison_operators">elsewhere</a>.
Robert Griesemer7bc03712010-06-07 15:49:39 -07002707For other binary operators, the operand types must be <a href="#Type_identity">identical</a>
Robert Griesemer32d12782011-05-23 14:12:42 -07002708unless the operation involves shifts or untyped <a href="#Constants">constants</a>.
Robert Griesemer19b1d352009-09-24 19:36:48 -07002709For operations involving constants only, see the section on
2710<a href="#Constant_expressions">constant expressions</a>.
Rob Pike4501d342009-02-19 17:31:36 -08002711</p>
Robert Griesemerc8e18762008-09-12 12:26:22 -07002712
Rob Pike83cbca52009-08-21 14:18:08 -07002713<p>
Robert Griesemer32d12782011-05-23 14:12:42 -07002714Except for shift operations, if one operand is an untyped <a href="#Constants">constant</a>
Robert Griesemer19b1d352009-09-24 19:36:48 -07002715and the other operand is not, the constant is <a href="#Conversions">converted</a>
2716to the type of the other operand.
Rob Pike83cbca52009-08-21 14:18:08 -07002717</p>
Robert Griesemer7539c852009-07-31 18:05:07 -07002718
Rob Pike83cbca52009-08-21 14:18:08 -07002719<p>
Robert Griesemer32d12782011-05-23 14:12:42 -07002720The right operand in a shift expression must have unsigned integer type
Robert Griesemer19b1d352009-09-24 19:36:48 -07002721or be an untyped constant that can be converted to unsigned integer type.
Robert Griesemer32d12782011-05-23 14:12:42 -07002722If the left operand of a non-constant shift expression is an untyped constant,
2723the type of the constant is what it would be if the shift expression were
Robert Griesemer636c5fa2011-06-08 09:11:18 -07002724replaced by its left operand alone; the type is <code>int</code> if it cannot
2725be determined from the context (for instance, if the shift expression is an
2726operand in a comparison against an untyped constant).
Rob Pike83cbca52009-08-21 14:18:08 -07002727</p>
Robert Griesemera6b546f2008-10-20 11:46:40 -07002728
Rob Pike83cbca52009-08-21 14:18:08 -07002729<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08002730var s uint = 33
Robert Griesemer32d12782011-05-23 14:12:42 -07002731var i = 1&lt;&lt;s // 1 has type int
2732var j int32 = 1&lt;&lt;s // 1 has type int32; j == 0
2733var k = uint64(1&lt;&lt;s) // 1 has type uint64; k == 1&lt;&lt;33
2734var m int = 1.0&lt;&lt;s // legal: 1.0 has type int
Robert Griesemer636c5fa2011-06-08 09:11:18 -07002735var n = 1.0&lt;&lt;s != 0 // legal: 1.0 has type int; n == false if ints are 32bits in size
2736var o = 1&lt;&lt;s == 2&lt;&lt;s // legal: 1 and 2 have type int; o == true if ints are 32bits in size
2737var 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 -07002738var u = 1.0&lt;&lt;s // illegal: 1.0 has type float64, cannot shift
2739var v float32 = 1&lt;&lt;s // illegal: 1 has type float32, cannot shift
2740var w int64 = 1.0&lt;&lt;33 // legal: 1.0&lt;&lt;33 is a constant shift expression
Rob Pike83cbca52009-08-21 14:18:08 -07002741</pre>
Robert Griesemer18b05c12009-01-26 09:34:19 -08002742
Robert Griesemerd36d1912009-09-18 11:58:35 -07002743<h3 id="Operator_precedence">Operator precedence</h3>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002744<p>
Russ Coxec9b0422009-07-09 16:44:13 -07002745Unary operators have the highest precedence.
2746As the <code>++</code> and <code>--</code> operators form
Rob Pikedf3183f2009-02-26 16:37:23 -08002747statements, not expressions, they fall
Russ Coxec9b0422009-07-09 16:44:13 -07002748outside the operator hierarchy.
Rob Pikedf3183f2009-02-26 16:37:23 -08002749As a consequence, statement <code>*p++</code> is the same as <code>(*p)++</code>.
2750<p>
Robert Griesemerb50ed022011-02-01 12:02:49 -08002751There are five precedence levels for binary operators.
Rob Pikedf3183f2009-02-26 16:37:23 -08002752Multiplication operators bind strongest, followed by addition
Robert Griesemerb50ed022011-02-01 12:02:49 -08002753operators, comparison operators, <code>&amp;&amp;</code> (logical and),
2754and finally <code>||</code> (logical or):
Rob Pikedf3183f2009-02-26 16:37:23 -08002755</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002756
Rob Pikeff70f092009-02-20 13:36:14 -08002757<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08002758Precedence Operator
Robert Griesemerb50ed022011-02-01 12:02:49 -08002759 5 * / % &lt;&lt; &gt;&gt; &amp; &amp;^
2760 4 + - | ^
Robert Griesemera1368a62011-02-22 15:31:57 -08002761 3 == != &lt; &lt;= &gt; &gt;=
Robert Griesemerc2d55862009-02-19 16:49:10 -08002762 2 &amp;&amp;
2763 1 ||
2764</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002765
Robert Griesemerc2d55862009-02-19 16:49:10 -08002766<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002767Binary operators of the same precedence associate from left to right.
Robert Griesemerd36d1912009-09-18 11:58:35 -07002768For instance, <code>x / y * z</code> is the same as <code>(x / y) * z</code>.
Rob Pikedf3183f2009-02-26 16:37:23 -08002769</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002770
Robert Griesemerc2d55862009-02-19 16:49:10 -08002771<pre>
2772+x
277323 + 3*x[i]
2774x &lt;= f()
Robert Griesemer4ed666e2009-08-27 16:45:42 -07002775^a &gt;&gt; b
Robert Griesemerc2d55862009-02-19 16:49:10 -08002776f() || g()
Robert Griesemera1368a62011-02-22 15:31:57 -08002777x == y+1 &amp;&amp; &lt;-chan_ptr &gt; 0
Robert Griesemerc2d55862009-02-19 16:49:10 -08002778</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002779
2780
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002781<h3 id="Arithmetic_operators">Arithmetic operators</h3>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002782<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07002783Arithmetic operators apply to numeric values and yield a result of the same
Rob Pikedf3183f2009-02-26 16:37:23 -08002784type as the first operand. The four standard arithmetic operators (<code>+</code>,
Rob Pike72970872010-03-04 12:35:16 -08002785<code>-</code>, <code>*</code>, <code>/</code>) apply to integer,
2786floating-point, and complex types; <code>+</code> also applies
Robert Griesemer19b1d352009-09-24 19:36:48 -07002787to strings. All other arithmetic operators apply to integers only.
Rob Pikedf3183f2009-02-26 16:37:23 -08002788</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002789
Rob Pikeff70f092009-02-20 13:36:14 -08002790<pre class="grammar">
Rob Pike72970872010-03-04 12:35:16 -08002791+ sum integers, floats, complex values, strings
2792- difference integers, floats, complex values
2793* product integers, floats, complex values
2794/ quotient integers, floats, complex values
Rob Pike307ec212009-03-12 15:53:56 -07002795% remainder integers
Robert Griesemerc2d55862009-02-19 16:49:10 -08002796
Rob Pike307ec212009-03-12 15:53:56 -07002797&amp; bitwise and integers
2798| bitwise or integers
2799^ bitwise xor integers
2800&amp;^ bit clear (and not) integers
Robert Griesemerc2d55862009-02-19 16:49:10 -08002801
Robert Griesemer4ed666e2009-08-27 16:45:42 -07002802&lt;&lt; left shift integer &lt;&lt; unsigned integer
2803&gt;&gt; right shift integer &gt;&gt; unsigned integer
Robert Griesemerc2d55862009-02-19 16:49:10 -08002804</pre>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002805
Rob Pikedf3183f2009-02-26 16:37:23 -08002806<p>
2807Strings can be concatenated using the <code>+</code> operator
2808or the <code>+=</code> assignment operator:
2809</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002810
Robert Griesemerc2d55862009-02-19 16:49:10 -08002811<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08002812s := "hi" + string(c)
2813s += " and good bye"
Robert Griesemerc2d55862009-02-19 16:49:10 -08002814</pre>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002815
Robert Griesemerc2d55862009-02-19 16:49:10 -08002816<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002817String addition creates a new string by concatenating the operands.
2818</p>
2819<p>
Robert Griesemerbb7eb402011-05-02 17:23:18 -07002820For two integer values <code>x</code> and <code>y</code>, the integer quotient
2821<code>q = x / y</code> and remainder <code>r = x % y</code> satisfy the following
2822relationships:
Rob Pikedf3183f2009-02-26 16:37:23 -08002823</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002824
Robert Griesemerc2d55862009-02-19 16:49:10 -08002825<pre>
Robert Griesemerbb7eb402011-05-02 17:23:18 -07002826x = q*y + r and |r| &lt; |y|
Robert Griesemerc2d55862009-02-19 16:49:10 -08002827</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002828
Rob Pikedf3183f2009-02-26 16:37:23 -08002829<p>
Robert Griesemerbb7eb402011-05-02 17:23:18 -07002830with <code>x / y</code> truncated towards zero
2831(<a href="http://en.wikipedia.org/wiki/Modulo_operation">"truncated division"</a>).
Rob Pikedf3183f2009-02-26 16:37:23 -08002832</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002833
Robert Griesemerc2d55862009-02-19 16:49:10 -08002834<pre>
2835 x y x / y x % y
2836 5 3 1 2
2837-5 3 -1 -2
2838 5 -3 -1 2
2839-5 -3 1 -2
2840</pre>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002841
Rob Pikedf3183f2009-02-26 16:37:23 -08002842<p>
Robert Griesemerbb7eb402011-05-02 17:23:18 -07002843As an exception to this rule, if the dividend <code>x</code> is the most
2844negative value for the int type of <code>x</code>, the quotient
2845<code>q = x / -1</code> is equal to <code>x</code> (and <code>r = 0</code>).
2846</p>
2847
2848<pre>
2849 x, q
2850int8 -128
2851int16 -32768
2852int32 -2147483648
2853int64 -9223372036854775808
2854</pre>
2855
2856<p>
Robert Griesemerdf674ff2010-05-04 17:31:40 -07002857If the divisor is zero, a <a href="#Run_time_panics">run-time panic</a> occurs.
Rob Pikedf3183f2009-02-26 16:37:23 -08002858If the dividend is positive and the divisor is a constant power of 2,
Rob Pikef3a33bc2009-09-14 17:39:17 -07002859the division may be replaced by a right shift, and computing the remainder may
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002860be replaced by a bitwise "and" operation:
Rob Pikedf3183f2009-02-26 16:37:23 -08002861</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002862
Robert Griesemerc2d55862009-02-19 16:49:10 -08002863<pre>
Robert Griesemer4ed666e2009-08-27 16:45:42 -07002864 x x / 4 x % 4 x &gt;&gt; 2 x &amp; 3
Robert Griesemerc2d55862009-02-19 16:49:10 -08002865 11 2 3 2 3
2866-11 -2 -3 -3 1
2867</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002868
Rob Pikedf3183f2009-02-26 16:37:23 -08002869<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002870The shift operators shift the left operand by the shift count specified by the
2871right operand. They implement arithmetic shifts if the left operand is a signed
Robert Griesemer32d12782011-05-23 14:12:42 -07002872integer and logical shifts if it is an unsigned integer.
2873There is no upper limit on the shift count. Shifts behave
Rob Pikedf3183f2009-02-26 16:37:23 -08002874as if the left operand is shifted <code>n</code> times by 1 for a shift
2875count of <code>n</code>.
Robert Griesemer4ed666e2009-08-27 16:45:42 -07002876As a result, <code>x &lt;&lt; 1</code> is the same as <code>x*2</code>
2877and <code>x &gt;&gt; 1</code> is the same as
Robert Griesemere9192752009-12-01 16:15:53 -08002878<code>x/2</code> but truncated towards negative infinity.
Rob Pikedf3183f2009-02-26 16:37:23 -08002879</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002880
Rob Pikedf3183f2009-02-26 16:37:23 -08002881<p>
2882For integer operands, the unary operators
2883<code>+</code>, <code>-</code>, and <code>^</code> are defined as
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002884follows:
Rob Pikedf3183f2009-02-26 16:37:23 -08002885</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002886
Rob Pikeff70f092009-02-20 13:36:14 -08002887<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08002888+x is 0 + x
2889-x negation is 0 - x
Russ Coxd83dc4f2009-05-29 16:04:16 -07002890^x bitwise complement is m ^ x with m = "all bits set to 1" for unsigned x
2891 and m = -1 for signed x
Robert Griesemerc2d55862009-02-19 16:49:10 -08002892</pre>
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002893
Russ Cox5958dd62009-03-04 17:19:21 -08002894<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07002895For floating-point numbers,
Russ Cox5958dd62009-03-04 17:19:21 -08002896<code>+x</code> is the same as <code>x</code>,
2897while <code>-x</code> is the negation of <code>x</code>.
Robert Griesemerdf674ff2010-05-04 17:31:40 -07002898The result of a floating-point division by zero is not specified beyond the
2899IEEE-754 standard; whether a <a href="#Run_time_panics">run-time panic</a>
2900occurs is implementation-specific.
Russ Cox5958dd62009-03-04 17:19:21 -08002901</p>
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002902
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002903<h3 id="Integer_overflow">Integer overflow</h3>
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002904
Rob Pikedf3183f2009-02-26 16:37:23 -08002905<p>
2906For unsigned integer values, the operations <code>+</code>,
2907<code>-</code>, <code>*</code>, and <code>&lt;&lt;</code> are
2908computed modulo 2<sup><i>n</i></sup>, where <i>n</i> is the bit width of
2909the unsigned integer's type
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002910<a href="#Numeric_types">Numeric types</a>). Loosely speaking, these unsigned integer operations
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002911discard high bits upon overflow, and programs may rely on ``wrap around''.
Rob Pikedf3183f2009-02-26 16:37:23 -08002912</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002913<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002914For signed integers, the operations <code>+</code>,
2915<code>-</code>, <code>*</code>, and <code>&lt;&lt;</code> may legally
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002916overflow and the resulting value exists and is deterministically defined
2917by the signed integer representation, the operation, and its operands.
Rob Pikedf3183f2009-02-26 16:37:23 -08002918No exception is raised as a result of overflow. A
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002919compiler may not optimize code under the assumption that overflow does
Rob Pikedf3183f2009-02-26 16:37:23 -08002920not occur. For instance, it may not assume that <code>x &lt; x + 1</code> is always true.
2921</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002922
2923
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002924<h3 id="Comparison_operators">Comparison operators</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002925
Rob Pike5af7de32009-02-24 15:17:59 -08002926<p>
Robert Griesemer1d282a82010-06-03 16:55:50 -07002927Comparison operators compare two operands and yield a value of type <code>bool</code>.
Rob Pike5af7de32009-02-24 15:17:59 -08002928</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002929
Rob Pikeff70f092009-02-20 13:36:14 -08002930<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08002931== equal
2932!= not equal
Anthony Martin0122a662011-02-08 14:51:15 -08002933&lt; less
2934&lt;= less or equal
Robert Griesemerc2d55862009-02-19 16:49:10 -08002935> greater
2936>= greater or equal
2937</pre>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002938
Rob Pike5af7de32009-02-24 15:17:59 -08002939<p>
Robert Griesemer1d282a82010-06-03 16:55:50 -07002940The operands must be <i>comparable</i>; that is, the first operand
Robert Griesemer440cc952010-06-07 17:40:21 -07002941must be <a href="#Assignability">assignable</a>
2942to the type of the second operand, or vice versa.
Rob Pike5af7de32009-02-24 15:17:59 -08002943</p>
Robert Griesemer7bc03712010-06-07 15:49:39 -07002944<p>
Robert Griesemer1d282a82010-06-03 16:55:50 -07002945The operators <code>==</code> and <code>!=</code> apply
2946to operands of all types except arrays and structs.
2947All other comparison operators apply only to integer, floating-point
2948and string values. The result of a comparison is defined as follows:
Rob Pike5af7de32009-02-24 15:17:59 -08002949</p>
Robert Griesemer1d282a82010-06-03 16:55:50 -07002950
2951<ul>
2952 <li>
2953 Integer values are compared in the usual way.
2954 </li>
2955 <li>
2956 Floating point values are compared as defined by the IEEE-754
2957 standard.
2958 </li>
2959 <li>
2960 Two complex values <code>u</code>, <code>v</code> are
2961 equal if both <code>real(u) == real(v)</code> and
2962 <code>imag(u) == imag(v)</code>.
2963 </li>
2964 <li>
2965 String values are compared byte-wise (lexically).
2966 </li>
2967 <li>
Peter Mundy5928e1d2010-11-09 10:10:57 -08002968 Boolean values are equal if they are either both
Robert Griesemer1d282a82010-06-03 16:55:50 -07002969 <code>true</code> or both <code>false</code>.
2970 </li>
2971 <li>
2972 Pointer values are equal if they point to the same location
2973 or if both are <code>nil</code>.
2974 </li>
2975 <li>
2976 Function values are equal if they refer to the same function
2977 or if both are <code>nil</code>.
2978 </li>
2979 <li>
2980 A slice value may only be compared to <code>nil</code>.
2981 </li>
2982 <li>
2983 Channel and map values are equal if they were created by the same call to <code>make</code>
2984<a href="#Making_slices_maps_and_channels">Making slices, maps, and channels</a>)
2985 or if both are <code>nil</code>.
2986 </li>
2987 <li>
Robert Griesemer7bc03712010-06-07 15:49:39 -07002988 Interface values are equal if they have <a href="#Type_identity">identical</a> dynamic types and
Robert Griesemer1d282a82010-06-03 16:55:50 -07002989 equal dynamic values or if both are <code>nil</code>.
2990 </li>
2991 <li>
2992 An interface value <code>x</code> is equal to a non-interface value
2993 <code>y</code> if the dynamic type of <code>x</code> is identical to
2994 the static type of <code>y</code> and the dynamic value of <code>x</code>
2995 is equal to <code>y</code>.
2996 </li>
2997 <li>
2998 A pointer, function, slice, channel, map, or interface value is equal
2999 to <code>nil</code> if it has been assigned the explicit value
3000 <code>nil</code>, if it is uninitialized, or if it has been assigned
3001 another value equal to <code>nil</code>.
3002 </li>
3003</ul>
Robert Griesemera3294712009-01-05 11:17:26 -08003004
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003005
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003006<h3 id="Logical_operators">Logical operators</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003007
Rob Pikedf3183f2009-02-26 16:37:23 -08003008<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07003009Logical operators apply to <a href="#Boolean_types">boolean</a> values
3010and yield a result of the same type as the operands.
Robert Griesemer41d65ac2008-09-04 15:17:27 -07003011The right operand is evaluated conditionally.
Rob Pikedf3183f2009-02-26 16:37:23 -08003012</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07003013
Rob Pikeff70f092009-02-20 13:36:14 -08003014<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08003015&amp;&amp; conditional and p &amp;&amp; q is "if p then q else false"
3016|| conditional or p || q is "if p then true else q"
3017! not !p is "not p"
3018</pre>
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -07003019
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003020
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003021<h3 id="Address_operators">Address operators</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003022
Rob Pikedf3183f2009-02-26 16:37:23 -08003023<p>
Robert Griesemer0e1d9412011-01-26 11:21:23 -08003024For an operand <code>x</code> of type <code>T</code>, the address operation
3025<code>&amp;x</code> generates a pointer of type <code>*T</code> to <code>x</code>.
3026The operand must be <i>addressable</i>,
Russ Coxe7561de2010-05-24 14:31:43 -07003027that is, either a variable, pointer indirection, or slice indexing
Robert Griesemer0e1d9412011-01-26 11:21:23 -08003028operation; or a field selector of an addressable struct operand;
Russ Coxe7561de2010-05-24 14:31:43 -07003029or an array indexing operation of an addressable array.
Robert Griesemer0e1d9412011-01-26 11:21:23 -08003030As an exception to the addressability requirement, <code>x</code> may also be a
3031<a href="#Composite_literals">composite literal</a>.
3032</p>
3033<p>
3034For an operand <code>x</code> of pointer type <code>*T</code>, the pointer
3035indirection <code>*x</code> denotes the value of type <code>T</code> pointed
3036to by <code>x</code>.
Rob Pikeafee1c52009-03-20 17:41:25 -07003037</p>
3038
3039<pre>
3040&amp;x
3041&amp;a[f(2)]
3042*p
3043*pf(x)
3044</pre>
3045
Robert Griesemerb50ed022011-02-01 12:02:49 -08003046
3047<h3 id="Receive_operator">Receive operator</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003048
Robert Griesemerc2d55862009-02-19 16:49:10 -08003049<p>
Robert Griesemerb50ed022011-02-01 12:02:49 -08003050For an operand <code>ch</code> of <a href="#Channel_types">channel type</a>,
3051the value of the receive operation <code>&lt;-ch</code> is the value received
3052from the channel <code>ch</code>. The type of the value is the element type of
3053the channel. The expression blocks until a value is available.
Robert Griesemer54731032011-05-12 09:15:59 -07003054Receiving from a <code>nil</code> channel blocks forever.
Rob Pikedf3183f2009-02-26 16:37:23 -08003055</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003056
Robert Griesemerc2d55862009-02-19 16:49:10 -08003057<pre>
Robert Griesemere1e76192009-09-25 14:11:03 -07003058v1 := &lt;-ch
3059v2 = &lt;-ch
3060f(&lt;-ch)
Robert Griesemerb50ed022011-02-01 12:02:49 -08003061&lt;-strobe // wait until clock pulse and discard received value
Robert Griesemerc2d55862009-02-19 16:49:10 -08003062</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003063
Rob Pikedf3183f2009-02-26 16:37:23 -08003064<p>
Robert Griesemerc1347182011-04-29 12:20:31 -07003065A receive expression used in an assignment or initialization of the form
Rob Pikedf3183f2009-02-26 16:37:23 -08003066</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003067
Robert Griesemerc2d55862009-02-19 16:49:10 -08003068<pre>
Robert Griesemere1e76192009-09-25 14:11:03 -07003069x, ok = &lt;-ch
3070x, ok := &lt;-ch
3071var x, ok = &lt;-ch
Robert Griesemerc2d55862009-02-19 16:49:10 -08003072</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003073
Rob Pikedf3183f2009-02-26 16:37:23 -08003074<p>
Russ Cox19d9a402011-01-27 15:34:28 -05003075yields an additional result.
3076The boolean variable <code>ok</code> indicates whether
3077the received value was sent on the channel (<code>true</code>)
3078or is a <a href="#The_zero_value">zero value</a> returned
3079because the channel is closed and empty (<code>false</code>).
Rob Pikedf3183f2009-02-26 16:37:23 -08003080</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003081
Robert Griesemer54731032011-05-12 09:15:59 -07003082<!--
Rob Pikedf3183f2009-02-26 16:37:23 -08003083<p>
Robert Griesemer90cc4a52009-10-22 09:41:38 -07003084<span class="alert">TODO: Probably in a separate section, communication semantics
3085need to be presented regarding send, receive, select, and goroutines.</span>
Rob Pikedf3183f2009-02-26 16:37:23 -08003086</p>
Rob Pike38d7bcf2011-05-08 14:05:18 -07003087-->
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003088
Robert Griesemerb50ed022011-02-01 12:02:49 -08003089
Rob Pike01cadde2009-09-15 15:56:44 -07003090<h3 id="Method_expressions">Method expressions</h3>
3091
3092<p>
Robert Griesemer2a838d62011-02-08 13:31:01 -08003093If <code>M</code> is in the <a href="#Method_sets">method set</a> of type <code>T</code>,
Rob Pike01cadde2009-09-15 15:56:44 -07003094<code>T.M</code> is a function that is callable as a regular function
3095with the same arguments as <code>M</code> prefixed by an additional
3096argument that is the receiver of the method.
3097</p>
3098
Robert Griesemerda961882009-09-17 11:01:50 -07003099<pre class="ebnf">
Rob Pike01cadde2009-09-15 15:56:44 -07003100MethodExpr = ReceiverType "." MethodName .
3101ReceiverType = TypeName | "(" "*" TypeName ")" .
Rob Pike01cadde2009-09-15 15:56:44 -07003102</pre>
3103
3104<p>
3105Consider a struct type <code>T</code> with two methods,
3106<code>Mv</code>, whose receiver is of type <code>T</code>, and
3107<code>Mp</code>, whose receiver is of type <code>*T</code>.
3108</p>
3109
3110<pre>
3111type T struct {
Robert Griesemer130ac742009-12-10 16:43:01 -08003112 a int
Rob Pike01cadde2009-09-15 15:56:44 -07003113}
Robert Griesemerb94c0d22011-01-19 23:07:21 -05003114func (tv T) Mv(a int) int { return 0 } // value receiver
3115func (tp *T) Mp(f float32) float32 { return 1 } // pointer receiver
Robert Griesemer130ac742009-12-10 16:43:01 -08003116var t T
Rob Pike01cadde2009-09-15 15:56:44 -07003117</pre>
3118
3119<p>
3120The expression
3121</p>
3122
3123<pre>
3124T.Mv
3125</pre>
3126
3127<p>
3128yields a function equivalent to <code>Mv</code> but
3129with an explicit receiver as its first argument; it has signature
3130</p>
3131
3132<pre>
Russ Cox46871692010-01-26 10:25:56 -08003133func(tv T, a int) int
Rob Pike01cadde2009-09-15 15:56:44 -07003134</pre>
3135
3136<p>
3137That function may be called normally with an explicit receiver, so
3138these three invocations are equivalent:
3139</p>
3140
3141<pre>
3142t.Mv(7)
3143T.Mv(t, 7)
3144f := T.Mv; f(t, 7)
3145</pre>
3146
3147<p>
3148Similarly, the expression
3149</p>
3150
3151<pre>
3152(*T).Mp
3153</pre>
3154
3155<p>
3156yields a function value representing <code>Mp</code> with signature
3157</p>
3158
3159<pre>
Robert Griesemerb94c0d22011-01-19 23:07:21 -05003160func(tp *T, f float32) float32
Rob Pike01cadde2009-09-15 15:56:44 -07003161</pre>
3162
3163<p>
3164For a method with a value receiver, one can derive a function
3165with an explicit pointer receiver, so
3166</p>
3167
3168<pre>
3169(*T).Mv
3170</pre>
3171
3172<p>
3173yields a function value representing <code>Mv</code> with signature
3174</p>
3175
3176<pre>
Russ Cox46871692010-01-26 10:25:56 -08003177func(tv *T, a int) int
Rob Pike01cadde2009-09-15 15:56:44 -07003178</pre>
3179
3180<p>
3181Such a function indirects through the receiver to create a value
3182to pass as the receiver to the underlying method;
3183the method does not overwrite the value whose address is passed in
3184the function call.
3185</p>
3186
3187<p>
3188The final case, a value-receiver function for a pointer-receiver method,
3189is illegal because pointer-receiver methods are not in the method set
3190of the value type.
3191</p>
3192
3193<p>
3194Function values derived from methods are called with function call syntax;
3195the receiver is provided as the first argument to the call.
3196That is, given <code>f := T.Mv</code>, <code>f</code> is invoked
3197as <code>f(t, 7)</code> not <code>t.f(7)</code>.
3198To construct a function that binds the receiver, use a
Robert Griesemerd36d1912009-09-18 11:58:35 -07003199<a href="#Function_literals">closure</a>.
Rob Pike01cadde2009-09-15 15:56:44 -07003200</p>
3201
3202<p>
3203It is legal to derive a function value from a method of an interface type.
3204The resulting function takes an explicit receiver of that interface type.
3205</p>
3206
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003207<h3 id="Conversions">Conversions</h3>
3208
3209<p>
3210Conversions are expressions of the form <code>T(x)</code>
3211where <code>T</code> is a type and <code>x</code> is an expression
3212that can be converted to type <code>T</code>.
3213</p>
3214
3215<pre class="ebnf">
Robert Griesemer934a5202010-05-24 14:58:26 -07003216Conversion = Type "(" Expression ")" .
3217</pre>
3218
3219<p>
3220If the type starts with an operator it must be parenthesized:
3221</p>
3222
3223<pre>
3224*Point(p) // same as *(Point(p))
3225(*Point)(p) // p is converted to (*Point)
3226&lt;-chan int(c) // same as &lt;-(chan int(c))
3227(&lt;-chan int)(c) // c is converted to (&lt;-chan int)
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003228</pre>
3229
3230<p>
Robert Griesemer27693562011-06-13 16:47:33 -07003231A <a href="#Constants">constant</a> value <code>x</code> can be converted to
3232type <code>T</code> in any of these cases:
3233</p>
3234
3235<ul>
3236 <li>
3237 <code>x</code> is representable by a value of type <code>T</code>.
3238 </li>
3239 <li>
3240 <code>x</code> is an integer constant and <code>T</code> is a
3241 <a href="#String_types">string type</a>.
3242 The same rule as for non-constant <code>x</code> applies in this case
3243<a href="#Conversions_to_and_from_a_string_type">Conversions to and from a string type</a>).
3244 </li>
3245</ul>
3246
3247<p>
3248Converting a constant yields a typed constant as result.
3249</p>
3250
3251<pre>
3252uint(iota) // iota value of type uint
3253float32(2.718281828) // 2.718281828 of type float32
3254complex128(1) // 1.0 + 0.0i of type complex128
3255string('x') // "x" of type string
3256string(0x266c) // "♬" of type string
3257MyString("foo" + "bar") // "foobar" of type MyString
3258string([]byte{'a'}) // not a constant: []byte{'a'} is not a constant
3259(*int)(nil) // not a constant: nil is not a constant, *int is not a boolean, numeric, or string type
3260int(1.2) // illegal: 1.2 cannot be represented as an int
3261string(65.0) // illegal: 65.0 is not an integer constant
3262</pre>
3263
3264<p>
3265A non-constant value <code>x</code> can be converted to type <code>T</code>
3266in any of these cases:
Robert Griesemer63f01492010-05-28 14:17:30 -07003267</p>
Robert Griesemer7bc03712010-06-07 15:49:39 -07003268
3269<ul>
3270 <li>
Robert Griesemer440cc952010-06-07 17:40:21 -07003271 <code>x</code> is <a href="#Assignability">assignable</a>
Robert Griesemer7bc03712010-06-07 15:49:39 -07003272 to <code>T</code>.
3273 </li>
3274 <li>
3275 <code>x</code>'s type and <code>T</code> have identical
3276 <a href="#Types">underlying types</a>.
3277 </li>
3278 <li>
3279 <code>x</code>'s type and <code>T</code> are unnamed pointer types
3280 and their pointer base types have identical underlying types.
3281 </li>
3282 <li>
3283 <code>x</code>'s type and <code>T</code> are both integer or floating
3284 point types.
3285 </li>
3286 <li>
3287 <code>x</code>'s type and <code>T</code> are both complex types.
3288 </li>
3289 <li>
3290 <code>x</code> is an integer or has type <code>[]byte</code> or
3291 <code>[]int</code> and <code>T</code> is a string type.
3292 </li>
3293 <li>
3294 <code>x</code> is a string and <code>T</code> is <code>[]byte</code> or
3295 <code>[]int</code>.
3296 </li>
3297</ul>
3298
3299<p>
Robert Griesemer27693562011-06-13 16:47:33 -07003300Specific rules apply to (non-constant) conversions between numeric types or
3301to and from a string type.
Robert Griesemer7bc03712010-06-07 15:49:39 -07003302These conversions may change the representation of <code>x</code>
3303and incur a run-time cost.
3304All other conversions only change the type but not the representation
3305of <code>x</code>.
3306</p>
3307
Robert Griesemer27693562011-06-13 16:47:33 -07003308<p>
3309There is no linguistic mechanism to convert between pointers and integers.
3310The package <a href="#Package_unsafe"><code>unsafe</code></a>
3311implements this functionality under
3312restricted circumstances.
3313</p>
3314
Robert Griesemer7bc03712010-06-07 15:49:39 -07003315<h4>Conversions between numeric types</h4>
Robert Griesemer27693562011-06-13 16:47:33 -07003316
3317<p>
3318For the conversion of non-constant numeric values, the following rules apply:
3319</p>
3320
Robert Griesemer63f01492010-05-28 14:17:30 -07003321<ol>
3322<li>
Robert Griesemer7bc03712010-06-07 15:49:39 -07003323When converting between integer types, if the value is a signed integer, it is
3324sign extended to implicit infinite precision; otherwise it is zero extended.
3325It is then truncated to fit in the result type's size.
3326For example, if <code>v := uint16(0x10F0)</code>, then <code>uint32(int8(v)) == 0xFFFFFFF0</code>.
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003327The conversion always yields a valid value; there is no indication of overflow.
Robert Griesemer7bc03712010-06-07 15:49:39 -07003328</li>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003329<li>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003330When converting a floating-point number to an integer, the fraction is discarded
3331(truncation towards zero).
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003332</li>
3333<li>
Robert Griesemer7bc03712010-06-07 15:49:39 -07003334When converting an integer or floating-point number to a floating-point type,
3335or a complex number to another complex type, the result value is rounded
Rob Pike72970872010-03-04 12:35:16 -08003336to the precision specified by the destination type.
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003337For instance, the value of a variable <code>x</code> of type <code>float32</code>
3338may be stored using additional precision beyond that of an IEEE-754 32-bit number,
3339but float32(x) represents the result of rounding <code>x</code>'s value to
334032-bit precision. Similarly, <code>x + 0.1</code> may use more than 32 bits
Evan Shawcb4e9f82010-05-23 11:21:47 -07003341of precision, but <code>float32(x + 0.1)</code> does not.
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003342</li>
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003343</ol>
3344
3345<p>
Robert Griesemer27693562011-06-13 16:47:33 -07003346In all non-constant conversions involving floating-point or complex values,
Rob Pike72970872010-03-04 12:35:16 -08003347if the result type cannot represent the value the conversion
Robert Griesemer27693562011-06-13 16:47:33 -07003348succeeds but the result value is implementation-dependent.
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003349</p>
3350
Robert Griesemer27693562011-06-13 16:47:33 -07003351<h4 id="Conversions_to_and_from_a_string_type">Conversions to and from a string type</h4>
Rob Pike1811fac2010-02-17 11:26:09 +11003352
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003353<ol>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003354<li>
Rob Pike1811fac2010-02-17 11:26:09 +11003355Converting a signed or unsigned integer value to a string type yields a
Robert Griesemer7bc03712010-06-07 15:49:39 -07003356string containing the UTF-8 representation of the integer. Values outside
3357the range of valid Unicode code points are converted to <code>"\uFFFD"</code>.
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003358
3359<pre>
Robert Griesemer934a5202010-05-24 14:58:26 -07003360string('a') // "a"
3361string(-1) // "\ufffd" == "\xef\xbf\xbd "
3362string(0xf8) // "\u00f8" == "ø" == "\xc3\xb8"
Rob Pike1811fac2010-02-17 11:26:09 +11003363type MyString string
Robert Griesemer934a5202010-05-24 14:58:26 -07003364MyString(0x65e5) // "\u65e5" == "日" == "\xe6\x97\xa5"
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003365</pre>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003366</li>
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003367
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003368<li>
Rob Pike1811fac2010-02-17 11:26:09 +11003369Converting a value of type <code>[]byte</code> (or
3370the equivalent <code>[]uint8</code>) to a string type yields a
3371string whose successive bytes are the elements of the slice. If
3372the slice value is <code>nil</code>, the result is the empty string.
3373
3374<pre>
3375string([]byte{'h', 'e', 'l', 'l', '\xc3', '\xb8'}) // "hellø"
3376</pre>
3377</li>
3378
3379<li>
3380Converting a value of type <code>[]int</code> to a string type yields
3381a string that is the concatenation of the individual integers
3382converted to strings. If the slice value is <code>nil</code>, the
3383result is the empty string.
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003384<pre>
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003385string([]int{0x767d, 0x9d6c, 0x7fd4}) // "\u767d\u9d6c\u7fd4" == "白鵬翔"
3386</pre>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003387</li>
3388
3389<li>
Rob Pike1811fac2010-02-17 11:26:09 +11003390Converting a value of a string type to <code>[]byte</code> (or <code>[]uint8</code>)
3391yields a slice whose successive elements are the bytes of the string.
3392If the string is empty, the result is <code>[]byte(nil)</code>.
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003393
3394<pre>
Rob Pike1811fac2010-02-17 11:26:09 +11003395[]byte("hellø") // []byte{'h', 'e', 'l', 'l', '\xc3', '\xb8'}
3396</pre>
3397</li>
3398
3399<li>
3400Converting a value of a string type to <code>[]int</code> yields a
3401slice containing the individual Unicode code points of the string.
3402If the string is empty, the result is <code>[]int(nil)</code>.
3403<pre>
3404[]int(MyString("白鵬翔")) // []int{0x767d, 0x9d6c, 0x7fd4}
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003405</pre>
3406</li>
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003407</ol>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003408
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003409
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003410<h3 id="Constant_expressions">Constant expressions</h3>
Robert Griesemerb90b2132008-09-19 15:49:55 -07003411
Rob Pikef27e9f02009-02-23 19:22:05 -08003412<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07003413Constant expressions may contain only <a href="#Constants">constant</a>
3414operands and are evaluated at compile-time.
Rob Pikef27e9f02009-02-23 19:22:05 -08003415</p>
3416
3417<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07003418Untyped boolean, numeric, and string constants may be used as operands
3419wherever it is legal to use an operand of boolean, numeric, or string type,
3420respectively. Except for shift operations, if the operands of a binary operation
3421are an untyped integer constant and an untyped floating-point constant,
3422the integer constant is converted to an untyped floating-point constant
3423(relevant for <code>/</code> and <code>%</code>).
Robert Griesemer32d12782011-05-23 14:12:42 -07003424Similarly, untyped integer or floating-point constants may be used as operands
Rob Pike72970872010-03-04 12:35:16 -08003425wherever it is legal to use an operand of complex type;
3426the integer or floating point constant is converted to a
3427complex constant with a zero imaginary part.
Robert Griesemer19b1d352009-09-24 19:36:48 -07003428</p>
Robert Griesemer997851e2009-09-25 15:36:25 -07003429
3430<p>
Robert Griesemer32d12782011-05-23 14:12:42 -07003431A constant <a href="#Comparison_operators">comparison</a> always yields
3432a constant of type <code>bool</code>. If the left operand of a constant
3433<a href="#Operators">shift expression</a> is an untyped constant, the
3434result is an integer constant; otherwise it is a constant of the same
3435type as the left operand, which must be of integer type
3436<a href="#Arithmetic_operators">Arithmetic operators</a>).
3437Applying all other operators to untyped constants results in an untyped
Rob Pike72970872010-03-04 12:35:16 -08003438constant of the same kind (that is, a boolean, integer, floating-point,
Robert Griesemer32d12782011-05-23 14:12:42 -07003439complex, or string constant).
Robert Griesemer19b1d352009-09-24 19:36:48 -07003440</p>
3441
Robert Griesemer32d12782011-05-23 14:12:42 -07003442<pre>
3443const a = 2 + 3.0 // a == 5.0 (floating-point constant)
3444const b = 15 / 4 // b == 3 (integer constant)
3445const c = 15 / 4.0 // c == 3.75 (floating-point constant)
3446const d = 1 &lt;&lt; 3.0 // d == 8 (integer constant)
3447const e = 1.0 &lt;&lt; 3 // e == 8 (integer constant)
3448const f = int32(1) &lt;&lt; 33 // f == 0 (type int32)
3449const g = float64(2) &gt;&gt; 1 // illegal (float64(2) is a typed floating-point constant)
3450const h = "foo" &gt; "bar" // h == true (type bool)
3451</pre>
3452
Robert Griesemer19b1d352009-09-24 19:36:48 -07003453<p>
Rob Pike72970872010-03-04 12:35:16 -08003454Imaginary literals are untyped complex constants (with zero real part)
3455and may be combined in binary
3456operations with untyped integer and floating-point constants; the
3457result is an untyped complex constant.
3458Complex constants are always constructed from
3459constant expressions involving imaginary
Robert Griesemeref4c2b82010-03-10 15:29:36 -08003460literals or constants derived from them, or calls of the built-in function
Robert Griesemerb94c0d22011-01-19 23:07:21 -05003461<a href="#Complex_numbers"><code>complex</code></a>.
Rob Pike72970872010-03-04 12:35:16 -08003462</p>
3463
3464<pre>
3465const Σ = 1 - 0.707i
Robert Griesemeref4c2b82010-03-10 15:29:36 -08003466const Δ = Σ + 2.0e-4 - 1/1i
Rob Pike72970872010-03-04 12:35:16 -08003467const Φ = iota * 1i
Robert Griesemerb94c0d22011-01-19 23:07:21 -05003468const iΓ = complex(0, Γ)
Rob Pike72970872010-03-04 12:35:16 -08003469</pre>
3470
3471<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07003472Constant expressions are always evaluated exactly; intermediate values and the
3473constants themselves may require precision significantly larger than supported
3474by any predeclared type in the language. The following are legal declarations:
Rob Pikef27e9f02009-02-23 19:22:05 -08003475</p>
3476
3477<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08003478const Huge = 1 &lt;&lt; 100
3479const Four int8 = Huge &gt;&gt; 98
Rob Pikef27e9f02009-02-23 19:22:05 -08003480</pre>
3481
3482<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07003483The values of <i>typed</i> constants must always be accurately representable as values
3484of the constant type. The following constant expressions are illegal:
Rob Pike21d03492009-03-24 19:16:42 -07003485</p>
3486
3487<pre>
Robert Griesemere9192752009-12-01 16:15:53 -08003488uint(-1) // -1 cannot be represented as a uint
3489int(3.14) // 3.14 cannot be represented as an int
3490int64(Huge) // 1&lt;&lt;100 cannot be represented as an int64
3491Four * 300 // 300 cannot be represented as an int8
3492Four * 100 // 400 cannot be represented as an int8
Rob Pike21d03492009-03-24 19:16:42 -07003493</pre>
3494
3495<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07003496The mask used by the unary bitwise complement operator <code>^</code> matches
Rob Pike678625d2009-09-15 09:54:22 -07003497the rule for non-constants: the mask is all 1s for unsigned constants
Robert Griesemer19b1d352009-09-24 19:36:48 -07003498and -1 for signed and untyped constants.
Rob Pike21d03492009-03-24 19:16:42 -07003499</p>
3500
3501<pre>
Robert Griesemer19b1d352009-09-24 19:36:48 -07003502^1 // untyped integer constant, equal to -2
Rob Pike21d03492009-03-24 19:16:42 -07003503uint8(^1) // error, same as uint8(-2), out of range
3504^uint8(1) // typed uint8 constant, same as 0xFF ^ uint8(1) = uint8(0xFE)
3505int8(^1) // same as int8(-2)
Russ Coxd83dc4f2009-05-29 16:04:16 -07003506^int8(1) // same as -1 ^ int8(1) = -2
Rob Pike21d03492009-03-24 19:16:42 -07003507</pre>
3508
Robert Griesemer54731032011-05-12 09:15:59 -07003509<!--
Rob Pike21d03492009-03-24 19:16:42 -07003510<p>
Robert Griesemer90cc4a52009-10-22 09:41:38 -07003511<span class="alert">
Rob Pike21d03492009-03-24 19:16:42 -07003512TODO: perhaps ^ should be disallowed on non-uints instead of assuming twos complement.
3513Also it may be possible to make typed constants more like variables, at the cost of fewer
3514overflow etc. errors being caught.
Robert Griesemer90cc4a52009-10-22 09:41:38 -07003515</span>
Rob Pike21d03492009-03-24 19:16:42 -07003516</p>
Rob Pike38d7bcf2011-05-08 14:05:18 -07003517-->
Robert Griesemer19b1d352009-09-24 19:36:48 -07003518
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003519<h3 id="Order_of_evaluation">Order of evaluation</h3>
Rob Pikec956e902009-04-14 20:10:49 -07003520
3521<p>
3522When evaluating the elements of an assignment or expression,
3523all function calls, method calls and
3524communication operations are evaluated in lexical left-to-right
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003525order.
3526</p>
3527
3528<p>
Robert Griesemer4f185492009-05-01 17:00:16 -07003529For example, in the assignment
Rob Pikec956e902009-04-14 20:10:49 -07003530</p>
3531<pre>
Robert Griesemere1e76192009-09-25 14:11:03 -07003532y[f()], ok = g(h(), i() + x[j()], &lt;-c), k()
Rob Pikec956e902009-04-14 20:10:49 -07003533</pre>
3534<p>
Robert Griesemer4f185492009-05-01 17:00:16 -07003535the function calls and communication happen in the order
3536<code>f()</code>, <code>h()</code>, <code>i()</code>, <code>j()</code>,
Robert Griesemere1e76192009-09-25 14:11:03 -07003537<code>&lt;-c</code>, <code>g()</code>, and <code>k()</code>.
Robert Griesemer4f185492009-05-01 17:00:16 -07003538However, the order of those events compared to the evaluation
3539and indexing of <code>x</code> and the evaluation
3540of <code>y</code> is not specified.
Rob Pikec956e902009-04-14 20:10:49 -07003541</p>
3542
Rob Pike4fe41922009-11-07 22:00:59 -08003543<p>
3544Floating-point operations within a single expression are evaluated according to
3545the associativity of the operators. Explicit parentheses affect the evaluation
3546by overriding the default associativity.
3547In the expression <code>x + (y + z)</code> the addition <code>y + z</code>
3548is performed before adding <code>x</code>.
3549</p>
3550
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003551<h2 id="Statements">Statements</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003552
Rob Pike96750f12009-02-27 16:47:48 -08003553<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003554Statements control execution.
Rob Pike96750f12009-02-27 16:47:48 -08003555</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003556
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003557<pre class="ebnf">
Robert Griesemerdea43942009-03-16 17:36:52 -07003558Statement =
Rob Pikef3a33bc2009-09-14 17:39:17 -07003559 Declaration | LabeledStmt | SimpleStmt |
3560 GoStmt | ReturnStmt | BreakStmt | ContinueStmt | GotoStmt |
Rob Pike11417162009-03-24 17:45:53 -07003561 FallthroughStmt | Block | IfStmt | SwitchStmt | SelectStmt | ForStmt |
3562 DeferStmt .
Robert Griesemer7abfcd92008-10-07 17:14:30 -07003563
Robert Griesemerb50ed022011-02-01 12:02:49 -08003564SimpleStmt = EmptyStmt | ExpressionStmt | SendStmt | IncDecStmt | Assignment | ShortVarDecl .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003565</pre>
Robert Griesemer7271e042008-10-09 20:05:24 -07003566
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003567
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003568<h3 id="Empty_statements">Empty statements</h3>
Robert Griesemeraed247f2008-10-08 17:05:30 -07003569
Rob Pike96750f12009-02-27 16:47:48 -08003570<p>
Robert Griesemeraed247f2008-10-08 17:05:30 -07003571The empty statement does nothing.
Rob Pike96750f12009-02-27 16:47:48 -08003572</p>
Robert Griesemeraed247f2008-10-08 17:05:30 -07003573
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003574<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003575EmptyStmt = .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003576</pre>
Robert Griesemeraed247f2008-10-08 17:05:30 -07003577
3578
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003579<h3 id="Labeled_statements">Labeled statements</h3>
Robert Griesemerdea43942009-03-16 17:36:52 -07003580
3581<p>
3582A labeled statement may be the target of a <code>goto</code>,
3583<code>break</code> or <code>continue</code> statement.
3584</p>
3585
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003586<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003587LabeledStmt = Label ":" Statement .
Robert Griesemerdea43942009-03-16 17:36:52 -07003588Label = identifier .
3589</pre>
3590
3591<pre>
Robert Griesemer7fc4e372011-02-01 12:51:10 -08003592Error: log.Panic("error encountered")
Robert Griesemerdea43942009-03-16 17:36:52 -07003593</pre>
3594
3595
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003596<h3 id="Expression_statements">Expression statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003597
Rob Pike96750f12009-02-27 16:47:48 -08003598<p>
Robert Griesemerc1347182011-04-29 12:20:31 -07003599Function calls, method calls, and receive operations
Robert Griesemer6af887e2011-05-02 09:16:31 -07003600can appear in statement context. Such statements may be parenthesized.
Rob Pike96750f12009-02-27 16:47:48 -08003601</p>
3602
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003603<pre class="ebnf">
Robert Griesemerc1347182011-04-29 12:20:31 -07003604ExpressionStmt = Expression .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003605</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003606
Robert Griesemerc2d55862009-02-19 16:49:10 -08003607<pre>
Robert Griesemerb50ed022011-02-01 12:02:49 -08003608h(x+y)
3609f.Close()
Robert Griesemere1e76192009-09-25 14:11:03 -07003610&lt;-ch
Robert Griesemer6af887e2011-05-02 09:16:31 -07003611(&lt;-ch)
Robert Griesemerc2d55862009-02-19 16:49:10 -08003612</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003613
3614
Robert Griesemerb50ed022011-02-01 12:02:49 -08003615<h3 id="Send_statements">Send statements</h3>
3616
3617<p>
3618A send statement sends a value on a channel.
3619The channel expression must be of <a href="#Channel_types">channel type</a>
3620and the type of the value must be <a href="#Assignability">assignable</a>
3621to the channel's element type.
3622</p>
3623
3624<pre class="ebnf">
3625SendStmt = Channel "&lt;-" Expression .
3626Channel = Expression .
3627</pre>
3628
3629<p>
3630Both the channel and the value expression are evaluated before communication
3631begins. Communication blocks until the send can proceed, at which point the
3632value is transmitted on the channel.
3633A send on an unbuffered channel can proceed if a receiver is ready.
3634A send on a buffered channel can proceed if there is room in the buffer.
Robert Griesemer54731032011-05-12 09:15:59 -07003635A send on a <code>nil</code> channel blocks forever.
Robert Griesemerb50ed022011-02-01 12:02:49 -08003636</p>
3637
3638<pre>
3639ch &lt;- 3
3640</pre>
3641
Robert Griesemerb50ed022011-02-01 12:02:49 -08003642
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003643<h3 id="IncDec_statements">IncDec statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003644
Rob Pike96750f12009-02-27 16:47:48 -08003645<p>
Robert Griesemer52a54802008-09-30 13:02:50 -07003646The "++" and "--" statements increment or decrement their operands
Robert Griesemer19b1d352009-09-24 19:36:48 -07003647by the untyped <a href="#Constants">constant</a> <code>1</code>.
Robert Griesemer5474e162010-09-28 14:44:19 -07003648As with an assignment, the operand must be <a href="#Address_operators">addressable</a>
3649or a map index expression.
Rob Pike96750f12009-02-27 16:47:48 -08003650</p>
Robert Griesemer52a54802008-09-30 13:02:50 -07003651
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003652<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003653IncDecStmt = Expression ( "++" | "--" ) .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003654</pre>
Russ Cox5958dd62009-03-04 17:19:21 -08003655
Rob Pike96750f12009-02-27 16:47:48 -08003656<p>
Robert Griesemer506c0082009-09-08 15:41:14 -07003657The following <a href="#Assignments">assignment statements</a> are semantically
Robert Griesemer52a54802008-09-30 13:02:50 -07003658equivalent:
Rob Pike96750f12009-02-27 16:47:48 -08003659</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003660
Rob Pikeff70f092009-02-20 13:36:14 -08003661<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08003662IncDec statement Assignment
3663x++ x += 1
3664x-- x -= 1
3665</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003666
Robert Griesemer5474e162010-09-28 14:44:19 -07003667
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003668<h3 id="Assignments">Assignments</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003669
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003670<pre class="ebnf">
Robert Griesemerc2d55862009-02-19 16:49:10 -08003671Assignment = ExpressionList assign_op ExpressionList .
Rob Pikeff70f092009-02-20 13:36:14 -08003672
Robert Griesemerc2d55862009-02-19 16:49:10 -08003673assign_op = [ add_op | mul_op ] "=" .
3674</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003675
Rob Pike96750f12009-02-27 16:47:48 -08003676<p>
Rob Pike678625d2009-09-15 09:54:22 -07003677Each left-hand side operand must be <a href="#Address_operators">addressable</a>,
Robert Griesemer6af887e2011-05-02 09:16:31 -07003678a map index expression, or the <a href="#Blank_identifier">blank identifier</a>.
3679Operands may be parenthesized.
Rob Pike96750f12009-02-27 16:47:48 -08003680</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003681
Robert Griesemerc2d55862009-02-19 16:49:10 -08003682<pre>
3683x = 1
3684*p = f()
3685a[i] = 23
Robert Griesemer6af887e2011-05-02 09:16:31 -07003686(k) = &lt;-ch // same as: k = &lt;-ch
Robert Griesemerc2d55862009-02-19 16:49:10 -08003687</pre>
Rob Pike96750f12009-02-27 16:47:48 -08003688
3689<p>
3690An <i>assignment operation</i> <code>x</code> <i>op</i><code>=</code>
3691<code>y</code> where <i>op</i> is a binary arithmetic operation is equivalent
3692to <code>x</code> <code>=</code> <code>x</code> <i>op</i>
Rob Pike4fe41922009-11-07 22:00:59 -08003693<code>y</code> but evaluates <code>x</code>
Rob Pike96750f12009-02-27 16:47:48 -08003694only once. The <i>op</i><code>=</code> construct is a single token.
Rob Pike678625d2009-09-15 09:54:22 -07003695In assignment operations, both the left- and right-hand expression lists
3696must contain exactly one single-valued expression.
Rob Pike96750f12009-02-27 16:47:48 -08003697</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003698
Robert Griesemerc2d55862009-02-19 16:49:10 -08003699<pre>
Robert Griesemer4ed666e2009-08-27 16:45:42 -07003700a[i] &lt;&lt;= 2
Rob Pike678625d2009-09-15 09:54:22 -07003701i &amp;^= 1&lt;&lt;n
Robert Griesemerc2d55862009-02-19 16:49:10 -08003702</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003703
Rob Pike96750f12009-02-27 16:47:48 -08003704<p>
3705A tuple assignment assigns the individual elements of a multi-valued
3706operation to a list of variables. There are two forms. In the
3707first, the right hand operand is a single multi-valued expression
Robert Griesemer506c0082009-09-08 15:41:14 -07003708such as a function evaluation or <a href="#Channel_types">channel</a> or
3709<a href="#Map_types">map</a> operation or a <a href="#Type_assertions">type assertion</a>.
Russ Cox5958dd62009-03-04 17:19:21 -08003710The number of operands on the left
Robert Griesemer4e56b332009-09-10 10:14:00 -07003711hand side must match the number of values. For instance, if
Rob Pike96750f12009-02-27 16:47:48 -08003712<code>f</code> is a function returning two values,
3713</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003714
Robert Griesemerc2d55862009-02-19 16:49:10 -08003715<pre>
3716x, y = f()
3717</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003718
Rob Pike96750f12009-02-27 16:47:48 -08003719<p>
3720assigns the first value to <code>x</code> and the second to <code>y</code>.
Rob Pike678625d2009-09-15 09:54:22 -07003721The <a href="#Blank_identifier">blank identifier</a> provides a
Robert Griesemer4e56b332009-09-10 10:14:00 -07003722way to ignore values returned by a multi-valued expression:
Rob Pike96750f12009-02-27 16:47:48 -08003723</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003724
Robert Griesemer4e56b332009-09-10 10:14:00 -07003725<pre>
3726x, _ = f() // ignore second value returned by f()
3727</pre>
3728
Rob Pike96750f12009-02-27 16:47:48 -08003729<p>
3730In the second form, the number of operands on the left must equal the number
Robert Griesemeref45e642009-08-21 11:25:00 -07003731of expressions on the right, each of which must be single-valued, and the
3732<i>n</i>th expression on the right is assigned to the <i>n</i>th
3733operand on the left.
Russ Cox5958dd62009-03-04 17:19:21 -08003734The expressions on the right are evaluated before assigning to
3735any of the operands on the left, but otherwise the evaluation
Rob Pike678625d2009-09-15 09:54:22 -07003736order is unspecified beyond <a href="#Order_of_evaluation">the usual rules</a>.
Rob Pike96750f12009-02-27 16:47:48 -08003737</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003738
Robert Griesemerc2d55862009-02-19 16:49:10 -08003739<pre>
Rob Pike96750f12009-02-27 16:47:48 -08003740a, b = b, a // exchange a and b
Robert Griesemerc2d55862009-02-19 16:49:10 -08003741</pre>
Rob Pike96750f12009-02-27 16:47:48 -08003742
3743<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07003744In assignments, each value must be
Robert Griesemer440cc952010-06-07 17:40:21 -07003745<a href="#Assignability">assignable</a> to the type of the
Robert Griesemer19b1d352009-09-24 19:36:48 -07003746operand to which it is assigned. If an untyped <a href="#Constants">constant</a>
3747is assigned to a variable of interface type, the constant is <a href="#Conversions">converted</a>
Robert Griesemerb94c0d22011-01-19 23:07:21 -05003748to type <code>bool</code>, <code>int</code>, <code>float64</code>,
3749<code>complex128</code> or <code>string</code>
Robert Griesemer19b1d352009-09-24 19:36:48 -07003750respectively, depending on whether the value is a boolean, integer, floating-point,
Rob Pike72970872010-03-04 12:35:16 -08003751complex, or string constant.
Rob Pike96750f12009-02-27 16:47:48 -08003752</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003753
3754
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003755<h3 id="If_statements">If statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003756
Rob Pike96750f12009-02-27 16:47:48 -08003757<p>
3758"If" statements specify the conditional execution of two branches
3759according to the value of a boolean expression. If the expression
3760evaluates to true, the "if" branch is executed, otherwise, if
Robert Griesemera1368a62011-02-22 15:31:57 -08003761present, the "else" branch is executed.
Rob Pike96750f12009-02-27 16:47:48 -08003762</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003763
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003764<pre class="ebnf">
Russ Cox58e19aa2011-07-14 17:15:52 -04003765IfStmt = "if" [ SimpleStmt ";" ] Expression Block [ "else" ( IfStmt | Block ) ] .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003766</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003767
Robert Griesemerc2d55862009-02-19 16:49:10 -08003768<pre>
Robert Griesemera1368a62011-02-22 15:31:57 -08003769if x &gt; max {
3770 x = max
Robert Griesemerc2d55862009-02-19 16:49:10 -08003771}
3772</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003773
Russ Cox5958dd62009-03-04 17:19:21 -08003774<p>
Russ Cox16b95ba2009-08-20 10:22:52 -07003775The expression may be preceded by a simple statement, which
3776executes before the expression is evaluated.
Russ Cox5958dd62009-03-04 17:19:21 -08003777</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003778
Robert Griesemerc2d55862009-02-19 16:49:10 -08003779<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08003780if x := f(); x &lt; y {
3781 return x
Robert Griesemera1368a62011-02-22 15:31:57 -08003782} else if x &gt; z {
Robert Griesemer130ac742009-12-10 16:43:01 -08003783 return z
Robert Griesemerc2d55862009-02-19 16:49:10 -08003784} else {
Robert Griesemer130ac742009-12-10 16:43:01 -08003785 return y
Robert Griesemerc2d55862009-02-19 16:49:10 -08003786}
3787</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003788
3789
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003790<h3 id="Switch_statements">Switch statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003791
Rob Pike96750f12009-02-27 16:47:48 -08003792<p>
3793"Switch" statements provide multi-way execution.
Rob Pike5a578492009-03-17 16:48:35 -07003794An expression or type specifier is compared to the "cases"
3795inside the "switch" to determine which branch
3796to execute.
Rob Pikeafee1c52009-03-20 17:41:25 -07003797</p>
Robert Griesemer091cba82009-03-19 08:39:40 -07003798
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003799<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003800SwitchStmt = ExprSwitchStmt | TypeSwitchStmt .
Robert Griesemer091cba82009-03-19 08:39:40 -07003801</pre>
3802
Rob Pikeafee1c52009-03-20 17:41:25 -07003803<p>
Rob Pike5a578492009-03-17 16:48:35 -07003804There are two forms: expression switches and type switches.
Rob Pike5a578492009-03-17 16:48:35 -07003805In an expression switch, the cases contain expressions that are compared
3806against the value of the switch expression.
3807In a type switch, the cases contain types that are compared against the
3808type of a specially annotated switch expression.
Rob Pike96750f12009-02-27 16:47:48 -08003809</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003810
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003811<h4 id="Expression_switches">Expression switches</h4>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003812
Rob Pike96750f12009-02-27 16:47:48 -08003813<p>
Rob Pike5a578492009-03-17 16:48:35 -07003814In an expression switch,
3815the switch expression is evaluated and
3816the case expressions, which need not be constants,
Robert Griesemer4f185492009-05-01 17:00:16 -07003817are evaluated left-to-right and top-to-bottom; the first one that equals the
Rob Pike5a578492009-03-17 16:48:35 -07003818switch expression
Rob Pike96750f12009-02-27 16:47:48 -08003819triggers execution of the statements of the associated case;
3820the other cases are skipped.
Rob Pike5a578492009-03-17 16:48:35 -07003821If no case matches and there is a "default" case,
3822its statements are executed.
Rob Pike96750f12009-02-27 16:47:48 -08003823There can be at most one default case and it may appear anywhere in the
3824"switch" statement.
Robert Griesemere9192752009-12-01 16:15:53 -08003825A missing switch expression is equivalent to
Rob Pike70c1a102009-03-18 19:23:59 -07003826the expression <code>true</code>.
Rob Pike96750f12009-02-27 16:47:48 -08003827</p>
Rob Pike70c1a102009-03-18 19:23:59 -07003828
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003829<pre class="ebnf">
Rob Pikef3a33bc2009-09-14 17:39:17 -07003830ExprSwitchStmt = "switch" [ SimpleStmt ";" ] [ Expression ] "{" { ExprCaseClause } "}" .
Robert Griesemer130ac742009-12-10 16:43:01 -08003831ExprCaseClause = ExprSwitchCase ":" { Statement ";" } .
Robert Griesemer091cba82009-03-19 08:39:40 -07003832ExprSwitchCase = "case" ExpressionList | "default" .
Rob Pike70c1a102009-03-18 19:23:59 -07003833</pre>
3834
Rob Pike96750f12009-02-27 16:47:48 -08003835<p>
3836In a case or default clause,
3837the last statement only may be a "fallthrough" statement
Evan Shaw67d30bb2010-05-25 18:24:07 -07003838<a href="#Fallthrough_statements">Fallthrough statement</a>) to
Rob Pike96750f12009-02-27 16:47:48 -08003839indicate that control should flow from the end of this clause to
Robert Griesemeraed247f2008-10-08 17:05:30 -07003840the first statement of the next clause.
Rob Pike96750f12009-02-27 16:47:48 -08003841Otherwise control flows to the end of the "switch" statement.
3842</p>
Robert Griesemer0a162a12009-08-19 16:44:04 -07003843
Robert Griesemerc2d55862009-02-19 16:49:10 -08003844<p>
Russ Cox16b95ba2009-08-20 10:22:52 -07003845The expression may be preceded by a simple statement, which
3846executes before the expression is evaluated.
Rob Pike96750f12009-02-27 16:47:48 -08003847</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003848
Robert Griesemerc2d55862009-02-19 16:49:10 -08003849<pre>
3850switch tag {
Rob Pike65ec16b2009-05-29 15:46:03 -07003851default: s3()
3852case 0, 1, 2, 3: s1()
3853case 4, 5, 6, 7: s2()
Robert Griesemerc2d55862009-02-19 16:49:10 -08003854}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003855
Andrew Gerrand10b77f72010-03-29 13:12:08 +11003856switch x := f(); { // missing switch expression means "true"
Rob Pike65ec16b2009-05-29 15:46:03 -07003857case x &lt; 0: return -x
3858default: return x
Robert Griesemerc2d55862009-02-19 16:49:10 -08003859}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003860
Robert Griesemere9192752009-12-01 16:15:53 -08003861switch {
Robert Griesemer130ac742009-12-10 16:43:01 -08003862case x &lt; y: f1()
3863case x &lt; z: f2()
3864case x == 4: f3()
Robert Griesemerc2d55862009-02-19 16:49:10 -08003865}
3866</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003867
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003868<h4 id="Type_switches">Type switches</h4>
Rob Pike70c1a102009-03-18 19:23:59 -07003869
Rob Pike5a578492009-03-17 16:48:35 -07003870<p>
Rob Pike70c1a102009-03-18 19:23:59 -07003871A type switch compares types rather than values. It is otherwise similar
Robert Griesemer1f95f0d2009-08-27 16:44:17 -07003872to an expression switch. It is marked by a special switch expression that
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003873has the form of a <a href="#Type_assertions">type assertion</a>
Rob Pike70c1a102009-03-18 19:23:59 -07003874using the reserved word <code>type</code> rather than an actual type.
3875Cases then match literal types against the dynamic type of the expression
Rob Pikef5387602009-03-30 16:08:41 -07003876in the type assertion.
Rob Pike5a578492009-03-17 16:48:35 -07003877</p>
3878
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003879<pre class="ebnf">
Rob Pikef3a33bc2009-09-14 17:39:17 -07003880TypeSwitchStmt = "switch" [ SimpleStmt ";" ] TypeSwitchGuard "{" { TypeCaseClause } "}" .
Russ Coxc1045db2009-12-23 13:48:44 -08003881TypeSwitchGuard = [ identifier ":=" ] PrimaryExpr "." "(" "type" ")" .
Robert Griesemer130ac742009-12-10 16:43:01 -08003882TypeCaseClause = TypeSwitchCase ":" { Statement ";" } .
Rob Pike678625d2009-09-15 09:54:22 -07003883TypeSwitchCase = "case" TypeList | "default" .
3884TypeList = Type { "," Type } .
Rob Pike5a578492009-03-17 16:48:35 -07003885</pre>
3886
3887<p>
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003888The TypeSwitchGuard may include a
3889<a href="#Short_variable_declarations">short variable declaration</a>.
3890When that form is used, the variable is declared in each clause.
3891In clauses with a case listing exactly one type, the variable
3892has that type; otherwise, the variable has the type of the expression
3893in the TypeSwitchGuard.
Rob Pike94b67eb2009-03-24 17:40:47 -07003894</p>
3895
3896<p>
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003897The type in a case may be <code>nil</code>
3898<a href="#Predeclared_identifiers">Predeclared identifiers</a>);
3899that case is used when the expression in the TypeSwitchGuard
Robert Griesemer1f95f0d2009-08-27 16:44:17 -07003900is a <code>nil</code> interface value.
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003901</p>
3902
3903<p>
Robert Griesemere9192752009-12-01 16:15:53 -08003904Given an expression <code>x</code> of type <code>interface{}</code>,
Rob Pike70c1a102009-03-18 19:23:59 -07003905the following type switch:
Rob Pike5a578492009-03-17 16:48:35 -07003906</p>
3907
3908<pre>
Robert Griesemere9192752009-12-01 16:15:53 -08003909switch i := x.(type) {
Rob Pike94b67eb2009-03-24 17:40:47 -07003910case nil:
Robert Griesemer130ac742009-12-10 16:43:01 -08003911 printString("x is nil")
Rob Pike5a578492009-03-17 16:48:35 -07003912case int:
Robert Griesemer130ac742009-12-10 16:43:01 -08003913 printInt(i) // i is an int
Robert Griesemerb94c0d22011-01-19 23:07:21 -05003914case float64:
3915 printFloat64(i) // i is a float64
3916case func(int) float64:
Robert Griesemer130ac742009-12-10 16:43:01 -08003917 printFunction(i) // i is a function
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003918case bool, string:
Robert Griesemer130ac742009-12-10 16:43:01 -08003919 printString("type is bool or string") // i is an interface{}
Rob Pike5a578492009-03-17 16:48:35 -07003920default:
Robert Griesemer130ac742009-12-10 16:43:01 -08003921 printString("don't know the type")
Rob Pike5a578492009-03-17 16:48:35 -07003922}
3923</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003924
Rob Pike70c1a102009-03-18 19:23:59 -07003925<p>
3926could be rewritten:
3927</p>
3928
3929<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08003930v := x // x is evaluated exactly once
Rob Pike94b67eb2009-03-24 17:40:47 -07003931if v == nil {
Robert Griesemer130ac742009-12-10 16:43:01 -08003932 printString("x is nil")
Rob Pike94b67eb2009-03-24 17:40:47 -07003933} else if i, is_int := v.(int); is_int {
Robert Griesemer130ac742009-12-10 16:43:01 -08003934 printInt(i) // i is an int
Robert Griesemerb94c0d22011-01-19 23:07:21 -05003935} else if i, is_float64 := v.(float64); is_float64 {
3936 printFloat64(i) // i is a float64
3937} else if i, is_func := v.(func(int) float64); is_func {
Robert Griesemer130ac742009-12-10 16:43:01 -08003938 printFunction(i) // i is a function
Rob Pike70c1a102009-03-18 19:23:59 -07003939} else {
Robert Griesemer130ac742009-12-10 16:43:01 -08003940 i1, is_bool := v.(bool)
3941 i2, is_string := v.(string)
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003942 if is_bool || is_string {
Robert Griesemer130ac742009-12-10 16:43:01 -08003943 i := v
3944 printString("type is bool or string") // i is an interface{}
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003945 } else {
Robert Griesemer130ac742009-12-10 16:43:01 -08003946 i := v
3947 printString("don't know the type") // i is an interface{}
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003948 }
Rob Pike70c1a102009-03-18 19:23:59 -07003949}
3950</pre>
3951
Robert Griesemeraeaab592009-08-31 17:30:55 -07003952<p>
Russ Cox16b95ba2009-08-20 10:22:52 -07003953The type switch guard may be preceded by a simple statement, which
3954executes before the guard is evaluated.
Robert Griesemer1f95f0d2009-08-27 16:44:17 -07003955</p>
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003956
3957<p>
3958The "fallthrough" statement is not permitted in a type switch.
Russ Cox16b95ba2009-08-20 10:22:52 -07003959</p>
3960
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003961<h3 id="For_statements">For statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003962
Rob Pike96750f12009-02-27 16:47:48 -08003963<p>
3964A "for" statement specifies repeated execution of a block. The iteration is
3965controlled by a condition, a "for" clause, or a "range" clause.
3966</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003967
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003968<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003969ForStmt = "for" [ Condition | ForClause | RangeClause ] Block .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003970Condition = Expression .
3971</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003972
Rob Pike96750f12009-02-27 16:47:48 -08003973<p>
3974In its simplest form, a "for" statement specifies the repeated execution of
3975a block as long as a boolean condition evaluates to true.
3976The condition is evaluated before each iteration.
3977If the condition is absent, it is equivalent to <code>true</code>.
3978</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003979
Robert Griesemerc2d55862009-02-19 16:49:10 -08003980<pre>
3981for a &lt; b {
3982 a *= 2
3983}
3984</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003985
Rob Pike96750f12009-02-27 16:47:48 -08003986<p>
Rob Pike678625d2009-09-15 09:54:22 -07003987A "for" statement with a ForClause is also controlled by its condition, but
Rob Pike96750f12009-02-27 16:47:48 -08003988additionally it may specify an <i>init</i>
3989and a <i>post</i> statement, such as an assignment,
Russ Cox16b95ba2009-08-20 10:22:52 -07003990an increment or decrement statement. The init statement may be a
Robert Griesemer4ed666e2009-08-27 16:45:42 -07003991<a href="#Short_variable_declarations">short variable declaration</a>, but the post statement must not.
Rob Pike96750f12009-02-27 16:47:48 -08003992</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003993
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003994<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -08003995ForClause = [ InitStmt ] ";" [ Condition ] ";" [ PostStmt ] .
Rob Pike11417162009-03-24 17:45:53 -07003996InitStmt = SimpleStmt .
3997PostStmt = SimpleStmt .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003998</pre>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003999
Robert Griesemerc2d55862009-02-19 16:49:10 -08004000<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08004001for i := 0; i &lt; 10; i++ {
Robert Griesemerc2d55862009-02-19 16:49:10 -08004002 f(i)
4003}
4004</pre>
Russ Cox5958dd62009-03-04 17:19:21 -08004005
Robert Griesemerc2d55862009-02-19 16:49:10 -08004006<p>
Rob Pike96750f12009-02-27 16:47:48 -08004007If non-empty, the init statement is executed once before evaluating the
4008condition for the first iteration;
4009the post statement is executed after each execution of the block (and
4010only if the block was executed).
Robert Griesemer130ac742009-12-10 16:43:01 -08004011Any element of the ForClause may be empty but the
4012<a href="#Semicolons">semicolons</a> are
Rob Pike96750f12009-02-27 16:47:48 -08004013required unless there is only a condition.
4014If the condition is absent, it is equivalent to <code>true</code>.
4015</p>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08004016
Robert Griesemerc2d55862009-02-19 16:49:10 -08004017<pre>
Rob Pike678625d2009-09-15 09:54:22 -07004018for cond { S() } is the same as for ; cond ; { S() }
4019for { S() } is the same as for true { S() }
Robert Griesemerc2d55862009-02-19 16:49:10 -08004020</pre>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08004021
Rob Pike96750f12009-02-27 16:47:48 -08004022<p>
4023A "for" statement with a "range" clause
Rob Pike7aee71b2009-04-15 20:28:25 -07004024iterates through all entries of an array, slice, string or map,
Robert Griesemer5474e162010-09-28 14:44:19 -07004025or values received on a channel. For each entry it assigns <i>iteration values</i>
4026to corresponding <i>iteration variables</i> and then executes the block.
Rob Pike96750f12009-02-27 16:47:48 -08004027</p>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08004028
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004029<pre class="ebnf">
Robert Griesemer5474e162010-09-28 14:44:19 -07004030RangeClause = Expression [ "," Expression ] ( "=" | ":=" ) "range" Expression .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004031</pre>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08004032
Robert Griesemerc2d55862009-02-19 16:49:10 -08004033<p>
Robert Griesemer5474e162010-09-28 14:44:19 -07004034The expression on the right in the "range" clause is called the <i>range expression</i>,
4035which may be an array, pointer to an array, slice, string, map, or channel.
4036As with an assignment, the operands on the left must be
4037<a href="#Address_operators">addressable</a> or map index expressions; they
4038denote the iteration variables. If the range expression is a channel, only
4039one iteration variable is permitted, otherwise there may be one or two.
Robert Griesemer54731032011-05-12 09:15:59 -07004040If the second iteration variable is the <a href="#Blank_identifier">blank identifier</a>,
4041the range clause is equivalent to the same clause with only the first variable present.
Robert Griesemer5474e162010-09-28 14:44:19 -07004042</p>
Rob Pike29d0f022011-01-05 11:39:57 -08004043
4044<p>
Robert Griesemer54731032011-05-12 09:15:59 -07004045The range expression is evaluated once before beginning the loop
4046except if the expression is an array, in which case, depending on
4047the expression, it might not be evaluated (see below).
Robert Griesemer5474e162010-09-28 14:44:19 -07004048Function calls on the left are evaluated once per iteration.
4049For each iteration, iteration values are produced as follows:
4050</p>
4051
4052<pre class="grammar">
4053Range expression 1st value 2nd value (if 2nd variable is present)
4054
4055array or slice a [n]E, *[n]E, or []E index i int a[i] E
4056string s string type index i int see below int
4057map m map[K]V key k K m[k] V
4058channel c chan E element e E
4059</pre>
4060
4061<ol>
4062<li>
Robert Griesemer54731032011-05-12 09:15:59 -07004063For an array, pointer to array, or slice value <code>a</code>, the index iteration
4064values are produced in increasing order, starting at element index 0. As a special
4065case, if only the first iteration variable is present, the range loop produces
4066iteration values from 0 up to <code>len(a)</code> and does not index into the array
4067or slice itself. For a <code>nil</code> slice, the number of iterations is 0.
Robert Griesemer5474e162010-09-28 14:44:19 -07004068</li>
4069
4070<li>
4071For a string value, the "range" clause iterates over the Unicode code points
4072in the string starting at byte index 0. On successive iterations, the index value will be the
4073index of the first byte of successive UTF-8-encoded code points in the string,
4074and the second value, of type <code>int</code>, will be the value of
Rob Pike7aee71b2009-04-15 20:28:25 -07004075the corresponding code point. If the iteration encounters an invalid
Robert Griesemer5474e162010-09-28 14:44:19 -07004076UTF-8 sequence, the second value will be <code>0xFFFD</code>,
Rob Pike7aee71b2009-04-15 20:28:25 -07004077the Unicode replacement character, and the next iteration will advance
4078a single byte in the string.
Robert Griesemer5474e162010-09-28 14:44:19 -07004079</li>
4080
4081<li>
4082The iteration order over maps is not specified.
4083If map entries that have not yet been reached are deleted during iteration,
4084the corresponding iteration values will not be produced. If map entries are
4085inserted during iteration, the behavior is implementation-dependent, but the
Robert Griesemer54731032011-05-12 09:15:59 -07004086iteration values for each entry will be produced at most once. If the map
4087is <code>nil</code>, the number of iterations is 0.
Robert Griesemer5474e162010-09-28 14:44:19 -07004088</li>
4089
4090<li>
4091For channels, the iteration values produced are the successive values sent on
Robert Griesemer54731032011-05-12 09:15:59 -07004092the channel until the channel is <a href="#Close">closed</a>. If the channel
4093is <code>nil</code>, the range expression blocks forever.
Robert Griesemer5474e162010-09-28 14:44:19 -07004094</li>
Anthony Martin0122a662011-02-08 14:51:15 -08004095</ol>
Robert Griesemer5474e162010-09-28 14:44:19 -07004096
Rob Pike7aee71b2009-04-15 20:28:25 -07004097<p>
Robert Griesemer5474e162010-09-28 14:44:19 -07004098The iteration values are assigned to the respective
4099iteration variables as in an <a href="#Assignments">assignment statement</a>.
Rob Pike94b67eb2009-03-24 17:40:47 -07004100</p>
Robert Griesemer5474e162010-09-28 14:44:19 -07004101
Rob Pike94b67eb2009-03-24 17:40:47 -07004102<p>
Robert Griesemer5474e162010-09-28 14:44:19 -07004103The iteration variables may be declared by the "range" clause (<code>:=</code>).
4104In this case their types are set to the types of the respective iteration values
4105and their <a href="#Declarations_and_scope">scope</a> ends at the end of the "for"
4106statement; they are re-used in each iteration.
Rob Pike96750f12009-02-27 16:47:48 -08004107If the iteration variables are declared outside the "for" statement,
4108after execution their values will be those of the last iteration.
4109</p>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08004110
Robert Griesemerc2d55862009-02-19 16:49:10 -08004111<pre>
Robert Griesemer54731032011-05-12 09:15:59 -07004112var testdata *struct {
4113 a *[7]int
4114}
4115for i, _ := range testdata.a {
4116 // testdata.a is never evaluated; len(testdata.a) is constant
4117 // i ranges from 0 to 6
4118 f(i)
4119}
4120
Robert Griesemer130ac742009-12-10 16:43:01 -08004121var a [10]string
4122m := map[string]int{"mon":0, "tue":1, "wed":2, "thu":3, "fri":4, "sat":5, "sun":6}
Robert Griesemerc2d55862009-02-19 16:49:10 -08004123for i, s := range a {
4124 // type of i is int
4125 // type of s is string
4126 // s == a[i]
4127 g(i, s)
4128}
4129
Robert Griesemer130ac742009-12-10 16:43:01 -08004130var key string
Robert Griesemer440cc952010-06-07 17:40:21 -07004131var val interface {} // value type of m is assignable to val
Rob Pike678625d2009-09-15 09:54:22 -07004132for key, val = range m {
4133 h(key, val)
Robert Griesemerc2d55862009-02-19 16:49:10 -08004134}
4135// key == last map key encountered in iteration
4136// val == map[key]
Robert Griesemer54731032011-05-12 09:15:59 -07004137
4138var ch chan Work = producer()
4139for w := range ch {
4140 doWork(w)
4141}
Robert Griesemerc2d55862009-02-19 16:49:10 -08004142</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004143
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004144
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004145<h3 id="Go_statements">Go statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004146
Rob Pike96750f12009-02-27 16:47:48 -08004147<p>
Russ Cox5958dd62009-03-04 17:19:21 -08004148A "go" statement starts the execution of a function or method call
Rob Pike96750f12009-02-27 16:47:48 -08004149as an independent concurrent thread of control, or <i>goroutine</i>,
4150within the same address space.
4151</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004152
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004153<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07004154GoStmt = "go" Expression .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004155</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004156
Rob Pike96750f12009-02-27 16:47:48 -08004157<p>
4158The expression must be a call, and
4159unlike with a regular call, program execution does not wait
Robert Griesemerb9f8b9c2008-09-26 13:38:38 -07004160for the invoked function to complete.
Rob Pike96750f12009-02-27 16:47:48 -08004161</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004162
Robert Griesemerc2d55862009-02-19 16:49:10 -08004163<pre>
4164go Server()
Robert Griesemere1e76192009-09-25 14:11:03 -07004165go func(ch chan&lt;- bool) { for { sleep(10); ch &lt;- true; }} (c)
Robert Griesemerc2d55862009-02-19 16:49:10 -08004166</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004167
4168
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004169<h3 id="Select_statements">Select statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004170
Rob Pike96750f12009-02-27 16:47:48 -08004171<p>
4172A "select" statement chooses which of a set of possible communications
4173will proceed. It looks similar to a "switch" statement but with the
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004174cases all referring to communication operations.
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">
Rob Pike11417162009-03-24 17:45:53 -07004178SelectStmt = "select" "{" { CommClause } "}" .
Robert Griesemer130ac742009-12-10 16:43:01 -08004179CommClause = CommCase ":" { Statement ";" } .
Robert Griesemerb50ed022011-02-01 12:02:49 -08004180CommCase = "case" ( SendStmt | RecvStmt ) | "default" .
Russ Cox9f2cb862011-03-11 14:47:02 -05004181RecvStmt = [ Expression [ "," Expression ] ( "=" | ":=" ) ] RecvExpr .
Robert Griesemerc1347182011-04-29 12:20:31 -07004182RecvExpr = Expression .
Russ Cox61439182011-01-31 17:42:10 -05004183</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004184
Rob Pike96750f12009-02-27 16:47:48 -08004185<p>
Robert Griesemerc1347182011-04-29 12:20:31 -07004186RecvExpr must be a <a href="#Receive_operator">receive operation</a>.
Robert Griesemerb50ed022011-02-01 12:02:49 -08004187For all the cases in the "select"
Rob Pike041d1162010-07-13 16:23:54 -07004188statement, the channel expressions are evaluated in top-to-bottom order, along with
Robert Griesemerb50ed022011-02-01 12:02:49 -08004189any expressions that appear on the right hand side of send statements.
Rob Pike132d2f12010-08-16 06:42:41 +10004190A channel may be <code>nil</code>,
Rob Pike96750f12009-02-27 16:47:48 -08004191which is equivalent to that case not
4192being present in the select statement
4193except, if a send, its expression is still evaluated.
Rob Pike041d1162010-07-13 16:23:54 -07004194If any of the resulting operations can proceed, one of those is
4195chosen and the corresponding communication and statements are
4196evaluated. Otherwise, if there is a default case, that executes;
4197if there is no default case, the statement blocks until one of the communications can
4198complete.
4199If there are no cases with non-<code>nil</code> channels,
4200the statement blocks forever.
4201Even if the statement blocks,
4202the channel and send expressions are evaluated only once,
4203upon entering the select statement.
Rob Pike96750f12009-02-27 16:47:48 -08004204</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004205<p>
Rob Pike569a1072008-10-03 11:18:45 -07004206Since all the channels and send expressions are evaluated, any side
4207effects in that evaluation will occur for all the communications
Rob Pike96750f12009-02-27 16:47:48 -08004208in the "select" statement.
4209</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004210<p>
Rob Pike041d1162010-07-13 16:23:54 -07004211If multiple cases can proceed, a pseudo-random fair choice is made to decide
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004212which single communication will execute.
Robert Griesemerc2d55862009-02-19 16:49:10 -08004213<p>
Russ Cox9f2cb862011-03-11 14:47:02 -05004214The receive case may declare one or two new variables using a
Robert Griesemer4ed666e2009-08-27 16:45:42 -07004215<a href="#Short_variable_declarations">short variable declaration</a>.
Rob Pike96750f12009-02-27 16:47:48 -08004216</p>
Robert Griesemer2902a822008-09-17 13:57:11 -07004217
Robert Griesemerc2d55862009-02-19 16:49:10 -08004218<pre>
Russ Cox9f2cb862011-03-11 14:47:02 -05004219var c, c1, c2, c3 chan int
Robert Griesemer130ac742009-12-10 16:43:01 -08004220var i1, i2 int
Robert Griesemerc2d55862009-02-19 16:49:10 -08004221select {
4222case i1 = &lt;-c1:
Robert Griesemer130ac742009-12-10 16:43:01 -08004223 print("received ", i1, " from c1\n")
Robert Griesemerc2d55862009-02-19 16:49:10 -08004224case c2 &lt;- i2:
Robert Griesemer130ac742009-12-10 16:43:01 -08004225 print("sent ", i2, " to c2\n")
Robert Griesemer6af887e2011-05-02 09:16:31 -07004226case i3, ok := (&lt;-c3): // same as: i3, ok := &lt;-c3
Russ Cox19d9a402011-01-27 15:34:28 -05004227 if ok {
4228 print("received ", i3, " from c3\n")
4229 } else {
4230 print("c3 is closed\n")
4231 }
Robert Griesemerc2d55862009-02-19 16:49:10 -08004232default:
Robert Griesemer130ac742009-12-10 16:43:01 -08004233 print("no communication\n")
Robert Griesemerc2d55862009-02-19 16:49:10 -08004234}
4235
4236for { // send random sequence of bits to c
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004237 select {
Robert Griesemerc2d55862009-02-19 16:49:10 -08004238 case c &lt;- 0: // note: no statement, no fallthrough, no folding of cases
4239 case c &lt;- 1:
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004240 }
Robert Griesemerc2d55862009-02-19 16:49:10 -08004241}
Rob Pike041d1162010-07-13 16:23:54 -07004242
4243select { } // block forever
Robert Griesemerc2d55862009-02-19 16:49:10 -08004244</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004245
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004246
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004247<h3 id="Return_statements">Return statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004248
Rob Pike96750f12009-02-27 16:47:48 -08004249<p>
4250A "return" statement terminates execution of the containing function
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004251and optionally provides a result value or values to the caller.
Rob Pike96750f12009-02-27 16:47:48 -08004252</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004253
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004254<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07004255ReturnStmt = "return" [ ExpressionList ] .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004256</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004257
Robert Griesemer4b908332009-08-07 17:05:41 -07004258<p>
4259In a function without a result type, a "return" statement must not
4260specify any result values.
4261</p>
Rob Pike96750f12009-02-27 16:47:48 -08004262<pre>
Robert Griesemer4b908332009-08-07 17:05:41 -07004263func no_result() {
Rob Pike96750f12009-02-27 16:47:48 -08004264 return
4265}
4266</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004267
Rob Pike96750f12009-02-27 16:47:48 -08004268<p>
Robert Griesemer4b908332009-08-07 17:05:41 -07004269There are three ways to return values from a function with a result
4270type:
Rob Pike96750f12009-02-27 16:47:48 -08004271</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004272
Robert Griesemer4b908332009-08-07 17:05:41 -07004273<ol>
4274 <li>The return value or values may be explicitly listed
4275 in the "return" statement. Each expression must be single-valued
Robert Griesemer440cc952010-06-07 17:40:21 -07004276 and <a href="#Assignability">assignable</a>
4277 to the corresponding element of the function's result type.
Robert Griesemerc2d55862009-02-19 16:49:10 -08004278<pre>
4279func simple_f() int {
Rob Pike96750f12009-02-27 16:47:48 -08004280 return 2
Robert Griesemerc2d55862009-02-19 16:49:10 -08004281}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004282
Robert Griesemerb94c0d22011-01-19 23:07:21 -05004283func complex_f1() (re float64, im float64) {
Rob Pike96750f12009-02-27 16:47:48 -08004284 return -7.0, -4.0
Robert Griesemerc2d55862009-02-19 16:49:10 -08004285}
4286</pre>
Robert Griesemer4b908332009-08-07 17:05:41 -07004287 </li>
4288 <li>The expression list in the "return" statement may be a single
4289 call to a multi-valued function. The effect is as if each value
4290 returned from that function were assigned to a temporary
4291 variable with the type of the respective value, followed by a
4292 "return" statement listing these variables, at which point the
4293 rules of the previous case apply.
Robert Griesemerc2d55862009-02-19 16:49:10 -08004294<pre>
Robert Griesemerb94c0d22011-01-19 23:07:21 -05004295func complex_f2() (re float64, im float64) {
Rob Pike96750f12009-02-27 16:47:48 -08004296 return complex_f1()
4297}
4298</pre>
Robert Griesemer4b908332009-08-07 17:05:41 -07004299 </li>
Peter Mundy5928e1d2010-11-09 10:10:57 -08004300 <li>The expression list may be empty if the function's result
Robert Griesemerfb64e0d2011-03-07 16:29:07 -08004301 type specifies names for its result parameters (§<a href="#Function_types">Function Types</a>).
Rob Pikedb8c2b182010-06-11 21:30:03 -07004302 The result parameters act as ordinary local variables
Robert Griesemer4b908332009-08-07 17:05:41 -07004303 and the function may assign values to them as necessary.
4304 The "return" statement returns the values of these variables.
Rob Pike96750f12009-02-27 16:47:48 -08004305<pre>
Robert Griesemerb94c0d22011-01-19 23:07:21 -05004306func complex_f3() (re float64, im float64) {
Robert Griesemer130ac742009-12-10 16:43:01 -08004307 re = 7.0
4308 im = 4.0
4309 return
Robert Griesemerc2d55862009-02-19 16:49:10 -08004310}
Robert Griesemerfb64e0d2011-03-07 16:29:07 -08004311
4312func (devnull) Write(p []byte) (n int, _ os.Error) {
4313 n = len(p)
4314 return
4315}
Robert Griesemerc2d55862009-02-19 16:49:10 -08004316</pre>
Robert Griesemer4b908332009-08-07 17:05:41 -07004317 </li>
4318</ol>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004319
Rob Pikedb8c2b182010-06-11 21:30:03 -07004320<p>
4321Regardless of how they are declared, all the result values are initialized to the zero values for their type (§<a href="#The_zero_value">The zero value</a>) upon entry to the function.
4322</p>
4323
Robert Griesemer54731032011-05-12 09:15:59 -07004324<!--
Russ Cox5958dd62009-03-04 17:19:21 -08004325<p>
Robert Griesemer90cc4a52009-10-22 09:41:38 -07004326<span class="alert">
Robert Griesemer4b908332009-08-07 17:05:41 -07004327TODO: Define when return is required.<br />
4328TODO: Language about result parameters needs to go into a section on
4329 function/method invocation<br />
Robert Griesemer90cc4a52009-10-22 09:41:38 -07004330</span>
Russ Cox5958dd62009-03-04 17:19:21 -08004331</p>
Rob Pike38d7bcf2011-05-08 14:05:18 -07004332-->
Russ Cox5958dd62009-03-04 17:19:21 -08004333
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004334<h3 id="Break_statements">Break statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004335
Rob Pike96750f12009-02-27 16:47:48 -08004336<p>
4337A "break" statement terminates execution of the innermost
4338"for", "switch" or "select" statement.
4339</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004340
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004341<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -08004342BreakStmt = "break" [ Label ] .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004343</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004344
Rob Pike96750f12009-02-27 16:47:48 -08004345<p>
4346If there is a label, it must be that of an enclosing
4347"for", "switch" or "select" statement, and that is the one whose execution
4348terminates
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004349<a href="#For_statements">For statements</a>, §<a href="#Switch_statements">Switch statements</a>, §<a href="#Select_statements">Select statements</a>).
Rob Pike96750f12009-02-27 16:47:48 -08004350</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004351
Robert Griesemerc2d55862009-02-19 16:49:10 -08004352<pre>
Russ Cox108564d2011-03-15 13:51:24 -04004353L:
4354 for i &lt; n {
4355 switch i {
4356 case 5:
4357 break L
4358 }
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004359 }
Robert Griesemerc2d55862009-02-19 16:49:10 -08004360</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004361
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004362<h3 id="Continue_statements">Continue statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004363
Rob Pike96750f12009-02-27 16:47:48 -08004364<p>
4365A "continue" statement begins the next iteration of the
Rob Pike678625d2009-09-15 09:54:22 -07004366innermost "for" loop at its post statement (§<a href="#For_statements">For statements</a>).
Rob Pike96750f12009-02-27 16:47:48 -08004367</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004368
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004369<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -08004370ContinueStmt = "continue" [ Label ] .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004371</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004372
Rob Pike96750f12009-02-27 16:47:48 -08004373<p>
Rob Pikede9219962010-04-28 13:18:40 -07004374If there is a label, it must be that of an enclosing
4375"for" statement, and that is the one whose execution
4376advances
4377<a href="#For_statements">For statements</a>).
Rob Pike96750f12009-02-27 16:47:48 -08004378</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004379
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004380<h3 id="Goto_statements">Goto statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004381
Rob Pike96750f12009-02-27 16:47:48 -08004382<p>
4383A "goto" statement transfers control to the statement with the corresponding label.
4384</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004385
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004386<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07004387GotoStmt = "goto" Label .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004388</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004389
Robert Griesemerc2d55862009-02-19 16:49:10 -08004390<pre>
4391goto Error
4392</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004393
Rob Pike96750f12009-02-27 16:47:48 -08004394<p>
4395Executing the "goto" statement must not cause any variables to come into
Russ Coxf4c7db02011-06-17 12:49:04 -04004396<a href="#Declarations_and_scope">scope</a> that were not already in scope at the point of the goto.
4397For instance, this example:
Rob Pike96750f12009-02-27 16:47:48 -08004398</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004399
Robert Griesemerc2d55862009-02-19 16:49:10 -08004400<pre>
Russ Cox108564d2011-03-15 13:51:24 -04004401 goto L // BAD
4402 v := 3
Robert Griesemerc2d55862009-02-19 16:49:10 -08004403L:
4404</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004405
Rob Pike96750f12009-02-27 16:47:48 -08004406<p>
4407is erroneous because the jump to label <code>L</code> skips
4408the creation of <code>v</code>.
Russ Coxf4c7db02011-06-17 12:49:04 -04004409</p>
4410
4411<p>
4412A "goto" statement outside a <a href="#Blocks">block</a> cannot jump to a label inside that block.
4413For instance, this example:
4414</p>
4415
4416<pre>
4417if n%2 == 1 {
4418 goto L1
4419}
4420for n &gt; 0 {
4421 f()
4422 n--
4423L1:
4424 f()
4425 n--
4426}
4427</pre>
4428
4429<p>
4430is erroneous because the label <code>L1</code> is inside
4431the "for" statement's block but the <code>goto</code> is not.
Rob Pike96750f12009-02-27 16:47:48 -08004432</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004433
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004434<h3 id="Fallthrough_statements">Fallthrough statements</h3>
Robert Griesemeraed247f2008-10-08 17:05:30 -07004435
Rob Pike96750f12009-02-27 16:47:48 -08004436<p>
4437A "fallthrough" statement transfers control to the first statement of the
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004438next case clause in a expression "switch" statement (§<a href="#Expression_switches">Expression switches</a>). It may
Robert Griesemer091cba82009-03-19 08:39:40 -07004439be used only as the final non-empty statement in a case or default clause in an
4440expression "switch" statement.
Rob Pike96750f12009-02-27 16:47:48 -08004441</p>
Robert Griesemeraed247f2008-10-08 17:05:30 -07004442
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004443<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07004444FallthroughStmt = "fallthrough" .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004445</pre>
Robert Griesemeraed247f2008-10-08 17:05:30 -07004446
4447
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004448<h3 id="Defer_statements">Defer statements</h3>
Robert Griesemer4a903e02009-01-27 09:29:40 -08004449
Rob Pike96750f12009-02-27 16:47:48 -08004450<p>
4451A "defer" statement invokes a function whose execution is deferred to the moment
4452the surrounding function returns.
4453</p>
Robert Griesemer4a903e02009-01-27 09:29:40 -08004454
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004455<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07004456DeferStmt = "defer" Expression .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004457</pre>
Robert Griesemer4a903e02009-01-27 09:29:40 -08004458
Rob Pike96750f12009-02-27 16:47:48 -08004459<p>
4460The expression must be a function or method call.
4461Each time the "defer" statement
Robert Griesemer7471eab2009-01-27 14:51:24 -08004462executes, the parameters to the function call are evaluated and saved anew but the
Rob Pike678625d2009-09-15 09:54:22 -07004463function is not invoked.
4464Deferred function calls are executed in LIFO order
4465immediately before the surrounding function returns,
Robert Griesemer48f0cd22010-03-23 17:30:14 -07004466after the return values, if any, have been evaluated, but before they
4467are returned to the caller. For instance, if the deferred function is
Rob Pike5bb29fb2010-03-25 17:59:59 -07004468a <a href="#Function_literals">function literal</a> and the surrounding
Robert Griesemer48f0cd22010-03-23 17:30:14 -07004469function has <a href="#Function_types">named result parameters</a> that
4470are in scope within the literal, the deferred function may access and modify
4471the result parameters before they are returned.
Rob Pike96750f12009-02-27 16:47:48 -08004472</p>
Robert Griesemer4a903e02009-01-27 09:29:40 -08004473
Robert Griesemerc2d55862009-02-19 16:49:10 -08004474<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08004475lock(l)
4476defer unlock(l) // unlocking happens before surrounding function returns
Robert Griesemer4a903e02009-01-27 09:29:40 -08004477
Robert Griesemerc2d55862009-02-19 16:49:10 -08004478// prints 3 2 1 0 before surrounding function returns
4479for i := 0; i &lt;= 3; i++ {
Robert Griesemer130ac742009-12-10 16:43:01 -08004480 defer fmt.Print(i)
Robert Griesemerc2d55862009-02-19 16:49:10 -08004481}
Robert Griesemer48f0cd22010-03-23 17:30:14 -07004482
4483// f returns 1
4484func f() (result int) {
4485 defer func() {
4486 result++
4487 }()
4488 return 0
4489}
Robert Griesemerc2d55862009-02-19 16:49:10 -08004490</pre>
Robert Griesemer4a903e02009-01-27 09:29:40 -08004491
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004492<h2 id="Built-in_functions">Built-in functions</h2>
4493
4494<p>
Rob Pike72970872010-03-04 12:35:16 -08004495Built-in functions are
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004496<a href="#Predeclared_identifiers">predeclared</a>.
4497They are called like any other function but some of them
4498accept a type instead of an expression as the first argument.
4499</p>
4500
Russ Cox2a5f0c62009-12-04 10:23:12 -08004501<p>
4502The built-in functions do not have standard Go types,
4503so they can only appear in <a href="#Calls">call expressions</a>;
4504they cannot be used as function values.
4505</p>
4506
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004507<pre class="ebnf">
Robert Griesemerac771a82010-09-24 14:08:28 -07004508BuiltinCall = identifier "(" [ BuiltinArgs [ "," ] ] ")" .
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004509BuiltinArgs = Type [ "," ExpressionList ] | ExpressionList .
4510</pre>
4511
Russ Cox9f2cb862011-03-11 14:47:02 -05004512<h3 id="Close">Close</h3>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004513
4514<p>
Robert Griesemerdc60c5a2010-07-14 16:09:22 -07004515For a channel <code>c</code>, the built-in function <code>close(c)</code>
4516marks the channel as unable to accept more values through a send operation;
Russ Cox27c74d32011-01-21 15:07:13 -05004517sending to or closing a closed channel causes a <a href="#Run_time_panics">run-time panic</a>.
Robert Griesemerdc60c5a2010-07-14 16:09:22 -07004518After calling <code>close</code>, and after any previously
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004519sent values have been received, receive operations will return
Robert Griesemerdc60c5a2010-07-14 16:09:22 -07004520the zero value for the channel's type without blocking.
Russ Cox19d9a402011-01-27 15:34:28 -05004521
Russ Cox9f2cb862011-03-11 14:47:02 -05004522The multi-valued <a href="#Receive_operator">receive operation</a>
4523returns a received value along with an indication of whether the channel is closed.
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004524</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004525
Robert Griesemerdc60c5a2010-07-14 16:09:22 -07004526
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004527<h3 id="Length_and_capacity">Length and capacity</h3>
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -07004528
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004529<p>
4530The built-in functions <code>len</code> and <code>cap</code> take arguments
4531of various types and return a result of type <code>int</code>.
4532The implementation guarantees that the result always fits into an <code>int</code>.
4533</p>
4534
Rob Pikeff70f092009-02-20 13:36:14 -08004535<pre class="grammar">
Robert Griesemer54731032011-05-12 09:15:59 -07004536Call Argument type Result
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -07004537
Robert Griesemer54731032011-05-12 09:15:59 -07004538len(s) string type string length in bytes
4539 [n]T, *[n]T array length (== n)
4540 []T slice length
4541 map[K]T map length (number of defined keys)
4542 chan T number of elements queued in channel buffer
Robert Griesemer4dc25282008-09-09 10:37:19 -07004543
Robert Griesemer54731032011-05-12 09:15:59 -07004544cap(s) [n]T, *[n]T array length (== n)
4545 []T slice capacity
4546 chan T channel buffer capacity
Robert Griesemerc2d55862009-02-19 16:49:10 -08004547</pre>
Robert Griesemer4dc25282008-09-09 10:37:19 -07004548
Robert Griesemerc2d55862009-02-19 16:49:10 -08004549<p>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004550The capacity of a slice is the number of elements for which there is
4551space allocated in the underlying array.
4552At any time the following relationship holds:
4553</p>
Robert Griesemer633957b2009-01-06 13:23:20 -08004554
Robert Griesemerc2d55862009-02-19 16:49:10 -08004555<pre>
Anthony Martin0122a662011-02-08 14:51:15 -080045560 &lt;= len(s) &lt;= cap(s)
Robert Griesemerc2d55862009-02-19 16:49:10 -08004557</pre>
Robert Griesemer667ef6c2008-09-10 13:00:32 -07004558
Russ Coxf4429182010-07-01 17:49:47 -07004559<p>
Robert Griesemer0c2e6b32010-07-13 11:54:57 -07004560The length and capacity of a <code>nil</code> slice, map, or channel are 0.
4561</p>
4562
4563<p>
Robert Griesemer54731032011-05-12 09:15:59 -07004564The expression <code>len(s)</code> is <a href="#Constants">constant</a> if
4565<code>s</code> is a string constant. The expressions <code>len(s)</code> and
4566<code>cap(s)</code> are constants if the type of <code>s</code> is an array
4567or pointer to an array and the expression <code>s</code> does not contain
4568<a href="#Receive_operator">channel receives</a> or
4569<a href="#Calls">function calls</a>; in this case <code>s</code> is not evaluated.
4570Otherwise, invocations of <code>len</code> and <code>cap</code> are not
4571constant and <code>s</code> is evaluated.
Russ Coxf4429182010-07-01 17:49:47 -07004572</p>
Robert Griesemer4dc25282008-09-09 10:37:19 -07004573
Robert Griesemer54731032011-05-12 09:15:59 -07004574
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004575<h3 id="Allocation">Allocation</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004576
Rob Pike96750f12009-02-27 16:47:48 -08004577<p>
4578The built-in function <code>new</code> takes a type <code>T</code> and
4579returns a value of type <code>*T</code>.
Robert Griesemera3294712009-01-05 11:17:26 -08004580The memory is initialized as described in the section on initial values
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004581<a href="#The_zero_value">The zero value</a>).
Rob Pike96750f12009-02-27 16:47:48 -08004582</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004583
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08004584<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08004585new(T)
4586</pre>
Robert Griesemer4dc25282008-09-09 10:37:19 -07004587
Rob Pike96750f12009-02-27 16:47:48 -08004588<p>
Robert Griesemer4dc25282008-09-09 10:37:19 -07004589For instance
Rob Pike96750f12009-02-27 16:47:48 -08004590</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004591
Robert Griesemerc2d55862009-02-19 16:49:10 -08004592<pre>
Robert Griesemerb94c0d22011-01-19 23:07:21 -05004593type S struct { a int; b float64 }
Robert Griesemerc2d55862009-02-19 16:49:10 -08004594new(S)
4595</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004596
Rob Pike96750f12009-02-27 16:47:48 -08004597<p>
4598dynamically allocates memory for a variable of type <code>S</code>,
4599initializes it (<code>a=0</code>, <code>b=0.0</code>),
4600and returns a value of type <code>*S</code> containing the address
4601of the memory.
4602</p>
4603
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004604<h3 id="Making_slices_maps_and_channels">Making slices, maps and channels</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004605
Robert Griesemerc2d55862009-02-19 16:49:10 -08004606<p>
Rob Pike96750f12009-02-27 16:47:48 -08004607Slices, maps and channels are reference types that do not require the
4608extra indirection of an allocation with <code>new</code>.
4609The built-in function <code>make</code> takes a type <code>T</code>,
4610which must be a slice, map or channel type,
4611optionally followed by a type-specific list of expressions.
4612It returns a value of type <code>T</code> (not <code>*T</code>).
Robert Griesemer633957b2009-01-06 13:23:20 -08004613The memory is initialized as described in the section on initial values
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004614<a href="#The_zero_value">The zero value</a>).
Rob Pike96750f12009-02-27 16:47:48 -08004615</p>
Robert Griesemer633957b2009-01-06 13:23:20 -08004616
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08004617<pre class="grammar">
Robert Griesemerdf674ff2010-05-04 17:31:40 -07004618Call Type T Result
4619
4620make(T, n) slice slice of type T with length n and capacity n
4621make(T, n, m) slice slice of type T with length n and capacity m
4622
4623make(T) map map of type T
4624make(T, n) map map of type T with initial space for n elements
4625
4626make(T) channel synchronous channel of type T
4627make(T, n) channel asynchronous channel of type T, buffer size n
Robert Griesemerc2d55862009-02-19 16:49:10 -08004628</pre>
Robert Griesemer633957b2009-01-06 13:23:20 -08004629
Robert Griesemer633957b2009-01-06 13:23:20 -08004630
Rob Pike96750f12009-02-27 16:47:48 -08004631<p>
Robert Griesemerdf674ff2010-05-04 17:31:40 -07004632The arguments <code>n</code> and <code>m</code> must be of integer type.
4633A <a href="#Run_time_panics">run-time panic</a> occurs if <code>n</code>
4634is negative or larger than <code>m</code>, or if <code>n</code> or
4635<code>m</code> cannot be represented by an <code>int</code>.
Rob Pike96750f12009-02-27 16:47:48 -08004636</p>
Robert Griesemer633957b2009-01-06 13:23:20 -08004637
Robert Griesemerc2d55862009-02-19 16:49:10 -08004638<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08004639s := make([]int, 10, 100) // slice with len(s) == 10, cap(s) == 100
4640s := make([]int, 10) // slice with len(s) == cap(s) == 10
4641c := make(chan int, 10) // channel with a buffer size of 10
4642m := make(map[string] int, 100) // map with initial space for 100 elements
Robert Griesemerc2d55862009-02-19 16:49:10 -08004643</pre>
Robert Griesemer633957b2009-01-06 13:23:20 -08004644
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004645
Robert Griesemer07e983a2010-10-25 16:50:31 -07004646<h3 id="Appending_and_copying_slices">Appending to and copying slices</h3>
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08004647
4648<p>
Robert Griesemer07e983a2010-10-25 16:50:31 -07004649Two built-in functions assist in common slice operations.
4650</p>
4651
4652<p>
Robert Griesemer95b81372011-06-12 12:09:50 -07004653The <a href="#Function_types">variadic</a> function <code>append</code>
4654appends zero or more values <code>x</code>
Robert Griesemer0f7acf12011-04-19 14:38:49 -07004655to <code>s</code> of type <code>S</code>, which must be a slice type, and
4656returns the resulting slice, also of type <code>S</code>.
Robert Griesemer95b81372011-06-12 12:09:50 -07004657The values <code>x</code> are passed to a parameter of type <code>...T</code>
4658where <code>T</code> is the <a href="#Slice_types">element type</a> of
4659<code>S</code> and the respective
4660<a href="#Passing_arguments_to_..._parameters">parameter passing rules</a> apply.
Robert Griesemer07e983a2010-10-25 16:50:31 -07004661</p>
4662
4663<pre class="grammar">
Robert Griesemer0f7acf12011-04-19 14:38:49 -07004664append(s S, x ...T) S // T is the element type of S
Robert Griesemer07e983a2010-10-25 16:50:31 -07004665</pre>
4666
4667<p>
4668If the capacity of <code>s</code> is not large enough to fit the additional
4669values, <code>append</code> allocates a new, sufficiently large slice that fits
4670both the existing slice elements and the additional values. Thus, the returned
4671slice may refer to a different underlying array.
4672</p>
4673
4674<pre>
4675s0 := []int{0, 0}
4676s1 := append(s0, 2) // append a single element s1 == []int{0, 0, 2}
4677s2 := append(s1, 3, 5, 7) // append multiple elements s2 == []int{0, 0, 2, 3, 5, 7}
4678s3 := append(s2, s0...) // append a slice s3 == []int{0, 0, 2, 3, 5, 7, 0, 0}
Robert Griesemer0f7acf12011-04-19 14:38:49 -07004679
4680var t []interface{}
4681t = append(t, 42, 3.1415, "foo") t == []interface{}{42, 3.1415, "foo"}
Robert Griesemer07e983a2010-10-25 16:50:31 -07004682</pre>
4683
4684<p>
4685The function <code>copy</code> copies slice elements from
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08004686a source <code>src</code> to a destination <code>dst</code> and returns the
4687number of elements copied. Source and destination may overlap.
Robert Griesemer7bc03712010-06-07 15:49:39 -07004688Both arguments must have <a href="#Type_identity">identical</a> element type <code>T</code> and must be
Robert Griesemer425bbad2010-10-25 16:41:06 -07004689<a href="#Assignability">assignable</a> to a slice of type <code>[]T</code>.
Nigel Tao703b0922011-05-15 16:04:37 -07004690The number of elements copied is the minimum of
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08004691<code>len(src)</code> and <code>len(dst)</code>.
Robert Griesemer425bbad2010-10-25 16:41:06 -07004692As a special case, <code>copy</code> also accepts a destination argument assignable
4693to type <code>[]byte</code> with a source argument of a string type.
4694This form copies the bytes from the string into the byte slice.
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08004695</p>
4696
4697<pre class="grammar">
4698copy(dst, src []T) int
Robert Griesemer425bbad2010-10-25 16:41:06 -07004699copy(dst []byte, src string) int
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08004700</pre>
4701
4702<p>
4703Examples:
4704</p>
4705
4706<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08004707var a = [...]int{0, 1, 2, 3, 4, 5, 6, 7}
4708var s = make([]int, 6)
Robert Griesemer425bbad2010-10-25 16:41:06 -07004709var b = make([]byte, 5)
4710n1 := copy(s, a[0:]) // n1 == 6, s == []int{0, 1, 2, 3, 4, 5}
4711n2 := copy(s, s[2:]) // n2 == 4, s == []int{2, 3, 4, 5, 4, 5}
4712n3 := copy(b, "Hello, World!") // n3 == 5, b == []byte("Hello")
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08004713</pre>
4714
Rob Pike72970872010-03-04 12:35:16 -08004715<h3 id="Complex_numbers">Assembling and disassembling complex numbers</h3>
4716
4717<p>
4718Three functions assemble and disassemble complex numbers.
Robert Griesemerb94c0d22011-01-19 23:07:21 -05004719The built-in function <code>complex</code> constructs a complex
Rob Pike72970872010-03-04 12:35:16 -08004720value from a floating-point real and imaginary part, while
4721<code>real</code> and <code>imag</code>
4722extract the real and imaginary parts of a complex value.
4723</p>
4724
4725<pre class="grammar">
Robert Griesemerb94c0d22011-01-19 23:07:21 -05004726complex(realPart, imaginaryPart floatT) complexT
Rob Pike72970872010-03-04 12:35:16 -08004727real(complexT) floatT
4728imag(complexT) floatT
4729</pre>
4730
4731<p>
4732The type of the arguments and return value correspond.
Robert Griesemerb94c0d22011-01-19 23:07:21 -05004733For <code>complex</code>, the two arguments must be of the same
Rob Pike72970872010-03-04 12:35:16 -08004734floating-point type and the return type is the complex type
4735with the corresponding floating-point constituents:
Rob Pike72970872010-03-04 12:35:16 -08004736<code>complex64</code> for <code>float32</code>,
4737<code>complex128</code> for <code>float64</code>.
4738The <code>real</code> and <code>imag</code> functions
4739together form the inverse, so for a complex value <code>z</code>,
Robert Griesemerb94c0d22011-01-19 23:07:21 -05004740<code>z</code> <code>==</code> <code>complex(real(z),</code> <code>imag(z))</code>.
Rob Pike72970872010-03-04 12:35:16 -08004741</p>
4742
4743<p>
4744If the operands of these functions are all constants, the return
4745value is a constant.
4746</p>
4747
4748<pre>
Robert Griesemerb94c0d22011-01-19 23:07:21 -05004749var a = complex(2, -2) // complex128
4750var b = complex(1.0, -1.4) // complex128
4751x := float32(math.Cos(math.Pi/2)) // float32
4752var c64 = complex(5, -x) // complex64
4753var im = imag(b) // float64
4754var rl = real(c64) // float32
Rob Pike72970872010-03-04 12:35:16 -08004755</pre>
4756
Rob Pike5bb29fb2010-03-25 17:59:59 -07004757<h3 id="Handling_panics">Handling panics</h3>
4758
4759<p> Two built-in functions, <code>panic</code> and <code>recover</code>,
4760assist in reporting and handling <a href="#Run_time_panics">run-time panics</a>
4761and program-defined error conditions.
4762</p>
4763
4764<pre class="grammar">
4765func panic(interface{})
4766func recover() interface{}
4767</pre>
4768
4769<p>
Rob Pike5bb29fb2010-03-25 17:59:59 -07004770When a function <code>F</code> calls <code>panic</code>, normal
4771execution of <code>F</code> stops immediately. Any functions whose
4772execution was <a href="#Defer_statements">deferred</a> by the
4773invocation of <code>F</code> are run in the usual way, and then
4774<code>F</code> returns to its caller. To the caller, <code>F</code>
4775then behaves like a call to <code>panic</code>, terminating its own
4776execution and running deferred functions. This continues until all
4777functions in the goroutine have ceased execution, in reverse order.
4778At that point, the program is
4779terminated and the error condition is reported, including the value of
4780the argument to <code>panic</code>. This termination sequence is
4781called <i>panicking</i>.
4782</p>
4783
Robert Griesemer76f32282011-02-04 08:43:21 -08004784<pre>
4785panic(42)
4786panic("unreachable")
4787panic(Error("cannot parse"))
4788</pre>
4789
Rob Pike5bb29fb2010-03-25 17:59:59 -07004790<p>
4791The <code>recover</code> function allows a program to manage behavior
4792of a panicking goroutine. Executing a <code>recover</code> call
Robert Griesemer76f32282011-02-04 08:43:21 -08004793<i>inside</i> a deferred function (but not any function called by it) stops
Rob Pike5bb29fb2010-03-25 17:59:59 -07004794the panicking sequence by restoring normal execution, and retrieves
4795the error value passed to the call of <code>panic</code>. If
4796<code>recover</code> is called outside the deferred function it will
Robert Griesemer76f32282011-02-04 08:43:21 -08004797not stop a panicking sequence. In this case, or when the goroutine
4798is not panicking, or if the argument supplied to <code>panic</code>
4799was <code>nil</code>, <code>recover</code> returns <code>nil</code>.
Rob Pike5bb29fb2010-03-25 17:59:59 -07004800</p>
4801
4802<p>
Robert Griesemer76f32282011-02-04 08:43:21 -08004803The <code>protect</code> function in the example below invokes
4804the function argument <code>g</code> and protects callers from
4805run-time panics raised by <code>g</code>.
Rob Pike5bb29fb2010-03-25 17:59:59 -07004806</p>
4807
4808<pre>
Robert Griesemer76f32282011-02-04 08:43:21 -08004809func protect(g func()) {
Rob Pike5bb29fb2010-03-25 17:59:59 -07004810 defer func() {
Robert Griesemer76f32282011-02-04 08:43:21 -08004811 log.Println("done") // Println executes normally even in there is a panic
Rob Pike5bb29fb2010-03-25 17:59:59 -07004812 if x := recover(); x != nil {
Rob Pike966bf712011-03-01 13:54:22 -08004813 log.Printf("run time panic: %v", x)
Rob Pike5bb29fb2010-03-25 17:59:59 -07004814 }
Rob Pikee6b1d422011-04-05 11:01:25 -07004815 }()
Robert Griesemer76f32282011-02-04 08:43:21 -08004816 log.Println("start")
4817 g()
Rob Pike5bb29fb2010-03-25 17:59:59 -07004818}
4819</pre>
4820
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08004821
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004822<h3 id="Bootstrapping">Bootstrapping</h3>
4823
Robert Griesemer5eb36242009-09-16 11:05:14 -07004824<p>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004825Current implementations provide several built-in functions useful during
4826bootstrapping. These functions are documented for completeness but are not
4827guaranteed to stay in the language. They do not return a result.
Robert Griesemer5eb36242009-09-16 11:05:14 -07004828</p>
4829
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004830<pre class="grammar">
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07004831Function Behavior
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004832
4833print prints all arguments; formatting of arguments is implementation-specific
4834println like print but prints spaces between arguments and a newline at the end
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004835</pre>
4836
4837
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004838<h2 id="Packages">Packages</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004839
Rob Pike46596852009-03-02 16:17:29 -08004840<p>
4841Go programs are constructed by linking together <i>packages</i>.
Robert Griesemer326ef132009-09-28 19:21:15 -07004842A package in turn is constructed from one or more source files
4843that together declare constants, types, variables and functions
4844belonging to the package and which are accessible in all files
4845of the same package. Those elements may be
4846<a href="#Exported_identifiers">exported</a> and used in another package.
Rob Pike46596852009-03-02 16:17:29 -08004847</p>
4848
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004849<h3 id="Source_file_organization">Source file organization</h3>
Rob Pike46596852009-03-02 16:17:29 -08004850
4851<p>
4852Each source file consists of a package clause defining the package
4853to which it belongs, followed by a possibly empty set of import
4854declarations that declare packages whose contents it wishes to use,
4855followed by a possibly empty set of declarations of functions,
Robert Griesemer0a162a12009-08-19 16:44:04 -07004856types, variables, and constants.
Rob Pike46596852009-03-02 16:17:29 -08004857</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004858
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004859<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -08004860SourceFile = PackageClause ";" { ImportDecl ";" } { TopLevelDecl ";" } .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004861</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004862
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004863<h3 id="Package_clause">Package clause</h3>
Rob Pike46596852009-03-02 16:17:29 -08004864
Robert Griesemerc2d55862009-02-19 16:49:10 -08004865<p>
Rob Pike46596852009-03-02 16:17:29 -08004866A package clause begins each source file and defines the package
4867to which the file belongs.
4868</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004869
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004870<pre class="ebnf">
Robert Griesemer4e56b332009-09-10 10:14:00 -07004871PackageClause = "package" PackageName .
4872PackageName = identifier .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004873</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004874
Robert Griesemer4e56b332009-09-10 10:14:00 -07004875<p>
4876The PackageName must not be the <a href="#Blank_identifier">blank identifier</a>.
4877</p>
4878
Rob Pike46596852009-03-02 16:17:29 -08004879<pre>
4880package math
4881</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004882
Rob Pike46596852009-03-02 16:17:29 -08004883<p>
4884A set of files sharing the same PackageName form the implementation of a package.
4885An implementation may require that all source files for a package inhabit the same directory.
4886</p>
4887
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004888<h3 id="Import_declarations">Import declarations</h3>
Rob Pike46596852009-03-02 16:17:29 -08004889
4890<p>
Rob Pike3aec2e42009-09-25 17:00:22 -07004891An import declaration states that the source file containing the
4892declaration uses identifiers
4893<a href="#Exported_identifiers">exported</a> by the <i>imported</i>
4894package and enables access to them. The import names an
4895identifier (PackageName) to be used for access and an ImportPath
4896that specifies the package to be imported.
Rob Pike46596852009-03-02 16:17:29 -08004897</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004898
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004899<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -08004900ImportDecl = "import" ( ImportSpec | "(" { ImportSpec ";" } ")" ) .
Robert Griesemer997851e2009-09-25 15:36:25 -07004901ImportSpec = [ "." | PackageName ] ImportPath .
Robert Griesemer130ac742009-12-10 16:43:01 -08004902ImportPath = string_lit .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004903</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004904
Robert Griesemerc2d55862009-02-19 16:49:10 -08004905<p>
Rob Pike3aec2e42009-09-25 17:00:22 -07004906The PackageName is used in <a href="#Qualified_identifiers">qualified identifiers</a>
4907to access the exported identifiers of the package within the importing source file.
4908It is declared in the <a href="#Blocks">file block</a>.
4909If the PackageName is omitted, it defaults to the identifier specified in the
Robert Griesemer556506e2011-02-22 09:34:13 -08004910<a href="#Package_clause">package clause</a> of the imported package.
Rob Pike3aec2e42009-09-25 17:00:22 -07004911If an explicit period (<code>.</code>) appears instead of a name, all the
4912package's exported identifiers will be declared in the current file's
4913file block and can be accessed without a qualifier.
Rob Pike46596852009-03-02 16:17:29 -08004914</p>
Russ Cox16b95ba2009-08-20 10:22:52 -07004915
Robert Griesemerc2d55862009-02-19 16:49:10 -08004916<p>
Rob Pike3aec2e42009-09-25 17:00:22 -07004917The interpretation of the ImportPath is implementation-dependent but
4918it is typically a substring of the full file name of the compiled
4919package and may be relative to a repository of installed packages.
4920</p>
4921
4922<p>
4923Assume we have compiled a package containing the package clause
4924<code>package math</code>, which exports function <code>Sin</code>, and
4925installed the compiled package in the file identified by
Rob Pike46596852009-03-02 16:17:29 -08004926<code>"lib/math"</code>.
Rob Pike3aec2e42009-09-25 17:00:22 -07004927This table illustrates how <code>Sin</code> may be accessed in files
4928that import the package after the
4929various types of import declaration.
Rob Pike46596852009-03-02 16:17:29 -08004930</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004931
Rob Pike46596852009-03-02 16:17:29 -08004932<pre class="grammar">
Robert Griesemer997851e2009-09-25 15:36:25 -07004933Import declaration Local name of Sin
Rob Pike46596852009-03-02 16:17:29 -08004934
Rob Pike46596852009-03-02 16:17:29 -08004935import "lib/math" math.Sin
Robert Griesemer997851e2009-09-25 15:36:25 -07004936import M "lib/math" M.Sin
Rob Pike46596852009-03-02 16:17:29 -08004937import . "lib/math" Sin
4938</pre>
4939
Robert Griesemer997851e2009-09-25 15:36:25 -07004940<p>
Rob Pike3aec2e42009-09-25 17:00:22 -07004941An import declaration declares a dependency relation between
4942the importing and imported package.
Robert Griesemer997851e2009-09-25 15:36:25 -07004943It is illegal for a package to import itself or to import a package without
4944referring to any of its exported identifiers. To import a package solely for
4945its side-effects (initialization), use the <a href="#Blank_identifier">blank</a>
4946identifier as explicit package name:
4947</p>
4948
4949<pre>
4950import _ "lib/math"
4951</pre>
4952
4953
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004954<h3 id="An_example_package">An example package</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004955
Robert Griesemerc2d55862009-02-19 16:49:10 -08004956<p>
Rob Pike46596852009-03-02 16:17:29 -08004957Here is a complete Go package that implements a concurrent prime sieve.
4958</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004959
Robert Griesemerc2d55862009-02-19 16:49:10 -08004960<pre>
4961package main
4962
Rob Pike46596852009-03-02 16:17:29 -08004963import "fmt"
4964
Robert Griesemer3c7271f2011-05-24 14:18:44 -07004965// Send the sequence 2, 3, 4, … to channel 'ch'.
Robert Griesemere1e76192009-09-25 14:11:03 -07004966func generate(ch chan&lt;- int) {
Robert Griesemerc2d55862009-02-19 16:49:10 -08004967 for i := 2; ; i++ {
Robert Griesemer130ac742009-12-10 16:43:01 -08004968 ch &lt;- i // Send 'i' to channel 'ch'.
Robert Griesemerc2d55862009-02-19 16:49:10 -08004969 }
4970}
4971
Fazlul Shahriar330139e2009-11-30 21:23:58 -08004972// Copy the values from channel 'src' to channel 'dst',
Robert Griesemerc2d55862009-02-19 16:49:10 -08004973// removing those divisible by 'prime'.
Robert Griesemere1e76192009-09-25 14:11:03 -07004974func filter(src &lt;-chan int, dst chan&lt;- int, prime int) {
4975 for i := range src { // Loop over values received from 'src'.
4976 if i%prime != 0 {
Robert Griesemer130ac742009-12-10 16:43:01 -08004977 dst &lt;- i // Send 'i' to channel 'dst'.
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004978 }
4979 }
Robert Griesemerc2d55862009-02-19 16:49:10 -08004980}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004981
Robert Griesemerc2d55862009-02-19 16:49:10 -08004982// The prime sieve: Daisy-chain filter processes together.
4983func sieve() {
Robert Griesemer130ac742009-12-10 16:43:01 -08004984 ch := make(chan int) // Create a new channel.
4985 go generate(ch) // Start generate() as a subprocess.
Robert Griesemerc2d55862009-02-19 16:49:10 -08004986 for {
Robert Griesemer130ac742009-12-10 16:43:01 -08004987 prime := &lt;-ch
4988 fmt.Print(prime, "\n")
4989 ch1 := make(chan int)
4990 go filter(ch, ch1, prime)
4991 ch = ch1
Robert Griesemerc2d55862009-02-19 16:49:10 -08004992 }
4993}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004994
Robert Griesemerc2d55862009-02-19 16:49:10 -08004995func main() {
Robert Griesemer130ac742009-12-10 16:43:01 -08004996 sieve()
Robert Griesemerc2d55862009-02-19 16:49:10 -08004997}
4998</pre>
Robert Griesemer67153582008-12-16 14:45:09 -08004999
Russ Cox7c4f7cc2009-08-20 11:11:03 -07005000<h2 id="Program_initialization_and_execution">Program initialization and execution</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005001
Russ Cox7c4f7cc2009-08-20 11:11:03 -07005002<h3 id="The_zero_value">The zero value</h3>
Rob Pike8f2330d2009-02-25 16:20:44 -08005003<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005004When memory is allocated to store a value, either through a declaration
Rob Pike46f482a2011-05-25 06:44:09 +10005005or a call of <code>make</code> or <code>new</code>,
Rob Pike678625d2009-09-15 09:54:22 -07005006and no explicit initialization is provided, the memory is
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005007given a default initialization. Each element of such a value is
Robert Griesemeref45e642009-08-21 11:25:00 -07005008set to the <i>zero value</i> for its type: <code>false</code> for booleans,
Rob Pike8f2330d2009-02-25 16:20:44 -08005009<code>0</code> for integers, <code>0.0</code> for floats, <code>""</code>
Robert Griesemer19b1d352009-09-24 19:36:48 -07005010for strings, and <code>nil</code> for pointers, functions, interfaces, slices, channels, and maps.
Rob Pikeff70f092009-02-20 13:36:14 -08005011This initialization is done recursively, so for instance each element of an
Rob Pike8f2330d2009-02-25 16:20:44 -08005012array of structs will have its fields zeroed if no value is specified.
5013</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08005014<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005015These two simple declarations are equivalent:
Rob Pike8f2330d2009-02-25 16:20:44 -08005016</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005017
Robert Griesemerc2d55862009-02-19 16:49:10 -08005018<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08005019var i int
5020var i int = 0
Robert Griesemerc2d55862009-02-19 16:49:10 -08005021</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005022
Rob Pike8f2330d2009-02-25 16:20:44 -08005023<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005024After
Rob Pike8f2330d2009-02-25 16:20:44 -08005025</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005026
Robert Griesemerc2d55862009-02-19 16:49:10 -08005027<pre>
Robert Griesemerb94c0d22011-01-19 23:07:21 -05005028type T struct { i int; f float64; next *T }
Robert Griesemer130ac742009-12-10 16:43:01 -08005029t := new(T)
Robert Griesemerc2d55862009-02-19 16:49:10 -08005030</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005031
Rob Pike8f2330d2009-02-25 16:20:44 -08005032<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005033the following holds:
Rob Pike8f2330d2009-02-25 16:20:44 -08005034</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005035
Robert Griesemerc2d55862009-02-19 16:49:10 -08005036<pre>
5037t.i == 0
5038t.f == 0.0
5039t.next == nil
5040</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005041
Rob Pike96750f12009-02-27 16:47:48 -08005042<p>
5043The same would also be true after
5044</p>
5045
5046<pre>
5047var t T
5048</pre>
5049
Russ Cox7c4f7cc2009-08-20 11:11:03 -07005050<h3 id="Program_execution">Program execution</h3>
Rob Pike8f2330d2009-02-25 16:20:44 -08005051<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005052A package with no imports is initialized by assigning initial values to
Rob Pike8cb91842009-09-15 11:56:39 -07005053all its package-level variables
Rob Pike678625d2009-09-15 09:54:22 -07005054and then calling any
Rob Pike96750f12009-02-27 16:47:48 -08005055package-level function with the name and signature of
5056</p>
5057<pre>
5058func init()
5059</pre>
5060<p>
Rob Pike4fe41922009-11-07 22:00:59 -08005061defined in its source.
5062A package may contain multiple
Rob Pike46f482a2011-05-25 06:44:09 +10005063<code>init</code> functions, even
Rob Pike4fe41922009-11-07 22:00:59 -08005064within a single source file; they execute
5065in unspecified order.
Rob Pike8f2330d2009-02-25 16:20:44 -08005066</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08005067<p>
Rob Pike8cb91842009-09-15 11:56:39 -07005068Within a package, package-level variables are initialized,
5069and constant values are determined, in
5070data-dependent order: if the initializer of <code>A</code>
5071depends on the value of <code>B</code>, <code>A</code>
5072will be set after <code>B</code>.
5073It is an error if such dependencies form a cycle.
5074Dependency analysis is done lexically: <code>A</code>
5075depends on <code>B</code> if the value of <code>A</code>
5076contains a mention of <code>B</code>, contains a value
5077whose initializer
5078mentions <code>B</code>, or mentions a function that
5079mentions <code>B</code>, recursively.
5080If two items are not interdependent, they will be initialized
5081in the order they appear in the source.
Rob Pike01cadde2009-09-15 15:56:44 -07005082Since the dependency analysis is done per package, it can produce
5083unspecified results if <code>A</code>'s initializer calls a function defined
Rob Pike8cb91842009-09-15 11:56:39 -07005084in another package that refers to <code>B</code>.
5085</p>
5086<p>
Robert Griesemer566e3b22008-09-26 16:41:50 -07005087Initialization code may contain "go" statements, but the functions
Robert Griesemerd8a764c2009-02-06 17:01:10 -08005088they invoke do not begin execution until initialization of the entire
5089program is complete. Therefore, all initialization code is run in a single
Rob Pike96750f12009-02-27 16:47:48 -08005090goroutine.
Rob Pike8f2330d2009-02-25 16:20:44 -08005091</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08005092<p>
Rob Pike46f482a2011-05-25 06:44:09 +10005093An <code>init</code> function cannot be referred to from anywhere
5094in a program. In particular, <code>init</code> cannot be called explicitly,
Rob Pike96750f12009-02-27 16:47:48 -08005095nor can a pointer to <code>init</code> be assigned to a function variable.
Rob Pike8f2330d2009-02-25 16:20:44 -08005096</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08005097<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005098If a package has imports, the imported packages are initialized
Robert Griesemer566e3b22008-09-26 16:41:50 -07005099before initializing the package itself. If multiple packages import
Rob Pike96750f12009-02-27 16:47:48 -08005100a package <code>P</code>, <code>P</code> will be initialized only once.
Rob Pike8f2330d2009-02-25 16:20:44 -08005101</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08005102<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07005103The importing of packages, by construction, guarantees that there can
5104be no cyclic dependencies in initialization.
Rob Pike8f2330d2009-02-25 16:20:44 -08005105</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08005106<p>
Russ Coxa6736ca2011-02-03 13:40:51 -05005107A complete program is created by linking a single, unimported package
5108called the <i>main package</i> with all the packages it imports, transitively.
5109The main package must
5110have package name <code>main</code> and
5111declare a function <code>main</code> that takes no
5112arguments and returns no value.
Rob Pike8f2330d2009-02-25 16:20:44 -08005113</p>
Robert Griesemer4dc25282008-09-09 10:37:19 -07005114
Robert Griesemerc2d55862009-02-19 16:49:10 -08005115<pre>
Robert Griesemer3c7271f2011-05-24 14:18:44 -07005116func main() { … }
Robert Griesemerc2d55862009-02-19 16:49:10 -08005117</pre>
Robert Griesemer4dc25282008-09-09 10:37:19 -07005118
Rob Pike8f2330d2009-02-25 16:20:44 -08005119<p>
Russ Coxa6736ca2011-02-03 13:40:51 -05005120Program execution begins by initializing the main package and then
5121invoking the function <code>main</code>.
Rob Pike8f2330d2009-02-25 16:20:44 -08005122</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08005123<p>
Russ Coxa6736ca2011-02-03 13:40:51 -05005124When the function <code>main</code> returns, the program exits.
5125It does not wait for other (non-<code>main</code>) goroutines to complete.
Rob Pike811dd252009-03-04 20:39:39 -08005126</p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08005127
Evan Shaw21110c72010-04-22 10:14:53 -07005128<h2 id="Run_time_panics">Run-time panics</h2>
Rob Pike5bb29fb2010-03-25 17:59:59 -07005129
5130<p>
5131Execution errors such as attempting to index an array out
5132of bounds trigger a <i>run-time panic</i> equivalent to a call of
5133the built-in function <a href="#Handling_panics"><code>panic</code></a>
5134with a value of the implementation-defined interface type <code>runtime.Error</code>.
5135That type defines at least the method
5136<code>String() string</code>. The exact error values that
5137represent distinct run-time error conditions are unspecified,
5138at least for now.
5139</p>
5140
5141<pre>
5142package runtime
5143
5144type Error interface {
5145 String() string
5146 // and perhaps others
5147}
5148</pre>
5149
Russ Cox7c4f7cc2009-08-20 11:11:03 -07005150<h2 id="System_considerations">System considerations</h2>
Robert Griesemer52c02c22009-02-11 13:46:30 -08005151
Russ Cox7c4f7cc2009-08-20 11:11:03 -07005152<h3 id="Package_unsafe">Package <code>unsafe</code></h3>
Robert Griesemer52c02c22009-02-11 13:46:30 -08005153
Robert Griesemerc2d55862009-02-19 16:49:10 -08005154<p>
Rob Pike96750f12009-02-27 16:47:48 -08005155The built-in package <code>unsafe</code>, known to the compiler,
5156provides facilities for low-level programming including operations
5157that violate the type system. A package using <code>unsafe</code>
5158must be vetted manually for type safety. The package provides the
5159following interface:
Rob Pikef27e9f02009-02-23 19:22:05 -08005160</p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08005161
Rob Pikeff70f092009-02-20 13:36:14 -08005162<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08005163package unsafe
Robert Griesemer52c02c22009-02-11 13:46:30 -08005164
Robert Griesemer98b4f6a2009-05-12 21:37:46 -07005165type ArbitraryType int // shorthand for an arbitrary Go type; it is not a real type
5166type Pointer *ArbitraryType
Robert Griesemer52c02c22009-02-11 13:46:30 -08005167
Robert Griesemereee70b02011-06-13 16:46:42 -07005168func Alignof(variable ArbitraryType) uintptr
5169func Offsetof(selector ArbitraryType) uinptr
5170func Sizeof(variable ArbitraryType) uintptr
Rob Pike678625d2009-09-15 09:54:22 -07005171
Gustavo Niemeyerf8404ee2011-02-04 09:29:08 -08005172func Reflect(val interface{}) (typ runtime.Type, addr uintptr)
5173func Typeof(val interface{}) (typ interface{})
Rob Pike678625d2009-09-15 09:54:22 -07005174func Unreflect(typ runtime.Type, addr uintptr) interface{}
Robert Griesemerc2d55862009-02-19 16:49:10 -08005175</pre>
Robert Griesemer52c02c22009-02-11 13:46:30 -08005176
Robert Griesemerc2d55862009-02-19 16:49:10 -08005177<p>
Rob Pikef27e9f02009-02-23 19:22:05 -08005178Any pointer or value of type <code>uintptr</code> can be converted into
5179a <code>Pointer</code> and vice versa.
5180</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08005181<p>
Rob Pikef27e9f02009-02-23 19:22:05 -08005182The function <code>Sizeof</code> takes an expression denoting a
Robert Griesemer4023dce2009-08-14 17:41:52 -07005183variable of any type and returns the size of the variable in bytes.
Rob Pikef27e9f02009-02-23 19:22:05 -08005184</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08005185<p>
Russ Cox7c4f7cc2009-08-20 11:11:03 -07005186The function <code>Offsetof</code> takes a selector (§<a href="#Selectors">Selectors</a>) denoting a struct
Robert Griesemer52c02c22009-02-11 13:46:30 -08005187field of any type and returns the field offset in bytes relative to the
Rob Pike678625d2009-09-15 09:54:22 -07005188struct's address.
5189For a struct <code>s</code> with field <code>f</code>:
Rob Pikef27e9f02009-02-23 19:22:05 -08005190</p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08005191
Robert Griesemerc2d55862009-02-19 16:49:10 -08005192<pre>
Robert Griesemereee70b02011-06-13 16:46:42 -07005193uintptr(unsafe.Pointer(&amp;s)) + unsafe.Offsetof(s.f) == uintptr(unsafe.Pointer(&amp;s.f))
Robert Griesemerc2d55862009-02-19 16:49:10 -08005194</pre>
Robert Griesemer52c02c22009-02-11 13:46:30 -08005195
Robert Griesemerc2d55862009-02-19 16:49:10 -08005196<p>
Rob Pikef27e9f02009-02-23 19:22:05 -08005197Computer architectures may require memory addresses to be <i>aligned</i>;
5198that is, for addresses of a variable to be a multiple of a factor,
5199the variable's type's <i>alignment</i>. The function <code>Alignof</code>
5200takes an expression denoting a variable of any type and returns the
5201alignment of the (type of the) variable in bytes. For a variable
5202<code>x</code>:
5203</p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08005204
Robert Griesemerc2d55862009-02-19 16:49:10 -08005205<pre>
Robert Griesemereee70b02011-06-13 16:46:42 -07005206uintptr(unsafe.Pointer(&amp;x)) % unsafe.Alignof(x) == 0
Robert Griesemerc2d55862009-02-19 16:49:10 -08005207</pre>
Robert Griesemer533dfd62009-05-13 16:56:00 -07005208
Rob Pikef27e9f02009-02-23 19:22:05 -08005209<p>
5210Calls to <code>Alignof</code>, <code>Offsetof</code>, and
Robert Griesemereee70b02011-06-13 16:46:42 -07005211<code>Sizeof</code> are compile-time constant expressions of type <code>uintptr</code>.
Rob Pikef27e9f02009-02-23 19:22:05 -08005212</p>
Robert Griesemer98b4f6a2009-05-12 21:37:46 -07005213<p>
Rob Pike678625d2009-09-15 09:54:22 -07005214The functions <code>unsafe.Typeof</code>,
5215<code>unsafe.Reflect</code>,
Russ Cox13dac652009-09-28 14:16:33 -07005216and <code>unsafe.Unreflect</code> allow access at run time to the dynamic
Rob Pike678625d2009-09-15 09:54:22 -07005217types and values stored in interfaces.
5218<code>Typeof</code> returns a representation of
5219<code>val</code>'s
5220dynamic type as a <code>runtime.Type</code>.
5221<code>Reflect</code> allocates a copy of
5222<code>val</code>'s dynamic
5223value and returns both the type and the address of the copy.
5224<code>Unreflect</code> inverts <code>Reflect</code>,
5225creating an
5226interface value from a type and address.
Russ Cox16205a32010-01-18 15:59:14 -08005227The <a href="/pkg/reflect/"><code>reflect</code> package</a> built on these primitives
Rob Pike678625d2009-09-15 09:54:22 -07005228provides a safe, more convenient way to inspect interface values.
Robert Griesemer98b4f6a2009-05-12 21:37:46 -07005229</p>
Robert Griesemer6f8df7a2009-02-11 21:57:15 -08005230
Robert Griesemer52c02c22009-02-11 13:46:30 -08005231
Russ Cox7c4f7cc2009-08-20 11:11:03 -07005232<h3 id="Size_and_alignment_guarantees">Size and alignment guarantees</h3>
Robert Griesemer52c02c22009-02-11 13:46:30 -08005233
Robert Griesemer997851e2009-09-25 15:36:25 -07005234<p>
Russ Cox7c4f7cc2009-08-20 11:11:03 -07005235For the numeric types (§<a href="#Numeric_types">Numeric types</a>), the following sizes are guaranteed:
Robert Griesemer997851e2009-09-25 15:36:25 -07005236</p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08005237
Rob Pikeff70f092009-02-20 13:36:14 -08005238<pre class="grammar">
Robert Griesemer777a96a2010-12-02 12:32:14 -08005239type size in bytes
Robert Griesemer52c02c22009-02-11 13:46:30 -08005240
Robert Griesemer777a96a2010-12-02 12:32:14 -08005241byte, uint8, int8 1
5242uint16, int16 2
5243uint32, int32, float32 4
5244uint64, int64, float64, complex64 8
5245complex128 16
Robert Griesemerc2d55862009-02-19 16:49:10 -08005246</pre>
Robert Griesemer52c02c22009-02-11 13:46:30 -08005247
Robert Griesemerc2d55862009-02-19 16:49:10 -08005248<p>
Rob Pikef27e9f02009-02-23 19:22:05 -08005249The following minimal alignment properties are guaranteed:
Rob Pike4501d342009-02-19 17:31:36 -08005250</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08005251<ol>
Robert Griesemerdd916be2011-01-10 14:25:17 -08005252<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 -07005253</li>
Robert Griesemer52c02c22009-02-11 13:46:30 -08005254
Rob Pikef27e9f02009-02-23 19:22:05 -08005255<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 -08005256 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 -07005257</li>
Robert Griesemer52c02c22009-02-11 13:46:30 -08005258
Rob Pikef27e9f02009-02-23 19:22:05 -08005259<li>For a variable <code>x</code> of array type: <code>unsafe.Alignof(x)</code> is the same as
5260 <code>unsafe.Alignof(x[0])</code>, but at least 1.
Robert Griesemer3af480372010-05-14 13:11:48 -07005261</li>
Robert Griesemerc2d55862009-02-19 16:49:10 -08005262</ol>
Robert Griesemer52c02c22009-02-11 13:46:30 -08005263
Robert Griesemereee70b02011-06-13 16:46:42 -07005264<span class="alert">
5265<h2 id="Implementation_differences">Implementation differences - TODO</h2>
Robert Griesemer237c8ab2009-09-01 14:07:30 -07005266<ul>
Robert Griesemereee70b02011-06-13 16:46:42 -07005267 <li><code>len(a)</code> is only a constant if <code>a</code> is a (qualified) identifier denoting an array or pointer to an array.</li>
5268 <li><code>nil</code> maps are not treated like empty maps.</li>
5269 <li>Trying to send/receive from a <code>nil</code> channel causes a run-time panic.</li>
5270 <li><code>unsafe.Alignof</code>, <code>unsafe.Offsetof</code>, and <code>unsafe.Sizeof</code> return an <code>int</code>.</li>
Robert Griesemer237c8ab2009-09-01 14:07:30 -07005271</ul>
Robert Griesemereee70b02011-06-13 16:46:42 -07005272</span>