blob: a95ed704a03bb407503b9bef9a1d38a49def188a [file] [log] [blame]
Robert Griesemere8e49872010-03-30 17:37:42 -07001<!-- title The Go Programming Language Specification -->
Robert Griesemer2a838d62011-02-08 13:31:01 -08002<!-- subtitle Version of February 8, 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
13[ ] may want to have some examples for the types of shift operations
Robert Griesemerb94c0d22011-01-19 23:07:21 -050014[ ] should string(1<<s) and float32(1<<s) be valid?
Robert Griesemer40d6bb52009-04-20 15:32:20 -070015[ ] should probably write something about evaluation order of statements even
16 though obvious
Robert Griesemere1b8cb82009-07-16 20:31:41 -070017[ ] review language on implicit dereferencing
Robert Griesemer1d282a82010-06-03 16:55:50 -070018[ ] clarify what it means for two functions to be "the same" when comparing them
Robert Griesemerc59d2f12008-09-09 10:48:14 -070019-->
20
Robert Griesemer40d6bb52009-04-20 15:32:20 -070021
Russ Cox7c4f7cc2009-08-20 11:11:03 -070022<h2 id="Introduction">Introduction</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -070023
Robert Griesemerc2d55862009-02-19 16:49:10 -080024<p>
Rob Pike4501d342009-02-19 17:31:36 -080025This is a reference manual for the Go programming language. For
Rob Pike9339e072009-11-02 15:28:41 -080026more information and other documents, see <a href="http://golang.org/">http://golang.org</a>.
Rob Pike4501d342009-02-19 17:31:36 -080027</p>
Robert Griesemer67153582008-12-16 14:45:09 -080028
Robert Griesemerc2d55862009-02-19 16:49:10 -080029<p>
Rob Pike4501d342009-02-19 17:31:36 -080030Go is a general-purpose language designed with systems programming
Rob Pike678625d2009-09-15 09:54:22 -070031in mind. It is strongly typed and garbage-collected and has explicit
Rob Pike4501d342009-02-19 17:31:36 -080032support for concurrent programming. Programs are constructed from
33<i>packages</i>, whose properties allow efficient management of
34dependencies. The existing implementations use a traditional
35compile/link model to generate executable binaries.
36</p>
37
Robert Griesemerc2d55862009-02-19 16:49:10 -080038<p>
Rob Pike4501d342009-02-19 17:31:36 -080039The grammar is compact and regular, allowing for easy analysis by
40automatic tools such as integrated development environments.
41</p>
Larry Hosken698c6c02009-09-17 08:05:12 -070042
Russ Cox7c4f7cc2009-08-20 11:11:03 -070043<h2 id="Notation">Notation</h2>
Rob Pike4501d342009-02-19 17:31:36 -080044<p>
Robert Griesemer4d230302008-12-17 15:39:15 -080045The syntax is specified using Extended Backus-Naur Form (EBNF):
Rob Pike4501d342009-02-19 17:31:36 -080046</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -070047
Rob Pikeff70f092009-02-20 13:36:14 -080048<pre class="grammar">
Robert Griesemer88a0c402009-04-23 14:42:21 -070049Production = production_name "=" Expression "." .
Rob Pike4501d342009-02-19 17:31:36 -080050Expression = Alternative { "|" Alternative } .
Robert Griesemerc2d55862009-02-19 16:49:10 -080051Alternative = Term { Term } .
Rob Pike4501d342009-02-19 17:31:36 -080052Term = production_name | token [ "..." token ] | Group | Option | Repetition .
53Group = "(" Expression ")" .
Rob Pikec956e902009-04-14 20:10:49 -070054Option = "[" Expression "]" .
Rob Pike4501d342009-02-19 17:31:36 -080055Repetition = "{" Expression "}" .
Robert Griesemerc2d55862009-02-19 16:49:10 -080056</pre>
Robert Griesemerbbfe3122008-10-09 17:12:09 -070057
Rob Pike4501d342009-02-19 17:31:36 -080058<p>
59Productions are expressions constructed from terms and the following
60operators, in increasing precedence:
61</p>
Rob Pikeff70f092009-02-20 13:36:14 -080062<pre class="grammar">
Rob Pike4501d342009-02-19 17:31:36 -080063| alternation
64() grouping
65[] option (0 or 1 times)
66{} repetition (0 to n times)
Robert Griesemerc2d55862009-02-19 16:49:10 -080067</pre>
Robert Griesemer4d230302008-12-17 15:39:15 -080068
Robert Griesemerc2d55862009-02-19 16:49:10 -080069<p>
Rob Pike4501d342009-02-19 17:31:36 -080070Lower-case production names are used to identify lexical tokens.
71Non-terminals are in CamelCase. Lexical symbols are enclosed in
Rob Pike678625d2009-09-15 09:54:22 -070072double quotes <code>""</code> or back quotes <code>``</code>.
Rob Pike4501d342009-02-19 17:31:36 -080073</p>
74
Robert Griesemerc2d55862009-02-19 16:49:10 -080075<p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -070076The form <code>a ... b</code> represents the set of characters from
Rob Pikef27e9f02009-02-23 19:22:05 -080077<code>a</code> through <code>b</code> as alternatives.
Rob Pike4501d342009-02-19 17:31:36 -080078</p>
79
Russ Cox7c4f7cc2009-08-20 11:11:03 -070080<h2 id="Source_code_representation">Source code representation</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -070081
Robert Griesemerc2d55862009-02-19 16:49:10 -080082<p>
Robert Griesemere9192752009-12-01 16:15:53 -080083Source code is Unicode text encoded in
84<a href="http://en.wikipedia.org/wiki/UTF-8">UTF-8</a>. The text is not
Rob Pikeff70f092009-02-20 13:36:14 -080085canonicalized, so a single accented code point is distinct from the
86same character constructed from combining an accent and a letter;
87those are treated as two code points. For simplicity, this document
88will use the term <i>character</i> to refer to a Unicode code point.
89</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -080090<p>
Rob Pikeff70f092009-02-20 13:36:14 -080091Each code point is distinct; for instance, upper and lower case letters
92are different characters.
93</p>
Russ Coxb7d9ffe2010-02-16 16:47:18 -080094<p>
Robert Griesemerf42e8832010-02-17 15:50:34 -080095Implementation restriction: For compatibility with other tools, a
96compiler may disallow the NUL character (U+0000) in the source text.
Russ Coxb7d9ffe2010-02-16 16:47:18 -080097</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -070098
Russ Cox7c4f7cc2009-08-20 11:11:03 -070099<h3 id="Characters">Characters</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700100
Robert Griesemerc2d55862009-02-19 16:49:10 -0800101<p>
Rob Pike4501d342009-02-19 17:31:36 -0800102The following terms are used to denote specific Unicode character classes:
103</p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700104<pre class="ebnf">
105unicode_char = /* an arbitrary Unicode code point */ .
106unicode_letter = /* a Unicode code point classified as "Letter" */ .
Robert Griesemer838b5ad2011-02-03 12:27:41 -0800107unicode_digit = /* a Unicode code point classified as "Decimal Digit" */ .
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700108</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700109
Rob Pike678625d2009-09-15 09:54:22 -0700110<p>
Robert Griesemer838b5ad2011-02-03 12:27:41 -0800111In <a href="http://www.unicode.org/versions/Unicode6.0.0/">The Unicode Standard 6.0</a>,
112Section 4.5 "General Category"
Rob Pike678625d2009-09-15 09:54:22 -0700113defines a set of character categories. Go treats
114those characters in category Lu, Ll, Lt, Lm, or Lo as Unicode letters,
115and those in category Nd as Unicode digits.
116</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700117
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700118<h3 id="Letters_and_digits">Letters and digits</h3>
Rob Pikeff70f092009-02-20 13:36:14 -0800119
120<p>
Rob Pikef27e9f02009-02-23 19:22:05 -0800121The underscore character <code>_</code> (U+005F) is considered a letter.
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700122</p>
123<pre class="ebnf">
Robert Griesemerc2d55862009-02-19 16:49:10 -0800124letter = unicode_letter | "_" .
125decimal_digit = "0" ... "9" .
126octal_digit = "0" ... "7" .
127hex_digit = "0" ... "9" | "A" ... "F" | "a" ... "f" .
128</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700129
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700130<h2 id="Lexical_elements">Lexical elements</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700131
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700132<h3 id="Comments">Comments</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700133
Rob Pikeff70f092009-02-20 13:36:14 -0800134<p>
Robert Griesemer130ac742009-12-10 16:43:01 -0800135There are two forms of comments:
Rob Pikeff70f092009-02-20 13:36:14 -0800136</p>
137
Robert Griesemer130ac742009-12-10 16:43:01 -0800138<ol>
139<li>
140<i>Line comments</i> start with the character sequence <code>//</code>
Robert Griesemerd73d1c52010-11-04 13:48:32 -0700141and stop at the end of the line. A line comment acts like a newline.
Robert Griesemer130ac742009-12-10 16:43:01 -0800142</li>
143<li>
144<i>General comments</i> start with the character sequence <code>/*</code>
145and continue through the character sequence <code>*/</code>. A general
146comment that spans multiple lines acts like a newline, otherwise it acts
147like a space.
148</li>
149</ol>
150
151<p>
152Comments do not nest.
153</p>
154
155
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700156<h3 id="Tokens">Tokens</h3>
Rob Pikeff70f092009-02-20 13:36:14 -0800157
158<p>
159Tokens form the vocabulary of the Go language.
Robert Griesemereb109a72009-12-28 14:40:42 -0800160There are four classes: <i>identifiers</i>, <i>keywords</i>, <i>operators
161and delimiters</i>, and <i>literals</i>. <i>White space</i>, formed from
Rob Pike4fe41922009-11-07 22:00:59 -0800162spaces (U+0020), horizontal tabs (U+0009),
163carriage returns (U+000D), and newlines (U+000A),
164is ignored except as it separates tokens
Robert Griesemer0e66a132010-09-27 18:59:11 -0700165that would otherwise combine into a single token. Also, a newline or end of file
Robert Griesemereb109a72009-12-28 14:40:42 -0800166may trigger the insertion of a <a href="#Semicolons">semicolon</a>.
Robert Griesemer130ac742009-12-10 16:43:01 -0800167While breaking the input into tokens,
Rob Pikeff70f092009-02-20 13:36:14 -0800168the next token is the longest sequence of characters that form a
169valid token.
170</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700171
Robert Griesemer130ac742009-12-10 16:43:01 -0800172<h3 id="Semicolons">Semicolons</h3>
173
174<p>
175The formal grammar uses semicolons <code>";"</code> as terminators in
176a number of productions. Go programs may omit most of these semicolons
177using the following two rules:
178</p>
179
180<ol>
181<li>
182<p>
183When the input is broken into tokens, a semicolon is automatically inserted
184into the token stream at the end of a non-blank line if the line's final
185token is
186</p>
187<ul>
Robert Griesemer3af480372010-05-14 13:11:48 -0700188 <li>an
189 <a href="#Identifiers">identifier</a>
Robert Griesemer130ac742009-12-10 16:43:01 -0800190 </li>
Robert Griesemer3af480372010-05-14 13:11:48 -0700191
192 <li>an
193 <a href="#Integer_literals">integer</a>,
194 <a href="#Floating-point_literals">floating-point</a>,
195 <a href="#Imaginary_literals">imaginary</a>,
196 <a href="#Character_literals">character</a>, or
197 <a href="#String_literals">string</a> literal
198 </li>
199
200 <li>one of the <a href="#Keywords">keywords</a>
201 <code>break</code>,
202 <code>continue</code>,
203 <code>fallthrough</code>, or
204 <code>return</code>
205 </li>
206
207 <li>one of the <a href="#Operators_and_Delimiters">operators and delimiters</a>
208 <code>++</code>,
209 <code>--</code>,
210 <code>)</code>,
211 <code>]</code>, or
212 <code>}</code>
Robert Griesemer130ac742009-12-10 16:43:01 -0800213 </li>
214</ul>
215</li>
216
217<li>
218To allow complex statements to occupy a single line, a semicolon
219may be omitted before a closing <code>")"</code> or <code>"}"</code>.
220</li>
221</ol>
222
223<p>
224To reflect idiomatic use, code examples in this document elide semicolons
225using these rules.
226</p>
227
228
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700229<h3 id="Identifiers">Identifiers</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700230
Rob Pikeff70f092009-02-20 13:36:14 -0800231<p>
232Identifiers name program entities such as variables and types.
233An identifier is a sequence of one or more letters and digits.
234The first character in an identifier must be a letter.
235</p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700236<pre class="ebnf">
Robert Griesemerd36d1912009-09-18 11:58:35 -0700237identifier = letter { letter | unicode_digit } .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800238</pre>
Robert Griesemerc2d55862009-02-19 16:49:10 -0800239<pre>
240a
241_x9
242ThisVariableIsExported
243αβ
244</pre>
Robert Griesemer130ac742009-12-10 16:43:01 -0800245
246<p>
Robert Griesemer4e56b332009-09-10 10:14:00 -0700247Some identifiers are <a href="#Predeclared_identifiers">predeclared</a>.
Robert Griesemer130ac742009-12-10 16:43:01 -0800248</p>
249
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -0700250
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700251<h3 id="Keywords">Keywords</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700252
Rob Pikeff70f092009-02-20 13:36:14 -0800253<p>
254The following keywords are reserved and may not be used as identifiers.
255</p>
256<pre class="grammar">
257break default func interface select
258case defer go map struct
259chan else goto package switch
260const fallthrough if range type
261continue for import return var
262</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700263
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700264<h3 id="Operators_and_Delimiters">Operators and Delimiters</h3>
Rob Pikeff70f092009-02-20 13:36:14 -0800265
266<p>
Robert Griesemerd36d1912009-09-18 11:58:35 -0700267The following character sequences represent <a href="#Operators">operators</a>, delimiters, and other special tokens:
Rob Pikeff70f092009-02-20 13:36:14 -0800268</p>
269<pre class="grammar">
270+ &amp; += &amp;= &amp;&amp; == != ( )
271- | -= |= || &lt; &lt;= [ ]
272* ^ *= ^= &lt;- &gt; &gt;= { }
Robert Griesemer4ed666e2009-08-27 16:45:42 -0700273/ &lt;&lt; /= &lt;&lt;= ++ = := , ;
274% &gt;&gt; %= &gt;&gt;= -- ! ... . :
Rob Pikecd04ec92009-03-11 21:59:05 -0700275 &amp;^ &amp;^=
Rob Pikeff70f092009-02-20 13:36:14 -0800276</pre>
277
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700278<h3 id="Integer_literals">Integer literals</h3>
Rob Pikeff70f092009-02-20 13:36:14 -0800279
280<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700281An integer literal is a sequence of digits representing an
282<a href="#Constants">integer constant</a>.
283An optional prefix sets a non-decimal base: <code>0</code> for octal, <code>0x</code> or
Rob Pikef27e9f02009-02-23 19:22:05 -0800284<code>0X</code> for hexadecimal. In hexadecimal literals, letters
285<code>a-f</code> and <code>A-F</code> represent values 10 through 15.
Rob Pikeff70f092009-02-20 13:36:14 -0800286</p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700287<pre class="ebnf">
Robert Griesemerd36d1912009-09-18 11:58:35 -0700288int_lit = decimal_lit | octal_lit | hex_lit .
289decimal_lit = ( "1" ... "9" ) { decimal_digit } .
290octal_lit = "0" { octal_digit } .
291hex_lit = "0" ( "x" | "X" ) hex_digit { hex_digit } .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800292</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700293
Robert Griesemerc2d55862009-02-19 16:49:10 -0800294<pre>
29542
2960600
2970xBadFace
298170141183460469231731687303715884105727
299</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700300
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700301<h3 id="Floating-point_literals">Floating-point literals</h3>
Rob Pikeff70f092009-02-20 13:36:14 -0800302<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700303A floating-point literal is a decimal representation of a
304<a href="#Constants">floating-point constant</a>.
305It has an integer part, a decimal point, a fractional part,
Rob Pikeff70f092009-02-20 13:36:14 -0800306and an exponent part. The integer and fractional part comprise
Rob Pikef27e9f02009-02-23 19:22:05 -0800307decimal digits; the exponent part is an <code>e</code> or <code>E</code>
Rob Pikeff70f092009-02-20 13:36:14 -0800308followed by an optionally signed decimal exponent. One of the
309integer part or the fractional part may be elided; one of the decimal
310point or the exponent may be elided.
311</p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700312<pre class="ebnf">
Robert Griesemerd36d1912009-09-18 11:58:35 -0700313float_lit = decimals "." [ decimals ] [ exponent ] |
314 decimals exponent |
315 "." decimals [ exponent ] .
316decimals = decimal_digit { decimal_digit } .
317exponent = ( "e" | "E" ) [ "+" | "-" ] decimals .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800318</pre>
Robert Griesemerad711102008-09-11 17:48:20 -0700319
Robert Griesemerc2d55862009-02-19 16:49:10 -0800320<pre>
3210.
Rob Pike72970872010-03-04 12:35:16 -080032272.40
323072.40 // == 72.40
Robert Griesemerc2d55862009-02-19 16:49:10 -08003242.71828
3251.e+0
3266.67428e-11
3271E6
328.25
329.12345E+5
330</pre>
Robert Griesemerad711102008-09-11 17:48:20 -0700331
Rob Pike72970872010-03-04 12:35:16 -0800332<h3 id="Imaginary_literals">Imaginary literals</h3>
333<p>
334An imaginary literal is a decimal representation of the imaginary part of a
335<a href="#Constants">complex constant</a>.
336It consists of a
337<a href="#Floating-point_literals">floating-point literal</a>
338or decimal integer followed
339by the lower-case letter <code>i</code>.
340</p>
341<pre class="ebnf">
342imaginary_lit = (decimals | float_lit) "i" .
343</pre>
344
345<pre>
3460i
347011i // == 11i
3480.i
3492.71828i
3501.e+0i
3516.67428e-11i
3521E6i
353.25i
354.12345E+5i
355</pre>
356
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700357
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700358<h3 id="Character_literals">Character literals</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700359
Rob Pike4501d342009-02-19 17:31:36 -0800360<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700361A character literal represents an <a href="#Constants">integer constant</a>,
362typically a Unicode code point, as one or more characters enclosed in single
Rob Pikeff70f092009-02-20 13:36:14 -0800363quotes. Within the quotes, any character may appear except single
364quote and newline. A single quoted character represents itself,
365while multi-character sequences beginning with a backslash encode
366values in various formats.
Rob Pike4501d342009-02-19 17:31:36 -0800367</p>
Rob Pikeff70f092009-02-20 13:36:14 -0800368<p>
369The simplest form represents the single character within the quotes;
370since Go source text is Unicode characters encoded in UTF-8, multiple
371UTF-8-encoded bytes may represent a single integer value. For
Rob Pikef27e9f02009-02-23 19:22:05 -0800372instance, the literal <code>'a'</code> holds a single byte representing
373a literal <code>a</code>, Unicode U+0061, value <code>0x61</code>, while
374<code>'ä'</code> holds two bytes (<code>0xc3</code> <code>0xa4</code>) representing
375a literal <code>a</code>-dieresis, U+00E4, value <code>0xe4</code>.
Rob Pikeff70f092009-02-20 13:36:14 -0800376</p>
377<p>
378Several backslash escapes allow arbitrary values to be represented
379as ASCII text. There are four ways to represent the integer value
Rob Pikef27e9f02009-02-23 19:22:05 -0800380as a numeric constant: <code>\x</code> followed by exactly two hexadecimal
381digits; <code>\u</code> followed by exactly four hexadecimal digits;
382<code>\U</code> followed by exactly eight hexadecimal digits, and a
383plain backslash <code>\</code> followed by exactly three octal digits.
Rob Pikeff70f092009-02-20 13:36:14 -0800384In each case the value of the literal is the value represented by
385the digits in the corresponding base.
386</p>
387<p>
388Although these representations all result in an integer, they have
389different valid ranges. Octal escapes must represent a value between
Rob Pike678625d2009-09-15 09:54:22 -07003900 and 255 inclusive. Hexadecimal escapes satisfy this condition
391by construction. The escapes <code>\u</code> and <code>\U</code>
Rob Pikeff70f092009-02-20 13:36:14 -0800392represent Unicode code points so within them some values are illegal,
Rob Pikef27e9f02009-02-23 19:22:05 -0800393in particular those above <code>0x10FFFF</code> and surrogate halves.
Rob Pikeff70f092009-02-20 13:36:14 -0800394</p>
395<p>
396After a backslash, certain single-character escapes represent special values:
397</p>
398<pre class="grammar">
399\a U+0007 alert or bell
400\b U+0008 backspace
401\f U+000C form feed
402\n U+000A line feed or newline
403\r U+000D carriage return
404\t U+0009 horizontal tab
405\v U+000b vertical tab
406\\ U+005c backslash
407\' U+0027 single quote (valid escape only within character literals)
408\" U+0022 double quote (valid escape only within string literals)
Robert Griesemerc2d55862009-02-19 16:49:10 -0800409</pre>
Robert Griesemerc2d55862009-02-19 16:49:10 -0800410<p>
Rob Pike4fe41922009-11-07 22:00:59 -0800411All other sequences starting with a backslash are illegal inside character literals.
Rob Pike4501d342009-02-19 17:31:36 -0800412</p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700413<pre class="ebnf">
Rob Pikeff70f092009-02-20 13:36:14 -0800414char_lit = "'" ( unicode_value | byte_value ) "'" .
415unicode_value = unicode_char | little_u_value | big_u_value | escaped_char .
416byte_value = octal_byte_value | hex_byte_value .
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700417octal_byte_value = `\` octal_digit octal_digit octal_digit .
418hex_byte_value = `\` "x" hex_digit hex_digit .
419little_u_value = `\` "u" hex_digit hex_digit hex_digit hex_digit .
420big_u_value = `\` "U" hex_digit hex_digit hex_digit hex_digit
Rob Pikeff70f092009-02-20 13:36:14 -0800421 hex_digit hex_digit hex_digit hex_digit .
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700422escaped_char = `\` ( "a" | "b" | "f" | "n" | "r" | "t" | "v" | `\` | "'" | `"` ) .
Rob Pikeff70f092009-02-20 13:36:14 -0800423</pre>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700424
Robert Griesemerc2d55862009-02-19 16:49:10 -0800425<pre>
426'a'
427'ä'
428'本'
429'\t'
430'\000'
431'\007'
432'\377'
433'\x07'
434'\xff'
435'\u12e4'
436'\U00101234'
437</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700438
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700439
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700440<h3 id="String_literals">String literals</h3>
Rob Pikeff70f092009-02-20 13:36:14 -0800441
442<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700443A string literal represents a <a href="#Constants">string constant</a>
444obtained from concatenating a sequence of characters. There are two forms:
445raw string literals and interpreted string literals.
Rob Pikeff70f092009-02-20 13:36:14 -0800446</p>
447<p>
448Raw string literals are character sequences between back quotes
Rob Pikef27e9f02009-02-23 19:22:05 -0800449<code>``</code>. Within the quotes, any character is legal except
Robert Griesemerdb7a6222009-06-18 13:51:14 -0700450back quote. The value of a raw string literal is the
451string composed of the uninterpreted characters between the quotes;
452in particular, backslashes have no special meaning and the string may
453span multiple lines.
Rob Pikeff70f092009-02-20 13:36:14 -0800454</p>
455<p>
456Interpreted string literals are character sequences between double
Rob Pike4fe41922009-11-07 22:00:59 -0800457quotes <code>&quot;&quot;</code>. The text between the quotes,
458which may not span multiple lines, forms the
Rob Pikeff70f092009-02-20 13:36:14 -0800459value of the literal, with backslash escapes interpreted as they
Rob Pikef27e9f02009-02-23 19:22:05 -0800460are in character literals (except that <code>\'</code> is illegal and
Robert Griesemere9192752009-12-01 16:15:53 -0800461<code>\"</code> is legal). The three-digit octal (<code>\</code><i>nnn</i>)
462and two-digit hexadecimal (<code>\x</code><i>nn</i>) escapes represent individual
Rob Pikeff70f092009-02-20 13:36:14 -0800463<i>bytes</i> of the resulting string; all other escapes represent
464the (possibly multi-byte) UTF-8 encoding of individual <i>characters</i>.
Rob Pikef27e9f02009-02-23 19:22:05 -0800465Thus inside a string literal <code>\377</code> and <code>\xFF</code> represent
466a single byte of value <code>0xFF</code>=255, while <code>ÿ</code>,
467<code>\u00FF</code>, <code>\U000000FF</code> and <code>\xc3\xbf</code> represent
Robert Griesemere1e76192009-09-25 14:11:03 -0700468the two bytes <code>0xc3</code> <code>0xbf</code> of the UTF-8 encoding of character
Rob Pikeff70f092009-02-20 13:36:14 -0800469U+00FF.
470</p>
471
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700472<pre class="ebnf">
Rob Pikeff70f092009-02-20 13:36:14 -0800473string_lit = raw_string_lit | interpreted_string_lit .
474raw_string_lit = "`" { unicode_char } "`" .
Robert Griesemerd4d4ff02009-10-19 13:13:59 -0700475interpreted_string_lit = `"` { unicode_value | byte_value } `"` .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800476</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700477
Robert Griesemerc2d55862009-02-19 16:49:10 -0800478<pre>
Robert Griesemerdb7a6222009-06-18 13:51:14 -0700479`abc` // same as "abc"
480`\n
481\n` // same as "\\n\n\\n"
Robert Griesemerc2d55862009-02-19 16:49:10 -0800482"\n"
483""
484"Hello, world!\n"
485"日本語"
486"\u65e5本\U00008a9e"
487"\xff\u00FF"
488</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700489
Rob Pikeff70f092009-02-20 13:36:14 -0800490<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700491These examples all represent the same string:
Rob Pikeff70f092009-02-20 13:36:14 -0800492</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700493
Robert Griesemerc2d55862009-02-19 16:49:10 -0800494<pre>
Rob Pikeff70f092009-02-20 13:36:14 -0800495"日本語" // UTF-8 input text
496`日本語` // UTF-8 input text as a raw literal
497"\u65e5\u672c\u8a9e" // The explicit Unicode code points
498"\U000065e5\U0000672c\U00008a9e" // The explicit Unicode code points
Robert Griesemerc2d55862009-02-19 16:49:10 -0800499"\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e" // The explicit UTF-8 bytes
500</pre>
Robert Griesemercd499272008-09-29 12:09:00 -0700501
Robert Griesemerc2d55862009-02-19 16:49:10 -0800502<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700503If the source code represents a character as two code points, such as
504a combining form involving an accent and a letter, the result will be
505an error if placed in a character literal (it is not a single code
506point), and will appear as two code points if placed in a string
507literal.
Rob Pikeff70f092009-02-20 13:36:14 -0800508</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700509
Robert Griesemer19b1d352009-09-24 19:36:48 -0700510
511<h2 id="Constants">Constants</h2>
512
Rob Pike72970872010-03-04 12:35:16 -0800513<p>There are <i>boolean constants</i>, <i>integer constants</i>,
514<i>floating-point constants</i>, <i>complex constants</i>,
515and <i>string constants</i>. Integer, floating-point,
516and complex constants are
Robert Griesemer19b1d352009-09-24 19:36:48 -0700517collectively called <i>numeric constants</i>.
518</p>
Rob Pike678625d2009-09-15 09:54:22 -0700519
520<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700521A constant value is represented by an
522<a href="#Integer_literals">integer</a>,
523<a href="#Floating-point_literals">floating-point</a>,
Rob Pike72970872010-03-04 12:35:16 -0800524<a href="#Imaginary_literals">imaginary</a>,
Robert Griesemer19b1d352009-09-24 19:36:48 -0700525<a href="#Character_literals">character</a>, or
526<a href="#String_literals">string</a> literal,
527an identifier denoting a constant,
528a <a href="#Constant_expressions">constant expression</a>, or
Russ Coxf4429182010-07-01 17:49:47 -0700529the result value of some built-in functions such as
530<code>unsafe.Sizeof</code> applied to any value,
531<code>cap</code> or <code>len</code> applied to
532<a href="#Length_and_capacity">some expressions</a>,
Rob Pike72970872010-03-04 12:35:16 -0800533<code>real</code> and <code>imag</code> applied to a complex constant
Robert Griesemerb94c0d22011-01-19 23:07:21 -0500534and <code>complex</code> applied to numeric constants.
Robert Griesemer19b1d352009-09-24 19:36:48 -0700535The boolean truth values are represented by the predeclared constants
536<code>true</code> and <code>false</code>. The predeclared identifier
537<a href="#Iota">iota</a> denotes an integer constant.
Rob Pike678625d2009-09-15 09:54:22 -0700538</p>
539
Robert Griesemer19b1d352009-09-24 19:36:48 -0700540<p>
Rob Pike72970872010-03-04 12:35:16 -0800541In general, complex constants are a form of
542<a href="#Constant_expressions">constant expression</a>
543and are discussed in that section.
544</p>
545
546<p>
Robert Griesemere9192752009-12-01 16:15:53 -0800547Numeric constants represent values of arbitrary precision and do not overflow.
Robert Griesemer19b1d352009-09-24 19:36:48 -0700548</p>
549
550<p>
551Constants may be <a href="#Types">typed</a> or untyped.
552Literal constants, <code>true</code>, <code>false</code>, <code>iota</code>,
553and certain <a href="#Constant_expressions">constant expressions</a>
554containing only untyped constant operands are untyped.
555</p>
556
557<p>
558A constant may be given a type explicitly by a <a href="#Constant_declarations">constant declaration</a>
559or <a href="#Conversions">conversion</a>, or implicitly when used in a
560<a href="#Variable_declarations">variable declaration</a> or an
561<a href="#Assignments">assignment</a> or as an
562operand in an <a href="#Expressions">expression</a>.
563It is an error if the constant value
Robert Griesemerdfc5bb52011-01-19 10:33:41 -0800564cannot be represented as a value of the respective type.
Robert Griesemere9192752009-12-01 16:15:53 -0800565For instance, <code>3.0</code> can be given any integer or any
Rob Pike4fe41922009-11-07 22:00:59 -0800566floating-point type, while <code>2147483648.0</code> (equal to <code>1&lt;&lt;31</code>)
567can be given the types <code>float32</code>, <code>float64</code>, or <code>uint32</code> but
568not <code>int32</code> or <code>string</code>.
Robert Griesemer19b1d352009-09-24 19:36:48 -0700569</p>
570
571<p>
Russ Cox16205a32010-01-18 15:59:14 -0800572There are no constants denoting the IEEE-754 infinity and not-a-number values,
573but the <a href="/pkg/math/"><code>math</code> package</a>'s
574<a href="/pkg/math/#Inf">Inf</a>,
575<a href="/pkg/math/#NaN">NaN</a>,
576<a href="/pkg/math/#IsInf">IsInf</a>, and
577<a href="/pkg/math/#IsNaN">IsNaN</a>
578functions return and test for those values at run time.
579</p>
580
581<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700582Implementation restriction: A compiler may implement numeric constants by choosing
583an internal representation with at least twice as many bits as any machine type;
584for floating-point values, both the mantissa and exponent must be twice as large.
585</p>
586
587
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700588<h2 id="Types">Types</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700589
Robert Griesemerc2d55862009-02-19 16:49:10 -0800590<p>
Robert Griesemer56809d02009-05-20 11:02:48 -0700591A type determines the set of values and operations specific to values of that
592type. A type may be specified by a (possibly qualified) <i>type name</i>
Robert Griesemer947e2182010-09-07 11:14:36 -0700593<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 -0700594which composes a new type from previously declared types.
Rob Pike5af7de32009-02-24 15:17:59 -0800595</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700596
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700597<pre class="ebnf">
Rob Pike8f2330d2009-02-25 16:20:44 -0800598Type = TypeName | TypeLit | "(" Type ")" .
Anthony Martin11a01612010-12-13 22:19:41 -0800599TypeName = QualifiedIdent .
Rob Pike8f2330d2009-02-25 16:20:44 -0800600TypeLit = ArrayType | StructType | PointerType | FunctionType | InterfaceType |
601 SliceType | MapType | ChannelType .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800602</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -0800603
Robert Griesemerc2d55862009-02-19 16:49:10 -0800604<p>
Robert Griesemerfc61b772009-09-28 14:10:20 -0700605Named instances of the boolean, numeric, and string types are
606<a href="#Predeclared_identifiers">predeclared</a>.
607<i>Composite types</i>&mdash;array, struct, pointer, function,
608interface, slice, map, and channel types&mdash;may be constructed using
609type literals.
Rob Pike5af7de32009-02-24 15:17:59 -0800610</p>
Robert Griesemer7abfcd92008-10-07 17:14:30 -0700611
Rob Pike5af7de32009-02-24 15:17:59 -0800612<p>
Robert Griesemer2a838d62011-02-08 13:31:01 -0800613The <i>static type</i> (or just <i>type</i>) of a variable is the
614type defined by its declaration. Variables of interface type
615also have a distinct <i>dynamic type</i>, which
616is the actual type of the value stored in the variable at run-time.
617The dynamic type may vary during execution but is always
618<a href="#Assignability">assignable</a>
619to the static type of the interface variable. For non-interface
620types, the dynamic type is always the static type.
621</p>
622
623<p>
Robert Griesemer7bc03712010-06-07 15:49:39 -0700624Each type <code>T</code> has an <i>underlying type</i>: If <code>T</code>
625is a predeclared type or a type literal, the corresponding underlying
626type is <code>T</code> itself. Otherwise, <code>T</code>'s underlying type
627is the underlying type of the type to which <code>T</code> refers in its
628<a href="#Type_declarations">type declaration</a>.
629</p>
630
631<pre>
632 type T1 string
633 type T2 T1
634 type T3 []T1
635 type T4 T3
636</pre>
637
638<p>
639The underlying type of <code>string</code>, <code>T1</code>, and <code>T2</code>
640is <code>string</code>. The underlying type of <code>[]T1</code>, <code>T3</code>,
641and <code>T4</code> is <code>[]T1</code>.
642</p>
643
Robert Griesemer2a838d62011-02-08 13:31:01 -0800644<h3 id="Method_sets">Method sets</h3>
Robert Griesemer7bc03712010-06-07 15:49:39 -0700645<p>
Robert Griesemer3b576a72009-06-17 14:31:33 -0700646A type may have a <i>method set</i> associated with it
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700647<a href="#Interface_types">Interface types</a>, §<a href="#Method_declarations">Method declarations</a>).
Robert Griesemer19b1d352009-09-24 19:36:48 -0700648The method set of an <a href="#Interface_types">interface type</a> is its interface.
Robert Griesemer56809d02009-05-20 11:02:48 -0700649The method set of any other named type <code>T</code>
Robert Griesemerfc61b772009-09-28 14:10:20 -0700650consists of all methods with receiver type <code>T</code>.
Robert Griesemer56809d02009-05-20 11:02:48 -0700651The method set of the corresponding pointer type <code>*T</code>
652is the set of all methods with receiver <code>*T</code> or <code>T</code>
653(that is, it also contains the method set of <code>T</code>).
654Any other type has an empty method set.
Robert Griesemerd4d4ff02009-10-19 13:13:59 -0700655In a method set, each method must have a unique name.
Rob Pike5af7de32009-02-24 15:17:59 -0800656</p>
Robert Griesemer1f3e8422008-09-29 18:41:30 -0700657
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700658
Robert Griesemer19b1d352009-09-24 19:36:48 -0700659<h3 id="Boolean_types">Boolean types</h3>
660
661A <i>boolean type</i> represents the set of Boolean truth values
662denoted by the predeclared constants <code>true</code>
663and <code>false</code>. The predeclared boolean type is <code>bool</code>.
664
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700665
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700666<h3 id="Numeric_types">Numeric types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700667
Rob Pike5af7de32009-02-24 15:17:59 -0800668<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700669A <i>numeric type</i> represents sets of integer or floating-point values.
670The predeclared architecture-independent numeric types are:
Rob Pike5af7de32009-02-24 15:17:59 -0800671</p>
Robert Griesemerebf14c62008-10-30 14:50:23 -0700672
Rob Pikeff70f092009-02-20 13:36:14 -0800673<pre class="grammar">
Rob Pike72970872010-03-04 12:35:16 -0800674uint8 the set of all unsigned 8-bit integers (0 to 255)
675uint16 the set of all unsigned 16-bit integers (0 to 65535)
676uint32 the set of all unsigned 32-bit integers (0 to 4294967295)
677uint64 the set of all unsigned 64-bit integers (0 to 18446744073709551615)
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700678
Rob Pike72970872010-03-04 12:35:16 -0800679int8 the set of all signed 8-bit integers (-128 to 127)
680int16 the set of all signed 16-bit integers (-32768 to 32767)
681int32 the set of all signed 32-bit integers (-2147483648 to 2147483647)
682int64 the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700683
Rob Pike72970872010-03-04 12:35:16 -0800684float32 the set of all IEEE-754 32-bit floating-point numbers
685float64 the set of all IEEE-754 64-bit floating-point numbers
686
687complex64 the set of all complex numbers with float32 real and imaginary parts
688complex128 the set of all complex numbers with float64 real and imaginary parts
Rob Pike5af7de32009-02-24 15:17:59 -0800689
Robert Griesemeref4c2b82010-03-10 15:29:36 -0800690byte familiar alias for uint8
Robert Griesemerc2d55862009-02-19 16:49:10 -0800691</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700692
Rob Pike5af7de32009-02-24 15:17:59 -0800693<p>
Robert Griesemere9192752009-12-01 16:15:53 -0800694The value of an <i>n</i>-bit integer is <i>n</i> bits wide and represented using
695<a href="http://en.wikipedia.org/wiki/Two's_complement">two's complement arithmetic</a>.
Rob Pike5af7de32009-02-24 15:17:59 -0800696</p>
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -0800697
Rob Pike5af7de32009-02-24 15:17:59 -0800698<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700699There is also a set of predeclared numeric types with implementation-specific sizes:
Rob Pike5af7de32009-02-24 15:17:59 -0800700</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700701
Rob Pikeff70f092009-02-20 13:36:14 -0800702<pre class="grammar">
Robert Griesemercfe92112009-06-18 13:29:40 -0700703uint either 32 or 64 bits
Robert Griesemer97025eb2011-01-13 10:24:04 -0800704int same size as uint
Robert Griesemercfe92112009-06-18 13:29:40 -0700705uintptr an unsigned integer large enough to store the uninterpreted bits of a pointer value
Robert Griesemerc2d55862009-02-19 16:49:10 -0800706</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700707
Robert Griesemerc2d55862009-02-19 16:49:10 -0800708<p>
Rob Pikeda389742009-03-02 19:13:40 -0800709To avoid portability issues all numeric types are distinct except
710<code>byte</code>, which is an alias for <code>uint8</code>.
711Conversions
Robert Griesemer7bc03712010-06-07 15:49:39 -0700712are required when different numeric types are mixed in an expression
Rob Pike5af7de32009-02-24 15:17:59 -0800713or assignment. For instance, <code>int32</code> and <code>int</code>
Russ Cox5958dd62009-03-04 17:19:21 -0800714are not the same type even though they may have the same size on a
Rob Pike5af7de32009-02-24 15:17:59 -0800715particular architecture.
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700716
717
Robert Griesemer19b1d352009-09-24 19:36:48 -0700718<h3 id="String_types">String types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700719
Rob Pike4501d342009-02-19 17:31:36 -0800720<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700721A <i>string type</i> represents the set of string values.
Rob Pike5af7de32009-02-24 15:17:59 -0800722Strings behave like arrays of bytes but are immutable: once created,
723it is impossible to change the contents of a string.
Robert Griesemer19b1d352009-09-24 19:36:48 -0700724The predeclared string type is <code>string</code>.
Rob Pike5af7de32009-02-24 15:17:59 -0800725
726<p>
727The elements of strings have type <code>byte</code> and may be
Robert Griesemerfc61b772009-09-28 14:10:20 -0700728accessed using the usual <a href="#Indexes">indexing operations</a>. It is
Rob Pike678625d2009-09-15 09:54:22 -0700729illegal to take the address of such an element; if
730<code>s[i]</code> is the <i>i</i>th byte of a
Robert Griesemercfe92112009-06-18 13:29:40 -0700731string, <code>&amp;s[i]</code> is invalid. The length of string
732<code>s</code> can be discovered using the built-in function
Robert Griesemer19b1d352009-09-24 19:36:48 -0700733<code>len</code>. The length is a compile-time constant if <code>s</code>
Robert Griesemercfe92112009-06-18 13:29:40 -0700734is a string literal.
Rob Pike4501d342009-02-19 17:31:36 -0800735</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700736
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700737
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700738<h3 id="Array_types">Array types</h3>
Russ Cox461dd912009-03-04 14:44:51 -0800739
740<p>
741An array is a numbered sequence of elements of a single
Robert Griesemer4023dce2009-08-14 17:41:52 -0700742type, called the element type.
743The number of elements is called the length and is never
Russ Cox461dd912009-03-04 14:44:51 -0800744negative.
745</p>
746
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700747<pre class="ebnf">
Russ Cox461dd912009-03-04 14:44:51 -0800748ArrayType = "[" ArrayLength "]" ElementType .
749ArrayLength = Expression .
Robert Griesemer4023dce2009-08-14 17:41:52 -0700750ElementType = Type .
Russ Cox461dd912009-03-04 14:44:51 -0800751</pre>
752
753<p>
Robert Griesemere9192752009-12-01 16:15:53 -0800754The length is part of the array's type and must be a
Robert Griesemer4ed666e2009-08-27 16:45:42 -0700755<a href="#Constant_expressions">constant expression</a> that evaluates to a non-negative
Russ Cox461dd912009-03-04 14:44:51 -0800756integer value. The length of array <code>a</code> can be discovered
Robert Griesemer0c2e6b32010-07-13 11:54:57 -0700757using the built-in function <a href="#Length_and_capacity"><code>len(a)</code></a>.
758The elements can be indexed by integer
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700759indices 0 through the <code>len(a)-1</code><a href="#Indexes">Indexes</a>).
Rob Pikeff6a8fd2009-11-20 15:47:15 -0800760Array types are always one-dimensional but may be composed to form
761multi-dimensional types.
Russ Cox461dd912009-03-04 14:44:51 -0800762</p>
763
764<pre>
765[32]byte
766[2*N] struct { x, y int32 }
767[1000]*float64
Rob Pikeff6a8fd2009-11-20 15:47:15 -0800768[3][5]int
769[2][2][2]float64 // same as [2]([2]([2]float64))
Russ Cox461dd912009-03-04 14:44:51 -0800770</pre>
771
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700772<h3 id="Slice_types">Slice types</h3>
Russ Cox461dd912009-03-04 14:44:51 -0800773
774<p>
775A slice is a reference to a contiguous segment of an array and
776contains a numbered sequence of elements from that array. A slice
777type denotes the set of all slices of arrays of its element type.
Robert Griesemer7bc03712010-06-07 15:49:39 -0700778The value of an uninitialized slice is <code>nil</code>.
Russ Cox461dd912009-03-04 14:44:51 -0800779</p>
780
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700781<pre class="ebnf">
Russ Cox461dd912009-03-04 14:44:51 -0800782SliceType = "[" "]" ElementType .
783</pre>
784
785<p>
786Like arrays, slices are indexable and have a length. The length of a
787slice <code>s</code> can be discovered by the built-in function
Robert Griesemer0c2e6b32010-07-13 11:54:57 -0700788<a href="#Length_and_capacity"><code>len(s)</code></a>; unlike with arrays it may change during
Russ Cox461dd912009-03-04 14:44:51 -0800789execution. The elements can be addressed by integer indices 0
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700790through <code>len(s)-1</code><a href="#Indexes">Indexes</a>). The slice index of a
Russ Cox461dd912009-03-04 14:44:51 -0800791given element may be less than the index of the same element in the
792underlying array.
793</p>
794<p>
795A slice, once initialized, is always associated with an underlying
Rob Pike7ec08562010-01-09 07:32:26 +1100796array that holds its elements. A slice therefore shares storage
Russ Cox461dd912009-03-04 14:44:51 -0800797with its array and with other slices of the same array; by contrast,
798distinct arrays always represent distinct storage.
799</p>
800<p>
801The array underlying a slice may extend past the end of the slice.
Russ Cox5958dd62009-03-04 17:19:21 -0800802The <i>capacity</i> is a measure of that extent: it is the sum of
Russ Cox461dd912009-03-04 14:44:51 -0800803the length of the slice and the length of the array beyond the slice;
804a slice of length up to that capacity can be created by `slicing' a new
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700805one from the original slice (§<a href="#Slices">Slices</a>).
Russ Cox461dd912009-03-04 14:44:51 -0800806The capacity of a slice <code>a</code> can be discovered using the
Robert Griesemer0c2e6b32010-07-13 11:54:57 -0700807built-in function <a href="#Length_and_capacity"><code>cap(a)</code></a>.
Russ Cox461dd912009-03-04 14:44:51 -0800808</p>
809
Russ Cox461dd912009-03-04 14:44:51 -0800810<p>
Robert Griesemer0c2e6b32010-07-13 11:54:57 -0700811A new, initialized slice value for a given element type <code>T</code> is
812made using the built-in function
813<a href="#Making_slices_maps_and_channels"><code>make</code></a>,
814which takes a slice type
Russ Cox461dd912009-03-04 14:44:51 -0800815and parameters specifying the length and optionally the capacity:
816</p>
817
818<pre>
819make([]T, length)
820make([]T, length, capacity)
821</pre>
Russ Cox5958dd62009-03-04 17:19:21 -0800822
Russ Cox461dd912009-03-04 14:44:51 -0800823<p>
824The <code>make()</code> call allocates a new, hidden array to which the returned
Rob Pike678625d2009-09-15 09:54:22 -0700825slice value refers. That is, executing
Russ Cox461dd912009-03-04 14:44:51 -0800826</p>
827
828<pre>
829make([]T, length, capacity)
830</pre>
831
832<p>
833produces the same slice as allocating an array and slicing it, so these two examples
834result in the same slice:
835</p>
836
837<pre>
838make([]int, 50, 100)
839new([100]int)[0:50]
840</pre>
841
Rob Pikeff6a8fd2009-11-20 15:47:15 -0800842<p>
843Like arrays, slices are always one-dimensional but may be composed to construct
844higher-dimensional objects.
845With arrays of arrays, the inner arrays are, by construction, always the same length;
846however with slices of slices (or arrays of slices), the lengths may vary dynamically.
847Moreover, the inner slices must be allocated individually (with <code>make</code>).
848</p>
Russ Cox461dd912009-03-04 14:44:51 -0800849
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700850<h3 id="Struct_types">Struct types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700851
Rob Pike5af7de32009-02-24 15:17:59 -0800852<p>
Robert Griesemerd3b15652009-11-16 08:58:55 -0800853A struct is a sequence of named elements, called fields, each of which has a
854name and a type. Field names may be specified explicitly (IdentifierList) or
855implicitly (AnonymousField).
856Within a struct, non-<a href="#Blank_identifier">blank</a> field names must
857be unique.
Rob Pike5af7de32009-02-24 15:17:59 -0800858</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700859
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700860<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -0800861StructType = "struct" "{" { FieldDecl ";" } "}" .
Robert Griesemerd3b15652009-11-16 08:58:55 -0800862FieldDecl = (IdentifierList Type | AnonymousField) [ Tag ] .
863AnonymousField = [ "*" ] TypeName .
Robert Griesemer130ac742009-12-10 16:43:01 -0800864Tag = string_lit .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800865</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700866
Robert Griesemerc2d55862009-02-19 16:49:10 -0800867<pre>
868// An empty struct.
869struct {}
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700870
Robert Griesemer4e56b332009-09-10 10:14:00 -0700871// A struct with 6 fields.
Robert Griesemerc2d55862009-02-19 16:49:10 -0800872struct {
Robert Griesemer130ac742009-12-10 16:43:01 -0800873 x, y int
Robert Griesemerb94c0d22011-01-19 23:07:21 -0500874 u float32
875 _ float32 // padding
Robert Griesemer130ac742009-12-10 16:43:01 -0800876 A *[]int
877 F func()
Robert Griesemerc2d55862009-02-19 16:49:10 -0800878}
879</pre>
Robert Griesemer1f3e8422008-09-29 18:41:30 -0700880
Rob Pike5af7de32009-02-24 15:17:59 -0800881<p>
Russ Coxbee2d5b2010-09-30 14:59:41 -0400882A field declared with a type but no explicit field name is an <i>anonymous field</i>
883(colloquially called an embedded field).
Rob Pike5af7de32009-02-24 15:17:59 -0800884Such a field type must be specified as
Russ Coxbee2d5b2010-09-30 14:59:41 -0400885a 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 -0700886and <code>T</code> itself may not be
Robert Griesemerd3b15652009-11-16 08:58:55 -0800887a pointer type. The unqualified type name acts as the field name.
Rob Pike5af7de32009-02-24 15:17:59 -0800888</p>
Robert Griesemer1f3e8422008-09-29 18:41:30 -0700889
Robert Griesemerc2d55862009-02-19 16:49:10 -0800890<pre>
891// A struct with four anonymous fields of type T1, *T2, P.T3 and *P.T4
892struct {
Robert Griesemer130ac742009-12-10 16:43:01 -0800893 T1 // field name is T1
894 *T2 // field name is T2
895 P.T3 // field name is T3
896 *P.T4 // field name is T4
897 x, y int // field names are x and y
Robert Griesemerc2d55862009-02-19 16:49:10 -0800898}
899</pre>
Robert Griesemer1f3e8422008-09-29 18:41:30 -0700900
Rob Pike5af7de32009-02-24 15:17:59 -0800901<p>
Robert Griesemerd3b15652009-11-16 08:58:55 -0800902The following declaration is illegal because field names must be unique
903in a struct type:
Rob Pike5af7de32009-02-24 15:17:59 -0800904</p>
Robert Griesemer071c91b2008-10-23 12:04:45 -0700905
Robert Griesemerc2d55862009-02-19 16:49:10 -0800906<pre>
907struct {
Robert Griesemer130ac742009-12-10 16:43:01 -0800908 T // conflicts with anonymous field *T and *P.T
909 *T // conflicts with anonymous field T and *P.T
910 *P.T // conflicts with anonymous field T and *T
Robert Griesemerc2d55862009-02-19 16:49:10 -0800911}
912</pre>
Robert Griesemer1f3e8422008-09-29 18:41:30 -0700913
Robert Griesemerc2d55862009-02-19 16:49:10 -0800914<p>
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700915Fields and methods (§<a href="#Method_declarations">Method declarations</a>) of an anonymous field are
916promoted to be ordinary fields and methods of the struct (§<a href="#Selectors">Selectors</a>).
Robert Griesemer56809d02009-05-20 11:02:48 -0700917The following rules apply for a struct type named <code>S</code> and
918a type named <code>T</code>:
Rob Pike5af7de32009-02-24 15:17:59 -0800919</p>
Robert Griesemer56809d02009-05-20 11:02:48 -0700920<ul>
921 <li>If <code>S</code> contains an anonymous field <code>T</code>, the
Robert Griesemer2a838d62011-02-08 13:31:01 -0800922 <a href="#Method_sets">method set</a> of <code>S</code> includes the
923 method set of <code>T</code>.
Robert Griesemer56809d02009-05-20 11:02:48 -0700924 </li>
925
926 <li>If <code>S</code> contains an anonymous field <code>*T</code>, the
927 method set of <code>S</code> includes the method set of <code>*T</code>
928 (which itself includes the method set of <code>T</code>).
929 </li>
930
931 <li>If <code>S</code> contains an anonymous field <code>T</code> or
932 <code>*T</code>, the method set of <code>*S</code> includes the
933 method set of <code>*T</code> (which itself includes the method
934 set of <code>T</code>).
935 </li>
936</ul>
Rob Pike5af7de32009-02-24 15:17:59 -0800937<p>
Robert Griesemer56809d02009-05-20 11:02:48 -0700938A field declaration may be followed by an optional string literal <i>tag</i>,
Robert Griesemerd3b15652009-11-16 08:58:55 -0800939which becomes an attribute for all the fields in the corresponding
Rob Pike5af7de32009-02-24 15:17:59 -0800940field declaration. The tags are made
Rob Pike8cb91842009-09-15 11:56:39 -0700941visible through a <a href="#Package_unsafe">reflection interface</a>
Rob Pike5af7de32009-02-24 15:17:59 -0800942but are otherwise ignored.
943</p>
Robert Griesemer2e90e542008-10-30 15:52:37 -0700944
Robert Griesemerc2d55862009-02-19 16:49:10 -0800945<pre>
Rob Pike678625d2009-09-15 09:54:22 -0700946// A struct corresponding to the TimeStamp protocol buffer.
Rob Pikecdbf6192009-02-24 17:47:45 -0800947// The tag strings define the protocol buffer field numbers.
Robert Griesemerc2d55862009-02-19 16:49:10 -0800948struct {
Robert Griesemer130ac742009-12-10 16:43:01 -0800949 microsec uint64 "field 1"
950 serverIP6 uint64 "field 2"
951 process string "field 3"
Robert Griesemerc2d55862009-02-19 16:49:10 -0800952}
953</pre>
Robert Griesemer2e90e542008-10-30 15:52:37 -0700954
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700955<h3 id="Pointer_types">Pointer types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700956
Rob Pike5af7de32009-02-24 15:17:59 -0800957<p>
Robert Griesemer4dc25282008-09-09 10:37:19 -0700958A pointer type denotes the set of all pointers to variables of a given
Rob Pikeda389742009-03-02 19:13:40 -0800959type, called the <i>base type</i> of the pointer.
Peter Mundy5928e1d2010-11-09 10:10:57 -0800960The value of an uninitialized pointer is <code>nil</code>.
Rob Pike5af7de32009-02-24 15:17:59 -0800961</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700962
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700963<pre class="ebnf">
Robert Griesemerc2d55862009-02-19 16:49:10 -0800964PointerType = "*" BaseType .
965BaseType = Type .
966</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700967
Robert Griesemerc2d55862009-02-19 16:49:10 -0800968<pre>
969*int
Rob Pike8f2330d2009-02-25 16:20:44 -0800970*map[string] *chan int
Robert Griesemerc2d55862009-02-19 16:49:10 -0800971</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700972
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700973<h3 id="Function_types">Function types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700974
Rob Pike8f2330d2009-02-25 16:20:44 -0800975<p>
Robert Griesemer7231ceb2008-09-08 15:01:04 -0700976A function type denotes the set of all functions with the same parameter
Peter Mundy5928e1d2010-11-09 10:10:57 -0800977and result types. The value of an uninitialized variable of function type
Robert Griesemer7bc03712010-06-07 15:49:39 -0700978is <code>nil</code>.
Rob Pike8f2330d2009-02-25 16:20:44 -0800979</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700980
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700981<pre class="ebnf">
Rob Pike8f2330d2009-02-25 16:20:44 -0800982FunctionType = "func" Signature .
983Signature = Parameters [ Result ] .
Robert Griesemer4023dce2009-08-14 17:41:52 -0700984Result = Parameters | Type .
Robert Griesemer130ac742009-12-10 16:43:01 -0800985Parameters = "(" [ ParameterList [ "," ] ] ")" .
Rob Pike8f2330d2009-02-25 16:20:44 -0800986ParameterList = ParameterDecl { "," ParameterDecl } .
Russ Cox95625922010-06-12 11:37:13 -0700987ParameterDecl = [ IdentifierList ] [ "..." ] Type .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800988</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700989
Robert Griesemerc2d55862009-02-19 16:49:10 -0800990<p>
Rob Pike8f2330d2009-02-25 16:20:44 -0800991Within a list of parameters or results, the names (IdentifierList)
992must either all be present or all be absent. If present, each name
993stands for one item (parameter or result) of the specified type; if absent, each
994type stands for one item of that type. Parameter and result
995lists are always parenthesized except that if there is exactly
Robert Griesemer73ca1272010-07-09 13:02:54 -0700996one unnamed result it may be written as an unparenthesized type.
Rob Pike8f2330d2009-02-25 16:20:44 -0800997</p>
Russ Cox95625922010-06-12 11:37:13 -0700998
Robert Griesemerac771a82010-09-24 14:08:28 -0700999<p>
1000The final parameter in a function signature may have
1001a type prefixed with <code>...</code>.
1002A function with such a parameter is called <i>variadic</i> and
1003may be invoked with zero or more arguments for that parameter.
Rob Pike8f2330d2009-02-25 16:20:44 -08001004</p>
Robert Griesemer2bfa9572008-10-24 13:13:12 -07001005
Robert Griesemerc2d55862009-02-19 16:49:10 -08001006<pre>
Russ Cox46871692010-01-26 10:25:56 -08001007func()
1008func(x int)
1009func() int
Russ Cox95625922010-06-12 11:37:13 -07001010func(prefix string, values ...int)
Robert Griesemerb94c0d22011-01-19 23:07:21 -05001011func(a, b int, z float32) bool
1012func(a, b int, z float32) (bool)
1013func(a, b int, z float64, opt ...interface{}) (success bool)
1014func(int, int, float64) (float64, *[]int)
Russ Cox46871692010-01-26 10:25:56 -08001015func(n int) func(p *T)
Robert Griesemerc2d55862009-02-19 16:49:10 -08001016</pre>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08001017
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001018
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001019<h3 id="Interface_types">Interface types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001020
Rob Pike8f2330d2009-02-25 16:20:44 -08001021<p>
Robert Griesemer2a838d62011-02-08 13:31:01 -08001022An interface type specifies a <a href="#Method_sets">method set</a> called its <i>interface</i>.
Robert Griesemer56809d02009-05-20 11:02:48 -07001023A variable of interface type can store a value of any type with a method set
1024that is any superset of the interface. Such a type is said to
Robert Griesemer7bc03712010-06-07 15:49:39 -07001025<i>implement the interface</i>.
Peter Mundy5928e1d2010-11-09 10:10:57 -08001026The value of an uninitialized variable of interface type is <code>nil</code>.
Rob Pike8f2330d2009-02-25 16:20:44 -08001027</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001028
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001029<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -08001030InterfaceType = "interface" "{" { MethodSpec ";" } "}" .
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07001031MethodSpec = MethodName Signature | InterfaceTypeName .
1032MethodName = identifier .
Rob Pike8f2330d2009-02-25 16:20:44 -08001033InterfaceTypeName = TypeName .
Robert Griesemerc2d55862009-02-19 16:49:10 -08001034</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001035
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07001036<p>
1037As with all method sets, in an interface type, each method must have a unique name.
1038</p>
1039
Robert Griesemerc2d55862009-02-19 16:49:10 -08001040<pre>
Rob Pike8f2330d2009-02-25 16:20:44 -08001041// A simple File interface
Robert Griesemerc2d55862009-02-19 16:49:10 -08001042interface {
Robert Griesemer130ac742009-12-10 16:43:01 -08001043 Read(b Buffer) bool
1044 Write(b Buffer) bool
1045 Close()
Robert Griesemerc2d55862009-02-19 16:49:10 -08001046}
1047</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001048
Rob Pike8f2330d2009-02-25 16:20:44 -08001049<p>
Robert Griesemer56809d02009-05-20 11:02:48 -07001050More than one type may implement an interface.
Rob Pike8f2330d2009-02-25 16:20:44 -08001051For instance, if two types <code>S1</code> and <code>S2</code>
Robert Griesemer56809d02009-05-20 11:02:48 -07001052have the method set
Rob Pike8f2330d2009-02-25 16:20:44 -08001053</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001054
Robert Griesemerc2d55862009-02-19 16:49:10 -08001055<pre>
1056func (p T) Read(b Buffer) bool { return ... }
1057func (p T) Write(b Buffer) bool { return ... }
1058func (p T) Close() { ... }
1059</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001060
Rob Pike8f2330d2009-02-25 16:20:44 -08001061<p>
1062(where <code>T</code> stands for either <code>S1</code> or <code>S2</code>)
1063then the <code>File</code> interface is implemented by both <code>S1</code> and
1064<code>S2</code>, regardless of what other methods
1065<code>S1</code> and <code>S2</code> may have or share.
1066</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001067
Rob Pike8f2330d2009-02-25 16:20:44 -08001068<p>
1069A type implements any interface comprising any subset of its methods
1070and may therefore implement several distinct interfaces. For
1071instance, all types implement the <i>empty interface</i>:
1072</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001073
Robert Griesemerc2d55862009-02-19 16:49:10 -08001074<pre>
Robert Griesemer19b1d352009-09-24 19:36:48 -07001075interface{}
Robert Griesemerc2d55862009-02-19 16:49:10 -08001076</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001077
Rob Pike8f2330d2009-02-25 16:20:44 -08001078<p>
1079Similarly, consider this interface specification,
Robert Griesemer4ed666e2009-08-27 16:45:42 -07001080which appears within a <a href="#Type_declarations">type declaration</a>
Rob Pike8f2330d2009-02-25 16:20:44 -08001081to define an interface called <code>Lock</code>:
1082</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001083
Robert Griesemerc2d55862009-02-19 16:49:10 -08001084<pre>
1085type Lock interface {
Robert Griesemer130ac742009-12-10 16:43:01 -08001086 Lock()
1087 Unlock()
Robert Griesemerc2d55862009-02-19 16:49:10 -08001088}
1089</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001090
Rob Pike8f2330d2009-02-25 16:20:44 -08001091<p>
1092If <code>S1</code> and <code>S2</code> also implement
1093</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001094
Robert Griesemerc2d55862009-02-19 16:49:10 -08001095<pre>
1096func (p T) Lock() { ... }
1097func (p T) Unlock() { ... }
1098</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001099
Robert Griesemerc2d55862009-02-19 16:49:10 -08001100<p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001101they implement the <code>Lock</code> interface as well
1102as the <code>File</code> interface.
1103</p>
1104<p>
1105An interface may contain an interface type name <code>T</code>
1106in place of a method specification.
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07001107The effect is equivalent to enumerating the methods of <code>T</code> explicitly
Rob Pike8f2330d2009-02-25 16:20:44 -08001108in the interface.
1109</p>
Robert Griesemer38c232f2009-02-11 15:09:15 -08001110
Robert Griesemerc2d55862009-02-19 16:49:10 -08001111<pre>
1112type ReadWrite interface {
Robert Griesemer130ac742009-12-10 16:43:01 -08001113 Read(b Buffer) bool
1114 Write(b Buffer) bool
Robert Griesemerc2d55862009-02-19 16:49:10 -08001115}
Robert Griesemer38c232f2009-02-11 15:09:15 -08001116
Robert Griesemerc2d55862009-02-19 16:49:10 -08001117type File interface {
Robert Griesemer130ac742009-12-10 16:43:01 -08001118 ReadWrite // same as enumerating the methods in ReadWrite
1119 Lock // same as enumerating the methods in Lock
1120 Close()
Robert Griesemerc2d55862009-02-19 16:49:10 -08001121}
1122</pre>
Robert Griesemer38c232f2009-02-11 15:09:15 -08001123
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001124<h3 id="Map_types">Map types</h3>
Robert Griesemera3294712009-01-05 11:17:26 -08001125
Rob Pike8f2330d2009-02-25 16:20:44 -08001126<p>
1127A map is an unordered group of elements of one type, called the
Robert Griesemer0660d242009-11-15 17:42:27 -08001128element type, indexed by a set of unique <i>keys</i> of another type,
Robert Griesemer4023dce2009-08-14 17:41:52 -07001129called the key type.
Robert Griesemer7bc03712010-06-07 15:49:39 -07001130The value of an uninitialized map is <code>nil</code>.
Rob Pike8f2330d2009-02-25 16:20:44 -08001131</p>
Robert Griesemera3294712009-01-05 11:17:26 -08001132
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001133<pre class="ebnf">
Robert Griesemer19b1d352009-09-24 19:36:48 -07001134MapType = "map" "[" KeyType "]" ElementType .
Robert Griesemer4023dce2009-08-14 17:41:52 -07001135KeyType = Type .
Robert Griesemerc2d55862009-02-19 16:49:10 -08001136</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001137
Robert Griesemerc2d55862009-02-19 16:49:10 -08001138<p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001139The comparison operators <code>==</code> and <code>!=</code>
Robert Griesemer1d282a82010-06-03 16:55:50 -07001140<a href="#Comparison_operators">Comparison operators</a>) must be fully defined
1141for operands of the key type; thus the key type must not be a struct, array or slice.
1142If the key type is an interface type, these
Rob Pike8f2330d2009-02-25 16:20:44 -08001143comparison operators must be defined for the dynamic key values;
Rob Pike5bb29fb2010-03-25 17:59:59 -07001144failure will cause a <a href="#Run_time_panics">run-time panic</a>.
Rob Pike8f2330d2009-02-25 16:20:44 -08001145
1146</p>
Robert Griesemera3294712009-01-05 11:17:26 -08001147
Robert Griesemerc2d55862009-02-19 16:49:10 -08001148<pre>
1149map [string] int
Robert Griesemerb94c0d22011-01-19 23:07:21 -05001150map [*T] struct { x, y float64 }
Robert Griesemerc2d55862009-02-19 16:49:10 -08001151map [string] interface {}
1152</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001153
Rob Pike5af7de32009-02-24 15:17:59 -08001154<p>
Robert Griesemer0c2e6b32010-07-13 11:54:57 -07001155The number of map elements is called its length.
1156For a map <code>m</code>, it can be discovered using the
1157built-in function <a href="#Length_and_capacity"><code>len(m)</code></a>
1158and may change during execution. Values may be added and removed
Rob Pike678625d2009-09-15 09:54:22 -07001159during execution using special forms of <a href="#Assignments">assignment</a>.
Rob Pike5af7de32009-02-24 15:17:59 -08001160</p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001161<p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001162A new, empty map value is made using the built-in
Robert Griesemer0c2e6b32010-07-13 11:54:57 -07001163function <a href="#Making_slices_maps_and_channels"><code>make</code></a>,
1164which takes the map type and an optional capacity hint as arguments:
Rob Pike8f2330d2009-02-25 16:20:44 -08001165</p>
1166
1167<pre>
Rob Pikeda389742009-03-02 19:13:40 -08001168make(map[string] int)
1169make(map[string] int, 100)
Rob Pike8f2330d2009-02-25 16:20:44 -08001170</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001171
Rob Pikeda389742009-03-02 19:13:40 -08001172<p>
1173The initial capacity does not bound its size:
1174maps grow to accommodate the number of items
1175stored in them.
1176</p>
1177
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001178<h3 id="Channel_types">Channel types</h3>
Robert Griesemera3294712009-01-05 11:17:26 -08001179
Rob Pike8f2330d2009-02-25 16:20:44 -08001180<p>
Robert Griesemera3294712009-01-05 11:17:26 -08001181A channel provides a mechanism for two concurrently executing functions
Rob Pike8f2330d2009-02-25 16:20:44 -08001182to synchronize execution and communicate by passing a value of a
Robert Griesemer4023dce2009-08-14 17:41:52 -07001183specified element type.
Robert Griesemer7bc03712010-06-07 15:49:39 -07001184The value of an uninitialized channel is <code>nil</code>.
Rob Pike8f2330d2009-02-25 16:20:44 -08001185</p>
Robert Griesemera3294712009-01-05 11:17:26 -08001186
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001187<pre class="ebnf">
Robert Griesemer56ca6972010-05-07 18:22:40 -07001188ChannelType = ( "chan" [ "&lt;-" ] | "&lt;-" "chan" ) ElementType .
Robert Griesemerc2d55862009-02-19 16:49:10 -08001189</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001190
Rob Pike8f2330d2009-02-25 16:20:44 -08001191<p>
Robert Griesemer56ca6972010-05-07 18:22:40 -07001192The <code>&lt;-</code> operator specifies the channel <i>direction</i>,
1193<i>send</i> or <i>receive</i>. If no direction is given, the channel is
1194<i>bi-directional</i>.
1195A channel may be constrained only to send or only to receive by
1196<a href="#Conversions">conversion</a> or <a href="#Assignments">assignment</a>.
1197</p>
1198
1199<pre>
1200chan T // can be used to send and receive values of type T
Robert Griesemerb94c0d22011-01-19 23:07:21 -05001201chan&lt;- float64 // can only be used to send float64s
Robert Griesemer56ca6972010-05-07 18:22:40 -07001202&lt;-chan int // can only be used to receive ints
1203</pre>
1204
1205<p>
1206The <code>&lt;-</code> operator associates with the leftmost <code>chan</code>
1207possible:
Robert Griesemer1c369bd2010-01-27 09:35:39 -08001208</p>
1209
1210<pre>
1211chan&lt;- chan int // same as chan&lt;- (chan int)
1212chan&lt;- &lt;-chan int // same as chan&lt;- (&lt;-chan int)
1213&lt;-chan &lt;-chan int // same as &lt;-chan (&lt;-chan int)
1214chan (&lt;-chan int)
1215</pre>
1216
1217<p>
Robert Griesemer7bc03712010-06-07 15:49:39 -07001218A new, initialized channel
Robert Griesemer56ca6972010-05-07 18:22:40 -07001219value can be made using the built-in function
1220<a href="#Making_slices_maps_and_channels"><code>make</code></a>,
Robert Griesemer633957b2009-01-06 13:23:20 -08001221which takes the channel type and an optional capacity as arguments:
Rob Pike8f2330d2009-02-25 16:20:44 -08001222</p>
Robert Griesemera3294712009-01-05 11:17:26 -08001223
Robert Griesemerc2d55862009-02-19 16:49:10 -08001224<pre>
Rob Pikeda389742009-03-02 19:13:40 -08001225make(chan int, 100)
Robert Griesemerc2d55862009-02-19 16:49:10 -08001226</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001227
Rob Pike8f2330d2009-02-25 16:20:44 -08001228<p>
1229The capacity, in number of elements, sets the size of the buffer in the channel. If the
Rob Pike678625d2009-09-15 09:54:22 -07001230capacity is greater than zero, the channel is asynchronous: provided the
Rob Pike8f2330d2009-02-25 16:20:44 -08001231buffer is not full, sends can succeed without blocking. If the capacity is zero
1232or absent, the communication succeeds only when both a sender and receiver are ready.
1233</p>
Robert Griesemera3294712009-01-05 11:17:26 -08001234
Rob Pike94b67eb2009-03-24 17:40:47 -07001235<p>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07001236A channel may be closed and tested for closure with the built-in functions
1237<a href="#Close_and_closed"><code>close</code> and <code>closed</code></a>.
Rob Pike94b67eb2009-03-24 17:40:47 -07001238</p>
1239
Rob Pike83cbca52009-08-21 14:18:08 -07001240<h2 id="Properties_of_types_and_values">Properties of types and values</h2>
Robert Griesemer434c6052008-11-07 13:34:37 -08001241
Robert Griesemer7bc03712010-06-07 15:49:39 -07001242<h3 id="Type_identity">Type identity</h3>
1243
Rob Pike4501d342009-02-19 17:31:36 -08001244<p>
Robert Griesemer7bc03712010-06-07 15:49:39 -07001245Two types are either <i>identical</i> or <i>different</i>.
Robert Griesemer19b1d352009-09-24 19:36:48 -07001246</p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001247
Robert Griesemerc2d55862009-02-19 16:49:10 -08001248<p>
Robert Griesemer533dfd62009-05-13 16:56:00 -07001249Two named types are identical if their type names originate in the same
Robert Griesemer7bc03712010-06-07 15:49:39 -07001250type <a href="#Declarations_and_scope">declaration</a>.
Robert Griesemer63f01492010-05-28 14:17:30 -07001251A named and an unnamed type are always different. Two unnamed types are identical
Robert Griesemer7bc03712010-06-07 15:49:39 -07001252if the corresponding type literals are identical, that is, if they have the same
Robert Griesemer63f01492010-05-28 14:17:30 -07001253literal structure and corresponding components have identical types. In detail:
Rob Pike4501d342009-02-19 17:31:36 -08001254</p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001255
Robert Griesemerc2d55862009-02-19 16:49:10 -08001256<ul>
Robert Griesemer533dfd62009-05-13 16:56:00 -07001257 <li>Two array types are identical if they have identical element types and
1258 the same array length.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001259
Robert Griesemer533dfd62009-05-13 16:56:00 -07001260 <li>Two slice types are identical if they have identical element types.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001261
Robert Griesemer533dfd62009-05-13 16:56:00 -07001262 <li>Two struct types are identical if they have the same sequence of fields,
Russ Coxe4953512010-06-21 12:42:33 -07001263 and if corresponding fields have the same names, and identical types,
1264 and identical tags.
Robert Griesemer63f01492010-05-28 14:17:30 -07001265 Two anonymous fields are considered to have the same name. Lower-case field
1266 names from different packages are always different.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001267
Robert Griesemer533dfd62009-05-13 16:56:00 -07001268 <li>Two pointer types are identical if they have identical base types.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001269
Robert Griesemer533dfd62009-05-13 16:56:00 -07001270 <li>Two function types are identical if they have the same number of parameters
Russ Cox95625922010-06-12 11:37:13 -07001271 and result values, corresponding parameter and result types are
1272 identical, and either both functions are variadic or neither is.
Robert Griesemer533dfd62009-05-13 16:56:00 -07001273 Parameter and result names are not required to match.</li>
Robert Griesemera3294712009-01-05 11:17:26 -08001274
Robert Griesemer533dfd62009-05-13 16:56:00 -07001275 <li>Two interface types are identical if they have the same set of methods
Robert Griesemer63f01492010-05-28 14:17:30 -07001276 with the same names and identical function types. Lower-case method names from
1277 different packages are always different. The order of the methods is irrelevant.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001278
Robert Griesemer533dfd62009-05-13 16:56:00 -07001279 <li>Two map types are identical if they have identical key and value types.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001280
Robert Griesemer533dfd62009-05-13 16:56:00 -07001281 <li>Two channel types are identical if they have identical value types and
1282 the same direction.</li>
Robert Griesemerc2d55862009-02-19 16:49:10 -08001283</ul>
Robert Griesemer434c6052008-11-07 13:34:37 -08001284
Robert Griesemerc2d55862009-02-19 16:49:10 -08001285<p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001286Given the declarations
1287</p>
Robert Griesemer434c6052008-11-07 13:34:37 -08001288
Robert Griesemerc2d55862009-02-19 16:49:10 -08001289<pre>
1290type (
Robert Griesemer130ac742009-12-10 16:43:01 -08001291 T0 []string
1292 T1 []string
1293 T2 struct { a, b int }
1294 T3 struct { a, c int }
Robert Griesemerb94c0d22011-01-19 23:07:21 -05001295 T4 func(int, float64) *T0
1296 T5 func(x int, y float64) *[]string
Robert Griesemerc2d55862009-02-19 16:49:10 -08001297)
1298</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -08001299
Rob Pike8f2330d2009-02-25 16:20:44 -08001300<p>
Robert Griesemer533dfd62009-05-13 16:56:00 -07001301these types are identical:
Rob Pike8f2330d2009-02-25 16:20:44 -08001302</p>
Robert Griesemer434c6052008-11-07 13:34:37 -08001303
Robert Griesemerc2d55862009-02-19 16:49:10 -08001304<pre>
1305T0 and T0
1306[]int and []int
1307struct { a, b *T5 } and struct { a, b *T5 }
Robert Griesemerb94c0d22011-01-19 23:07:21 -05001308func(x int, y float64) *[]string and func(int, float64) (result *[]string)
Robert Griesemerc2d55862009-02-19 16:49:10 -08001309</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -08001310
Rob Pike8f2330d2009-02-25 16:20:44 -08001311<p>
Robert Griesemer7bc03712010-06-07 15:49:39 -07001312<code>T0</code> and <code>T1</code> are different because they are named types
Robert Griesemerb94c0d22011-01-19 23:07:21 -05001313with distinct declarations; <code>func(int, float64) *T0</code> and
1314<code>func(x int, y float64) *[]string</code> are different because <code>T0</code>
Robert Griesemer7bc03712010-06-07 15:49:39 -07001315is different from <code>[]string</code>.
Robert Griesemer533dfd62009-05-13 16:56:00 -07001316</p>
1317
Robert Griesemer434c6052008-11-07 13:34:37 -08001318
Robert Griesemer440cc952010-06-07 17:40:21 -07001319<h3 id="Assignability">Assignability</h3>
Rob Pike5af7de32009-02-24 15:17:59 -08001320
Rob Pike5af7de32009-02-24 15:17:59 -08001321<p>
Robert Griesemer440cc952010-06-07 17:40:21 -07001322A value <code>x</code> is <i>assignable</i> to a variable of type <code>T</code>
1323("<code>x</code> is assignable to <code>T</code>") in any of these cases:
Rob Pike5af7de32009-02-24 15:17:59 -08001324</p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07001325
Rob Pike5af7de32009-02-24 15:17:59 -08001326<ul>
1327<li>
Robert Griesemer7bc03712010-06-07 15:49:39 -07001328<code>x</code>'s type is identical to <code>T</code>.
1329</li>
1330<li>
Rob Pike68f16092010-09-01 10:40:50 +10001331<code>x</code>'s type <code>V</code> and <code>T</code> have identical
1332<a href="#Types">underlying types</a> and at least one of <code>V</code>
1333or <code>T</code> is not a named type.
Rob Pike5af7de32009-02-24 15:17:59 -08001334</li>
1335<li>
Robert Griesemer19b1d352009-09-24 19:36:48 -07001336<code>T</code> is an interface type and
Robert Griesemer7bc03712010-06-07 15:49:39 -07001337<code>x</code> <a href="#Interface_types">implements</a> <code>T</code>.
Robert Griesemere2cb60b2009-06-19 13:03:01 -07001338</li>
1339<li>
Robert Griesemer7bc03712010-06-07 15:49:39 -07001340<code>x</code> is a bidirectional channel value, <code>T</code> is a channel type,
1341<code>x</code>'s type <code>V</code> and <code>T</code> have identical element types,
Rob Pike68f16092010-09-01 10:40:50 +10001342and at least one of <code>V</code> or <code>T</code> is not a named type.
Robert Griesemer7bc03712010-06-07 15:49:39 -07001343</li>
1344<li>
1345<code>x</code> is the predeclared identifier <code>nil</code> and <code>T</code>
1346is a pointer, function, slice, map, channel, or interface type.
1347</li>
1348<li>
1349<code>x</code> is an untyped <a href="#Constants">constant</a> representable
1350by a value of type <code>T</code>.
Robert Griesemer4e56b332009-09-10 10:14:00 -07001351</li>
Rob Pike5af7de32009-02-24 15:17:59 -08001352</ul>
1353
Robert Griesemer19b1d352009-09-24 19:36:48 -07001354<p>
Robert Griesemerfa3d0d72011-02-03 10:53:31 -08001355If <code>T</code> is a struct type with non-<a href="#Exported_identifiers">exported</a>
1356fields, the assignment must be in the same package in which <code>T</code> is declared,
1357or <code>x</code> must be the receiver of a method call.
Robert Griesemer326ef132009-09-28 19:21:15 -07001358In other words, a struct value can be assigned to a struct variable only if
Robert Griesemerfa3d0d72011-02-03 10:53:31 -08001359every field of the struct may be legally assigned individually by the program,
1360or if the assignment is initializing the receiver of a method of the struct type.
Robert Griesemer326ef132009-09-28 19:21:15 -07001361</p>
1362
1363<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07001364Any value may be assigned to the <a href="#Blank_identifier">blank identifier</a>.
1365</p>
1366
Rob Pikea9ed30f2009-02-23 19:26:07 -08001367
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001368<h2 id="Blocks">Blocks</h2>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001369
1370<p>
Robert Griesemer0a162a12009-08-19 16:44:04 -07001371A <i>block</i> is a sequence of declarations and statements within matching
1372brace brackets.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001373</p>
1374
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001375<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -08001376Block = "{" { Statement ";" } "}" .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001377</pre>
Russ Cox5958dd62009-03-04 17:19:21 -08001378
Rob Pikea9ed30f2009-02-23 19:26:07 -08001379<p>
Robert Griesemer0a162a12009-08-19 16:44:04 -07001380In addition to explicit blocks in the source code, there are implicit blocks:
Rob Pikea9ed30f2009-02-23 19:26:07 -08001381</p>
1382
1383<ol>
Robert Griesemer0a162a12009-08-19 16:44:04 -07001384 <li>The <i>universe block</i> encompasses all Go source text.</li>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001385
Robert Griesemer4e56b332009-09-10 10:14:00 -07001386 <li>Each <a href="#Packages">package</a> has a <i>package block</i> containing all
Robert Griesemer0a162a12009-08-19 16:44:04 -07001387 Go source text for that package.</li>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001388
Robert Griesemer0a162a12009-08-19 16:44:04 -07001389 <li>Each file has a <i>file block</i> containing all Go source text
1390 in that file.</li>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001391
Robert Griesemer0a162a12009-08-19 16:44:04 -07001392 <li>Each <code>if</code>, <code>for</code>, and <code>switch</code>
1393 statement is considered to be in its own implicit block.</li>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001394
Russ Cox16b95ba2009-08-20 10:22:52 -07001395 <li>Each clause in a <code>switch</code> or <code>select</code> statement
Robert Griesemer0a162a12009-08-19 16:44:04 -07001396 acts as an implicit block.</li>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001397</ol>
1398
Robert Griesemer0a162a12009-08-19 16:44:04 -07001399<p>
Robert Griesemer4e56b332009-09-10 10:14:00 -07001400Blocks nest and influence <a href="#Declarations_and_scope">scoping</a>.
Robert Griesemer0a162a12009-08-19 16:44:04 -07001401</p>
1402
1403
Robert Griesemeraeaab592009-08-31 17:30:55 -07001404<h2 id="Declarations_and_scope">Declarations and scope</h2>
Robert Griesemer0a162a12009-08-19 16:44:04 -07001405
1406<p>
Robert Griesemer4e56b332009-09-10 10:14:00 -07001407A declaration binds a non-<a href="#Blank_identifier">blank</a>
1408identifier to a constant, type, variable, function, or package.
Robert Griesemer0a162a12009-08-19 16:44:04 -07001409Every identifier in a program must be declared.
1410No identifier may be declared twice in the same block, and
1411no identifier may be declared in both the file and package block.
1412</p>
1413
1414<pre class="ebnf">
1415Declaration = ConstDecl | TypeDecl | VarDecl .
1416TopLevelDecl = Declaration | FunctionDecl | MethodDecl .
1417</pre>
1418
1419<p>
1420The <i>scope</i> of a declared identifier is the extent of source text in which
1421the identifier denotes the specified constant, type, variable, function, or package.
1422</p>
1423
1424<p>
1425Go is lexically scoped using blocks:
1426</p>
1427
1428<ol>
1429 <li>The scope of a predeclared identifier is the universe block.</li>
1430
1431 <li>The scope of an identifier denoting a constant, type, variable,
1432 or function declared at top level (outside any function) is the
1433 package block.</li>
1434
1435 <li>The scope of an imported package identifier is the file block
1436 of the file containing the import declaration.</li>
1437
1438 <li>The scope of an identifier denoting a function parameter or
1439 result variable is the function body.</li>
1440
1441 <li>The scope of a constant or variable identifier declared
1442 inside a function begins at the end of the ConstSpec or VarSpec
1443 and ends at the end of the innermost containing block.</li>
1444
1445 <li>The scope of a type identifier declared inside a function
Russ Cox16b95ba2009-08-20 10:22:52 -07001446 begins at the identifier in the TypeSpec
Robert Griesemer0a162a12009-08-19 16:44:04 -07001447 and ends at the end of the innermost containing block.</li>
1448</ol>
1449
1450<p>
1451An identifier declared in a block may be redeclared in an inner block.
1452While the identifier of the inner declaration is in scope, it denotes
1453the entity declared by the inner declaration.
1454</p>
1455
1456<p>
Robert Griesemer506c0082009-09-08 15:41:14 -07001457The <a href="#Package_clause">package clause</a> is not a declaration; the package name
Robert Griesemer0a162a12009-08-19 16:44:04 -07001458does not appear in any scope. Its purpose is to identify the files belonging
Robert Griesemer997851e2009-09-25 15:36:25 -07001459to the same <a href="#Packages">package</a> and to specify the default package name for import
Robert Griesemer0a162a12009-08-19 16:44:04 -07001460declarations.
1461</p>
1462
1463
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001464<h3 id="Label_scopes">Label scopes</h3>
Robert Griesemer0a162a12009-08-19 16:44:04 -07001465
1466<p>
Robert Griesemer506c0082009-09-08 15:41:14 -07001467Labels are declared by <a href="#Labeled_statements">labeled statements</a> and are
Robert Griesemer0a162a12009-08-19 16:44:04 -07001468used in the <code>break</code>, <code>continue</code>, and <code>goto</code>
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001469statements (§<a href="#Break_statements">Break statements</a>, §<a href="#Continue_statements">Continue statements</a>, §<a href="#Goto_statements">Goto statements</a>).
Robert Griesemer0a162a12009-08-19 16:44:04 -07001470In contrast to other identifiers, labels are not block scoped and do
1471not conflict with identifiers that are not labels. The scope of a label
1472is the body of the function in which it is declared and excludes
1473the body of any nested function.
1474</p>
1475
1476
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001477<h3 id="Predeclared_identifiers">Predeclared identifiers</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001478
1479<p>
Robert Griesemer0a162a12009-08-19 16:44:04 -07001480The following identifiers are implicitly declared in the universe block:
Rob Pikea9ed30f2009-02-23 19:26:07 -08001481</p>
1482<pre class="grammar">
1483Basic types:
Robert Griesemer7bc03712010-06-07 15:49:39 -07001484 bool byte complex64 complex128 float32 float64
1485 int8 int16 int32 int64 string uint8 uint16 uint32 uint64
Rob Pikea9ed30f2009-02-23 19:26:07 -08001486
Rob Pike5af7de32009-02-24 15:17:59 -08001487Architecture-specific convenience types:
Robert Griesemerb94c0d22011-01-19 23:07:21 -05001488 int uint uintptr
Rob Pikea9ed30f2009-02-23 19:26:07 -08001489
1490Constants:
Robert Griesemer19b1d352009-09-24 19:36:48 -07001491 true false iota
1492
1493Zero value:
1494 nil
Rob Pikea9ed30f2009-02-23 19:26:07 -08001495
1496Functions:
Robert Griesemerb94c0d22011-01-19 23:07:21 -05001497 append cap close closed complex copy imag len
Robert Griesemer07e983a2010-10-25 16:50:31 -07001498 make new panic print println real recover
Rob Pikea9ed30f2009-02-23 19:26:07 -08001499</pre>
1500
Robert Griesemeraeaab592009-08-31 17:30:55 -07001501
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001502<h3 id="Exported_identifiers">Exported identifiers</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001503
1504<p>
Robert Griesemeraeaab592009-08-31 17:30:55 -07001505An identifier may be <i>exported</i> to permit access to it from another package
1506using a <a href="#Qualified_identifiers">qualified identifier</a>. An identifier
1507is exported if both:
Rob Pikea9ed30f2009-02-23 19:26:07 -08001508</p>
1509<ol>
Robert Griesemer3af480372010-05-14 13:11:48 -07001510 <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 -07001511 <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 -07001512 declared in that block.</li>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001513</ol>
1514<p>
Robert Griesemeraeaab592009-08-31 17:30:55 -07001515All other identifiers are not exported.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001516</p>
1517
Robert Griesemeraeaab592009-08-31 17:30:55 -07001518
Robert Griesemer4e56b332009-09-10 10:14:00 -07001519<h3 id="Blank_identifier">Blank identifier</h3>
1520
1521<p>
1522The <i>blank identifier</i>, represented by the underscore character <code>_</code>, may be used in a declaration like
1523any other identifier but the declaration does not introduce a new binding.
1524</p>
1525
1526
Robert Griesemer19b1d352009-09-24 19:36:48 -07001527<h3 id="Constant_declarations">Constant declarations</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001528
1529<p>
1530A constant declaration binds a list of identifiers (the names of
Robert Griesemer506c0082009-09-08 15:41:14 -07001531the constants) to the values of a list of <a href="#Constant_expressions">constant expressions</a>.
1532The number of identifiers must be equal
1533to the number of expressions, and the <i>n</i>th identifier on
1534the left is bound to the value of the <i>n</i>th expression on the
Rob Pikea9ed30f2009-02-23 19:26:07 -08001535right.
1536</p>
1537
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001538<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -08001539ConstDecl = "const" ( ConstSpec | "(" { ConstSpec ";" } ")" ) .
Robert Griesemer4023dce2009-08-14 17:41:52 -07001540ConstSpec = IdentifierList [ [ Type ] "=" ExpressionList ] .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001541
1542IdentifierList = identifier { "," identifier } .
1543ExpressionList = Expression { "," Expression } .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001544</pre>
1545
1546<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07001547If the type is present, all constants take the type specified, and
Robert Griesemer440cc952010-06-07 17:40:21 -07001548the expressions must be <a href="#Assignability">assignable</a> to that type.
Robert Griesemer4023dce2009-08-14 17:41:52 -07001549If the type is omitted, the constants take the
Robert Griesemer19b1d352009-09-24 19:36:48 -07001550individual types of the corresponding expressions.
1551If the expression values are untyped <a href="#Constants">constants</a>,
1552the declared constants remain untyped and the constant identifiers
1553denote the constant values. For instance, if the expression is a
1554floating-point literal, the constant identifier denotes a floating-point
1555constant, even if the literal's fractional part is zero.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001556</p>
1557
1558<pre>
1559const Pi float64 = 3.14159265358979323846
Robert Griesemer19b1d352009-09-24 19:36:48 -07001560const zero = 0.0 // untyped floating-point constant
Rob Pikea9ed30f2009-02-23 19:26:07 -08001561const (
Robert Griesemer130ac742009-12-10 16:43:01 -08001562 size int64 = 1024
1563 eof = -1 // untyped integer constant
Rob Pikea9ed30f2009-02-23 19:26:07 -08001564)
Robert Griesemer19b1d352009-09-24 19:36:48 -07001565const a, b, c = 3, 4, "foo" // a = 3, b = 4, c = "foo", untyped integer and string constants
Robert Griesemerb94c0d22011-01-19 23:07:21 -05001566const u, v float32 = 0, 3 // u = 0.0, v = 3.0
Rob Pikea9ed30f2009-02-23 19:26:07 -08001567</pre>
1568
1569<p>
1570Within a parenthesized <code>const</code> declaration list the
1571expression list may be omitted from any but the first declaration.
1572Such an empty list is equivalent to the textual substitution of the
Rob Pike678625d2009-09-15 09:54:22 -07001573first preceding non-empty expression list and its type if any.
Russ Cox5958dd62009-03-04 17:19:21 -08001574Omitting the list of expressions is therefore equivalent to
1575repeating the previous list. The number of identifiers must be equal
1576to the number of expressions in the previous list.
Robert Griesemer506c0082009-09-08 15:41:14 -07001577Together with the <a href="#Iota"><code>iota</code> constant generator</a>
1578this mechanism permits light-weight declaration of sequential values:
Rob Pikea9ed30f2009-02-23 19:26:07 -08001579</p>
1580
1581<pre>
1582const (
Robert Griesemer130ac742009-12-10 16:43:01 -08001583 Sunday = iota
1584 Monday
1585 Tuesday
1586 Wednesday
1587 Thursday
1588 Friday
1589 Partyday
1590 numberOfDays // this constant is not exported
Rob Pikea9ed30f2009-02-23 19:26:07 -08001591)
1592</pre>
1593
1594
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001595<h3 id="Iota">Iota</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001596
1597<p>
Robert Griesemer39f009c2010-04-29 10:57:27 -07001598Within a <a href="#Constant_declarations">constant declaration</a>, the predeclared identifier
Robert Griesemer19b1d352009-09-24 19:36:48 -07001599<code>iota</code> represents successive untyped integer <a href="#Constants">
1600constants</a>. It is reset to 0 whenever the reserved word <code>const</code>
Robert Griesemer39f009c2010-04-29 10:57:27 -07001601appears in the source and increments after each <a href="#ConstSpec">ConstSpec</a>.
1602It can be used to construct a set of related constants:
Rob Pikea9ed30f2009-02-23 19:26:07 -08001603</p>
1604
1605<pre>
Robert Griesemer4e56b332009-09-10 10:14:00 -07001606const ( // iota is reset to 0
Robert Griesemer130ac742009-12-10 16:43:01 -08001607 c0 = iota // c0 == 0
1608 c1 = iota // c1 == 1
1609 c2 = iota // c2 == 2
Rob Pikea9ed30f2009-02-23 19:26:07 -08001610)
1611
1612const (
Robert Griesemer130ac742009-12-10 16:43:01 -08001613 a = 1 &lt;&lt; iota // a == 1 (iota has been reset)
1614 b = 1 &lt;&lt; iota // b == 2
1615 c = 1 &lt;&lt; iota // c == 4
Rob Pikea9ed30f2009-02-23 19:26:07 -08001616)
1617
1618const (
Robert Griesemerb94c0d22011-01-19 23:07:21 -05001619 u = iota * 42 // u == 0 (untyped integer constant)
1620 v float64 = iota * 42 // v == 42.0 (float64 constant)
1621 w = iota * 42 // w == 84 (untyped integer constant)
Rob Pikea9ed30f2009-02-23 19:26:07 -08001622)
1623
Robert Griesemer130ac742009-12-10 16:43:01 -08001624const x = iota // x == 0 (iota has been reset)
1625const y = iota // y == 0 (iota has been reset)
Rob Pikea9ed30f2009-02-23 19:26:07 -08001626</pre>
1627
1628<p>
1629Within an ExpressionList, the value of each <code>iota</code> is the same because
Robert Griesemer39f009c2010-04-29 10:57:27 -07001630it is only incremented after each ConstSpec:
Rob Pikea9ed30f2009-02-23 19:26:07 -08001631</p>
1632
1633<pre>
1634const (
Robert Griesemer130ac742009-12-10 16:43:01 -08001635 bit0, mask0 = 1 &lt;&lt; iota, 1 &lt;&lt; iota - 1 // bit0 == 1, mask0 == 0
1636 bit1, mask1 // bit1 == 2, mask1 == 1
1637 _, _ // skips iota == 2
1638 bit3, mask3 // bit3 == 8, mask3 == 7
Rob Pikea9ed30f2009-02-23 19:26:07 -08001639)
1640</pre>
1641
1642<p>
1643This last example exploits the implicit repetition of the
1644last non-empty expression list.
1645</p>
1646
1647
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001648<h3 id="Type_declarations">Type declarations</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001649
1650<p>
Robert Griesemerfc61b772009-09-28 14:10:20 -07001651A type declaration binds an identifier, the <i>type name</i>, to a new type
Robert Griesemer7bc03712010-06-07 15:49:39 -07001652that has the same <a href="#Types">underlying type</a> as
1653an existing type. The new type is <a href="#Type_identity">different</a> from
1654the existing type.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001655</p>
1656
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001657<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -08001658TypeDecl = "type" ( TypeSpec | "(" { TypeSpec ";" } ")" ) .
Robert Griesemerbdec3302009-08-31 17:57:14 -07001659TypeSpec = identifier Type .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001660</pre>
1661
1662<pre>
Robert Griesemerbdec3302009-08-31 17:57:14 -07001663type IntArray [16]int
Rob Pikea9ed30f2009-02-23 19:26:07 -08001664
1665type (
Robert Griesemer777a96a2010-12-02 12:32:14 -08001666 Point struct { x, y float64 }
Rob Pikea9ed30f2009-02-23 19:26:07 -08001667 Polar Point
1668)
1669
1670type TreeNode struct {
Robert Griesemer130ac742009-12-10 16:43:01 -08001671 left, right *TreeNode
1672 value *Comparable
Rob Pikea9ed30f2009-02-23 19:26:07 -08001673}
1674
Rob Pike678625d2009-09-15 09:54:22 -07001675type Cipher interface {
Robert Griesemer130ac742009-12-10 16:43:01 -08001676 BlockSize() int
1677 Encrypt(src, dst []byte)
1678 Decrypt(src, dst []byte)
Rob Pikea9ed30f2009-02-23 19:26:07 -08001679}
1680</pre>
1681
Robert Griesemerfc61b772009-09-28 14:10:20 -07001682<p>
1683The declared type does not inherit any <a href="#Method_declarations">methods</a>
Robert Griesemer2a838d62011-02-08 13:31:01 -08001684bound to the existing type, but the <a href="#Method_sets">method set</a>
Robert Griesemerd4a16192010-04-01 12:48:34 -07001685of an interface type or of elements of a composite type remains unchanged:
Robert Griesemerfc61b772009-09-28 14:10:20 -07001686</p>
1687
1688<pre>
1689// A Mutex is a data type with two methods Lock and Unlock.
1690type Mutex struct { /* Mutex fields */ }
1691func (m *Mutex) Lock() { /* Lock implementation */ }
1692func (m *Mutex) Unlock() { /* Unlock implementation */ }
1693
1694// NewMutex has the same composition as Mutex but its method set is empty.
1695type NewMutex Mutex
1696
Robert Griesemer2a838d62011-02-08 13:31:01 -08001697// The method set of the <a href="#Pointer_types">base type</a> of PtrMutex remains unchanged,
1698// but the method set of PtrMutex is empty.
1699type PtrMutex *Mutex
1700
Robert Griesemerf5b3c142010-04-27 17:52:44 -07001701// The method set of *PrintableMutex contains the methods
Rob Pike4fe41922009-11-07 22:00:59 -08001702// Lock and Unlock bound to its anonymous field Mutex.
Robert Griesemerfc61b772009-09-28 14:10:20 -07001703type PrintableMutex struct {
Robert Griesemer130ac742009-12-10 16:43:01 -08001704 Mutex
Robert Griesemerfc61b772009-09-28 14:10:20 -07001705}
Robert Griesemer735e00d2010-03-31 16:37:22 -07001706
Robert Griesemerd4a16192010-04-01 12:48:34 -07001707// MyCipher is an interface type that has the same method set as Cipher.
Robert Griesemer735e00d2010-03-31 16:37:22 -07001708type MyCipher Cipher
Robert Griesemerfc61b772009-09-28 14:10:20 -07001709</pre>
1710
1711<p>
1712A type declaration may be used to define a different boolean, numeric, or string
1713type and attach methods to it:
1714</p>
1715
1716<pre>
1717type TimeZone int
1718
1719const (
Robert Griesemer130ac742009-12-10 16:43:01 -08001720 EST TimeZone = -(5 + iota)
1721 CST
1722 MST
1723 PST
Robert Griesemerfc61b772009-09-28 14:10:20 -07001724)
1725
1726func (tz TimeZone) String() string {
Robert Griesemer130ac742009-12-10 16:43:01 -08001727 return fmt.Sprintf("GMT+%dh", tz)
Robert Griesemerfc61b772009-09-28 14:10:20 -07001728}
1729</pre>
1730
1731
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001732<h3 id="Variable_declarations">Variable declarations</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001733
1734<p>
1735A variable declaration creates a variable, binds an identifier to it and
1736gives it a type and optionally an initial value.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001737</p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001738<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -08001739VarDecl = "var" ( VarSpec | "(" { VarSpec ";" } ")" ) .
Robert Griesemer4023dce2009-08-14 17:41:52 -07001740VarSpec = IdentifierList ( Type [ "=" ExpressionList ] | "=" ExpressionList ) .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001741</pre>
1742
1743<pre>
1744var i int
Robert Griesemerb94c0d22011-01-19 23:07:21 -05001745var U, V, W float64
Rob Pikea9ed30f2009-02-23 19:26:07 -08001746var k = 0
Robert Griesemerb94c0d22011-01-19 23:07:21 -05001747var x, y float32 = -1, -2
Rob Pikea9ed30f2009-02-23 19:26:07 -08001748var (
Robert Griesemer130ac742009-12-10 16:43:01 -08001749 i int
Rob Pikea9ed30f2009-02-23 19:26:07 -08001750 u, v, s = 2.0, 3.0, "bar"
1751)
Robert Griesemer4e56b332009-09-10 10:14:00 -07001752var re, im = complexSqrt(-1)
Robert Griesemer130ac742009-12-10 16:43:01 -08001753var _, found = entries[name] // map lookup; only interested in "found"
Rob Pikea9ed30f2009-02-23 19:26:07 -08001754</pre>
1755
1756<p>
Robert Griesemeref45e642009-08-21 11:25:00 -07001757If a list of expressions is given, the variables are initialized
Rob Pike678625d2009-09-15 09:54:22 -07001758by assigning the expressions to the variables (§<a href="#Assignments">Assignments</a>)
1759in order; all expressions must be consumed and all variables initialized from them.
Rob Pike4fe41922009-11-07 22:00:59 -08001760Otherwise, each variable is initialized to its <a href="#The_zero_value">zero value</a>.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001761</p>
Robert Griesemeref45e642009-08-21 11:25:00 -07001762
Rob Pikea9ed30f2009-02-23 19:26:07 -08001763<p>
Robert Griesemeref45e642009-08-21 11:25:00 -07001764If the type is present, each variable is given that type.
1765Otherwise, the types are deduced from the assignment
1766of the expression list.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001767</p>
Robert Griesemeref45e642009-08-21 11:25:00 -07001768
Rob Pikea9ed30f2009-02-23 19:26:07 -08001769<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07001770If the type is absent and the corresponding expression evaluates to an
1771untyped <a href="#Constants">constant</a>, the type of the declared variable
Robert Griesemerb94c0d22011-01-19 23:07:21 -05001772is <code>bool</code>, <code>int</code>, <code>float64</code>, or <code>string</code>
Robert Griesemer19b1d352009-09-24 19:36:48 -07001773respectively, depending on whether the value is a boolean, integer,
1774floating-point, or string constant:
Rob Pikea9ed30f2009-02-23 19:26:07 -08001775</p>
1776
1777<pre>
Robert Griesemer19b1d352009-09-24 19:36:48 -07001778var b = true // t has type bool
Rob Pikea9ed30f2009-02-23 19:26:07 -08001779var i = 0 // i has type int
Robert Griesemerb94c0d22011-01-19 23:07:21 -05001780var f = 3.0 // f has type float64
Robert Griesemeref45e642009-08-21 11:25:00 -07001781var s = "OMDB" // s has type string
Rob Pikea9ed30f2009-02-23 19:26:07 -08001782</pre>
1783
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001784<h3 id="Short_variable_declarations">Short variable declarations</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001785
Robert Griesemer997851e2009-09-25 15:36:25 -07001786<p>
Robert Griesemeref45e642009-08-21 11:25:00 -07001787A <i>short variable declaration</i> uses the syntax:
Robert Griesemer997851e2009-09-25 15:36:25 -07001788</p>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001789
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001790<pre class="ebnf">
Robert Griesemere1b8cb82009-07-16 20:31:41 -07001791ShortVarDecl = IdentifierList ":=" ExpressionList .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001792</pre>
1793
Robert Griesemer997851e2009-09-25 15:36:25 -07001794<p>
Robert Griesemeref45e642009-08-21 11:25:00 -07001795It is a shorthand for a regular variable declaration with
1796initializer expressions but no types:
Robert Griesemer997851e2009-09-25 15:36:25 -07001797</p>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001798
1799<pre class="grammar">
1800"var" IdentifierList = ExpressionList .
1801</pre>
1802
1803<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08001804i, j := 0, 10
1805f := func() int { return 7 }
1806ch := make(chan int)
1807r, w := os.Pipe(fd) // os.Pipe() returns two values
1808_, y, _ := coord(p) // coord() returns three values; only interested in y coordinate
Rob Pikea9ed30f2009-02-23 19:26:07 -08001809</pre>
1810
1811<p>
Robert Griesemeref45e642009-08-21 11:25:00 -07001812Unlike regular variable declarations, a short variable declaration may redeclare variables provided they
Rob Pike2a1683a2009-04-19 20:04:15 -07001813were originally declared in the same block with the same type, and at
Robert Griesemer4e56b332009-09-10 10:14:00 -07001814least one of the non-<a href="#Blank_identifier">blank</a> variables is new. As a consequence, redeclaration
Rob Pike2a1683a2009-04-19 20:04:15 -07001815can only appear in a multi-variable short declaration.
1816Redeclaration does not introduce a new
1817variable; it just assigns a new value to the original.
1818</p>
1819
1820<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08001821field1, offset := nextField(str, 0)
1822field2, offset := nextField(str, offset) // redeclares offset
Rob Pike2a1683a2009-04-19 20:04:15 -07001823</pre>
1824
1825<p>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001826Short variable declarations may appear only inside functions.
1827In some contexts such as the initializers for <code>if</code>,
1828<code>for</code>, or <code>switch</code> statements,
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001829they can be used to declare local temporary variables (§<a href="#Statements">Statements</a>).
Rob Pikea9ed30f2009-02-23 19:26:07 -08001830</p>
1831
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001832<h3 id="Function_declarations">Function declarations</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001833
1834<p>
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001835A function declaration binds an identifier to a function (§<a href="#Function_types">Function types</a>).
Rob Pikea9ed30f2009-02-23 19:26:07 -08001836</p>
1837
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001838<pre class="ebnf">
Robert Griesemer0a162a12009-08-19 16:44:04 -07001839FunctionDecl = "func" identifier Signature [ Body ] .
Anthony Martin11a01612010-12-13 22:19:41 -08001840Body = Block .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001841</pre>
1842
Robert Griesemer4023dce2009-08-14 17:41:52 -07001843<p>
1844A function declaration may omit the body. Such a declaration provides the
1845signature for a function implemented outside Go, such as an assembly routine.
1846</p>
1847
Rob Pikea9ed30f2009-02-23 19:26:07 -08001848<pre>
1849func min(x int, y int) int {
1850 if x &lt; y {
Robert Griesemer130ac742009-12-10 16:43:01 -08001851 return x
Rob Pikea9ed30f2009-02-23 19:26:07 -08001852 }
Robert Griesemer130ac742009-12-10 16:43:01 -08001853 return y
Rob Pikea9ed30f2009-02-23 19:26:07 -08001854}
Robert Griesemer4023dce2009-08-14 17:41:52 -07001855
1856func flushICache(begin, end uintptr) // implemented externally
Rob Pikea9ed30f2009-02-23 19:26:07 -08001857</pre>
1858
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001859<h3 id="Method_declarations">Method declarations</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001860
1861<p>
Robert Griesemere9192752009-12-01 16:15:53 -08001862A method is a function with a <i>receiver</i>.
1863A method declaration binds an identifier to a method.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001864</p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001865<pre class="ebnf">
Rob Pike4fe41922009-11-07 22:00:59 -08001866MethodDecl = "func" Receiver MethodName Signature [ Body ] .
1867Receiver = "(" [ identifier ] [ "*" ] BaseTypeName ")" .
Robert Griesemerfc61b772009-09-28 14:10:20 -07001868BaseTypeName = identifier .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001869</pre>
1870
1871<p>
Robert Griesemer56809d02009-05-20 11:02:48 -07001872The receiver type must be of the form <code>T</code> or <code>*T</code> where
1873<code>T</code> is a type name. <code>T</code> is called the
1874<i>receiver base type</i> or just <i>base type</i>.
1875The base type must not be a pointer or interface type and must be
Stephen Ma5db1d382009-09-02 20:09:25 -07001876declared in the same package as the method.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001877The method is said to be <i>bound</i> to the base type
1878and is visible only within selectors for that type
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001879<a href="#Type_declarations">Type declarations</a>, §<a href="#Selectors">Selectors</a>).
Rob Pikea9ed30f2009-02-23 19:26:07 -08001880</p>
1881
1882<p>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001883Given type <code>Point</code>, the declarations
1884</p>
1885
1886<pre>
Robert Griesemer777a96a2010-12-02 12:32:14 -08001887func (p *Point) Length() float64 {
1888 return math.Sqrt(p.x * p.x + p.y * p.y)
Rob Pikea9ed30f2009-02-23 19:26:07 -08001889}
1890
Robert Griesemer777a96a2010-12-02 12:32:14 -08001891func (p *Point) Scale(factor float64) {
1892 p.x *= factor
1893 p.y *= factor
Rob Pikea9ed30f2009-02-23 19:26:07 -08001894}
1895</pre>
1896
1897<p>
Rob Pike678625d2009-09-15 09:54:22 -07001898bind the methods <code>Length</code> and <code>Scale</code>,
1899with receiver type <code>*Point</code>,
Rob Pikea9ed30f2009-02-23 19:26:07 -08001900to the base type <code>Point</code>.
1901</p>
1902
1903<p>
Robert Griesemere9192752009-12-01 16:15:53 -08001904If the receiver's value is not referenced inside the body of the method,
Rob Pikea9ed30f2009-02-23 19:26:07 -08001905its identifier may be omitted in the declaration. The same applies in
1906general to parameters of functions and methods.
1907</p>
1908
1909<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08001910The type of a method is the type of a function with the receiver as first
1911argument. For instance, the method <code>Scale</code> has type
1912</p>
1913
1914<pre>
Robert Griesemer777a96a2010-12-02 12:32:14 -08001915func(p *Point, factor float64)
Rob Pikedf3183f2009-02-26 16:37:23 -08001916</pre>
1917
1918<p>
1919However, a function declared this way is not a method.
1920</p>
1921
Rob Pikea9ed30f2009-02-23 19:26:07 -08001922
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001923<h2 id="Expressions">Expressions</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001924
Rob Pikedf3183f2009-02-26 16:37:23 -08001925<p>
1926An expression specifies the computation of a value by applying
Robert Griesemer19b1d352009-09-24 19:36:48 -07001927operators and functions to operands.
Rob Pikedf3183f2009-02-26 16:37:23 -08001928</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001929
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001930<h3 id="Operands">Operands</h3>
Robert Griesemerad711102008-09-11 17:48:20 -07001931
Robert Griesemer997851e2009-09-25 15:36:25 -07001932<p>
Robert Griesemerad711102008-09-11 17:48:20 -07001933Operands denote the elementary values in an expression.
Robert Griesemer997851e2009-09-25 15:36:25 -07001934</p>
Robert Griesemerad711102008-09-11 17:48:20 -07001935
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001936<pre class="ebnf">
Rob Pike01cadde2009-09-15 15:56:44 -07001937Operand = Literal | QualifiedIdent | MethodExpr | "(" Expression ")" .
Rob Pikedf3183f2009-02-26 16:37:23 -08001938Literal = BasicLit | CompositeLit | FunctionLit .
Rob Pike72970872010-03-04 12:35:16 -08001939BasicLit = int_lit | float_lit | imaginary_lit | char_lit | string_lit .
Robert Griesemerc2d55862009-02-19 16:49:10 -08001940</pre>
Robert Griesemerad711102008-09-11 17:48:20 -07001941
1942
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001943<h3 id="Qualified_identifiers">Qualified identifiers</h3>
Robert Griesemerad711102008-09-11 17:48:20 -07001944
Robert Griesemerc2d55862009-02-19 16:49:10 -08001945<p>
Robert Griesemer4e56b332009-09-10 10:14:00 -07001946A 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 -08001947</p>
Robert Griesemer337af312008-11-17 18:11:36 -08001948
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001949<pre class="ebnf">
Russ Cox16b95ba2009-08-20 10:22:52 -07001950QualifiedIdent = [ PackageName "." ] identifier .
Robert Griesemerc2d55862009-02-19 16:49:10 -08001951</pre>
Robert Griesemerad711102008-09-11 17:48:20 -07001952
Rob Pikedf3183f2009-02-26 16:37:23 -08001953<p>
Robert Griesemeraeaab592009-08-31 17:30:55 -07001954A qualified identifier accesses an identifier in a separate package.
1955The identifier must be <a href="#Exported_identifiers">exported</a> by that
1956package, which means that it must begin with a Unicode upper case letter.
Rob Pikedf3183f2009-02-26 16:37:23 -08001957</p>
Rob Pikedf3183f2009-02-26 16:37:23 -08001958
1959<pre>
Rob Pike678625d2009-09-15 09:54:22 -07001960math.Sin
Rob Pikedf3183f2009-02-26 16:37:23 -08001961</pre>
Robert Griesemerad711102008-09-11 17:48:20 -07001962
Rob Pike0b4de7a2009-11-09 16:09:57 -08001963<!---
Robert Griesemer4e56b332009-09-10 10:14:00 -07001964<p>
Robert Griesemer90cc4a52009-10-22 09:41:38 -07001965<span class="alert">TODO: Unify this section with Selectors - it's the same syntax.</span>
Robert Griesemer4e56b332009-09-10 10:14:00 -07001966</p>
Rob Pike0b4de7a2009-11-09 16:09:57 -08001967--->
Robert Griesemer4e56b332009-09-10 10:14:00 -07001968
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001969<h3 id="Composite_literals">Composite literals</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001970
Rob Pikedf3183f2009-02-26 16:37:23 -08001971<p>
1972Composite literals construct values for structs, arrays, slices, and maps
1973and create a new value each time they are evaluated.
1974They consist of the type of the value
Robert Griesemer838cf122009-05-22 10:25:06 -07001975followed by a brace-bound list of composite elements. An element may be
1976a single expression or a key-value pair.
Rob Pikedf3183f2009-02-26 16:37:23 -08001977</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001978
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001979<pre class="ebnf">
Robert Griesemera12141e2010-10-22 08:58:52 -07001980CompositeLit = LiteralType LiteralValue .
Rob Pikedf3183f2009-02-26 16:37:23 -08001981LiteralType = StructType | ArrayType | "[" "..." "]" ElementType |
Robert Griesemer07cc6442010-07-29 18:13:41 -07001982 SliceType | MapType | TypeName .
Robert Griesemera12141e2010-10-22 08:58:52 -07001983LiteralValue = "{" [ ElementList [ "," ] ] "}" .
Robert Griesemer130ac742009-12-10 16:43:01 -08001984ElementList = Element { "," Element } .
Robert Griesemer838cf122009-05-22 10:25:06 -07001985Element = [ Key ":" ] Value .
Robert Griesemerfb5fce52009-11-09 12:35:56 -08001986Key = FieldName | ElementIndex .
Rob Pike678625d2009-09-15 09:54:22 -07001987FieldName = identifier .
Robert Griesemerfb5fce52009-11-09 12:35:56 -08001988ElementIndex = Expression .
Robert Griesemera12141e2010-10-22 08:58:52 -07001989Value = Expression | LiteralValue .
Robert Griesemerc2d55862009-02-19 16:49:10 -08001990</pre>
Robert Griesemer0976e342008-09-03 13:37:44 -07001991
Rob Pikedf3183f2009-02-26 16:37:23 -08001992<p>
Robert Griesemer838cf122009-05-22 10:25:06 -07001993The LiteralType must be a struct, array, slice, or map type
1994(the grammar enforces this constraint except when the type is given
1995as a TypeName).
Robert Griesemer440cc952010-06-07 17:40:21 -07001996The types of the expressions must be <a href="#Assignability">assignable</a>
1997to the respective field, element, and key types of the LiteralType;
Russ Cox7a5e97b2009-03-03 15:40:30 -08001998there is no additional conversion.
Robert Griesemer838cf122009-05-22 10:25:06 -07001999The key is interpreted as a field name for struct literals,
Rob Pike678625d2009-09-15 09:54:22 -07002000an index expression for array and slice literals, and a key for map literals.
Robert Griesemer838cf122009-05-22 10:25:06 -07002001For map literals, all elements must have a key. It is an error
2002to specify multiple elements with the same field name or
2003constant key value.
Rob Pikedf3183f2009-02-26 16:37:23 -08002004</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002005
Robert Griesemer838cf122009-05-22 10:25:06 -07002006<p>
2007For struct literals the following rules apply:
Robert Griesemercfe92112009-06-18 13:29:40 -07002008</p>
Robert Griesemer838cf122009-05-22 10:25:06 -07002009<ul>
Robert Griesemerd3b15652009-11-16 08:58:55 -08002010 <li>A key must be a field name declared in the LiteralType.
2011 </li>
Rob Pike678625d2009-09-15 09:54:22 -07002012 <li>A literal that does not contain any keys must
Robert Griesemer838cf122009-05-22 10:25:06 -07002013 list an element for each struct field in the
2014 order in which the fields are declared.
2015 </li>
2016 <li>If any element has a key, every element must have a key.
2017 </li>
Rob Pike678625d2009-09-15 09:54:22 -07002018 <li>A literal that contains keys does not need to
Robert Griesemer838cf122009-05-22 10:25:06 -07002019 have an element for each struct field. Omitted fields
2020 get the zero value for that field.
2021 </li>
2022 <li>A literal may omit the element list; such a literal evaluates
2023 to the zero value for its type.
2024 </li>
2025 <li>It is an error to specify an element for a non-exported
2026 field of a struct belonging to a different package.
2027 </li>
2028</ul>
Robert Griesemer838cf122009-05-22 10:25:06 -07002029
2030<p>
2031Given the declarations
2032</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002033<pre>
Robert Griesemer777a96a2010-12-02 12:32:14 -08002034type Point3D struct { x, y, z float64 }
2035type Line struct { p, q Point3D }
Robert Griesemerc2d55862009-02-19 16:49:10 -08002036</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002037
Rob Pikedf3183f2009-02-26 16:37:23 -08002038<p>
2039one may write
2040</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002041
Robert Griesemerc2d55862009-02-19 16:49:10 -08002042<pre>
Robert Griesemer777a96a2010-12-02 12:32:14 -08002043origin := Point3D{} // zero value for Point3D
2044line := Line{origin, Point3D{y: -4, z: 12.3}} // zero value for line.q.x
Rob Pike37ab8382009-03-18 22:58:36 -07002045</pre>
2046
Robert Griesemercfe92112009-06-18 13:29:40 -07002047<p>
2048For array and slice literals the following rules apply:
2049</p>
Robert Griesemer838cf122009-05-22 10:25:06 -07002050<ul>
2051 <li>Each element has an associated integer index marking
2052 its position in the array.
2053 </li>
2054 <li>An element with a key uses the key as its index; the
2055 key must be a constant integer expression.
2056 </li>
2057 <li>An element without a key uses the previous element's index plus one.
2058 If the first element has no key, its index is zero.
2059 </li>
2060</ul>
Robert Griesemer838cf122009-05-22 10:25:06 -07002061
Rob Pike37ab8382009-03-18 22:58:36 -07002062<p>
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002063Taking the address of a composite literal (§<a href="#Address_operators">Address operators</a>)
Robert Griesemer0e1d9412011-01-26 11:21:23 -08002064generates a pointer to a unique instance of the literal's value.
Rob Pike37ab8382009-03-18 22:58:36 -07002065</p>
Rob Pike37ab8382009-03-18 22:58:36 -07002066<pre>
Robert Griesemer777a96a2010-12-02 12:32:14 -08002067var pointer *Point3D = &amp;Point3D{y: 1000}
Robert Griesemerc2d55862009-02-19 16:49:10 -08002068</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002069
Rob Pikedf3183f2009-02-26 16:37:23 -08002070<p>
Robert Griesemera3294712009-01-05 11:17:26 -08002071The length of an array literal is the length specified in the LiteralType.
2072If fewer elements than the length are provided in the literal, the missing
Rob Pike8f2330d2009-02-25 16:20:44 -08002073elements are set to the zero value for the array element type.
Robert Griesemer838cf122009-05-22 10:25:06 -07002074It is an error to provide elements with index values outside the index range
2075of the array. The notation <code>...</code> specifies an array length equal
2076to the maximum element index plus one.
Rob Pikedf3183f2009-02-26 16:37:23 -08002077</p>
Robert Griesemerb90b2132008-09-19 15:49:55 -07002078
Robert Griesemerc2d55862009-02-19 16:49:10 -08002079<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08002080buffer := [10]string{} // len(buffer) == 10
2081intSet := [6]int{1, 2, 3, 5} // len(intSet) == 6
2082days := [...]string{"Sat", "Sun"} // len(days) == 2
Robert Griesemerc2d55862009-02-19 16:49:10 -08002083</pre>
Robert Griesemer91bbd642009-01-07 09:31:35 -08002084
Rob Pikedf3183f2009-02-26 16:37:23 -08002085<p>
2086A slice literal describes the entire underlying array literal.
Rob Pike678625d2009-09-15 09:54:22 -07002087Thus, the length and capacity of a slice literal are the maximum
Robert Griesemer838cf122009-05-22 10:25:06 -07002088element index plus one. A slice literal has the form
Rob Pikedf3183f2009-02-26 16:37:23 -08002089</p>
Robert Griesemer91bbd642009-01-07 09:31:35 -08002090
Robert Griesemerc2d55862009-02-19 16:49:10 -08002091<pre>
Rob Pike426335f2009-03-02 17:52:52 -08002092[]T{x1, x2, ... xn}
Robert Griesemerc2d55862009-02-19 16:49:10 -08002093</pre>
Robert Griesemer91bbd642009-01-07 09:31:35 -08002094
Rob Pikedf3183f2009-02-26 16:37:23 -08002095<p>
2096and is a shortcut for a slice operation applied to an array literal:
2097</p>
Robert Griesemer91bbd642009-01-07 09:31:35 -08002098
Robert Griesemerc2d55862009-02-19 16:49:10 -08002099<pre>
Rob Pike426335f2009-03-02 17:52:52 -08002100[n]T{x1, x2, ... xn}[0 : n]
Robert Griesemerc2d55862009-02-19 16:49:10 -08002101</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002102
Rob Pikedf3183f2009-02-26 16:37:23 -08002103<p>
Robert Griesemera12141e2010-10-22 08:58:52 -07002104Within a composite literal of array, slice, or map type <code>T</code>,
2105elements that are themselves composite literals may elide the respective
2106literal type if it is identical to the element type of <code>T</code>.
2107</p>
2108
2109<pre>
2110[...]Point{{1.5, -3.5}, {0, 0}} // same as [...]Point{Point{1.5, -3.5}, Point{0, 0}}
2111[][]int{{1, 2, 3}, {4, 5}} // same as [][]int{[]int{1, 2, 3}, []int{4, 5}}
2112</pre>
2113
2114<p>
Russ Cox7a5e97b2009-03-03 15:40:30 -08002115A parsing ambiguity arises when a composite literal using the
Robert Griesemer07cc6442010-07-29 18:13:41 -07002116TypeName form of the LiteralType appears between the
2117<a href="#Keywords">keyword</a> and the opening brace of the block of an
Russ Cox7a5e97b2009-03-03 15:40:30 -08002118"if", "for", or "switch" statement, because the braces surrounding
2119the expressions in the literal are confused with those introducing
Robert Griesemer07cc6442010-07-29 18:13:41 -07002120the block of statements. To resolve the ambiguity in this rare case,
Russ Cox7a5e97b2009-03-03 15:40:30 -08002121the composite literal must appear within
2122parentheses.
2123</p>
2124
2125<pre>
2126if x == (T{a,b,c}[i]) { ... }
2127if (x == T{a,b,c}[i]) { ... }
2128</pre>
2129
Robert Griesemer838cf122009-05-22 10:25:06 -07002130<p>
2131Examples of valid array, slice, and map literals:
2132</p>
2133
2134<pre>
2135// list of prime numbers
Robert Griesemer130ac742009-12-10 16:43:01 -08002136primes := []int{2, 3, 5, 7, 9, 11, 13, 17, 19, 991}
Robert Griesemer838cf122009-05-22 10:25:06 -07002137
2138// vowels[ch] is true if ch is a vowel
Robert Griesemer130ac742009-12-10 16:43:01 -08002139vowels := [128]bool{'a': true, 'e': true, 'i': true, 'o': true, 'u': true, 'y': true}
Robert Griesemer838cf122009-05-22 10:25:06 -07002140
Robert Griesemerb94c0d22011-01-19 23:07:21 -05002141// the array [10]float32{-1, 0, 0, 0, -0.1, -0.1, 0, 0, 0, -1}
2142filter := [10]float32{-1, 4: -0.1, -0.1, 9: -1}
Robert Griesemer838cf122009-05-22 10:25:06 -07002143
2144// frequencies in Hz for equal-tempered scale (A4 = 440Hz)
Robert Griesemerb94c0d22011-01-19 23:07:21 -05002145noteFrequency := map[string]float32{
Robert Griesemer838cf122009-05-22 10:25:06 -07002146 "C0": 16.35, "D0": 18.35, "E0": 20.60, "F0": 21.83,
2147 "G0": 24.50, "A0": 27.50, "B0": 30.87,
2148}
2149</pre>
2150
2151
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002152<h3 id="Function_literals">Function literals</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002153
Rob Pikedf3183f2009-02-26 16:37:23 -08002154<p>
2155A function literal represents an anonymous function.
2156It consists of a specification of the function type and a function body.
2157</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002158
Robert Griesemerf7ac31362009-07-10 16:06:40 -07002159<pre class="ebnf">
Robert Griesemer0a162a12009-08-19 16:44:04 -07002160FunctionLit = FunctionType Body .
Robert Griesemerc2d55862009-02-19 16:49:10 -08002161</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002162
Robert Griesemerc2d55862009-02-19 16:49:10 -08002163<pre>
Robert Griesemerb94c0d22011-01-19 23:07:21 -05002164func(a, b int, z float64) bool { return a*b &lt; int(z) }
Robert Griesemerc2d55862009-02-19 16:49:10 -08002165</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002166
Rob Pikedf3183f2009-02-26 16:37:23 -08002167<p>
2168A function literal can be assigned to a variable or invoked directly.
2169</p>
Robert Griesemer7231ceb2008-09-08 15:01:04 -07002170
Robert Griesemerc2d55862009-02-19 16:49:10 -08002171<pre>
Rob Pikedf3183f2009-02-26 16:37:23 -08002172f := func(x, y int) int { return x + y }
2173func(ch chan int) { ch &lt;- ACK } (reply_chan)
Robert Griesemerc2d55862009-02-19 16:49:10 -08002174</pre>
Robert Griesemer7231ceb2008-09-08 15:01:04 -07002175
Rob Pikedf3183f2009-02-26 16:37:23 -08002176<p>
2177Function literals are <i>closures</i>: they may refer to variables
Robert Griesemerd8a764c2009-02-06 17:01:10 -08002178defined in a surrounding function. Those variables are then shared between
2179the surrounding function and the function literal, and they survive as long
Rob Pikedf3183f2009-02-26 16:37:23 -08002180as they are accessible.
2181</p>
Robert Griesemer7231ceb2008-09-08 15:01:04 -07002182
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002183
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002184<h3 id="Primary_expressions">Primary expressions</h3>
Russ Cox5958dd62009-03-04 17:19:21 -08002185
Robert Griesemer164a7bc2009-09-30 12:00:25 -07002186<p>
2187Primary expressions are the operands for unary and binary expressions.
2188</p>
2189
Robert Griesemerf7ac31362009-07-10 16:06:40 -07002190<pre class="ebnf">
Robert Griesemerc2d55862009-02-19 16:49:10 -08002191PrimaryExpr =
2192 Operand |
Robert Griesemer5eb36242009-09-16 11:05:14 -07002193 Conversion |
Robert Griesemer164a7bc2009-09-30 12:00:25 -07002194 BuiltinCall |
Robert Griesemerc2d55862009-02-19 16:49:10 -08002195 PrimaryExpr Selector |
2196 PrimaryExpr Index |
2197 PrimaryExpr Slice |
Russ Cox5958dd62009-03-04 17:19:21 -08002198 PrimaryExpr TypeAssertion |
Robert Griesemerc2d55862009-02-19 16:49:10 -08002199 PrimaryExpr Call .
Robert Griesemer57b34612008-10-10 12:45:44 -07002200
Russ Cox5958dd62009-03-04 17:19:21 -08002201Selector = "." identifier .
2202Index = "[" Expression "]" .
Scott Lawrence0c1695b2010-09-07 14:30:17 -07002203Slice = "[" [ Expression ] ":" [ Expression ] "]" .
Russ Cox5958dd62009-03-04 17:19:21 -08002204TypeAssertion = "." "(" Type ")" .
Robert Griesemerac771a82010-09-24 14:08:28 -07002205Call = "(" [ ArgumentList [ "," ] ] ")" .
2206ArgumentList = ExpressionList [ "..." ] .
Robert Griesemerc2d55862009-02-19 16:49:10 -08002207</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002208
2209
Robert Griesemerc2d55862009-02-19 16:49:10 -08002210<pre>
2211x
22122
2213(s + ".txt")
2214f(3.1415, true)
Rob Pike426335f2009-03-02 17:52:52 -08002215Point{1, 2}
Robert Griesemerc2d55862009-02-19 16:49:10 -08002216m["foo"]
2217s[i : j + 1]
2218obj.color
Robert Griesemer777a96a2010-12-02 12:32:14 -08002219math.Sin
Robert Griesemerc2d55862009-02-19 16:49:10 -08002220f.p[i].x()
2221</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002222
2223
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002224<h3 id="Selectors">Selectors</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002225
Rob Pikedf3183f2009-02-26 16:37:23 -08002226<p>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002227A primary expression of the form
Rob Pikedf3183f2009-02-26 16:37:23 -08002228</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002229
Robert Griesemerc2d55862009-02-19 16:49:10 -08002230<pre>
2231x.f
2232</pre>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002233
Robert Griesemerc2d55862009-02-19 16:49:10 -08002234<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002235denotes the field or method <code>f</code> of the value denoted by <code>x</code>
Russ Coxbee2d5b2010-09-30 14:59:41 -04002236(or sometimes <code>*x</code>; see below). The identifier <code>f</code>
Rob Pikedf3183f2009-02-26 16:37:23 -08002237is called the (field or method)
Robert Griesemer4e56b332009-09-10 10:14:00 -07002238<i>selector</i>; it must not be the <a href="#Blank_identifier">blank identifier</a>.
Rob Pikedf3183f2009-02-26 16:37:23 -08002239The type of the expression is the type of <code>f</code>.
2240</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002241<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002242A selector <code>f</code> may denote a field or method <code>f</code> of
2243a type <code>T</code>, or it may refer
2244to a field or method <code>f</code> of a nested anonymous field of
2245<code>T</code>.
2246The number of anonymous fields traversed
2247to reach <code>f</code> is called its <i>depth</i> in <code>T</code>.
2248The depth of a field or method <code>f</code>
2249declared in <code>T</code> is zero.
2250The depth of a field or method <code>f</code> declared in
2251an anonymous field <code>A</code> in <code>T</code> is the
2252depth of <code>f</code> in <code>A</code> plus one.
2253</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002254<p>
Robert Griesemer071c91b2008-10-23 12:04:45 -07002255The following rules apply to selectors:
Rob Pikedf3183f2009-02-26 16:37:23 -08002256</p>
2257<ol>
2258<li>
2259For a value <code>x</code> of type <code>T</code> or <code>*T</code>
2260where <code>T</code> is not an interface type,
2261<code>x.f</code> denotes the field or method at the shallowest depth
2262in <code>T</code> where there
2263is such an <code>f</code>.
2264If there is not exactly one <code>f</code> with shallowest depth, the selector
Robert Griesemer071c91b2008-10-23 12:04:45 -07002265expression is illegal.
Rob Pikedf3183f2009-02-26 16:37:23 -08002266</li>
2267<li>
2268For a variable <code>x</code> of type <code>I</code> or <code>*I</code>
2269where <code>I</code> is an interface type,
2270<code>x.f</code> denotes the actual method with name <code>f</code> of the value assigned
2271to <code>x</code> if there is such a method.
2272If no value or <code>nil</code> was assigned to <code>x</code>, <code>x.f</code> is illegal.
2273</li>
2274<li>
2275In all other cases, <code>x.f</code> is illegal.
Robert Griesemer3af480372010-05-14 13:11:48 -07002276</li>
Rob Pikedf3183f2009-02-26 16:37:23 -08002277</ol>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002278<p>
Russ Coxbee2d5b2010-09-30 14:59:41 -04002279Selectors automatically dereference pointers to structs.
2280If <code>x</code> is a pointer to a struct, <code>x.y</code>
2281is shorthand for <code>(*x).y</code>; if the field <code>y</code>
2282is also a pointer to a struct, <code>x.y.z</code> is shorthand
Rob Pikedf3183f2009-02-26 16:37:23 -08002283for <code>(*(*x).y).z</code>, and so on.
Russ Coxbee2d5b2010-09-30 14:59:41 -04002284If <code>x</code> contains an anonymous field of type <code>*A</code>,
2285where <code>A</code> is also a struct type,
Rob Pikedf3183f2009-02-26 16:37:23 -08002286<code>x.f</code> is a shortcut for <code>(*x.A).f</code>.
2287</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002288<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002289For example, given the declarations:
2290</p>
Robert Griesemer071c91b2008-10-23 12:04:45 -07002291
Robert Griesemerc2d55862009-02-19 16:49:10 -08002292<pre>
2293type T0 struct {
Robert Griesemer130ac742009-12-10 16:43:01 -08002294 x int
Robert Griesemerc2d55862009-02-19 16:49:10 -08002295}
Robert Griesemer071c91b2008-10-23 12:04:45 -07002296
Robert Griesemerc2d55862009-02-19 16:49:10 -08002297func (recv *T0) M0()
Robert Griesemer071c91b2008-10-23 12:04:45 -07002298
Robert Griesemerc2d55862009-02-19 16:49:10 -08002299type T1 struct {
Robert Griesemer130ac742009-12-10 16:43:01 -08002300 y int
Robert Griesemerc2d55862009-02-19 16:49:10 -08002301}
Robert Griesemer071c91b2008-10-23 12:04:45 -07002302
Robert Griesemerc2d55862009-02-19 16:49:10 -08002303func (recv T1) M1()
Robert Griesemer071c91b2008-10-23 12:04:45 -07002304
Robert Griesemerc2d55862009-02-19 16:49:10 -08002305type T2 struct {
Robert Griesemer130ac742009-12-10 16:43:01 -08002306 z int
2307 T1
2308 *T0
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 *T2) M2()
Robert Griesemer071c91b2008-10-23 12:04:45 -07002312
Robert Griesemer130ac742009-12-10 16:43:01 -08002313var p *T2 // with p != nil and p.T1 != nil
Robert Griesemerc2d55862009-02-19 16:49:10 -08002314</pre>
Robert Griesemer071c91b2008-10-23 12:04:45 -07002315
Rob Pikedf3183f2009-02-26 16:37:23 -08002316<p>
2317one may write:
2318</p>
Robert Griesemer071c91b2008-10-23 12:04:45 -07002319
Robert Griesemerc2d55862009-02-19 16:49:10 -08002320<pre>
2321p.z // (*p).z
2322p.y // ((*p).T1).y
2323p.x // (*(*p).T0).x
Robert Griesemer071c91b2008-10-23 12:04:45 -07002324
Robert Griesemerc2d55862009-02-19 16:49:10 -08002325p.M2 // (*p).M2
2326p.M1 // ((*p).T1).M1
2327p.M0 // ((*p).T0).M0
2328</pre>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002329
2330
Rob Pike0b4de7a2009-11-09 16:09:57 -08002331<!---
Robert Griesemer90cc4a52009-10-22 09:41:38 -07002332<span class="alert">
Robert Griesemer071c91b2008-10-23 12:04:45 -07002333TODO: Specify what happens to receivers.
Robert Griesemer90cc4a52009-10-22 09:41:38 -07002334</span>
Rob Pike0b4de7a2009-11-09 16:09:57 -08002335--->
Robert Griesemer7abfcd92008-10-07 17:14:30 -07002336
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002337
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002338<h3 id="Indexes">Indexes</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002339
Rob Pikedf3183f2009-02-26 16:37:23 -08002340<p>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002341A primary expression of the form
Rob Pikedf3183f2009-02-26 16:37:23 -08002342</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002343
Robert Griesemerc2d55862009-02-19 16:49:10 -08002344<pre>
2345a[x]
2346</pre>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002347
Rob Pike4501d342009-02-19 17:31:36 -08002348<p>
Rob Pike678625d2009-09-15 09:54:22 -07002349denotes the element of the array, slice, string or map <code>a</code> indexed by <code>x</code>.
Rob Pikedf3183f2009-02-26 16:37:23 -08002350The value <code>x</code> is called the
Rob Pike678625d2009-09-15 09:54:22 -07002351<i>index</i> or <i>map key</i>, respectively. The following
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002352rules apply:
Rob Pike4501d342009-02-19 17:31:36 -08002353</p>
Russ Coxeaa92e02009-06-25 14:43:55 -07002354
Robert Griesemerc2d55862009-02-19 16:49:10 -08002355<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002356For <code>a</code> of type <code>A</code> or <code>*A</code>
Robert Griesemer506c0082009-09-08 15:41:14 -07002357where <code>A</code> is an <a href="#Array_types">array type</a>,
2358or 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 -08002359</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002360<ul>
Robert Griesemer29f1ca52010-03-23 14:01:51 -07002361 <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 -08002362 <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 -07002363 <code>a[x]</code> is the element type of <code>A</code></li>
Rob Pike5bb29fb2010-03-25 17:59:59 -07002364 <li>if the index <code>x</code> is out of range,
2365 a <a href="#Run_time_panics">run-time panic</a> occurs</li>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002366</ul>
Russ Coxeaa92e02009-06-25 14:43:55 -07002367
Robert Griesemerc2d55862009-02-19 16:49:10 -08002368<p>
Russ Coxeaa92e02009-06-25 14:43:55 -07002369For <code>a</code> of type <code>T</code>
Robert Griesemer19b1d352009-09-24 19:36:48 -07002370where <code>T</code> is a <a href="#String_types">string type</a>:
Russ Coxeaa92e02009-06-25 14:43:55 -07002371</p>
2372<ul>
Robert Griesemer29f1ca52010-03-23 14:01:51 -07002373 <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 -07002374 <li><code>a[x]</code> is the byte at index <code>x</code> and the type of
Robert Griesemer29f1ca52010-03-23 14:01:51 -07002375 <code>a[x]</code> is <code>byte</code></li>
Robert Griesemer3af480372010-05-14 13:11:48 -07002376 <li><code>a[x]</code> may not be assigned to</li>
Rob Pike5bb29fb2010-03-25 17:59:59 -07002377 <li>if the index <code>x</code> is out of range,
2378 a <a href="#Run_time_panics">run-time panic</a> occurs</li>
Russ Coxeaa92e02009-06-25 14:43:55 -07002379</ul>
2380
2381<p>
2382For <code>a</code> of type <code>M</code>
Robert Griesemer506c0082009-09-08 15:41:14 -07002383where <code>M</code> is a <a href="#Map_types">map type</a>:
Rob Pike4501d342009-02-19 17:31:36 -08002384</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002385<ul>
Robert Griesemer29f1ca52010-03-23 14:01:51 -07002386 <li><code>x</code>'s type must be
Robert Griesemer440cc952010-06-07 17:40:21 -07002387 <a href="#Assignability">assignable</a>
2388 to the key type of <code>M</code></li>
Robert Griesemer29f1ca52010-03-23 14:01:51 -07002389 <li>if the map contains an entry with key <code>x</code>,
2390 <code>a[x]</code> is the map value with key <code>x</code>
2391 and the type of <code>a[x]</code> is the value type of <code>M</code></li>
2392 <li>if the map does not contain such an entry,
2393 <code>a[x]</code> is the <a href="#The_zero_value">zero value</a>
2394 for the value type of <code>M</code></li>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002395</ul>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002396
Robert Griesemerc2d55862009-02-19 16:49:10 -08002397<p>
Robert Griesemer29f1ca52010-03-23 14:01:51 -07002398Otherwise <code>a[x]</code> is illegal.
Rob Pikedf3183f2009-02-26 16:37:23 -08002399</p>
Robert Griesemer7abfcd92008-10-07 17:14:30 -07002400
Rob Pikedf3183f2009-02-26 16:37:23 -08002401<p>
Robert Griesemer29f1ca52010-03-23 14:01:51 -07002402An index expression on a map <code>a</code> of type <code>map[K]V</code>
2403may be used in an assignment or initialization of the special form
Rob Pikedf3183f2009-02-26 16:37:23 -08002404</p>
2405
2406<pre>
Robert Griesemer29f1ca52010-03-23 14:01:51 -07002407v, ok = a[x]
2408v, ok := a[x]
2409var v, ok = a[x]
Rob Pikedf3183f2009-02-26 16:37:23 -08002410</pre>
2411
2412<p>
Robert Griesemer29f1ca52010-03-23 14:01:51 -07002413where the result of the index expression is a pair of values with types
2414<code>(V, bool)</code>. In this form, the value of <code>ok</code> is
2415<code>true</code> if the key <code>x</code> is present in the map, and
2416<code>false</code> otherwise. The value of <code>v</code> is the value
2417<code>a[x]</code> as in the single-result form.
Rob Pikedf3183f2009-02-26 16:37:23 -08002418</p>
2419
2420<p>
2421Similarly, if an assignment to a map has the special form
2422</p>
2423
2424<pre>
Robert Griesemer29f1ca52010-03-23 14:01:51 -07002425a[x] = v, ok
Rob Pikedf3183f2009-02-26 16:37:23 -08002426</pre>
2427
2428<p>
2429and boolean <code>ok</code> has the value <code>false</code>,
2430the entry for key <code>x</code> is deleted from the map; if
2431<code>ok</code> is <code>true</code>, the construct acts like
2432a regular assignment to an element of the map.
2433</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002434
Robert Griesemer29f1ca52010-03-23 14:01:51 -07002435
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002436<h3 id="Slices">Slices</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002437
Rob Pikedf3183f2009-02-26 16:37:23 -08002438<p>
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08002439For a string, array, or slice <code>a</code>, the primary expression
Rob Pikedf3183f2009-02-26 16:37:23 -08002440</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002441
Robert Griesemerc2d55862009-02-19 16:49:10 -08002442<pre>
Robert Griesemer9e5bf272010-09-07 16:32:35 -07002443a[low : high]
Robert Griesemerc2d55862009-02-19 16:49:10 -08002444</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002445
Rob Pikedf3183f2009-02-26 16:37:23 -08002446<p>
Robert Griesemer9e5bf272010-09-07 16:32:35 -07002447constructs a substring or slice. The index expressions <code>low</code> and
2448<code>high</code> select which elements appear in the result. The result has
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08002449indexes starting at 0 and length equal to
Robert Griesemer9e5bf272010-09-07 16:32:35 -07002450<code>high</code>&nbsp;-&nbsp;<code>low</code>.
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08002451After slicing the array <code>a</code>
2452</p>
2453
2454<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08002455a := [5]int{1, 2, 3, 4, 5}
2456s := a[1:4]
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08002457</pre>
2458
2459<p>
2460the slice <code>s</code> has type <code>[]int</code>, length 3, capacity 4, and elements
Rob Pikedf3183f2009-02-26 16:37:23 -08002461</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002462
Robert Griesemerc2d55862009-02-19 16:49:10 -08002463<pre>
2464s[0] == 2
2465s[1] == 3
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08002466s[2] == 4
Robert Griesemerc2d55862009-02-19 16:49:10 -08002467</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002468
Rob Pikedf3183f2009-02-26 16:37:23 -08002469<p>
Robert Griesemer9e5bf272010-09-07 16:32:35 -07002470For convenience, any of the index expressions may be omitted. A missing <code>low</code>
2471index defaults to zero; a missing <code>high</code> index defaults to the length of the
2472sliced operand:
Scott Lawrence0c1695b2010-09-07 14:30:17 -07002473</p>
2474
Robert Griesemer9e5bf272010-09-07 16:32:35 -07002475<pre>
2476a[2:] // same a[2 : len(a)]
2477a[:3] // same as a[0 : 3]
2478a[:] // same as a[0 : len(a)]
2479</pre>
2480
Scott Lawrence0c1695b2010-09-07 14:30:17 -07002481<p>
2482For arrays or strings, the indexes <code>low</code> and <code>high</code> must
2483satisfy 0 &lt;= <code>low</code> &lt;= <code>high</code> &lt;= length; for
2484slices, the upper bound is the capacity rather than the length.
Robert Griesemer19b1d352009-09-24 19:36:48 -07002485</p>
2486
Robert Griesemerc2d55862009-02-19 16:49:10 -08002487<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07002488If the sliced operand is a string or slice, the result of the slice operation
2489is a string or slice of the same type.
Robert Griesemerc423e952010-09-02 10:16:31 -07002490If the sliced operand is an array, it must be <a href="#Address_operators">addressable</a>
2491and the result of the slice operation is a slice with the same element type as the array.
Rob Pikedf3183f2009-02-26 16:37:23 -08002492</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002493
2494
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002495<h3 id="Type_assertions">Type assertions</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002496
Rob Pikedf3183f2009-02-26 16:37:23 -08002497<p>
Robert Griesemere9192752009-12-01 16:15:53 -08002498For an expression <code>x</code> of <a href="#Interface_types">interface type</a>
2499and a type <code>T</code>, the primary expression
Rob Pikedf3183f2009-02-26 16:37:23 -08002500</p>
Robert Griesemer434c6052008-11-07 13:34:37 -08002501
Robert Griesemerc2d55862009-02-19 16:49:10 -08002502<pre>
2503x.(T)
2504</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -08002505
Robert Griesemerc2d55862009-02-19 16:49:10 -08002506<p>
Robert Griesemere9192752009-12-01 16:15:53 -08002507asserts that <code>x</code> is not <code>nil</code>
Russ Coxb89a54e2009-05-20 18:16:04 -07002508and that the value stored in <code>x</code> is of type <code>T</code>.
Russ Cox5958dd62009-03-04 17:19:21 -08002509The notation <code>x.(T)</code> is called a <i>type assertion</i>.
Rob Pikedf3183f2009-02-26 16:37:23 -08002510</p>
2511<p>
Russ Cox5958dd62009-03-04 17:19:21 -08002512More precisely, if <code>T</code> is not an interface type, <code>x.(T)</code> asserts
Robert Griesemer7bc03712010-06-07 15:49:39 -07002513that the dynamic type of <code>x</code> is <a href="#Type_identity">identical</a>
2514to the type <code>T</code>.
Russ Cox5958dd62009-03-04 17:19:21 -08002515If <code>T</code> is an interface type, <code>x.(T)</code> asserts that the dynamic type
Rob Pike632a9852010-01-13 12:06:33 +11002516of <code>x</code> implements the interface <code>T</code><a href="#Interface_types">Interface types</a>).
Rob Pikedf3183f2009-02-26 16:37:23 -08002517</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002518<p>
Russ Cox5958dd62009-03-04 17:19:21 -08002519If the type assertion holds, the value of the expression is the value
Rob Pike5bb29fb2010-03-25 17:59:59 -07002520stored in <code>x</code> and its type is <code>T</code>. If the type assertion is false,
2521a <a href="#Run_time_panics">run-time panic</a> occurs.
2522In other words, even though the dynamic type of <code>x</code>
Russ Cox5958dd62009-03-04 17:19:21 -08002523is known only at run-time, the type of <code>x.(T)</code> is
Rob Pikedf3183f2009-02-26 16:37:23 -08002524known to be <code>T</code> in a correct program.
2525</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002526<p>
Rob Piked5537072009-08-22 00:04:04 -07002527If a type assertion is used in an assignment or initialization of the form
Rob Pikedf3183f2009-02-26 16:37:23 -08002528</p>
Robert Griesemer434c6052008-11-07 13:34:37 -08002529
Robert Griesemerc2d55862009-02-19 16:49:10 -08002530<pre>
2531v, ok = x.(T)
2532v, ok := x.(T)
Rob Piked5537072009-08-22 00:04:04 -07002533var v, ok = x.(T)
Robert Griesemerc2d55862009-02-19 16:49:10 -08002534</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -08002535
Robert Griesemerc2d55862009-02-19 16:49:10 -08002536<p>
Russ Cox5958dd62009-03-04 17:19:21 -08002537the result of the assertion is a pair of values with types <code>(T, bool)</code>.
2538If the assertion holds, the expression returns the pair <code>(x.(T), true)</code>;
Rob Pikedf3183f2009-02-26 16:37:23 -08002539otherwise, the expression returns <code>(Z, false)</code> where <code>Z</code>
Robert Griesemer506c0082009-09-08 15:41:14 -07002540is the <a href="#The_zero_value">zero value</a> for type <code>T</code>.
Rob Pike5bb29fb2010-03-25 17:59:59 -07002541No run-time panic occurs in this case.
Russ Cox5958dd62009-03-04 17:19:21 -08002542The type assertion in this construct thus acts like a function call
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002543returning a value and a boolean indicating success. (§<a href="#Assignments">Assignments</a>)
Rob Pikedf3183f2009-02-26 16:37:23 -08002544</p>
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -07002545
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002546
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002547<h3 id="Calls">Calls</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002548
Robert Griesemerc2d55862009-02-19 16:49:10 -08002549<p>
Rob Pike96750f12009-02-27 16:47:48 -08002550Given an expression <code>f</code> of function type
2551<code>F</code>,
Rob Pikedf3183f2009-02-26 16:37:23 -08002552</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002553
Robert Griesemerc2d55862009-02-19 16:49:10 -08002554<pre>
Rob Pike96750f12009-02-27 16:47:48 -08002555f(a1, a2, ... an)
Robert Griesemerc2d55862009-02-19 16:49:10 -08002556</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002557
Robert Griesemerc2d55862009-02-19 16:49:10 -08002558<p>
Rob Pike96750f12009-02-27 16:47:48 -08002559calls <code>f</code> with arguments <code>a1, a2, ... an</code>.
Rob Pike4fe41922009-11-07 22:00:59 -08002560Except for one special case, arguments must be single-valued expressions
Robert Griesemer440cc952010-06-07 17:40:21 -07002561<a href="#Assignability">assignable</a> to the parameter types of
Rob Pikedf3183f2009-02-26 16:37:23 -08002562<code>F</code> and are evaluated before the function is called.
2563The type of the expression is the result type
2564of <code>F</code>.
2565A method invocation is similar but the method itself
2566is specified as a selector upon a value of the receiver type for
2567the method.
2568</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002569
Robert Griesemerc2d55862009-02-19 16:49:10 -08002570<pre>
Robert Griesemere9192752009-12-01 16:15:53 -08002571math.Atan2(x, y) // function call
Robert Griesemer130ac742009-12-10 16:43:01 -08002572var pt *Point
Rob Pikedf3183f2009-02-26 16:37:23 -08002573pt.Scale(3.5) // method call with receiver pt
Robert Griesemerc2d55862009-02-19 16:49:10 -08002574</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002575
Rob Pikedf3183f2009-02-26 16:37:23 -08002576<p>
Rob Pike4fe41922009-11-07 22:00:59 -08002577As a special case, if the return parameters of a function or method
Robert Griesemer440cc952010-06-07 17:40:21 -07002578<code>g</code> are equal in number and individually
2579assignable to the parameters of another function or method
Rob Pike4fe41922009-11-07 22:00:59 -08002580<code>f</code>, then the call <code>f(g(<i>parameters_of_g</i>))</code>
2581will invoke <code>f</code> after binding the return values of
2582<code>g</code> to the parameters of <code>f</code> in order. The call
2583of <code>f</code> must contain no parameters other than the call of <code>g</code>.
2584If <code>f</code> has a final <code>...</code> parameter, it is
2585assigned the return values of <code>g</code> that remain after
2586assignment of regular parameters.
2587</p>
2588
2589<pre>
2590func Split(s string, pos int) (string, string) {
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08002591 return s[0:pos], s[pos:]
Rob Pike4fe41922009-11-07 22:00:59 -08002592}
2593
2594func Join(s, t string) string {
2595 return s + t
2596}
2597
2598if Join(Split(value, len(value)/2)) != value {
Robert Griesemer7fc4e372011-02-01 12:51:10 -08002599 log.Panic("test fails")
Rob Pike4fe41922009-11-07 22:00:59 -08002600}
2601</pre>
2602
2603<p>
Robert Griesemer2a838d62011-02-08 13:31:01 -08002604A method call <code>x.m()</code> is valid if the <a href="#Method_sets">method set</a>
2605of (the type of) <code>x</code> contains <code>m</code> and the
Robert Griesemer63f01492010-05-28 14:17:30 -07002606argument list can be assigned to the parameter list of <code>m</code>.
Rob Pike678625d2009-09-15 09:54:22 -07002607If <code>x</code> is <a href="#Address_operators">addressable</a> and <code>&amp;x</code>'s method
Robert Griesemer56809d02009-05-20 11:02:48 -07002608set contains <code>m</code>, <code>x.m()</code> is shorthand
2609for <code>(&amp;x).m()</code>:
Rob Pikedf3183f2009-02-26 16:37:23 -08002610</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002611
Robert Griesemerc2d55862009-02-19 16:49:10 -08002612<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08002613var p Point
Rob Pikedf3183f2009-02-26 16:37:23 -08002614p.Scale(3.5)
Robert Griesemerc2d55862009-02-19 16:49:10 -08002615</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002616
Robert Griesemerc2d55862009-02-19 16:49:10 -08002617<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002618There is no distinct method type and there are no method literals.
Rob Pikedf3183f2009-02-26 16:37:23 -08002619</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002620
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002621<h3 id="Passing_arguments_to_..._parameters">Passing arguments to <code>...</code> parameters</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002622
Robert Griesemerc2d55862009-02-19 16:49:10 -08002623<p>
Russ Cox95625922010-06-12 11:37:13 -07002624If <code>f</code> is variadic with final parameter type <code>...T</code>,
2625then within the function the argument is equivalent to a parameter of type
2626<code>[]T</code>. At each call of <code>f</code>, the argument
2627passed to the final parameter is
2628a new slice of type <code>[]T</code> whose successive elements are
Robert Griesemerac771a82010-09-24 14:08:28 -07002629the actual arguments, which all must be <a href="#Assignability">assignable</a>
2630to the type <code>T</code>. The length of the slice is therefore the number of
2631arguments bound to the final parameter and may differ for each call site.
Rob Pikeb81065d2010-01-27 13:14:40 -08002632</p>
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002633
Rob Pikedf3183f2009-02-26 16:37:23 -08002634<p>
Rob Pikeb81065d2010-01-27 13:14:40 -08002635Given the function and call
2636</p>
2637<pre>
2638func Greeting(prefix string, who ... string)
2639Greeting("hello:", "Joe", "Anna", "Eileen")
2640</pre>
2641
2642<p>
Robert Griesemerac771a82010-09-24 14:08:28 -07002643within <code>Greeting</code>, <code>who</code> will have the value
Joe Poirierd4c8a542010-09-20 10:51:05 -07002644<code>[]string{"Joe", "Anna", "Eileen"}</code>
Rob Pikeb81065d2010-01-27 13:14:40 -08002645</p>
2646
Robert Griesemerac771a82010-09-24 14:08:28 -07002647<p>
Robert Griesemer904adfd2010-10-27 10:44:31 -07002648If the final argument is assignable to a slice type <code>[]T</code>, it may be
2649passed unchanged as the value for a <code>...T</code> parameter if the argument
2650is followed by <code>...</code>. In this case no new slice is created.
Robert Griesemerac771a82010-09-24 14:08:28 -07002651</p>
Rob Pikeb81065d2010-01-27 13:14:40 -08002652
2653<p>
Robert Griesemerac771a82010-09-24 14:08:28 -07002654Given the slice <code>s</code> and call
Rob Pikeb81065d2010-01-27 13:14:40 -08002655</p>
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002656
Robert Griesemerac771a82010-09-24 14:08:28 -07002657<pre>
2658s := []string{"James", "Jasmine"}
2659Greeting("goodbye:", s...)
2660</pre>
2661
2662<p>
2663within <code>Greeting</code>, <code>who</code> will have the same value as <code>s</code>
2664with the same underlying array.
2665</p>
2666
2667
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002668<h3 id="Operators">Operators</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002669
Rob Pikedf3183f2009-02-26 16:37:23 -08002670<p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002671Operators combine operands into expressions.
Rob Pikedf3183f2009-02-26 16:37:23 -08002672</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002673
Robert Griesemerf7ac31362009-07-10 16:06:40 -07002674<pre class="ebnf">
Russ Cox5958dd62009-03-04 17:19:21 -08002675Expression = UnaryExpr | Expression binary_op UnaryExpr .
Rob Pikedf3183f2009-02-26 16:37:23 -08002676UnaryExpr = PrimaryExpr | unary_op UnaryExpr .
Robert Griesemer57b34612008-10-10 12:45:44 -07002677
Robert Griesemerb50ed022011-02-01 12:02:49 -08002678binary_op = "||" | "&amp;&amp;" | rel_op | add_op | mul_op .
Rob Pikedf3183f2009-02-26 16:37:23 -08002679rel_op = "==" | "!=" | "&lt;" | "&lt;=" | ">" | ">=" .
2680add_op = "+" | "-" | "|" | "^" .
Robert Griesemer4ed666e2009-08-27 16:45:42 -07002681mul_op = "*" | "/" | "%" | "&lt;&lt;" | "&gt;&gt;" | "&amp;" | "&amp;^" .
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002682
Rob Pikedf3183f2009-02-26 16:37:23 -08002683unary_op = "+" | "-" | "!" | "^" | "*" | "&amp;" | "&lt;-" .
Robert Griesemerc2d55862009-02-19 16:49:10 -08002684</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002685
Robert Griesemerc2d55862009-02-19 16:49:10 -08002686<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07002687Comparisons are discussed <a href="#Comparison_operators">elsewhere</a>.
Robert Griesemer7bc03712010-06-07 15:49:39 -07002688For other binary operators, the operand types must be <a href="#Type_identity">identical</a>
Robert Griesemer19b1d352009-09-24 19:36:48 -07002689unless the operation involves channels, shifts, or untyped <a href="#Constants">constants</a>.
2690For operations involving constants only, see the section on
2691<a href="#Constant_expressions">constant expressions</a>.
Rob Pike4501d342009-02-19 17:31:36 -08002692</p>
Robert Griesemerc8e18762008-09-12 12:26:22 -07002693
Rob Pike83cbca52009-08-21 14:18:08 -07002694<p>
Robert Griesemerb691e082009-10-28 18:17:24 -07002695In a channel send, the first operand is always a channel and the second
Robert Griesemer440cc952010-06-07 17:40:21 -07002696must be a value <a href="#Assignability">assignable</a>
2697to the channel's element type.
Rob Pike83cbca52009-08-21 14:18:08 -07002698</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002699
Rob Pike83cbca52009-08-21 14:18:08 -07002700<p>
2701Except for shift operations,
Robert Griesemer19b1d352009-09-24 19:36:48 -07002702if one operand is an untyped <a href="#Constants">constant</a>
2703and the other operand is not, the constant is <a href="#Conversions">converted</a>
2704to the type of the other operand.
Rob Pike83cbca52009-08-21 14:18:08 -07002705</p>
Robert Griesemer7539c852009-07-31 18:05:07 -07002706
Rob Pike83cbca52009-08-21 14:18:08 -07002707<p>
2708The right operand in a shift operation must have unsigned integer type
Robert Griesemer19b1d352009-09-24 19:36:48 -07002709or be an untyped constant that can be converted to unsigned integer type.
Rob Pike83cbca52009-08-21 14:18:08 -07002710</p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002711
Rob Pike83cbca52009-08-21 14:18:08 -07002712<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07002713If the left operand of a non-constant shift operation is an untyped constant,
2714the type of constant is what it would be if the shift operation were replaced by
2715the left operand alone.
Rob Pike83cbca52009-08-21 14:18:08 -07002716</p>
Robert Griesemera6b546f2008-10-20 11:46:40 -07002717
Rob Pike83cbca52009-08-21 14:18:08 -07002718<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08002719var s uint = 33
Robert Griesemerb94c0d22011-01-19 23:07:21 -05002720var i = 1&lt;&lt;s // 1 has type int
2721var j = int32(1&lt;&lt;s) // 1 has type int32; j == 0
2722var u = uint64(1&lt;&lt;s) // 1 has type uint64; u == 1&lt;&lt;33
2723var f = float32(1&lt;&lt;s) // illegal: 1 has type float32, cannot shift
2724var g = float32(1&lt;&lt;33) // legal; 1&lt;&lt;33 is a constant shift operation; g == 1&lt;&lt;33
Rob Pike83cbca52009-08-21 14:18:08 -07002725</pre>
Robert Griesemer18b05c12009-01-26 09:34:19 -08002726
Robert Griesemerd36d1912009-09-18 11:58:35 -07002727<h3 id="Operator_precedence">Operator precedence</h3>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002728<p>
Russ Coxec9b0422009-07-09 16:44:13 -07002729Unary operators have the highest precedence.
2730As the <code>++</code> and <code>--</code> operators form
Rob Pikedf3183f2009-02-26 16:37:23 -08002731statements, not expressions, they fall
Russ Coxec9b0422009-07-09 16:44:13 -07002732outside the operator hierarchy.
Rob Pikedf3183f2009-02-26 16:37:23 -08002733As a consequence, statement <code>*p++</code> is the same as <code>(*p)++</code>.
2734<p>
Robert Griesemerb50ed022011-02-01 12:02:49 -08002735There are five precedence levels for binary operators.
Rob Pikedf3183f2009-02-26 16:37:23 -08002736Multiplication operators bind strongest, followed by addition
Robert Griesemerb50ed022011-02-01 12:02:49 -08002737operators, comparison operators, <code>&amp;&amp;</code> (logical and),
2738and finally <code>||</code> (logical or):
Rob Pikedf3183f2009-02-26 16:37:23 -08002739</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002740
Rob Pikeff70f092009-02-20 13:36:14 -08002741<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08002742Precedence Operator
Robert Griesemerb50ed022011-02-01 12:02:49 -08002743 5 * / % &lt;&lt; &gt;&gt; &amp; &amp;^
2744 4 + - | ^
2745 3 == != &lt; &lt;= > >=
Robert Griesemerc2d55862009-02-19 16:49:10 -08002746 2 &amp;&amp;
2747 1 ||
2748</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002749
Robert Griesemerc2d55862009-02-19 16:49:10 -08002750<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002751Binary operators of the same precedence associate from left to right.
Robert Griesemerd36d1912009-09-18 11:58:35 -07002752For instance, <code>x / y * z</code> is the same as <code>(x / y) * z</code>.
Rob Pikedf3183f2009-02-26 16:37:23 -08002753</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002754
Robert Griesemerc2d55862009-02-19 16:49:10 -08002755<pre>
2756+x
275723 + 3*x[i]
2758x &lt;= f()
Robert Griesemer4ed666e2009-08-27 16:45:42 -07002759^a &gt;&gt; b
Robert Griesemerc2d55862009-02-19 16:49:10 -08002760f() || g()
Robert Griesemerd36d1912009-09-18 11:58:35 -07002761x == y+1 &amp;&amp; &lt;-chan_ptr > 0
Robert Griesemerc2d55862009-02-19 16:49:10 -08002762</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002763
2764
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002765<h3 id="Arithmetic_operators">Arithmetic operators</h3>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002766<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07002767Arithmetic operators apply to numeric values and yield a result of the same
Rob Pikedf3183f2009-02-26 16:37:23 -08002768type as the first operand. The four standard arithmetic operators (<code>+</code>,
Rob Pike72970872010-03-04 12:35:16 -08002769<code>-</code>, <code>*</code>, <code>/</code>) apply to integer,
2770floating-point, and complex types; <code>+</code> also applies
Robert Griesemer19b1d352009-09-24 19:36:48 -07002771to strings. All other arithmetic operators apply to integers only.
Rob Pikedf3183f2009-02-26 16:37:23 -08002772</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002773
Rob Pikeff70f092009-02-20 13:36:14 -08002774<pre class="grammar">
Rob Pike72970872010-03-04 12:35:16 -08002775+ sum integers, floats, complex values, strings
2776- difference integers, floats, complex values
2777* product integers, floats, complex values
2778/ quotient integers, floats, complex values
Rob Pike307ec212009-03-12 15:53:56 -07002779% remainder integers
Robert Griesemerc2d55862009-02-19 16:49:10 -08002780
Rob Pike307ec212009-03-12 15:53:56 -07002781&amp; bitwise and integers
2782| bitwise or integers
2783^ bitwise xor integers
2784&amp;^ bit clear (and not) integers
Robert Griesemerc2d55862009-02-19 16:49:10 -08002785
Robert Griesemer4ed666e2009-08-27 16:45:42 -07002786&lt;&lt; left shift integer &lt;&lt; unsigned integer
2787&gt;&gt; right shift integer &gt;&gt; unsigned integer
Robert Griesemerc2d55862009-02-19 16:49:10 -08002788</pre>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002789
Rob Pikedf3183f2009-02-26 16:37:23 -08002790<p>
2791Strings can be concatenated using the <code>+</code> operator
2792or the <code>+=</code> assignment operator:
2793</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002794
Robert Griesemerc2d55862009-02-19 16:49:10 -08002795<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08002796s := "hi" + string(c)
2797s += " and good bye"
Robert Griesemerc2d55862009-02-19 16:49:10 -08002798</pre>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002799
Robert Griesemerc2d55862009-02-19 16:49:10 -08002800<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002801String addition creates a new string by concatenating the operands.
2802</p>
2803<p>
2804For integer values, <code>/</code> and <code>%</code> satisfy the following relationship:
2805</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002806
Robert Griesemerc2d55862009-02-19 16:49:10 -08002807<pre>
2808(a / b) * b + a % b == a
2809</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002810
Rob Pikedf3183f2009-02-26 16:37:23 -08002811<p>
2812with <code>(a / b)</code> truncated towards zero.
Rob Pikedf3183f2009-02-26 16:37:23 -08002813</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002814
Robert Griesemerc2d55862009-02-19 16:49:10 -08002815<pre>
2816 x y x / y x % y
2817 5 3 1 2
2818-5 3 -1 -2
2819 5 -3 -1 2
2820-5 -3 1 -2
2821</pre>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002822
Rob Pikedf3183f2009-02-26 16:37:23 -08002823<p>
Robert Griesemerdf674ff2010-05-04 17:31:40 -07002824If the divisor is zero, a <a href="#Run_time_panics">run-time panic</a> occurs.
Rob Pikedf3183f2009-02-26 16:37:23 -08002825If the dividend is positive and the divisor is a constant power of 2,
Rob Pikef3a33bc2009-09-14 17:39:17 -07002826the division may be replaced by a right shift, and computing the remainder may
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002827be replaced by a bitwise "and" operation:
Rob Pikedf3183f2009-02-26 16:37:23 -08002828</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002829
Robert Griesemerc2d55862009-02-19 16:49:10 -08002830<pre>
Robert Griesemer4ed666e2009-08-27 16:45:42 -07002831 x x / 4 x % 4 x &gt;&gt; 2 x &amp; 3
Robert Griesemerc2d55862009-02-19 16:49:10 -08002832 11 2 3 2 3
2833-11 -2 -3 -3 1
2834</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002835
Rob Pikedf3183f2009-02-26 16:37:23 -08002836<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002837The shift operators shift the left operand by the shift count specified by the
2838right operand. They implement arithmetic shifts if the left operand is a signed
Rob Pikedf3183f2009-02-26 16:37:23 -08002839integer and logical shifts if it is an unsigned integer. The shift count must
2840be an unsigned integer. There is no upper limit on the shift count. Shifts behave
2841as if the left operand is shifted <code>n</code> times by 1 for a shift
2842count of <code>n</code>.
Robert Griesemer4ed666e2009-08-27 16:45:42 -07002843As a result, <code>x &lt;&lt; 1</code> is the same as <code>x*2</code>
2844and <code>x &gt;&gt; 1</code> is the same as
Robert Griesemere9192752009-12-01 16:15:53 -08002845<code>x/2</code> but truncated towards negative infinity.
Rob Pikedf3183f2009-02-26 16:37:23 -08002846</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002847
Rob Pikedf3183f2009-02-26 16:37:23 -08002848<p>
2849For integer operands, the unary operators
2850<code>+</code>, <code>-</code>, and <code>^</code> are defined as
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002851follows:
Rob Pikedf3183f2009-02-26 16:37:23 -08002852</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002853
Rob Pikeff70f092009-02-20 13:36:14 -08002854<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08002855+x is 0 + x
2856-x negation is 0 - x
Russ Coxd83dc4f2009-05-29 16:04:16 -07002857^x bitwise complement is m ^ x with m = "all bits set to 1" for unsigned x
2858 and m = -1 for signed x
Robert Griesemerc2d55862009-02-19 16:49:10 -08002859</pre>
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002860
Russ Cox5958dd62009-03-04 17:19:21 -08002861<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07002862For floating-point numbers,
Russ Cox5958dd62009-03-04 17:19:21 -08002863<code>+x</code> is the same as <code>x</code>,
2864while <code>-x</code> is the negation of <code>x</code>.
Robert Griesemerdf674ff2010-05-04 17:31:40 -07002865The result of a floating-point division by zero is not specified beyond the
2866IEEE-754 standard; whether a <a href="#Run_time_panics">run-time panic</a>
2867occurs is implementation-specific.
Russ Cox5958dd62009-03-04 17:19:21 -08002868</p>
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002869
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002870<h3 id="Integer_overflow">Integer overflow</h3>
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002871
Rob Pikedf3183f2009-02-26 16:37:23 -08002872<p>
2873For unsigned integer values, the operations <code>+</code>,
2874<code>-</code>, <code>*</code>, and <code>&lt;&lt;</code> are
2875computed modulo 2<sup><i>n</i></sup>, where <i>n</i> is the bit width of
2876the unsigned integer's type
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002877<a href="#Numeric_types">Numeric types</a>). Loosely speaking, these unsigned integer operations
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002878discard high bits upon overflow, and programs may rely on ``wrap around''.
Rob Pikedf3183f2009-02-26 16:37:23 -08002879</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002880<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002881For signed integers, the operations <code>+</code>,
2882<code>-</code>, <code>*</code>, and <code>&lt;&lt;</code> may legally
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002883overflow and the resulting value exists and is deterministically defined
2884by the signed integer representation, the operation, and its operands.
Rob Pikedf3183f2009-02-26 16:37:23 -08002885No exception is raised as a result of overflow. A
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002886compiler may not optimize code under the assumption that overflow does
Rob Pikedf3183f2009-02-26 16:37:23 -08002887not occur. For instance, it may not assume that <code>x &lt; x + 1</code> is always true.
2888</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002889
2890
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002891<h3 id="Comparison_operators">Comparison operators</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002892
Rob Pike5af7de32009-02-24 15:17:59 -08002893<p>
Robert Griesemer1d282a82010-06-03 16:55:50 -07002894Comparison operators compare two operands and yield a value of type <code>bool</code>.
Rob Pike5af7de32009-02-24 15:17:59 -08002895</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002896
Rob Pikeff70f092009-02-20 13:36:14 -08002897<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08002898== equal
2899!= not equal
Anthony Martin0122a662011-02-08 14:51:15 -08002900&lt; less
2901&lt;= less or equal
Robert Griesemerc2d55862009-02-19 16:49:10 -08002902> greater
2903>= greater or equal
2904</pre>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002905
Rob Pike5af7de32009-02-24 15:17:59 -08002906<p>
Robert Griesemer1d282a82010-06-03 16:55:50 -07002907The operands must be <i>comparable</i>; that is, the first operand
Robert Griesemer440cc952010-06-07 17:40:21 -07002908must be <a href="#Assignability">assignable</a>
2909to the type of the second operand, or vice versa.
Rob Pike5af7de32009-02-24 15:17:59 -08002910</p>
Robert Griesemer7bc03712010-06-07 15:49:39 -07002911<p>
Robert Griesemer1d282a82010-06-03 16:55:50 -07002912The operators <code>==</code> and <code>!=</code> apply
2913to operands of all types except arrays and structs.
2914All other comparison operators apply only to integer, floating-point
2915and string values. The result of a comparison is defined as follows:
Rob Pike5af7de32009-02-24 15:17:59 -08002916</p>
Robert Griesemer1d282a82010-06-03 16:55:50 -07002917
2918<ul>
2919 <li>
2920 Integer values are compared in the usual way.
2921 </li>
2922 <li>
2923 Floating point values are compared as defined by the IEEE-754
2924 standard.
2925 </li>
2926 <li>
2927 Two complex values <code>u</code>, <code>v</code> are
2928 equal if both <code>real(u) == real(v)</code> and
2929 <code>imag(u) == imag(v)</code>.
2930 </li>
2931 <li>
2932 String values are compared byte-wise (lexically).
2933 </li>
2934 <li>
Peter Mundy5928e1d2010-11-09 10:10:57 -08002935 Boolean values are equal if they are either both
Robert Griesemer1d282a82010-06-03 16:55:50 -07002936 <code>true</code> or both <code>false</code>.
2937 </li>
2938 <li>
2939 Pointer values are equal if they point to the same location
2940 or if both are <code>nil</code>.
2941 </li>
2942 <li>
2943 Function values are equal if they refer to the same function
2944 or if both are <code>nil</code>.
2945 </li>
2946 <li>
2947 A slice value may only be compared to <code>nil</code>.
2948 </li>
2949 <li>
2950 Channel and map values are equal if they were created by the same call to <code>make</code>
2951<a href="#Making_slices_maps_and_channels">Making slices, maps, and channels</a>)
2952 or if both are <code>nil</code>.
2953 </li>
2954 <li>
Robert Griesemer7bc03712010-06-07 15:49:39 -07002955 Interface values are equal if they have <a href="#Type_identity">identical</a> dynamic types and
Robert Griesemer1d282a82010-06-03 16:55:50 -07002956 equal dynamic values or if both are <code>nil</code>.
2957 </li>
2958 <li>
2959 An interface value <code>x</code> is equal to a non-interface value
2960 <code>y</code> if the dynamic type of <code>x</code> is identical to
2961 the static type of <code>y</code> and the dynamic value of <code>x</code>
2962 is equal to <code>y</code>.
2963 </li>
2964 <li>
2965 A pointer, function, slice, channel, map, or interface value is equal
2966 to <code>nil</code> if it has been assigned the explicit value
2967 <code>nil</code>, if it is uninitialized, or if it has been assigned
2968 another value equal to <code>nil</code>.
2969 </li>
2970</ul>
Robert Griesemera3294712009-01-05 11:17:26 -08002971
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002972
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002973<h3 id="Logical_operators">Logical operators</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002974
Rob Pikedf3183f2009-02-26 16:37:23 -08002975<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07002976Logical operators apply to <a href="#Boolean_types">boolean</a> values
2977and yield a result of the same type as the operands.
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002978The right operand is evaluated conditionally.
Rob Pikedf3183f2009-02-26 16:37:23 -08002979</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002980
Rob Pikeff70f092009-02-20 13:36:14 -08002981<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08002982&amp;&amp; conditional and p &amp;&amp; q is "if p then q else false"
2983|| conditional or p || q is "if p then true else q"
2984! not !p is "not p"
2985</pre>
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -07002986
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002987
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002988<h3 id="Address_operators">Address operators</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002989
Rob Pikedf3183f2009-02-26 16:37:23 -08002990<p>
Robert Griesemer0e1d9412011-01-26 11:21:23 -08002991For an operand <code>x</code> of type <code>T</code>, the address operation
2992<code>&amp;x</code> generates a pointer of type <code>*T</code> to <code>x</code>.
2993The operand must be <i>addressable</i>,
Russ Coxe7561de2010-05-24 14:31:43 -07002994that is, either a variable, pointer indirection, or slice indexing
Robert Griesemer0e1d9412011-01-26 11:21:23 -08002995operation; or a field selector of an addressable struct operand;
Russ Coxe7561de2010-05-24 14:31:43 -07002996or an array indexing operation of an addressable array.
Robert Griesemer0e1d9412011-01-26 11:21:23 -08002997As an exception to the addressability requirement, <code>x</code> may also be a
2998<a href="#Composite_literals">composite literal</a>.
2999</p>
3000<p>
3001For an operand <code>x</code> of pointer type <code>*T</code>, the pointer
3002indirection <code>*x</code> denotes the value of type <code>T</code> pointed
3003to by <code>x</code>.
Rob Pikeafee1c52009-03-20 17:41:25 -07003004</p>
3005
3006<pre>
3007&amp;x
3008&amp;a[f(2)]
3009*p
3010*pf(x)
3011</pre>
3012
Robert Griesemerb50ed022011-02-01 12:02:49 -08003013
3014<h3 id="Receive_operator">Receive operator</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003015
Robert Griesemerc2d55862009-02-19 16:49:10 -08003016<p>
Robert Griesemerb50ed022011-02-01 12:02:49 -08003017For an operand <code>ch</code> of <a href="#Channel_types">channel type</a>,
3018the value of the receive operation <code>&lt;-ch</code> is the value received
3019from the channel <code>ch</code>. The type of the value is the element type of
3020the channel. The expression blocks until a value is available.
Rob Pikedf3183f2009-02-26 16:37:23 -08003021</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003022
Robert Griesemerc2d55862009-02-19 16:49:10 -08003023<pre>
Robert Griesemere1e76192009-09-25 14:11:03 -07003024v1 := &lt;-ch
3025v2 = &lt;-ch
3026f(&lt;-ch)
Robert Griesemerb50ed022011-02-01 12:02:49 -08003027&lt;-strobe // wait until clock pulse and discard received value
Robert Griesemerc2d55862009-02-19 16:49:10 -08003028</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003029
Russ Cox19d9a402011-01-27 15:34:28 -05003030<!--
3031 TODO(rsc): Add after a release or two without any x,ok := <-c.
3032
Rob Pikedf3183f2009-02-26 16:37:23 -08003033<p>
Russ Cox19d9a402011-01-27 15:34:28 -05003034A receive expression used in an assignment or initialization of the form
Rob Pikedf3183f2009-02-26 16:37:23 -08003035</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003036
Robert Griesemerc2d55862009-02-19 16:49:10 -08003037<pre>
Robert Griesemere1e76192009-09-25 14:11:03 -07003038x, ok = &lt;-ch
3039x, ok := &lt;-ch
3040var x, ok = &lt;-ch
Robert Griesemerc2d55862009-02-19 16:49:10 -08003041</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003042
Rob Pikedf3183f2009-02-26 16:37:23 -08003043<p>
Russ Cox19d9a402011-01-27 15:34:28 -05003044yields an additional result.
3045The boolean variable <code>ok</code> indicates whether
3046the received value was sent on the channel (<code>true</code>)
3047or is a <a href="#The_zero_value">zero value</a> returned
3048because the channel is closed and empty (<code>false</code>).
Rob Pikedf3183f2009-02-26 16:37:23 -08003049</p>
Russ Cox19d9a402011-01-27 15:34:28 -05003050-->
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003051
Rob Pike041d1162010-07-13 16:23:54 -07003052<p>
Robert Griesemerb50ed022011-02-01 12:02:49 -08003053Receiving from a <code>nil</code> channel causes a
Rob Pike041d1162010-07-13 16:23:54 -07003054<a href="#Run_time_panics">run-time panic</a>.
3055</p>
3056
Rob Pike0b4de7a2009-11-09 16:09:57 -08003057<!---
Rob Pikedf3183f2009-02-26 16:37:23 -08003058<p>
Robert Griesemer90cc4a52009-10-22 09:41:38 -07003059<span class="alert">TODO: Probably in a separate section, communication semantics
3060need to be presented regarding send, receive, select, and goroutines.</span>
Rob Pikedf3183f2009-02-26 16:37:23 -08003061</p>
Rob Pike0b4de7a2009-11-09 16:09:57 -08003062--->
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003063
Robert Griesemerb50ed022011-02-01 12:02:49 -08003064
Rob Pike01cadde2009-09-15 15:56:44 -07003065<h3 id="Method_expressions">Method expressions</h3>
3066
3067<p>
Robert Griesemer2a838d62011-02-08 13:31:01 -08003068If <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 -07003069<code>T.M</code> is a function that is callable as a regular function
3070with the same arguments as <code>M</code> prefixed by an additional
3071argument that is the receiver of the method.
3072</p>
3073
Robert Griesemerda961882009-09-17 11:01:50 -07003074<pre class="ebnf">
Rob Pike01cadde2009-09-15 15:56:44 -07003075MethodExpr = ReceiverType "." MethodName .
3076ReceiverType = TypeName | "(" "*" TypeName ")" .
Rob Pike01cadde2009-09-15 15:56:44 -07003077</pre>
3078
3079<p>
3080Consider a struct type <code>T</code> with two methods,
3081<code>Mv</code>, whose receiver is of type <code>T</code>, and
3082<code>Mp</code>, whose receiver is of type <code>*T</code>.
3083</p>
3084
3085<pre>
3086type T struct {
Robert Griesemer130ac742009-12-10 16:43:01 -08003087 a int
Rob Pike01cadde2009-09-15 15:56:44 -07003088}
Robert Griesemerb94c0d22011-01-19 23:07:21 -05003089func (tv T) Mv(a int) int { return 0 } // value receiver
3090func (tp *T) Mp(f float32) float32 { return 1 } // pointer receiver
Robert Griesemer130ac742009-12-10 16:43:01 -08003091var t T
Rob Pike01cadde2009-09-15 15:56:44 -07003092</pre>
3093
3094<p>
3095The expression
3096</p>
3097
3098<pre>
3099T.Mv
3100</pre>
3101
3102<p>
3103yields a function equivalent to <code>Mv</code> but
3104with an explicit receiver as its first argument; it has signature
3105</p>
3106
3107<pre>
Russ Cox46871692010-01-26 10:25:56 -08003108func(tv T, a int) int
Rob Pike01cadde2009-09-15 15:56:44 -07003109</pre>
3110
3111<p>
3112That function may be called normally with an explicit receiver, so
3113these three invocations are equivalent:
3114</p>
3115
3116<pre>
3117t.Mv(7)
3118T.Mv(t, 7)
3119f := T.Mv; f(t, 7)
3120</pre>
3121
3122<p>
3123Similarly, the expression
3124</p>
3125
3126<pre>
3127(*T).Mp
3128</pre>
3129
3130<p>
3131yields a function value representing <code>Mp</code> with signature
3132</p>
3133
3134<pre>
Robert Griesemerb94c0d22011-01-19 23:07:21 -05003135func(tp *T, f float32) float32
Rob Pike01cadde2009-09-15 15:56:44 -07003136</pre>
3137
3138<p>
3139For a method with a value receiver, one can derive a function
3140with an explicit pointer receiver, so
3141</p>
3142
3143<pre>
3144(*T).Mv
3145</pre>
3146
3147<p>
3148yields a function value representing <code>Mv</code> with signature
3149</p>
3150
3151<pre>
Russ Cox46871692010-01-26 10:25:56 -08003152func(tv *T, a int) int
Rob Pike01cadde2009-09-15 15:56:44 -07003153</pre>
3154
3155<p>
3156Such a function indirects through the receiver to create a value
3157to pass as the receiver to the underlying method;
3158the method does not overwrite the value whose address is passed in
3159the function call.
3160</p>
3161
3162<p>
3163The final case, a value-receiver function for a pointer-receiver method,
3164is illegal because pointer-receiver methods are not in the method set
3165of the value type.
3166</p>
3167
3168<p>
3169Function values derived from methods are called with function call syntax;
3170the receiver is provided as the first argument to the call.
3171That is, given <code>f := T.Mv</code>, <code>f</code> is invoked
3172as <code>f(t, 7)</code> not <code>t.f(7)</code>.
3173To construct a function that binds the receiver, use a
Robert Griesemerd36d1912009-09-18 11:58:35 -07003174<a href="#Function_literals">closure</a>.
Rob Pike01cadde2009-09-15 15:56:44 -07003175</p>
3176
3177<p>
3178It is legal to derive a function value from a method of an interface type.
3179The resulting function takes an explicit receiver of that interface type.
3180</p>
3181
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003182<h3 id="Conversions">Conversions</h3>
3183
3184<p>
3185Conversions are expressions of the form <code>T(x)</code>
3186where <code>T</code> is a type and <code>x</code> is an expression
3187that can be converted to type <code>T</code>.
3188</p>
3189
3190<pre class="ebnf">
Robert Griesemer934a5202010-05-24 14:58:26 -07003191Conversion = Type "(" Expression ")" .
3192</pre>
3193
3194<p>
3195If the type starts with an operator it must be parenthesized:
3196</p>
3197
3198<pre>
3199*Point(p) // same as *(Point(p))
3200(*Point)(p) // p is converted to (*Point)
3201&lt;-chan int(c) // same as &lt;-(chan int(c))
3202(&lt;-chan int)(c) // c is converted to (&lt;-chan int)
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003203</pre>
3204
3205<p>
Robert Griesemer7bc03712010-06-07 15:49:39 -07003206A value <code>x</code> can be converted to type <code>T</code> in any
3207of these cases:
Robert Griesemer63f01492010-05-28 14:17:30 -07003208</p>
Robert Griesemer7bc03712010-06-07 15:49:39 -07003209
3210<ul>
3211 <li>
Robert Griesemer440cc952010-06-07 17:40:21 -07003212 <code>x</code> is <a href="#Assignability">assignable</a>
Robert Griesemer7bc03712010-06-07 15:49:39 -07003213 to <code>T</code>.
3214 </li>
3215 <li>
3216 <code>x</code>'s type and <code>T</code> have identical
3217 <a href="#Types">underlying types</a>.
3218 </li>
3219 <li>
3220 <code>x</code>'s type and <code>T</code> are unnamed pointer types
3221 and their pointer base types have identical underlying types.
3222 </li>
3223 <li>
3224 <code>x</code>'s type and <code>T</code> are both integer or floating
3225 point types.
3226 </li>
3227 <li>
3228 <code>x</code>'s type and <code>T</code> are both complex types.
3229 </li>
3230 <li>
3231 <code>x</code> is an integer or has type <code>[]byte</code> or
3232 <code>[]int</code> and <code>T</code> is a string type.
3233 </li>
3234 <li>
3235 <code>x</code> is a string and <code>T</code> is <code>[]byte</code> or
3236 <code>[]int</code>.
3237 </li>
3238</ul>
3239
3240<p>
3241Specific rules apply to conversions between numeric types or to and from
3242a string type.
3243These conversions may change the representation of <code>x</code>
3244and incur a run-time cost.
3245All other conversions only change the type but not the representation
3246of <code>x</code>.
3247</p>
3248
3249<h4>Conversions between numeric types</h4>
Robert Griesemer63f01492010-05-28 14:17:30 -07003250<ol>
3251<li>
Robert Griesemer7bc03712010-06-07 15:49:39 -07003252When converting between integer types, if the value is a signed integer, it is
3253sign extended to implicit infinite precision; otherwise it is zero extended.
3254It is then truncated to fit in the result type's size.
3255For example, if <code>v := uint16(0x10F0)</code>, then <code>uint32(int8(v)) == 0xFFFFFFF0</code>.
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003256The conversion always yields a valid value; there is no indication of overflow.
Robert Griesemer7bc03712010-06-07 15:49:39 -07003257</li>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003258<li>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003259When converting a floating-point number to an integer, the fraction is discarded
3260(truncation towards zero).
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003261</li>
3262<li>
Robert Griesemer7bc03712010-06-07 15:49:39 -07003263When converting an integer or floating-point number to a floating-point type,
3264or a complex number to another complex type, the result value is rounded
Rob Pike72970872010-03-04 12:35:16 -08003265to the precision specified by the destination type.
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003266For instance, the value of a variable <code>x</code> of type <code>float32</code>
3267may be stored using additional precision beyond that of an IEEE-754 32-bit number,
3268but float32(x) represents the result of rounding <code>x</code>'s value to
326932-bit precision. Similarly, <code>x + 0.1</code> may use more than 32 bits
Evan Shawcb4e9f82010-05-23 11:21:47 -07003270of precision, but <code>float32(x + 0.1)</code> does not.
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003271</li>
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003272</ol>
3273
3274<p>
Rob Pike72970872010-03-04 12:35:16 -08003275In all conversions involving floating-point or complex values,
3276if the result type cannot represent the value the conversion
3277succeeds but the result value is
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003278implementation-dependent.
3279</p>
3280
Rob Pike1811fac2010-02-17 11:26:09 +11003281<h4>Conversions to and from a string type</h4>
3282
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003283<ol>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003284<li>
Rob Pike1811fac2010-02-17 11:26:09 +11003285Converting a signed or unsigned integer value to a string type yields a
Robert Griesemer7bc03712010-06-07 15:49:39 -07003286string containing the UTF-8 representation of the integer. Values outside
3287the range of valid Unicode code points are converted to <code>"\uFFFD"</code>.
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003288
3289<pre>
Robert Griesemer934a5202010-05-24 14:58:26 -07003290string('a') // "a"
3291string(-1) // "\ufffd" == "\xef\xbf\xbd "
3292string(0xf8) // "\u00f8" == "ø" == "\xc3\xb8"
Rob Pike1811fac2010-02-17 11:26:09 +11003293type MyString string
Robert Griesemer934a5202010-05-24 14:58:26 -07003294MyString(0x65e5) // "\u65e5" == "日" == "\xe6\x97\xa5"
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003295</pre>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003296</li>
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003297
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003298<li>
Rob Pike1811fac2010-02-17 11:26:09 +11003299Converting a value of type <code>[]byte</code> (or
3300the equivalent <code>[]uint8</code>) to a string type yields a
3301string whose successive bytes are the elements of the slice. If
3302the slice value is <code>nil</code>, the result is the empty string.
3303
3304<pre>
3305string([]byte{'h', 'e', 'l', 'l', '\xc3', '\xb8'}) // "hellø"
3306</pre>
3307</li>
3308
3309<li>
3310Converting a value of type <code>[]int</code> to a string type yields
3311a string that is the concatenation of the individual integers
3312converted to strings. If the slice value is <code>nil</code>, the
3313result is the empty string.
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003314<pre>
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003315string([]int{0x767d, 0x9d6c, 0x7fd4}) // "\u767d\u9d6c\u7fd4" == "白鵬翔"
3316</pre>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003317</li>
3318
3319<li>
Rob Pike1811fac2010-02-17 11:26:09 +11003320Converting a value of a string type to <code>[]byte</code> (or <code>[]uint8</code>)
3321yields a slice whose successive elements are the bytes of the string.
3322If the string is empty, the result is <code>[]byte(nil)</code>.
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003323
3324<pre>
Rob Pike1811fac2010-02-17 11:26:09 +11003325[]byte("hellø") // []byte{'h', 'e', 'l', 'l', '\xc3', '\xb8'}
3326</pre>
3327</li>
3328
3329<li>
3330Converting a value of a string type to <code>[]int</code> yields a
3331slice containing the individual Unicode code points of the string.
3332If the string is empty, the result is <code>[]int(nil)</code>.
3333<pre>
3334[]int(MyString("白鵬翔")) // []int{0x767d, 0x9d6c, 0x7fd4}
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003335</pre>
3336</li>
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003337</ol>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003338
3339<p>
3340There is no linguistic mechanism to convert between pointers and integers.
3341The package <a href="#Package_unsafe"><code>unsafe</code></a>
3342implements this functionality under
3343restricted circumstances.
3344</p>
3345
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003346<h3 id="Constant_expressions">Constant expressions</h3>
Robert Griesemerb90b2132008-09-19 15:49:55 -07003347
Rob Pikef27e9f02009-02-23 19:22:05 -08003348<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07003349Constant expressions may contain only <a href="#Constants">constant</a>
3350operands and are evaluated at compile-time.
Rob Pikef27e9f02009-02-23 19:22:05 -08003351</p>
3352
3353<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07003354Untyped boolean, numeric, and string constants may be used as operands
3355wherever it is legal to use an operand of boolean, numeric, or string type,
3356respectively. Except for shift operations, if the operands of a binary operation
3357are an untyped integer constant and an untyped floating-point constant,
3358the integer constant is converted to an untyped floating-point constant
3359(relevant for <code>/</code> and <code>%</code>).
Rob Pike72970872010-03-04 12:35:16 -08003360Similarly,
3361untyped integer or floating-point constants may be used as operands
3362wherever it is legal to use an operand of complex type;
3363the integer or floating point constant is converted to a
3364complex constant with a zero imaginary part.
Robert Griesemer19b1d352009-09-24 19:36:48 -07003365</p>
Robert Griesemer997851e2009-09-25 15:36:25 -07003366
3367<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07003368Applying an operator to untyped constants results in an untyped
Rob Pike72970872010-03-04 12:35:16 -08003369constant of the same kind (that is, a boolean, integer, floating-point,
3370complex, or string constant), except for
3371<a href="#Comparison_operators">comparison operators</a>, which result in
Robert Griesemer19b1d352009-09-24 19:36:48 -07003372a constant of type <code>bool</code>.
3373</p>
3374
3375<p>
Rob Pike72970872010-03-04 12:35:16 -08003376Imaginary literals are untyped complex constants (with zero real part)
3377and may be combined in binary
3378operations with untyped integer and floating-point constants; the
3379result is an untyped complex constant.
3380Complex constants are always constructed from
3381constant expressions involving imaginary
Robert Griesemeref4c2b82010-03-10 15:29:36 -08003382literals or constants derived from them, or calls of the built-in function
Robert Griesemerb94c0d22011-01-19 23:07:21 -05003383<a href="#Complex_numbers"><code>complex</code></a>.
Rob Pike72970872010-03-04 12:35:16 -08003384</p>
3385
3386<pre>
3387const Σ = 1 - 0.707i
Robert Griesemeref4c2b82010-03-10 15:29:36 -08003388const Δ = Σ + 2.0e-4 - 1/1i
Rob Pike72970872010-03-04 12:35:16 -08003389const Φ = iota * 1i
Robert Griesemerb94c0d22011-01-19 23:07:21 -05003390const iΓ = complex(0, Γ)
Rob Pike72970872010-03-04 12:35:16 -08003391</pre>
3392
3393<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07003394Constant expressions are always evaluated exactly; intermediate values and the
3395constants themselves may require precision significantly larger than supported
3396by any predeclared type in the language. The following are legal declarations:
Rob Pikef27e9f02009-02-23 19:22:05 -08003397</p>
3398
3399<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08003400const Huge = 1 &lt;&lt; 100
3401const Four int8 = Huge &gt;&gt; 98
Rob Pikef27e9f02009-02-23 19:22:05 -08003402</pre>
3403
3404<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07003405The values of <i>typed</i> constants must always be accurately representable as values
3406of the constant type. The following constant expressions are illegal:
Rob Pike21d03492009-03-24 19:16:42 -07003407</p>
3408
3409<pre>
Robert Griesemere9192752009-12-01 16:15:53 -08003410uint(-1) // -1 cannot be represented as a uint
3411int(3.14) // 3.14 cannot be represented as an int
3412int64(Huge) // 1&lt;&lt;100 cannot be represented as an int64
3413Four * 300 // 300 cannot be represented as an int8
3414Four * 100 // 400 cannot be represented as an int8
Rob Pike21d03492009-03-24 19:16:42 -07003415</pre>
3416
3417<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07003418The mask used by the unary bitwise complement operator <code>^</code> matches
Rob Pike678625d2009-09-15 09:54:22 -07003419the rule for non-constants: the mask is all 1s for unsigned constants
Robert Griesemer19b1d352009-09-24 19:36:48 -07003420and -1 for signed and untyped constants.
Rob Pike21d03492009-03-24 19:16:42 -07003421</p>
3422
3423<pre>
Robert Griesemer19b1d352009-09-24 19:36:48 -07003424^1 // untyped integer constant, equal to -2
Rob Pike21d03492009-03-24 19:16:42 -07003425uint8(^1) // error, same as uint8(-2), out of range
3426^uint8(1) // typed uint8 constant, same as 0xFF ^ uint8(1) = uint8(0xFE)
3427int8(^1) // same as int8(-2)
Russ Coxd83dc4f2009-05-29 16:04:16 -07003428^int8(1) // same as -1 ^ int8(1) = -2
Rob Pike21d03492009-03-24 19:16:42 -07003429</pre>
3430
Rob Pike0b4de7a2009-11-09 16:09:57 -08003431<!---
Rob Pike21d03492009-03-24 19:16:42 -07003432<p>
Robert Griesemer90cc4a52009-10-22 09:41:38 -07003433<span class="alert">
Rob Pike21d03492009-03-24 19:16:42 -07003434TODO: perhaps ^ should be disallowed on non-uints instead of assuming twos complement.
3435Also it may be possible to make typed constants more like variables, at the cost of fewer
3436overflow etc. errors being caught.
Robert Griesemer90cc4a52009-10-22 09:41:38 -07003437</span>
Rob Pike21d03492009-03-24 19:16:42 -07003438</p>
Rob Pike0b4de7a2009-11-09 16:09:57 -08003439--->
Robert Griesemer19b1d352009-09-24 19:36:48 -07003440
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003441<h3 id="Order_of_evaluation">Order of evaluation</h3>
Rob Pikec956e902009-04-14 20:10:49 -07003442
3443<p>
3444When evaluating the elements of an assignment or expression,
3445all function calls, method calls and
3446communication operations are evaluated in lexical left-to-right
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003447order.
3448</p>
3449
3450<p>
Robert Griesemer4f185492009-05-01 17:00:16 -07003451For example, in the assignment
Rob Pikec956e902009-04-14 20:10:49 -07003452</p>
3453<pre>
Robert Griesemere1e76192009-09-25 14:11:03 -07003454y[f()], ok = g(h(), i() + x[j()], &lt;-c), k()
Rob Pikec956e902009-04-14 20:10:49 -07003455</pre>
3456<p>
Robert Griesemer4f185492009-05-01 17:00:16 -07003457the function calls and communication happen in the order
3458<code>f()</code>, <code>h()</code>, <code>i()</code>, <code>j()</code>,
Robert Griesemere1e76192009-09-25 14:11:03 -07003459<code>&lt;-c</code>, <code>g()</code>, and <code>k()</code>.
Robert Griesemer4f185492009-05-01 17:00:16 -07003460However, the order of those events compared to the evaluation
3461and indexing of <code>x</code> and the evaluation
3462of <code>y</code> is not specified.
Rob Pikec956e902009-04-14 20:10:49 -07003463</p>
3464
Rob Pike4fe41922009-11-07 22:00:59 -08003465<p>
3466Floating-point operations within a single expression are evaluated according to
3467the associativity of the operators. Explicit parentheses affect the evaluation
3468by overriding the default associativity.
3469In the expression <code>x + (y + z)</code> the addition <code>y + z</code>
3470is performed before adding <code>x</code>.
3471</p>
3472
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003473<h2 id="Statements">Statements</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003474
Rob Pike96750f12009-02-27 16:47:48 -08003475<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003476Statements control execution.
Rob Pike96750f12009-02-27 16:47:48 -08003477</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003478
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003479<pre class="ebnf">
Robert Griesemerdea43942009-03-16 17:36:52 -07003480Statement =
Rob Pikef3a33bc2009-09-14 17:39:17 -07003481 Declaration | LabeledStmt | SimpleStmt |
3482 GoStmt | ReturnStmt | BreakStmt | ContinueStmt | GotoStmt |
Rob Pike11417162009-03-24 17:45:53 -07003483 FallthroughStmt | Block | IfStmt | SwitchStmt | SelectStmt | ForStmt |
3484 DeferStmt .
Robert Griesemer7abfcd92008-10-07 17:14:30 -07003485
Robert Griesemerb50ed022011-02-01 12:02:49 -08003486SimpleStmt = EmptyStmt | ExpressionStmt | SendStmt | IncDecStmt | Assignment | ShortVarDecl .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003487</pre>
Robert Griesemer7271e042008-10-09 20:05:24 -07003488
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003489
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003490<h3 id="Empty_statements">Empty statements</h3>
Robert Griesemeraed247f2008-10-08 17:05:30 -07003491
Rob Pike96750f12009-02-27 16:47:48 -08003492<p>
Robert Griesemeraed247f2008-10-08 17:05:30 -07003493The empty statement does nothing.
Rob Pike96750f12009-02-27 16:47:48 -08003494</p>
Robert Griesemeraed247f2008-10-08 17:05:30 -07003495
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003496<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003497EmptyStmt = .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003498</pre>
Robert Griesemeraed247f2008-10-08 17:05:30 -07003499
3500
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003501<h3 id="Labeled_statements">Labeled statements</h3>
Robert Griesemerdea43942009-03-16 17:36:52 -07003502
3503<p>
3504A labeled statement may be the target of a <code>goto</code>,
3505<code>break</code> or <code>continue</code> statement.
3506</p>
3507
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003508<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003509LabeledStmt = Label ":" Statement .
Robert Griesemerdea43942009-03-16 17:36:52 -07003510Label = identifier .
3511</pre>
3512
3513<pre>
Robert Griesemer7fc4e372011-02-01 12:51:10 -08003514Error: log.Panic("error encountered")
Robert Griesemerdea43942009-03-16 17:36:52 -07003515</pre>
3516
3517
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003518<h3 id="Expression_statements">Expression statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003519
Rob Pike96750f12009-02-27 16:47:48 -08003520<p>
Robert Griesemerb50ed022011-02-01 12:02:49 -08003521Function calls, method calls, and receive operations
Rob Pike96750f12009-02-27 16:47:48 -08003522can appear in statement context.
3523</p>
3524
3525
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003526<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003527ExpressionStmt = Expression .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003528</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003529
Robert Griesemerc2d55862009-02-19 16:49:10 -08003530<pre>
Robert Griesemerb50ed022011-02-01 12:02:49 -08003531h(x+y)
3532f.Close()
Robert Griesemere1e76192009-09-25 14:11:03 -07003533&lt;-ch
Robert Griesemerc2d55862009-02-19 16:49:10 -08003534</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003535
3536
Robert Griesemerb50ed022011-02-01 12:02:49 -08003537<h3 id="Send_statements">Send statements</h3>
3538
3539<p>
3540A send statement sends a value on a channel.
3541The channel expression must be of <a href="#Channel_types">channel type</a>
3542and the type of the value must be <a href="#Assignability">assignable</a>
3543to the channel's element type.
3544</p>
3545
3546<pre class="ebnf">
3547SendStmt = Channel "&lt;-" Expression .
3548Channel = Expression .
3549</pre>
3550
3551<p>
3552Both the channel and the value expression are evaluated before communication
3553begins. Communication blocks until the send can proceed, at which point the
3554value is transmitted on the channel.
3555A send on an unbuffered channel can proceed if a receiver is ready.
3556A send on a buffered channel can proceed if there is room in the buffer.
3557</p>
3558
3559<pre>
3560ch &lt;- 3
3561</pre>
3562
3563<p>
3564Sending to a <code>nil</code> channel causes a
3565<a href="#Run_time_panics">run-time panic</a>.
3566</p>
3567
3568
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003569<h3 id="IncDec_statements">IncDec statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003570
Rob Pike96750f12009-02-27 16:47:48 -08003571<p>
Robert Griesemer52a54802008-09-30 13:02:50 -07003572The "++" and "--" statements increment or decrement their operands
Robert Griesemer19b1d352009-09-24 19:36:48 -07003573by the untyped <a href="#Constants">constant</a> <code>1</code>.
Robert Griesemer5474e162010-09-28 14:44:19 -07003574As with an assignment, the operand must be <a href="#Address_operators">addressable</a>
3575or a map index expression.
Rob Pike96750f12009-02-27 16:47:48 -08003576</p>
Robert Griesemer52a54802008-09-30 13:02:50 -07003577
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003578<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003579IncDecStmt = Expression ( "++" | "--" ) .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003580</pre>
Russ Cox5958dd62009-03-04 17:19:21 -08003581
Rob Pike96750f12009-02-27 16:47:48 -08003582<p>
Robert Griesemer506c0082009-09-08 15:41:14 -07003583The following <a href="#Assignments">assignment statements</a> are semantically
Robert Griesemer52a54802008-09-30 13:02:50 -07003584equivalent:
Rob Pike96750f12009-02-27 16:47:48 -08003585</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003586
Rob Pikeff70f092009-02-20 13:36:14 -08003587<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08003588IncDec statement Assignment
3589x++ x += 1
3590x-- x -= 1
3591</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003592
Robert Griesemer5474e162010-09-28 14:44:19 -07003593
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003594<h3 id="Assignments">Assignments</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003595
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003596<pre class="ebnf">
Robert Griesemerc2d55862009-02-19 16:49:10 -08003597Assignment = ExpressionList assign_op ExpressionList .
Rob Pikeff70f092009-02-20 13:36:14 -08003598
Robert Griesemerc2d55862009-02-19 16:49:10 -08003599assign_op = [ add_op | mul_op ] "=" .
3600</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003601
Rob Pike96750f12009-02-27 16:47:48 -08003602<p>
Rob Pike678625d2009-09-15 09:54:22 -07003603Each left-hand side operand must be <a href="#Address_operators">addressable</a>,
Rob Pike4fe41922009-11-07 22:00:59 -08003604a map index expression,
Rob Pike678625d2009-09-15 09:54:22 -07003605or the <a href="#Blank_identifier">blank identifier</a>.
Rob Pike96750f12009-02-27 16:47:48 -08003606</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003607
Robert Griesemerc2d55862009-02-19 16:49:10 -08003608<pre>
3609x = 1
3610*p = f()
3611a[i] = 23
Robert Griesemere1e76192009-09-25 14:11:03 -07003612k = &lt;-ch
Robert Griesemerc2d55862009-02-19 16:49:10 -08003613</pre>
Rob Pike96750f12009-02-27 16:47:48 -08003614
3615<p>
3616An <i>assignment operation</i> <code>x</code> <i>op</i><code>=</code>
3617<code>y</code> where <i>op</i> is a binary arithmetic operation is equivalent
3618to <code>x</code> <code>=</code> <code>x</code> <i>op</i>
Rob Pike4fe41922009-11-07 22:00:59 -08003619<code>y</code> but evaluates <code>x</code>
Rob Pike96750f12009-02-27 16:47:48 -08003620only once. The <i>op</i><code>=</code> construct is a single token.
Rob Pike678625d2009-09-15 09:54:22 -07003621In assignment operations, both the left- and right-hand expression lists
3622must contain exactly one single-valued expression.
Rob Pike96750f12009-02-27 16:47:48 -08003623</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003624
Robert Griesemerc2d55862009-02-19 16:49:10 -08003625<pre>
Robert Griesemer4ed666e2009-08-27 16:45:42 -07003626a[i] &lt;&lt;= 2
Rob Pike678625d2009-09-15 09:54:22 -07003627i &amp;^= 1&lt;&lt;n
Robert Griesemerc2d55862009-02-19 16:49:10 -08003628</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003629
Rob Pike96750f12009-02-27 16:47:48 -08003630<p>
3631A tuple assignment assigns the individual elements of a multi-valued
3632operation to a list of variables. There are two forms. In the
3633first, the right hand operand is a single multi-valued expression
Robert Griesemer506c0082009-09-08 15:41:14 -07003634such as a function evaluation or <a href="#Channel_types">channel</a> or
3635<a href="#Map_types">map</a> operation or a <a href="#Type_assertions">type assertion</a>.
Russ Cox5958dd62009-03-04 17:19:21 -08003636The number of operands on the left
Robert Griesemer4e56b332009-09-10 10:14:00 -07003637hand side must match the number of values. For instance, if
Rob Pike96750f12009-02-27 16:47:48 -08003638<code>f</code> is a function returning two values,
3639</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003640
Robert Griesemerc2d55862009-02-19 16:49:10 -08003641<pre>
3642x, y = f()
3643</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003644
Rob Pike96750f12009-02-27 16:47:48 -08003645<p>
3646assigns the first value to <code>x</code> and the second to <code>y</code>.
Rob Pike678625d2009-09-15 09:54:22 -07003647The <a href="#Blank_identifier">blank identifier</a> provides a
Robert Griesemer4e56b332009-09-10 10:14:00 -07003648way to ignore values returned by a multi-valued expression:
Rob Pike96750f12009-02-27 16:47:48 -08003649</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003650
Robert Griesemer4e56b332009-09-10 10:14:00 -07003651<pre>
3652x, _ = f() // ignore second value returned by f()
3653</pre>
3654
Rob Pike96750f12009-02-27 16:47:48 -08003655<p>
3656In the second form, the number of operands on the left must equal the number
Robert Griesemeref45e642009-08-21 11:25:00 -07003657of expressions on the right, each of which must be single-valued, and the
3658<i>n</i>th expression on the right is assigned to the <i>n</i>th
3659operand on the left.
Russ Cox5958dd62009-03-04 17:19:21 -08003660The expressions on the right are evaluated before assigning to
3661any of the operands on the left, but otherwise the evaluation
Rob Pike678625d2009-09-15 09:54:22 -07003662order is unspecified beyond <a href="#Order_of_evaluation">the usual rules</a>.
Rob Pike96750f12009-02-27 16:47:48 -08003663</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003664
Robert Griesemerc2d55862009-02-19 16:49:10 -08003665<pre>
Rob Pike96750f12009-02-27 16:47:48 -08003666a, b = b, a // exchange a and b
Robert Griesemerc2d55862009-02-19 16:49:10 -08003667</pre>
Rob Pike96750f12009-02-27 16:47:48 -08003668
3669<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07003670In assignments, each value must be
Robert Griesemer440cc952010-06-07 17:40:21 -07003671<a href="#Assignability">assignable</a> to the type of the
Robert Griesemer19b1d352009-09-24 19:36:48 -07003672operand to which it is assigned. If an untyped <a href="#Constants">constant</a>
3673is assigned to a variable of interface type, the constant is <a href="#Conversions">converted</a>
Robert Griesemerb94c0d22011-01-19 23:07:21 -05003674to type <code>bool</code>, <code>int</code>, <code>float64</code>,
3675<code>complex128</code> or <code>string</code>
Robert Griesemer19b1d352009-09-24 19:36:48 -07003676respectively, depending on whether the value is a boolean, integer, floating-point,
Rob Pike72970872010-03-04 12:35:16 -08003677complex, or string constant.
Rob Pike96750f12009-02-27 16:47:48 -08003678</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003679
3680
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003681<h3 id="If_statements">If statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003682
Rob Pike96750f12009-02-27 16:47:48 -08003683<p>
3684"If" statements specify the conditional execution of two branches
3685according to the value of a boolean expression. If the expression
3686evaluates to true, the "if" branch is executed, otherwise, if
3687present, the "else" branch is executed. A missing condition
3688is equivalent to <code>true</code>.
3689</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003690
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003691<pre class="ebnf">
Rob Pikef3a33bc2009-09-14 17:39:17 -07003692IfStmt = "if" [ SimpleStmt ";" ] [ Expression ] Block [ "else" Statement ] .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003693</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003694
Robert Griesemerc2d55862009-02-19 16:49:10 -08003695<pre>
3696if x > 0 {
3697 return true;
3698}
3699</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003700
Russ Cox5958dd62009-03-04 17:19:21 -08003701<p>
Russ Cox16b95ba2009-08-20 10:22:52 -07003702The expression may be preceded by a simple statement, which
3703executes before the expression is evaluated.
Russ Cox5958dd62009-03-04 17:19:21 -08003704</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003705
Robert Griesemerc2d55862009-02-19 16:49:10 -08003706<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08003707if x := f(); x &lt; y {
3708 return x
Robert Griesemerc2d55862009-02-19 16:49:10 -08003709} else if x > z {
Robert Griesemer130ac742009-12-10 16:43:01 -08003710 return z
Robert Griesemerc2d55862009-02-19 16:49:10 -08003711} else {
Robert Griesemer130ac742009-12-10 16:43:01 -08003712 return y
Robert Griesemerc2d55862009-02-19 16:49:10 -08003713}
3714</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003715
3716
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003717<h3 id="Switch_statements">Switch statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003718
Rob Pike96750f12009-02-27 16:47:48 -08003719<p>
3720"Switch" statements provide multi-way execution.
Rob Pike5a578492009-03-17 16:48:35 -07003721An expression or type specifier is compared to the "cases"
3722inside the "switch" to determine which branch
3723to execute.
Rob Pikeafee1c52009-03-20 17:41:25 -07003724</p>
Robert Griesemer091cba82009-03-19 08:39:40 -07003725
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003726<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003727SwitchStmt = ExprSwitchStmt | TypeSwitchStmt .
Robert Griesemer091cba82009-03-19 08:39:40 -07003728</pre>
3729
Rob Pikeafee1c52009-03-20 17:41:25 -07003730<p>
Rob Pike5a578492009-03-17 16:48:35 -07003731There are two forms: expression switches and type switches.
Rob Pike5a578492009-03-17 16:48:35 -07003732In an expression switch, the cases contain expressions that are compared
3733against the value of the switch expression.
3734In a type switch, the cases contain types that are compared against the
3735type of a specially annotated switch expression.
Rob Pike96750f12009-02-27 16:47:48 -08003736</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003737
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003738<h4 id="Expression_switches">Expression switches</h4>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003739
Rob Pike96750f12009-02-27 16:47:48 -08003740<p>
Rob Pike5a578492009-03-17 16:48:35 -07003741In an expression switch,
3742the switch expression is evaluated and
3743the case expressions, which need not be constants,
Robert Griesemer4f185492009-05-01 17:00:16 -07003744are evaluated left-to-right and top-to-bottom; the first one that equals the
Rob Pike5a578492009-03-17 16:48:35 -07003745switch expression
Rob Pike96750f12009-02-27 16:47:48 -08003746triggers execution of the statements of the associated case;
3747the other cases are skipped.
Rob Pike5a578492009-03-17 16:48:35 -07003748If no case matches and there is a "default" case,
3749its statements are executed.
Rob Pike96750f12009-02-27 16:47:48 -08003750There can be at most one default case and it may appear anywhere in the
3751"switch" statement.
Robert Griesemere9192752009-12-01 16:15:53 -08003752A missing switch expression is equivalent to
Rob Pike70c1a102009-03-18 19:23:59 -07003753the expression <code>true</code>.
Rob Pike96750f12009-02-27 16:47:48 -08003754</p>
Rob Pike70c1a102009-03-18 19:23:59 -07003755
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003756<pre class="ebnf">
Rob Pikef3a33bc2009-09-14 17:39:17 -07003757ExprSwitchStmt = "switch" [ SimpleStmt ";" ] [ Expression ] "{" { ExprCaseClause } "}" .
Robert Griesemer130ac742009-12-10 16:43:01 -08003758ExprCaseClause = ExprSwitchCase ":" { Statement ";" } .
Robert Griesemer091cba82009-03-19 08:39:40 -07003759ExprSwitchCase = "case" ExpressionList | "default" .
Rob Pike70c1a102009-03-18 19:23:59 -07003760</pre>
3761
Rob Pike96750f12009-02-27 16:47:48 -08003762<p>
3763In a case or default clause,
3764the last statement only may be a "fallthrough" statement
Evan Shaw67d30bb2010-05-25 18:24:07 -07003765<a href="#Fallthrough_statements">Fallthrough statement</a>) to
Rob Pike96750f12009-02-27 16:47:48 -08003766indicate that control should flow from the end of this clause to
Robert Griesemeraed247f2008-10-08 17:05:30 -07003767the first statement of the next clause.
Rob Pike96750f12009-02-27 16:47:48 -08003768Otherwise control flows to the end of the "switch" statement.
3769</p>
Robert Griesemer0a162a12009-08-19 16:44:04 -07003770
Robert Griesemerc2d55862009-02-19 16:49:10 -08003771<p>
Russ Cox16b95ba2009-08-20 10:22:52 -07003772The expression may be preceded by a simple statement, which
3773executes before the expression is evaluated.
Rob Pike96750f12009-02-27 16:47:48 -08003774</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003775
Robert Griesemerc2d55862009-02-19 16:49:10 -08003776<pre>
3777switch tag {
Rob Pike65ec16b2009-05-29 15:46:03 -07003778default: s3()
3779case 0, 1, 2, 3: s1()
3780case 4, 5, 6, 7: s2()
Robert Griesemerc2d55862009-02-19 16:49:10 -08003781}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003782
Andrew Gerrand10b77f72010-03-29 13:12:08 +11003783switch x := f(); { // missing switch expression means "true"
Rob Pike65ec16b2009-05-29 15:46:03 -07003784case x &lt; 0: return -x
3785default: return x
Robert Griesemerc2d55862009-02-19 16:49:10 -08003786}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003787
Robert Griesemere9192752009-12-01 16:15:53 -08003788switch {
Robert Griesemer130ac742009-12-10 16:43:01 -08003789case x &lt; y: f1()
3790case x &lt; z: f2()
3791case x == 4: f3()
Robert Griesemerc2d55862009-02-19 16:49:10 -08003792}
3793</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003794
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003795<h4 id="Type_switches">Type switches</h4>
Rob Pike70c1a102009-03-18 19:23:59 -07003796
Rob Pike5a578492009-03-17 16:48:35 -07003797<p>
Rob Pike70c1a102009-03-18 19:23:59 -07003798A type switch compares types rather than values. It is otherwise similar
Robert Griesemer1f95f0d2009-08-27 16:44:17 -07003799to an expression switch. It is marked by a special switch expression that
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003800has the form of a <a href="#Type_assertions">type assertion</a>
Rob Pike70c1a102009-03-18 19:23:59 -07003801using the reserved word <code>type</code> rather than an actual type.
3802Cases then match literal types against the dynamic type of the expression
Rob Pikef5387602009-03-30 16:08:41 -07003803in the type assertion.
Rob Pike5a578492009-03-17 16:48:35 -07003804</p>
3805
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003806<pre class="ebnf">
Rob Pikef3a33bc2009-09-14 17:39:17 -07003807TypeSwitchStmt = "switch" [ SimpleStmt ";" ] TypeSwitchGuard "{" { TypeCaseClause } "}" .
Russ Coxc1045db2009-12-23 13:48:44 -08003808TypeSwitchGuard = [ identifier ":=" ] PrimaryExpr "." "(" "type" ")" .
Robert Griesemer130ac742009-12-10 16:43:01 -08003809TypeCaseClause = TypeSwitchCase ":" { Statement ";" } .
Rob Pike678625d2009-09-15 09:54:22 -07003810TypeSwitchCase = "case" TypeList | "default" .
3811TypeList = Type { "," Type } .
Rob Pike5a578492009-03-17 16:48:35 -07003812</pre>
3813
3814<p>
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003815The TypeSwitchGuard may include a
3816<a href="#Short_variable_declarations">short variable declaration</a>.
3817When that form is used, the variable is declared in each clause.
3818In clauses with a case listing exactly one type, the variable
3819has that type; otherwise, the variable has the type of the expression
3820in the TypeSwitchGuard.
Rob Pike94b67eb2009-03-24 17:40:47 -07003821</p>
3822
3823<p>
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003824The type in a case may be <code>nil</code>
3825<a href="#Predeclared_identifiers">Predeclared identifiers</a>);
3826that case is used when the expression in the TypeSwitchGuard
Robert Griesemer1f95f0d2009-08-27 16:44:17 -07003827is a <code>nil</code> interface value.
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003828</p>
3829
3830<p>
Robert Griesemere9192752009-12-01 16:15:53 -08003831Given an expression <code>x</code> of type <code>interface{}</code>,
Rob Pike70c1a102009-03-18 19:23:59 -07003832the following type switch:
Rob Pike5a578492009-03-17 16:48:35 -07003833</p>
3834
3835<pre>
Robert Griesemere9192752009-12-01 16:15:53 -08003836switch i := x.(type) {
Rob Pike94b67eb2009-03-24 17:40:47 -07003837case nil:
Robert Griesemer130ac742009-12-10 16:43:01 -08003838 printString("x is nil")
Rob Pike5a578492009-03-17 16:48:35 -07003839case int:
Robert Griesemer130ac742009-12-10 16:43:01 -08003840 printInt(i) // i is an int
Robert Griesemerb94c0d22011-01-19 23:07:21 -05003841case float64:
3842 printFloat64(i) // i is a float64
3843case func(int) float64:
Robert Griesemer130ac742009-12-10 16:43:01 -08003844 printFunction(i) // i is a function
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003845case bool, string:
Robert Griesemer130ac742009-12-10 16:43:01 -08003846 printString("type is bool or string") // i is an interface{}
Rob Pike5a578492009-03-17 16:48:35 -07003847default:
Robert Griesemer130ac742009-12-10 16:43:01 -08003848 printString("don't know the type")
Rob Pike5a578492009-03-17 16:48:35 -07003849}
3850</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003851
Rob Pike70c1a102009-03-18 19:23:59 -07003852<p>
3853could be rewritten:
3854</p>
3855
3856<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08003857v := x // x is evaluated exactly once
Rob Pike94b67eb2009-03-24 17:40:47 -07003858if v == nil {
Robert Griesemer130ac742009-12-10 16:43:01 -08003859 printString("x is nil")
Rob Pike94b67eb2009-03-24 17:40:47 -07003860} else if i, is_int := v.(int); is_int {
Robert Griesemer130ac742009-12-10 16:43:01 -08003861 printInt(i) // i is an int
Robert Griesemerb94c0d22011-01-19 23:07:21 -05003862} else if i, is_float64 := v.(float64); is_float64 {
3863 printFloat64(i) // i is a float64
3864} else if i, is_func := v.(func(int) float64); is_func {
Robert Griesemer130ac742009-12-10 16:43:01 -08003865 printFunction(i) // i is a function
Rob Pike70c1a102009-03-18 19:23:59 -07003866} else {
Robert Griesemer130ac742009-12-10 16:43:01 -08003867 i1, is_bool := v.(bool)
3868 i2, is_string := v.(string)
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003869 if is_bool || is_string {
Robert Griesemer130ac742009-12-10 16:43:01 -08003870 i := v
3871 printString("type is bool or string") // i is an interface{}
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003872 } else {
Robert Griesemer130ac742009-12-10 16:43:01 -08003873 i := v
3874 printString("don't know the type") // i is an interface{}
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003875 }
Rob Pike70c1a102009-03-18 19:23:59 -07003876}
3877</pre>
3878
Robert Griesemeraeaab592009-08-31 17:30:55 -07003879<p>
Russ Cox16b95ba2009-08-20 10:22:52 -07003880The type switch guard may be preceded by a simple statement, which
3881executes before the guard is evaluated.
Robert Griesemer1f95f0d2009-08-27 16:44:17 -07003882</p>
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003883
3884<p>
3885The "fallthrough" statement is not permitted in a type switch.
Russ Cox16b95ba2009-08-20 10:22:52 -07003886</p>
3887
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003888<h3 id="For_statements">For statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003889
Rob Pike96750f12009-02-27 16:47:48 -08003890<p>
3891A "for" statement specifies repeated execution of a block. The iteration is
3892controlled by a condition, a "for" clause, or a "range" clause.
3893</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003894
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003895<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003896ForStmt = "for" [ Condition | ForClause | RangeClause ] Block .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003897Condition = Expression .
3898</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003899
Rob Pike96750f12009-02-27 16:47:48 -08003900<p>
3901In its simplest form, a "for" statement specifies the repeated execution of
3902a block as long as a boolean condition evaluates to true.
3903The condition is evaluated before each iteration.
3904If the condition is absent, it is equivalent to <code>true</code>.
3905</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003906
Robert Griesemerc2d55862009-02-19 16:49:10 -08003907<pre>
3908for a &lt; b {
3909 a *= 2
3910}
3911</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003912
Rob Pike96750f12009-02-27 16:47:48 -08003913<p>
Rob Pike678625d2009-09-15 09:54:22 -07003914A "for" statement with a ForClause is also controlled by its condition, but
Rob Pike96750f12009-02-27 16:47:48 -08003915additionally it may specify an <i>init</i>
3916and a <i>post</i> statement, such as an assignment,
Russ Cox16b95ba2009-08-20 10:22:52 -07003917an increment or decrement statement. The init statement may be a
Robert Griesemer4ed666e2009-08-27 16:45:42 -07003918<a href="#Short_variable_declarations">short variable declaration</a>, but the post statement must not.
Rob Pike96750f12009-02-27 16:47:48 -08003919</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003920
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003921<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -08003922ForClause = [ InitStmt ] ";" [ Condition ] ";" [ PostStmt ] .
Rob Pike11417162009-03-24 17:45:53 -07003923InitStmt = SimpleStmt .
3924PostStmt = SimpleStmt .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003925</pre>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003926
Robert Griesemerc2d55862009-02-19 16:49:10 -08003927<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08003928for i := 0; i &lt; 10; i++ {
Robert Griesemerc2d55862009-02-19 16:49:10 -08003929 f(i)
3930}
3931</pre>
Russ Cox5958dd62009-03-04 17:19:21 -08003932
Robert Griesemerc2d55862009-02-19 16:49:10 -08003933<p>
Rob Pike96750f12009-02-27 16:47:48 -08003934If non-empty, the init statement is executed once before evaluating the
3935condition for the first iteration;
3936the post statement is executed after each execution of the block (and
3937only if the block was executed).
Robert Griesemer130ac742009-12-10 16:43:01 -08003938Any element of the ForClause may be empty but the
3939<a href="#Semicolons">semicolons</a> are
Rob Pike96750f12009-02-27 16:47:48 -08003940required unless there is only a condition.
3941If the condition is absent, it is equivalent to <code>true</code>.
3942</p>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003943
Robert Griesemerc2d55862009-02-19 16:49:10 -08003944<pre>
Rob Pike678625d2009-09-15 09:54:22 -07003945for cond { S() } is the same as for ; cond ; { S() }
3946for { S() } is the same as for true { S() }
Robert Griesemerc2d55862009-02-19 16:49:10 -08003947</pre>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003948
Rob Pike96750f12009-02-27 16:47:48 -08003949<p>
3950A "for" statement with a "range" clause
Rob Pike7aee71b2009-04-15 20:28:25 -07003951iterates through all entries of an array, slice, string or map,
Robert Griesemer5474e162010-09-28 14:44:19 -07003952or values received on a channel. For each entry it assigns <i>iteration values</i>
3953to corresponding <i>iteration variables</i> and then executes the block.
Rob Pike96750f12009-02-27 16:47:48 -08003954</p>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003955
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003956<pre class="ebnf">
Robert Griesemer5474e162010-09-28 14:44:19 -07003957RangeClause = Expression [ "," Expression ] ( "=" | ":=" ) "range" Expression .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003958</pre>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003959
Robert Griesemerc2d55862009-02-19 16:49:10 -08003960<p>
Robert Griesemer5474e162010-09-28 14:44:19 -07003961The expression on the right in the "range" clause is called the <i>range expression</i>,
3962which may be an array, pointer to an array, slice, string, map, or channel.
3963As with an assignment, the operands on the left must be
3964<a href="#Address_operators">addressable</a> or map index expressions; they
3965denote the iteration variables. If the range expression is a channel, only
3966one iteration variable is permitted, otherwise there may be one or two.
Robert Griesemer5474e162010-09-28 14:44:19 -07003967</p>
Rob Pike29d0f022011-01-05 11:39:57 -08003968
3969<p>
Robert Griesemer5474e162010-09-28 14:44:19 -07003970The range expression is evaluated once before beginning the loop.
3971Function calls on the left are evaluated once per iteration.
3972For each iteration, iteration values are produced as follows:
3973</p>
3974
3975<pre class="grammar">
3976Range expression 1st value 2nd value (if 2nd variable is present)
3977
3978array or slice a [n]E, *[n]E, or []E index i int a[i] E
3979string s string type index i int see below int
3980map m map[K]V key k K m[k] V
3981channel c chan E element e E
3982</pre>
3983
3984<ol>
3985<li>
3986For an array or slice value, the index iteration values are produced in
3987increasing order, starting at element index 0.
3988</li>
3989
3990<li>
3991For a string value, the "range" clause iterates over the Unicode code points
3992in the string starting at byte index 0. On successive iterations, the index value will be the
3993index of the first byte of successive UTF-8-encoded code points in the string,
3994and the second value, of type <code>int</code>, will be the value of
Rob Pike7aee71b2009-04-15 20:28:25 -07003995the corresponding code point. If the iteration encounters an invalid
Robert Griesemer5474e162010-09-28 14:44:19 -07003996UTF-8 sequence, the second value will be <code>0xFFFD</code>,
Rob Pike7aee71b2009-04-15 20:28:25 -07003997the Unicode replacement character, and the next iteration will advance
3998a single byte in the string.
Robert Griesemer5474e162010-09-28 14:44:19 -07003999</li>
4000
4001<li>
4002The iteration order over maps is not specified.
4003If map entries that have not yet been reached are deleted during iteration,
4004the corresponding iteration values will not be produced. If map entries are
4005inserted during iteration, the behavior is implementation-dependent, but the
4006iteration values for each entry will be produced at most once.
4007</li>
4008
4009<li>
4010For channels, the iteration values produced are the successive values sent on
4011the channel until the channel is closed; it does not produce the zero value sent
4012before the channel is closed
4013<a href="#Close_and_closed"><code>close</code> and <code>closed</code></a>).
4014</li>
Anthony Martin0122a662011-02-08 14:51:15 -08004015</ol>
Robert Griesemer5474e162010-09-28 14:44:19 -07004016
Rob Pike7aee71b2009-04-15 20:28:25 -07004017<p>
Robert Griesemer5474e162010-09-28 14:44:19 -07004018The iteration values are assigned to the respective
4019iteration variables as in an <a href="#Assignments">assignment statement</a>.
Rob Pike94b67eb2009-03-24 17:40:47 -07004020</p>
Robert Griesemer5474e162010-09-28 14:44:19 -07004021
Rob Pike94b67eb2009-03-24 17:40:47 -07004022<p>
Robert Griesemer5474e162010-09-28 14:44:19 -07004023The iteration variables may be declared by the "range" clause (<code>:=</code>).
4024In this case their types are set to the types of the respective iteration values
4025and their <a href="#Declarations_and_scope">scope</a> ends at the end of the "for"
4026statement; they are re-used in each iteration.
Rob Pike96750f12009-02-27 16:47:48 -08004027If the iteration variables are declared outside the "for" statement,
4028after execution their values will be those of the last iteration.
4029</p>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08004030
Robert Griesemerc2d55862009-02-19 16:49:10 -08004031<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08004032var a [10]string
4033m := map[string]int{"mon":0, "tue":1, "wed":2, "thu":3, "fri":4, "sat":5, "sun":6}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004034
Robert Griesemerc2d55862009-02-19 16:49:10 -08004035for i, s := range a {
4036 // type of i is int
4037 // type of s is string
4038 // s == a[i]
4039 g(i, s)
4040}
4041
Robert Griesemer130ac742009-12-10 16:43:01 -08004042var key string
Robert Griesemer440cc952010-06-07 17:40:21 -07004043var val interface {} // value type of m is assignable to val
Rob Pike678625d2009-09-15 09:54:22 -07004044for key, val = range m {
4045 h(key, val)
Robert Griesemerc2d55862009-02-19 16:49:10 -08004046}
4047// key == last map key encountered in iteration
4048// val == map[key]
4049</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004050
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004051
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004052<h3 id="Go_statements">Go statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004053
Rob Pike96750f12009-02-27 16:47:48 -08004054<p>
Russ Cox5958dd62009-03-04 17:19:21 -08004055A "go" statement starts the execution of a function or method call
Rob Pike96750f12009-02-27 16:47:48 -08004056as an independent concurrent thread of control, or <i>goroutine</i>,
4057within the same address space.
4058</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004059
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004060<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07004061GoStmt = "go" Expression .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004062</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004063
Rob Pike96750f12009-02-27 16:47:48 -08004064<p>
4065The expression must be a call, and
4066unlike with a regular call, program execution does not wait
Robert Griesemerb9f8b9c2008-09-26 13:38:38 -07004067for the invoked function to complete.
Rob Pike96750f12009-02-27 16:47:48 -08004068</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004069
Robert Griesemerc2d55862009-02-19 16:49:10 -08004070<pre>
4071go Server()
Robert Griesemere1e76192009-09-25 14:11:03 -07004072go func(ch chan&lt;- bool) { for { sleep(10); ch &lt;- true; }} (c)
Robert Griesemerc2d55862009-02-19 16:49:10 -08004073</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004074
4075
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004076<h3 id="Select_statements">Select statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004077
Rob Pike96750f12009-02-27 16:47:48 -08004078<p>
4079A "select" statement chooses which of a set of possible communications
4080will proceed. It looks similar to a "switch" statement but with the
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004081cases all referring to communication operations.
Rob Pike96750f12009-02-27 16:47:48 -08004082</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004083
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004084<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07004085SelectStmt = "select" "{" { CommClause } "}" .
Robert Griesemer130ac742009-12-10 16:43:01 -08004086CommClause = CommCase ":" { Statement ";" } .
Robert Griesemerb50ed022011-02-01 12:02:49 -08004087CommCase = "case" ( SendStmt | RecvStmt ) | "default" .
4088RecvStmt = [ Expression ( "=" | ":=" ) ] RecvExpr .
4089RecvExpr = Expression .
Russ Cox61439182011-01-31 17:42:10 -05004090</pre>
Russ Cox19d9a402011-01-27 15:34:28 -05004091<!-- TODO(rsc):
Robert Griesemerb50ed022011-02-01 12:02:49 -08004092RecvStmt = [ Expression [ "," Expression ] ( "=" | ":=" ) ] RecvExpr .
Russ Cox19d9a402011-01-27 15:34:28 -05004093-->
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004094
Rob Pike96750f12009-02-27 16:47:48 -08004095<p>
Robert Griesemerb50ed022011-02-01 12:02:49 -08004096RecvExpr must be a <a href="#Receive_operator">receive operation</a>.
4097For all the cases in the "select"
Rob Pike041d1162010-07-13 16:23:54 -07004098statement, the channel expressions are evaluated in top-to-bottom order, along with
Robert Griesemerb50ed022011-02-01 12:02:49 -08004099any expressions that appear on the right hand side of send statements.
Rob Pike132d2f12010-08-16 06:42:41 +10004100A channel may be <code>nil</code>,
Rob Pike96750f12009-02-27 16:47:48 -08004101which is equivalent to that case not
4102being present in the select statement
4103except, if a send, its expression is still evaluated.
Rob Pike041d1162010-07-13 16:23:54 -07004104If any of the resulting operations can proceed, one of those is
4105chosen and the corresponding communication and statements are
4106evaluated. Otherwise, if there is a default case, that executes;
4107if there is no default case, the statement blocks until one of the communications can
4108complete.
4109If there are no cases with non-<code>nil</code> channels,
4110the statement blocks forever.
4111Even if the statement blocks,
4112the channel and send expressions are evaluated only once,
4113upon entering the select statement.
Rob Pike96750f12009-02-27 16:47:48 -08004114</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004115<p>
Rob Pike569a1072008-10-03 11:18:45 -07004116Since all the channels and send expressions are evaluated, any side
4117effects in that evaluation will occur for all the communications
Rob Pike96750f12009-02-27 16:47:48 -08004118in the "select" statement.
4119</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004120<p>
Rob Pike041d1162010-07-13 16:23:54 -07004121If multiple cases can proceed, a pseudo-random fair choice is made to decide
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004122which single communication will execute.
Robert Griesemerc2d55862009-02-19 16:49:10 -08004123<p>
Russ Cox19d9a402011-01-27 15:34:28 -05004124<!-- TODO(rsc): s/variable/& or &s/ -->
Robert Griesemer4ed666e2009-08-27 16:45:42 -07004125The receive case may declare a new variable using a
4126<a href="#Short_variable_declarations">short variable declaration</a>.
Rob Pike96750f12009-02-27 16:47:48 -08004127</p>
Robert Griesemer2902a822008-09-17 13:57:11 -07004128
Robert Griesemerc2d55862009-02-19 16:49:10 -08004129<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08004130var c, c1, c2 chan int
4131var i1, i2 int
Robert Griesemerc2d55862009-02-19 16:49:10 -08004132select {
4133case i1 = &lt;-c1:
Robert Griesemer130ac742009-12-10 16:43:01 -08004134 print("received ", i1, " from c1\n")
Robert Griesemerc2d55862009-02-19 16:49:10 -08004135case c2 &lt;- i2:
Robert Griesemer130ac742009-12-10 16:43:01 -08004136 print("sent ", i2, " to c2\n")
Russ Cox19d9a402011-01-27 15:34:28 -05004137<!-- TODO(rsc): add , c3 to channel list above too
4138case i3, ok := &lt;-c3:
4139 if ok {
4140 print("received ", i3, " from c3\n")
4141 } else {
4142 print("c3 is closed\n")
4143 }
4144-->
Robert Griesemerc2d55862009-02-19 16:49:10 -08004145default:
Robert Griesemer130ac742009-12-10 16:43:01 -08004146 print("no communication\n")
Robert Griesemerc2d55862009-02-19 16:49:10 -08004147}
4148
4149for { // send random sequence of bits to c
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004150 select {
Robert Griesemerc2d55862009-02-19 16:49:10 -08004151 case c &lt;- 0: // note: no statement, no fallthrough, no folding of cases
4152 case c &lt;- 1:
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004153 }
Robert Griesemerc2d55862009-02-19 16:49:10 -08004154}
Rob Pike041d1162010-07-13 16:23:54 -07004155
4156select { } // block forever
Robert Griesemerc2d55862009-02-19 16:49:10 -08004157</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004158
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004159
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004160<h3 id="Return_statements">Return statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004161
Rob Pike96750f12009-02-27 16:47:48 -08004162<p>
4163A "return" statement terminates execution of the containing function
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004164and optionally provides a result value or values to the caller.
Rob Pike96750f12009-02-27 16:47:48 -08004165</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004166
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004167<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07004168ReturnStmt = "return" [ ExpressionList ] .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004169</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004170
Robert Griesemer4b908332009-08-07 17:05:41 -07004171<p>
4172In a function without a result type, a "return" statement must not
4173specify any result values.
4174</p>
Rob Pike96750f12009-02-27 16:47:48 -08004175<pre>
Robert Griesemer4b908332009-08-07 17:05:41 -07004176func no_result() {
Rob Pike96750f12009-02-27 16:47:48 -08004177 return
4178}
4179</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004180
Rob Pike96750f12009-02-27 16:47:48 -08004181<p>
Robert Griesemer4b908332009-08-07 17:05:41 -07004182There are three ways to return values from a function with a result
4183type:
Rob Pike96750f12009-02-27 16:47:48 -08004184</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004185
Robert Griesemer4b908332009-08-07 17:05:41 -07004186<ol>
4187 <li>The return value or values may be explicitly listed
4188 in the "return" statement. Each expression must be single-valued
Robert Griesemer440cc952010-06-07 17:40:21 -07004189 and <a href="#Assignability">assignable</a>
4190 to the corresponding element of the function's result type.
Robert Griesemerc2d55862009-02-19 16:49:10 -08004191<pre>
4192func simple_f() int {
Rob Pike96750f12009-02-27 16:47:48 -08004193 return 2
Robert Griesemerc2d55862009-02-19 16:49:10 -08004194}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004195
Robert Griesemerb94c0d22011-01-19 23:07:21 -05004196func complex_f1() (re float64, im float64) {
Rob Pike96750f12009-02-27 16:47:48 -08004197 return -7.0, -4.0
Robert Griesemerc2d55862009-02-19 16:49:10 -08004198}
4199</pre>
Robert Griesemer4b908332009-08-07 17:05:41 -07004200 </li>
4201 <li>The expression list in the "return" statement may be a single
4202 call to a multi-valued function. The effect is as if each value
4203 returned from that function were assigned to a temporary
4204 variable with the type of the respective value, followed by a
4205 "return" statement listing these variables, at which point the
4206 rules of the previous case apply.
Robert Griesemerc2d55862009-02-19 16:49:10 -08004207<pre>
Robert Griesemerb94c0d22011-01-19 23:07:21 -05004208func complex_f2() (re float64, im float64) {
Rob Pike96750f12009-02-27 16:47:48 -08004209 return complex_f1()
4210}
4211</pre>
Robert Griesemer4b908332009-08-07 17:05:41 -07004212 </li>
Peter Mundy5928e1d2010-11-09 10:10:57 -08004213 <li>The expression list may be empty if the function's result
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004214 type specifies names for its result parameters (§<a href="#Function_Types">Function Types</a>).
Rob Pikedb8c2b182010-06-11 21:30:03 -07004215 The result parameters act as ordinary local variables
Robert Griesemer4b908332009-08-07 17:05:41 -07004216 and the function may assign values to them as necessary.
4217 The "return" statement returns the values of these variables.
Rob Pike96750f12009-02-27 16:47:48 -08004218<pre>
Robert Griesemerb94c0d22011-01-19 23:07:21 -05004219func complex_f3() (re float64, im float64) {
Robert Griesemer130ac742009-12-10 16:43:01 -08004220 re = 7.0
4221 im = 4.0
4222 return
Robert Griesemerc2d55862009-02-19 16:49:10 -08004223}
4224</pre>
Robert Griesemer4b908332009-08-07 17:05:41 -07004225 </li>
4226</ol>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004227
Rob Pikedb8c2b182010-06-11 21:30:03 -07004228<p>
4229Regardless 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.
4230</p>
4231
Rob Pike0b4de7a2009-11-09 16:09:57 -08004232<!---
Russ Cox5958dd62009-03-04 17:19:21 -08004233<p>
Robert Griesemer90cc4a52009-10-22 09:41:38 -07004234<span class="alert">
Robert Griesemer4b908332009-08-07 17:05:41 -07004235TODO: Define when return is required.<br />
4236TODO: Language about result parameters needs to go into a section on
4237 function/method invocation<br />
Robert Griesemer90cc4a52009-10-22 09:41:38 -07004238</span>
Russ Cox5958dd62009-03-04 17:19:21 -08004239</p>
Rob Pike0b4de7a2009-11-09 16:09:57 -08004240--->
Russ Cox5958dd62009-03-04 17:19:21 -08004241
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004242<h3 id="Break_statements">Break statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004243
Rob Pike96750f12009-02-27 16:47:48 -08004244<p>
4245A "break" statement terminates execution of the innermost
4246"for", "switch" or "select" statement.
4247</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004248
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004249<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -08004250BreakStmt = "break" [ Label ] .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004251</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004252
Rob Pike96750f12009-02-27 16:47:48 -08004253<p>
4254If there is a label, it must be that of an enclosing
4255"for", "switch" or "select" statement, and that is the one whose execution
4256terminates
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004257<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 -08004258</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004259
Robert Griesemerc2d55862009-02-19 16:49:10 -08004260<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08004261L: for i &lt; n {
Robert Griesemerc2d55862009-02-19 16:49:10 -08004262 switch i {
Rob Pike96750f12009-02-27 16:47:48 -08004263 case 5: break L
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004264 }
Robert Griesemerc2d55862009-02-19 16:49:10 -08004265}
4266</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004267
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004268<h3 id="Continue_statements">Continue statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004269
Rob Pike96750f12009-02-27 16:47:48 -08004270<p>
4271A "continue" statement begins the next iteration of the
Rob Pike678625d2009-09-15 09:54:22 -07004272innermost "for" loop at its post statement (§<a href="#For_statements">For statements</a>).
Rob Pike96750f12009-02-27 16:47:48 -08004273</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004274
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004275<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -08004276ContinueStmt = "continue" [ Label ] .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004277</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004278
Rob Pike96750f12009-02-27 16:47:48 -08004279<p>
Rob Pikede9219962010-04-28 13:18:40 -07004280If there is a label, it must be that of an enclosing
4281"for" statement, and that is the one whose execution
4282advances
4283<a href="#For_statements">For statements</a>).
Rob Pike96750f12009-02-27 16:47:48 -08004284</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004285
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004286<h3 id="Goto_statements">Goto statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004287
Rob Pike96750f12009-02-27 16:47:48 -08004288<p>
4289A "goto" statement transfers control to the statement with the corresponding label.
4290</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004291
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004292<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07004293GotoStmt = "goto" Label .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004294</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004295
Robert Griesemerc2d55862009-02-19 16:49:10 -08004296<pre>
4297goto Error
4298</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004299
Rob Pike96750f12009-02-27 16:47:48 -08004300<p>
4301Executing the "goto" statement must not cause any variables to come into
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004302scope that were not already in scope at the point of the goto. For
4303instance, this example:
Rob Pike96750f12009-02-27 16:47:48 -08004304</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004305
Robert Griesemerc2d55862009-02-19 16:49:10 -08004306<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08004307goto L // BAD
4308v := 3
Robert Griesemerc2d55862009-02-19 16:49:10 -08004309L:
4310</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004311
Rob Pike96750f12009-02-27 16:47:48 -08004312<p>
4313is erroneous because the jump to label <code>L</code> skips
4314the creation of <code>v</code>.
Rob Pike0b4de7a2009-11-09 16:09:57 -08004315<!---
Robert Griesemer90cc4a52009-10-22 09:41:38 -07004316(<span class="alert">TODO: Eliminate in favor of used and not set errors?</span>)
Rob Pike0b4de7a2009-11-09 16:09:57 -08004317--->
Rob Pike96750f12009-02-27 16:47:48 -08004318</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004319
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004320<h3 id="Fallthrough_statements">Fallthrough statements</h3>
Robert Griesemeraed247f2008-10-08 17:05:30 -07004321
Rob Pike96750f12009-02-27 16:47:48 -08004322<p>
4323A "fallthrough" statement transfers control to the first statement of the
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004324next case clause in a expression "switch" statement (§<a href="#Expression_switches">Expression switches</a>). It may
Robert Griesemer091cba82009-03-19 08:39:40 -07004325be used only as the final non-empty statement in a case or default clause in an
4326expression "switch" statement.
Rob Pike96750f12009-02-27 16:47:48 -08004327</p>
Robert Griesemeraed247f2008-10-08 17:05:30 -07004328
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004329<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07004330FallthroughStmt = "fallthrough" .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004331</pre>
Robert Griesemeraed247f2008-10-08 17:05:30 -07004332
4333
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004334<h3 id="Defer_statements">Defer statements</h3>
Robert Griesemer4a903e02009-01-27 09:29:40 -08004335
Rob Pike96750f12009-02-27 16:47:48 -08004336<p>
4337A "defer" statement invokes a function whose execution is deferred to the moment
4338the surrounding function returns.
4339</p>
Robert Griesemer4a903e02009-01-27 09:29:40 -08004340
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004341<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07004342DeferStmt = "defer" Expression .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004343</pre>
Robert Griesemer4a903e02009-01-27 09:29:40 -08004344
Rob Pike96750f12009-02-27 16:47:48 -08004345<p>
4346The expression must be a function or method call.
4347Each time the "defer" statement
Robert Griesemer7471eab2009-01-27 14:51:24 -08004348executes, the parameters to the function call are evaluated and saved anew but the
Rob Pike678625d2009-09-15 09:54:22 -07004349function is not invoked.
4350Deferred function calls are executed in LIFO order
4351immediately before the surrounding function returns,
Robert Griesemer48f0cd22010-03-23 17:30:14 -07004352after the return values, if any, have been evaluated, but before they
4353are returned to the caller. For instance, if the deferred function is
Rob Pike5bb29fb2010-03-25 17:59:59 -07004354a <a href="#Function_literals">function literal</a> and the surrounding
Robert Griesemer48f0cd22010-03-23 17:30:14 -07004355function has <a href="#Function_types">named result parameters</a> that
4356are in scope within the literal, the deferred function may access and modify
4357the result parameters before they are returned.
Rob Pike96750f12009-02-27 16:47:48 -08004358</p>
Robert Griesemer4a903e02009-01-27 09:29:40 -08004359
Robert Griesemerc2d55862009-02-19 16:49:10 -08004360<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08004361lock(l)
4362defer unlock(l) // unlocking happens before surrounding function returns
Robert Griesemer4a903e02009-01-27 09:29:40 -08004363
Robert Griesemerc2d55862009-02-19 16:49:10 -08004364// prints 3 2 1 0 before surrounding function returns
4365for i := 0; i &lt;= 3; i++ {
Robert Griesemer130ac742009-12-10 16:43:01 -08004366 defer fmt.Print(i)
Robert Griesemerc2d55862009-02-19 16:49:10 -08004367}
Robert Griesemer48f0cd22010-03-23 17:30:14 -07004368
4369// f returns 1
4370func f() (result int) {
4371 defer func() {
4372 result++
4373 }()
4374 return 0
4375}
Robert Griesemerc2d55862009-02-19 16:49:10 -08004376</pre>
Robert Griesemer4a903e02009-01-27 09:29:40 -08004377
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004378<h2 id="Built-in_functions">Built-in functions</h2>
4379
4380<p>
Rob Pike72970872010-03-04 12:35:16 -08004381Built-in functions are
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004382<a href="#Predeclared_identifiers">predeclared</a>.
4383They are called like any other function but some of them
4384accept a type instead of an expression as the first argument.
4385</p>
4386
Russ Cox2a5f0c62009-12-04 10:23:12 -08004387<p>
4388The built-in functions do not have standard Go types,
4389so they can only appear in <a href="#Calls">call expressions</a>;
4390they cannot be used as function values.
4391</p>
4392
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004393<pre class="ebnf">
Robert Griesemerac771a82010-09-24 14:08:28 -07004394BuiltinCall = identifier "(" [ BuiltinArgs [ "," ] ] ")" .
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004395BuiltinArgs = Type [ "," ExpressionList ] | ExpressionList .
4396</pre>
4397
Russ Cox19d9a402011-01-27 15:34:28 -05004398<!-- TODO(rsc): s/.and.closed//g -->
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004399<h3 id="Close_and_closed">Close and closed</h3>
4400
4401<p>
Robert Griesemerdc60c5a2010-07-14 16:09:22 -07004402For a channel <code>c</code>, the built-in function <code>close(c)</code>
4403marks the channel as unable to accept more values through a send operation;
Russ Cox27c74d32011-01-21 15:07:13 -05004404sending to or closing a closed channel causes a <a href="#Run_time_panics">run-time panic</a>.
Robert Griesemerdc60c5a2010-07-14 16:09:22 -07004405After calling <code>close</code>, and after any previously
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004406sent values have been received, receive operations will return
Robert Griesemerdc60c5a2010-07-14 16:09:22 -07004407the zero value for the channel's type without blocking.
Russ Cox19d9a402011-01-27 15:34:28 -05004408
4409<!-- TODO(rsc): delete next sentence, replace with
Robert Griesemerb50ed022011-02-01 12:02:49 -08004410 The multi-valued <a href="#Receive_operator">receive operation</a>
Russ Cox19d9a402011-01-27 15:34:28 -05004411 returns a received value along with an indication of whether the channel is closed.
4412-->
Robert Griesemerdc60c5a2010-07-14 16:09:22 -07004413After at least one such zero value has been
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004414received, <code>closed(c)</code> returns true.
4415</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004416
Robert Griesemerdc60c5a2010-07-14 16:09:22 -07004417
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004418<h3 id="Length_and_capacity">Length and capacity</h3>
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -07004419
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004420<p>
4421The built-in functions <code>len</code> and <code>cap</code> take arguments
4422of various types and return a result of type <code>int</code>.
4423The implementation guarantees that the result always fits into an <code>int</code>.
4424</p>
4425
Rob Pikeff70f092009-02-20 13:36:14 -08004426<pre class="grammar">
Robert Griesemerd3ffc5e2009-09-03 10:35:09 -07004427Call Argument type Result
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -07004428
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004429len(s) string type string length in bytes
Russ Coxf4429182010-07-01 17:49:47 -07004430 [n]T, *[n]T array length (== n)
Russ Coxfe537952009-08-20 10:47:40 -07004431 []T slice length
Rob Pike678625d2009-09-15 09:54:22 -07004432 map[K]T map length (number of defined keys)
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004433 chan T number of elements queued in channel buffer
Robert Griesemer4dc25282008-09-09 10:37:19 -07004434
Russ Coxf4429182010-07-01 17:49:47 -07004435cap(s) [n]T, *[n]T array length (== n)
Russ Coxfe537952009-08-20 10:47:40 -07004436 []T slice capacity
Rob Pikedf3183f2009-02-26 16:37:23 -08004437 chan T channel buffer capacity
Robert Griesemerc2d55862009-02-19 16:49:10 -08004438</pre>
Robert Griesemer4dc25282008-09-09 10:37:19 -07004439
Robert Griesemerc2d55862009-02-19 16:49:10 -08004440<p>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004441The capacity of a slice is the number of elements for which there is
4442space allocated in the underlying array.
4443At any time the following relationship holds:
4444</p>
Robert Griesemer633957b2009-01-06 13:23:20 -08004445
Robert Griesemerc2d55862009-02-19 16:49:10 -08004446<pre>
Anthony Martin0122a662011-02-08 14:51:15 -080044470 &lt;= len(s) &lt;= cap(s)
Robert Griesemerc2d55862009-02-19 16:49:10 -08004448</pre>
Robert Griesemer667ef6c2008-09-10 13:00:32 -07004449
Russ Coxf4429182010-07-01 17:49:47 -07004450<p>
Robert Griesemer0c2e6b32010-07-13 11:54:57 -07004451The length and capacity of a <code>nil</code> slice, map, or channel are 0.
4452</p>
4453
4454<p>
Russ Coxf4429182010-07-01 17:49:47 -07004455The expression
4456<code>len(s)</code> is a
4457<a href="#Constants">constant</a> if <code>s</code> is a string constant.
4458The expressions
4459<code>len(s)</code> and
4460<code>cap(s)</code> are
4461constants if <code>s</code> is an (optionally parenthesized)
4462identifier or
4463<a href="#Qualified_identifiers">qualified identifier</a>
4464denoting an array or pointer to array.
4465Otherwise invocations of <code>len</code> and <code>cap</code> are not
4466constant.
4467</p>
Robert Griesemer4dc25282008-09-09 10:37:19 -07004468
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004469<h3 id="Allocation">Allocation</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004470
Rob Pike96750f12009-02-27 16:47:48 -08004471<p>
4472The built-in function <code>new</code> takes a type <code>T</code> and
4473returns a value of type <code>*T</code>.
Robert Griesemera3294712009-01-05 11:17:26 -08004474The memory is initialized as described in the section on initial values
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004475<a href="#The_zero_value">The zero value</a>).
Rob Pike96750f12009-02-27 16:47:48 -08004476</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004477
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08004478<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08004479new(T)
4480</pre>
Robert Griesemer4dc25282008-09-09 10:37:19 -07004481
Rob Pike96750f12009-02-27 16:47:48 -08004482<p>
Robert Griesemer4dc25282008-09-09 10:37:19 -07004483For instance
Rob Pike96750f12009-02-27 16:47:48 -08004484</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004485
Robert Griesemerc2d55862009-02-19 16:49:10 -08004486<pre>
Robert Griesemerb94c0d22011-01-19 23:07:21 -05004487type S struct { a int; b float64 }
Robert Griesemerc2d55862009-02-19 16:49:10 -08004488new(S)
4489</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004490
Rob Pike96750f12009-02-27 16:47:48 -08004491<p>
4492dynamically allocates memory for a variable of type <code>S</code>,
4493initializes it (<code>a=0</code>, <code>b=0.0</code>),
4494and returns a value of type <code>*S</code> containing the address
4495of the memory.
4496</p>
4497
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004498<h3 id="Making_slices_maps_and_channels">Making slices, maps and channels</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004499
Robert Griesemerc2d55862009-02-19 16:49:10 -08004500<p>
Rob Pike96750f12009-02-27 16:47:48 -08004501Slices, maps and channels are reference types that do not require the
4502extra indirection of an allocation with <code>new</code>.
4503The built-in function <code>make</code> takes a type <code>T</code>,
4504which must be a slice, map or channel type,
4505optionally followed by a type-specific list of expressions.
4506It returns a value of type <code>T</code> (not <code>*T</code>).
Robert Griesemer633957b2009-01-06 13:23:20 -08004507The memory is initialized as described in the section on initial values
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004508<a href="#The_zero_value">The zero value</a>).
Rob Pike96750f12009-02-27 16:47:48 -08004509</p>
Robert Griesemer633957b2009-01-06 13:23:20 -08004510
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08004511<pre class="grammar">
Robert Griesemerdf674ff2010-05-04 17:31:40 -07004512Call Type T Result
4513
4514make(T, n) slice slice of type T with length n and capacity n
4515make(T, n, m) slice slice of type T with length n and capacity m
4516
4517make(T) map map of type T
4518make(T, n) map map of type T with initial space for n elements
4519
4520make(T) channel synchronous channel of type T
4521make(T, n) channel asynchronous channel of type T, buffer size n
Robert Griesemerc2d55862009-02-19 16:49:10 -08004522</pre>
Robert Griesemer633957b2009-01-06 13:23:20 -08004523
Robert Griesemer633957b2009-01-06 13:23:20 -08004524
Rob Pike96750f12009-02-27 16:47:48 -08004525<p>
Robert Griesemerdf674ff2010-05-04 17:31:40 -07004526The arguments <code>n</code> and <code>m</code> must be of integer type.
4527A <a href="#Run_time_panics">run-time panic</a> occurs if <code>n</code>
4528is negative or larger than <code>m</code>, or if <code>n</code> or
4529<code>m</code> cannot be represented by an <code>int</code>.
Rob Pike96750f12009-02-27 16:47:48 -08004530</p>
Robert Griesemer633957b2009-01-06 13:23:20 -08004531
Robert Griesemerc2d55862009-02-19 16:49:10 -08004532<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08004533s := make([]int, 10, 100) // slice with len(s) == 10, cap(s) == 100
4534s := make([]int, 10) // slice with len(s) == cap(s) == 10
4535c := make(chan int, 10) // channel with a buffer size of 10
4536m := make(map[string] int, 100) // map with initial space for 100 elements
Robert Griesemerc2d55862009-02-19 16:49:10 -08004537</pre>
Robert Griesemer633957b2009-01-06 13:23:20 -08004538
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004539
Robert Griesemer07e983a2010-10-25 16:50:31 -07004540<h3 id="Appending_and_copying_slices">Appending to and copying slices</h3>
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08004541
4542<p>
Robert Griesemer07e983a2010-10-25 16:50:31 -07004543Two built-in functions assist in common slice operations.
4544</p>
4545
4546<p>
4547The function <code>append</code> appends zero or more values <code>x</code>
Robert Griesemer904adfd2010-10-27 10:44:31 -07004548to a slice <code>s</code> and returns the resulting slice, with the same type
4549as s. Each value must be <a href="#Assignability">assignable</a> to the slice's
4550element type.
Robert Griesemer07e983a2010-10-25 16:50:31 -07004551</p>
4552
4553<pre class="grammar">
Robert Griesemer904adfd2010-10-27 10:44:31 -07004554append(s S, x ...T) S // S is assignable to []T
Robert Griesemer07e983a2010-10-25 16:50:31 -07004555</pre>
4556
4557<p>
4558If the capacity of <code>s</code> is not large enough to fit the additional
4559values, <code>append</code> allocates a new, sufficiently large slice that fits
4560both the existing slice elements and the additional values. Thus, the returned
4561slice may refer to a different underlying array.
4562</p>
4563
4564<pre>
4565s0 := []int{0, 0}
4566s1 := append(s0, 2) // append a single element s1 == []int{0, 0, 2}
4567s2 := append(s1, 3, 5, 7) // append multiple elements s2 == []int{0, 0, 2, 3, 5, 7}
4568s3 := append(s2, s0...) // append a slice s3 == []int{0, 0, 2, 3, 5, 7, 0, 0}
4569</pre>
4570
4571<p>
4572The function <code>copy</code> copies slice elements from
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08004573a source <code>src</code> to a destination <code>dst</code> and returns the
4574number of elements copied. Source and destination may overlap.
Robert Griesemer7bc03712010-06-07 15:49:39 -07004575Both arguments must have <a href="#Type_identity">identical</a> element type <code>T</code> and must be
Robert Griesemer425bbad2010-10-25 16:41:06 -07004576<a href="#Assignability">assignable</a> to a slice of type <code>[]T</code>.
4577The number of arguments copied is the minimum of
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08004578<code>len(src)</code> and <code>len(dst)</code>.
Robert Griesemer425bbad2010-10-25 16:41:06 -07004579As a special case, <code>copy</code> also accepts a destination argument assignable
4580to type <code>[]byte</code> with a source argument of a string type.
4581This form copies the bytes from the string into the byte slice.
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08004582</p>
4583
4584<pre class="grammar">
4585copy(dst, src []T) int
Robert Griesemer425bbad2010-10-25 16:41:06 -07004586copy(dst []byte, src string) int
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08004587</pre>
4588
4589<p>
4590Examples:
4591</p>
4592
4593<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08004594var a = [...]int{0, 1, 2, 3, 4, 5, 6, 7}
4595var s = make([]int, 6)
Robert Griesemer425bbad2010-10-25 16:41:06 -07004596var b = make([]byte, 5)
4597n1 := copy(s, a[0:]) // n1 == 6, s == []int{0, 1, 2, 3, 4, 5}
4598n2 := copy(s, s[2:]) // n2 == 4, s == []int{2, 3, 4, 5, 4, 5}
4599n3 := copy(b, "Hello, World!") // n3 == 5, b == []byte("Hello")
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08004600</pre>
4601
Rob Pike72970872010-03-04 12:35:16 -08004602<h3 id="Complex_numbers">Assembling and disassembling complex numbers</h3>
4603
4604<p>
4605Three functions assemble and disassemble complex numbers.
Robert Griesemerb94c0d22011-01-19 23:07:21 -05004606The built-in function <code>complex</code> constructs a complex
Rob Pike72970872010-03-04 12:35:16 -08004607value from a floating-point real and imaginary part, while
4608<code>real</code> and <code>imag</code>
4609extract the real and imaginary parts of a complex value.
4610</p>
4611
4612<pre class="grammar">
Robert Griesemerb94c0d22011-01-19 23:07:21 -05004613complex(realPart, imaginaryPart floatT) complexT
Rob Pike72970872010-03-04 12:35:16 -08004614real(complexT) floatT
4615imag(complexT) floatT
4616</pre>
4617
4618<p>
4619The type of the arguments and return value correspond.
Robert Griesemerb94c0d22011-01-19 23:07:21 -05004620For <code>complex</code>, the two arguments must be of the same
Rob Pike72970872010-03-04 12:35:16 -08004621floating-point type and the return type is the complex type
4622with the corresponding floating-point constituents:
Rob Pike72970872010-03-04 12:35:16 -08004623<code>complex64</code> for <code>float32</code>,
4624<code>complex128</code> for <code>float64</code>.
4625The <code>real</code> and <code>imag</code> functions
4626together form the inverse, so for a complex value <code>z</code>,
Robert Griesemerb94c0d22011-01-19 23:07:21 -05004627<code>z</code> <code>==</code> <code>complex(real(z),</code> <code>imag(z))</code>.
Rob Pike72970872010-03-04 12:35:16 -08004628</p>
4629
4630<p>
4631If the operands of these functions are all constants, the return
4632value is a constant.
4633</p>
4634
4635<pre>
Robert Griesemerb94c0d22011-01-19 23:07:21 -05004636var a = complex(2, -2) // complex128
4637var b = complex(1.0, -1.4) // complex128
4638x := float32(math.Cos(math.Pi/2)) // float32
4639var c64 = complex(5, -x) // complex64
4640var im = imag(b) // float64
4641var rl = real(c64) // float32
Rob Pike72970872010-03-04 12:35:16 -08004642</pre>
4643
Rob Pike5bb29fb2010-03-25 17:59:59 -07004644<h3 id="Handling_panics">Handling panics</h3>
4645
4646<p> Two built-in functions, <code>panic</code> and <code>recover</code>,
4647assist in reporting and handling <a href="#Run_time_panics">run-time panics</a>
4648and program-defined error conditions.
4649</p>
4650
4651<pre class="grammar">
4652func panic(interface{})
4653func recover() interface{}
4654</pre>
4655
4656<p>
Rob Pike5bb29fb2010-03-25 17:59:59 -07004657When a function <code>F</code> calls <code>panic</code>, normal
4658execution of <code>F</code> stops immediately. Any functions whose
4659execution was <a href="#Defer_statements">deferred</a> by the
4660invocation of <code>F</code> are run in the usual way, and then
4661<code>F</code> returns to its caller. To the caller, <code>F</code>
4662then behaves like a call to <code>panic</code>, terminating its own
4663execution and running deferred functions. This continues until all
4664functions in the goroutine have ceased execution, in reverse order.
4665At that point, the program is
4666terminated and the error condition is reported, including the value of
4667the argument to <code>panic</code>. This termination sequence is
4668called <i>panicking</i>.
4669</p>
4670
Robert Griesemer76f32282011-02-04 08:43:21 -08004671<pre>
4672panic(42)
4673panic("unreachable")
4674panic(Error("cannot parse"))
4675</pre>
4676
Rob Pike5bb29fb2010-03-25 17:59:59 -07004677<p>
4678The <code>recover</code> function allows a program to manage behavior
4679of a panicking goroutine. Executing a <code>recover</code> call
Robert Griesemer76f32282011-02-04 08:43:21 -08004680<i>inside</i> a deferred function (but not any function called by it) stops
Rob Pike5bb29fb2010-03-25 17:59:59 -07004681the panicking sequence by restoring normal execution, and retrieves
4682the error value passed to the call of <code>panic</code>. If
4683<code>recover</code> is called outside the deferred function it will
Robert Griesemer76f32282011-02-04 08:43:21 -08004684not stop a panicking sequence. In this case, or when the goroutine
4685is not panicking, or if the argument supplied to <code>panic</code>
4686was <code>nil</code>, <code>recover</code> returns <code>nil</code>.
Rob Pike5bb29fb2010-03-25 17:59:59 -07004687</p>
4688
4689<p>
Robert Griesemer76f32282011-02-04 08:43:21 -08004690The <code>protect</code> function in the example below invokes
4691the function argument <code>g</code> and protects callers from
4692run-time panics raised by <code>g</code>.
Rob Pike5bb29fb2010-03-25 17:59:59 -07004693</p>
4694
4695<pre>
Robert Griesemer76f32282011-02-04 08:43:21 -08004696func protect(g func()) {
Rob Pike5bb29fb2010-03-25 17:59:59 -07004697 defer func() {
Robert Griesemer76f32282011-02-04 08:43:21 -08004698 log.Println("done") // Println executes normally even in there is a panic
Rob Pike5bb29fb2010-03-25 17:59:59 -07004699 if x := recover(); x != nil {
Robert Griesemer76f32282011-02-04 08:43:21 -08004700 log.Printf("runtime panic: %v", x)
Rob Pike5bb29fb2010-03-25 17:59:59 -07004701 }
Robert Griesemer76f32282011-02-04 08:43:21 -08004702 }
4703 log.Println("start")
4704 g()
Rob Pike5bb29fb2010-03-25 17:59:59 -07004705}
4706</pre>
4707
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08004708
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004709<h3 id="Bootstrapping">Bootstrapping</h3>
4710
Robert Griesemer5eb36242009-09-16 11:05:14 -07004711<p>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004712Current implementations provide several built-in functions useful during
4713bootstrapping. These functions are documented for completeness but are not
4714guaranteed to stay in the language. They do not return a result.
Robert Griesemer5eb36242009-09-16 11:05:14 -07004715</p>
4716
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004717<pre class="grammar">
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07004718Function Behavior
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004719
4720print prints all arguments; formatting of arguments is implementation-specific
4721println like print but prints spaces between arguments and a newline at the end
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004722</pre>
4723
4724
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004725<h2 id="Packages">Packages</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004726
Rob Pike46596852009-03-02 16:17:29 -08004727<p>
4728Go programs are constructed by linking together <i>packages</i>.
Robert Griesemer326ef132009-09-28 19:21:15 -07004729A package in turn is constructed from one or more source files
4730that together declare constants, types, variables and functions
4731belonging to the package and which are accessible in all files
4732of the same package. Those elements may be
4733<a href="#Exported_identifiers">exported</a> and used in another package.
Rob Pike46596852009-03-02 16:17:29 -08004734</p>
4735
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004736<h3 id="Source_file_organization">Source file organization</h3>
Rob Pike46596852009-03-02 16:17:29 -08004737
4738<p>
4739Each source file consists of a package clause defining the package
4740to which it belongs, followed by a possibly empty set of import
4741declarations that declare packages whose contents it wishes to use,
4742followed by a possibly empty set of declarations of functions,
Robert Griesemer0a162a12009-08-19 16:44:04 -07004743types, variables, and constants.
Rob Pike46596852009-03-02 16:17:29 -08004744</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004745
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004746<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -08004747SourceFile = PackageClause ";" { ImportDecl ";" } { TopLevelDecl ";" } .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004748</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004749
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004750<h3 id="Package_clause">Package clause</h3>
Rob Pike46596852009-03-02 16:17:29 -08004751
Robert Griesemerc2d55862009-02-19 16:49:10 -08004752<p>
Rob Pike46596852009-03-02 16:17:29 -08004753A package clause begins each source file and defines the package
4754to which the file belongs.
4755</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004756
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004757<pre class="ebnf">
Robert Griesemer4e56b332009-09-10 10:14:00 -07004758PackageClause = "package" PackageName .
4759PackageName = identifier .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004760</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004761
Robert Griesemer4e56b332009-09-10 10:14:00 -07004762<p>
4763The PackageName must not be the <a href="#Blank_identifier">blank identifier</a>.
4764</p>
4765
Rob Pike46596852009-03-02 16:17:29 -08004766<pre>
4767package math
4768</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004769
Rob Pike46596852009-03-02 16:17:29 -08004770<p>
4771A set of files sharing the same PackageName form the implementation of a package.
4772An implementation may require that all source files for a package inhabit the same directory.
4773</p>
4774
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004775<h3 id="Import_declarations">Import declarations</h3>
Rob Pike46596852009-03-02 16:17:29 -08004776
4777<p>
Rob Pike3aec2e42009-09-25 17:00:22 -07004778An import declaration states that the source file containing the
4779declaration uses identifiers
4780<a href="#Exported_identifiers">exported</a> by the <i>imported</i>
4781package and enables access to them. The import names an
4782identifier (PackageName) to be used for access and an ImportPath
4783that specifies the package to be imported.
Rob Pike46596852009-03-02 16:17:29 -08004784</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004785
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004786<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -08004787ImportDecl = "import" ( ImportSpec | "(" { ImportSpec ";" } ")" ) .
Robert Griesemer997851e2009-09-25 15:36:25 -07004788ImportSpec = [ "." | PackageName ] ImportPath .
Robert Griesemer130ac742009-12-10 16:43:01 -08004789ImportPath = string_lit .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004790</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004791
Robert Griesemerc2d55862009-02-19 16:49:10 -08004792<p>
Rob Pike3aec2e42009-09-25 17:00:22 -07004793The PackageName is used in <a href="#Qualified_identifiers">qualified identifiers</a>
4794to access the exported identifiers of the package within the importing source file.
4795It is declared in the <a href="#Blocks">file block</a>.
4796If the PackageName is omitted, it defaults to the identifier specified in the
4797<a href="#Package_clauses">package clause</a> of the imported package.
4798If an explicit period (<code>.</code>) appears instead of a name, all the
4799package's exported identifiers will be declared in the current file's
4800file block and can be accessed without a qualifier.
Rob Pike46596852009-03-02 16:17:29 -08004801</p>
Russ Cox16b95ba2009-08-20 10:22:52 -07004802
Robert Griesemerc2d55862009-02-19 16:49:10 -08004803<p>
Rob Pike3aec2e42009-09-25 17:00:22 -07004804The interpretation of the ImportPath is implementation-dependent but
4805it is typically a substring of the full file name of the compiled
4806package and may be relative to a repository of installed packages.
4807</p>
4808
4809<p>
4810Assume we have compiled a package containing the package clause
4811<code>package math</code>, which exports function <code>Sin</code>, and
4812installed the compiled package in the file identified by
Rob Pike46596852009-03-02 16:17:29 -08004813<code>"lib/math"</code>.
Rob Pike3aec2e42009-09-25 17:00:22 -07004814This table illustrates how <code>Sin</code> may be accessed in files
4815that import the package after the
4816various types of import declaration.
Rob Pike46596852009-03-02 16:17:29 -08004817</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004818
Rob Pike46596852009-03-02 16:17:29 -08004819<pre class="grammar">
Robert Griesemer997851e2009-09-25 15:36:25 -07004820Import declaration Local name of Sin
Rob Pike46596852009-03-02 16:17:29 -08004821
Rob Pike46596852009-03-02 16:17:29 -08004822import "lib/math" math.Sin
Robert Griesemer997851e2009-09-25 15:36:25 -07004823import M "lib/math" M.Sin
Rob Pike46596852009-03-02 16:17:29 -08004824import . "lib/math" Sin
4825</pre>
4826
Robert Griesemer997851e2009-09-25 15:36:25 -07004827<p>
Rob Pike3aec2e42009-09-25 17:00:22 -07004828An import declaration declares a dependency relation between
4829the importing and imported package.
Robert Griesemer997851e2009-09-25 15:36:25 -07004830It is illegal for a package to import itself or to import a package without
4831referring to any of its exported identifiers. To import a package solely for
4832its side-effects (initialization), use the <a href="#Blank_identifier">blank</a>
4833identifier as explicit package name:
4834</p>
4835
4836<pre>
4837import _ "lib/math"
4838</pre>
4839
4840
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004841<h3 id="An_example_package">An example package</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004842
Robert Griesemerc2d55862009-02-19 16:49:10 -08004843<p>
Rob Pike46596852009-03-02 16:17:29 -08004844Here is a complete Go package that implements a concurrent prime sieve.
4845</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004846
Robert Griesemerc2d55862009-02-19 16:49:10 -08004847<pre>
4848package main
4849
Rob Pike46596852009-03-02 16:17:29 -08004850import "fmt"
4851
Robert Griesemerc2d55862009-02-19 16:49:10 -08004852// Send the sequence 2, 3, 4, ... to channel 'ch'.
Robert Griesemere1e76192009-09-25 14:11:03 -07004853func generate(ch chan&lt;- int) {
Robert Griesemerc2d55862009-02-19 16:49:10 -08004854 for i := 2; ; i++ {
Robert Griesemer130ac742009-12-10 16:43:01 -08004855 ch &lt;- i // Send 'i' to channel 'ch'.
Robert Griesemerc2d55862009-02-19 16:49:10 -08004856 }
4857}
4858
Fazlul Shahriar330139e2009-11-30 21:23:58 -08004859// Copy the values from channel 'src' to channel 'dst',
Robert Griesemerc2d55862009-02-19 16:49:10 -08004860// removing those divisible by 'prime'.
Robert Griesemere1e76192009-09-25 14:11:03 -07004861func filter(src &lt;-chan int, dst chan&lt;- int, prime int) {
4862 for i := range src { // Loop over values received from 'src'.
4863 if i%prime != 0 {
Robert Griesemer130ac742009-12-10 16:43:01 -08004864 dst &lt;- i // Send 'i' to channel 'dst'.
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004865 }
4866 }
Robert Griesemerc2d55862009-02-19 16:49:10 -08004867}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004868
Robert Griesemerc2d55862009-02-19 16:49:10 -08004869// The prime sieve: Daisy-chain filter processes together.
4870func sieve() {
Robert Griesemer130ac742009-12-10 16:43:01 -08004871 ch := make(chan int) // Create a new channel.
4872 go generate(ch) // Start generate() as a subprocess.
Robert Griesemerc2d55862009-02-19 16:49:10 -08004873 for {
Robert Griesemer130ac742009-12-10 16:43:01 -08004874 prime := &lt;-ch
4875 fmt.Print(prime, "\n")
4876 ch1 := make(chan int)
4877 go filter(ch, ch1, prime)
4878 ch = ch1
Robert Griesemerc2d55862009-02-19 16:49:10 -08004879 }
4880}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004881
Robert Griesemerc2d55862009-02-19 16:49:10 -08004882func main() {
Robert Griesemer130ac742009-12-10 16:43:01 -08004883 sieve()
Robert Griesemerc2d55862009-02-19 16:49:10 -08004884}
4885</pre>
Robert Griesemer67153582008-12-16 14:45:09 -08004886
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004887<h2 id="Program_initialization_and_execution">Program initialization and execution</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004888
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004889<h3 id="The_zero_value">The zero value</h3>
Rob Pike8f2330d2009-02-25 16:20:44 -08004890<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004891When memory is allocated to store a value, either through a declaration
Rob Pike678625d2009-09-15 09:54:22 -07004892or <code>make()</code> or <code>new()</code> call,
4893and no explicit initialization is provided, the memory is
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004894given a default initialization. Each element of such a value is
Robert Griesemeref45e642009-08-21 11:25:00 -07004895set to the <i>zero value</i> for its type: <code>false</code> for booleans,
Rob Pike8f2330d2009-02-25 16:20:44 -08004896<code>0</code> for integers, <code>0.0</code> for floats, <code>""</code>
Robert Griesemer19b1d352009-09-24 19:36:48 -07004897for strings, and <code>nil</code> for pointers, functions, interfaces, slices, channels, and maps.
Rob Pikeff70f092009-02-20 13:36:14 -08004898This initialization is done recursively, so for instance each element of an
Rob Pike8f2330d2009-02-25 16:20:44 -08004899array of structs will have its fields zeroed if no value is specified.
4900</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004901<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004902These two simple declarations are equivalent:
Rob Pike8f2330d2009-02-25 16:20:44 -08004903</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004904
Robert Griesemerc2d55862009-02-19 16:49:10 -08004905<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08004906var i int
4907var i int = 0
Robert Griesemerc2d55862009-02-19 16:49:10 -08004908</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004909
Rob Pike8f2330d2009-02-25 16:20:44 -08004910<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004911After
Rob Pike8f2330d2009-02-25 16:20:44 -08004912</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004913
Robert Griesemerc2d55862009-02-19 16:49:10 -08004914<pre>
Robert Griesemerb94c0d22011-01-19 23:07:21 -05004915type T struct { i int; f float64; next *T }
Robert Griesemer130ac742009-12-10 16:43:01 -08004916t := new(T)
Robert Griesemerc2d55862009-02-19 16:49:10 -08004917</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004918
Rob Pike8f2330d2009-02-25 16:20:44 -08004919<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004920the following holds:
Rob Pike8f2330d2009-02-25 16:20:44 -08004921</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004922
Robert Griesemerc2d55862009-02-19 16:49:10 -08004923<pre>
4924t.i == 0
4925t.f == 0.0
4926t.next == nil
4927</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004928
Rob Pike96750f12009-02-27 16:47:48 -08004929<p>
4930The same would also be true after
4931</p>
4932
4933<pre>
4934var t T
4935</pre>
4936
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004937<h3 id="Program_execution">Program execution</h3>
Rob Pike8f2330d2009-02-25 16:20:44 -08004938<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004939A package with no imports is initialized by assigning initial values to
Rob Pike8cb91842009-09-15 11:56:39 -07004940all its package-level variables
Rob Pike678625d2009-09-15 09:54:22 -07004941and then calling any
Rob Pike96750f12009-02-27 16:47:48 -08004942package-level function with the name and signature of
4943</p>
4944<pre>
4945func init()
4946</pre>
4947<p>
Rob Pike4fe41922009-11-07 22:00:59 -08004948defined in its source.
4949A package may contain multiple
4950<code>init()</code> functions, even
4951within a single source file; they execute
4952in unspecified order.
Rob Pike8f2330d2009-02-25 16:20:44 -08004953</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004954<p>
Rob Pike8cb91842009-09-15 11:56:39 -07004955Within a package, package-level variables are initialized,
4956and constant values are determined, in
4957data-dependent order: if the initializer of <code>A</code>
4958depends on the value of <code>B</code>, <code>A</code>
4959will be set after <code>B</code>.
4960It is an error if such dependencies form a cycle.
4961Dependency analysis is done lexically: <code>A</code>
4962depends on <code>B</code> if the value of <code>A</code>
4963contains a mention of <code>B</code>, contains a value
4964whose initializer
4965mentions <code>B</code>, or mentions a function that
4966mentions <code>B</code>, recursively.
4967If two items are not interdependent, they will be initialized
4968in the order they appear in the source.
Rob Pike01cadde2009-09-15 15:56:44 -07004969Since the dependency analysis is done per package, it can produce
4970unspecified results if <code>A</code>'s initializer calls a function defined
Rob Pike8cb91842009-09-15 11:56:39 -07004971in another package that refers to <code>B</code>.
4972</p>
4973<p>
Robert Griesemer566e3b22008-09-26 16:41:50 -07004974Initialization code may contain "go" statements, but the functions
Robert Griesemerd8a764c2009-02-06 17:01:10 -08004975they invoke do not begin execution until initialization of the entire
4976program is complete. Therefore, all initialization code is run in a single
Rob Pike96750f12009-02-27 16:47:48 -08004977goroutine.
Rob Pike8f2330d2009-02-25 16:20:44 -08004978</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004979<p>
Rob Pike96750f12009-02-27 16:47:48 -08004980An <code>init()</code> function cannot be referred to from anywhere
4981in a program. In particular, <code>init()</code> cannot be called explicitly,
4982nor can a pointer to <code>init</code> be assigned to a function variable.
Rob Pike8f2330d2009-02-25 16:20:44 -08004983</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004984<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004985If a package has imports, the imported packages are initialized
Robert Griesemer566e3b22008-09-26 16:41:50 -07004986before initializing the package itself. If multiple packages import
Rob Pike96750f12009-02-27 16:47:48 -08004987a package <code>P</code>, <code>P</code> will be initialized only once.
Rob Pike8f2330d2009-02-25 16:20:44 -08004988</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004989<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004990The importing of packages, by construction, guarantees that there can
4991be no cyclic dependencies in initialization.
Rob Pike8f2330d2009-02-25 16:20:44 -08004992</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004993<p>
Russ Coxa6736ca2011-02-03 13:40:51 -05004994A complete program is created by linking a single, unimported package
4995called the <i>main package</i> with all the packages it imports, transitively.
4996The main package must
4997have package name <code>main</code> and
4998declare a function <code>main</code> that takes no
4999arguments and returns no value.
Rob Pike8f2330d2009-02-25 16:20:44 -08005000</p>
Robert Griesemer4dc25282008-09-09 10:37:19 -07005001
Robert Griesemerc2d55862009-02-19 16:49:10 -08005002<pre>
Rob Pike96750f12009-02-27 16:47:48 -08005003func main() { ... }
Robert Griesemerc2d55862009-02-19 16:49:10 -08005004</pre>
Robert Griesemer4dc25282008-09-09 10:37:19 -07005005
Rob Pike8f2330d2009-02-25 16:20:44 -08005006<p>
Russ Coxa6736ca2011-02-03 13:40:51 -05005007Program execution begins by initializing the main package and then
5008invoking the function <code>main</code>.
Rob Pike8f2330d2009-02-25 16:20:44 -08005009</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08005010<p>
Russ Coxa6736ca2011-02-03 13:40:51 -05005011When the function <code>main</code> returns, the program exits.
5012It does not wait for other (non-<code>main</code>) goroutines to complete.
Rob Pike811dd252009-03-04 20:39:39 -08005013</p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08005014
Evan Shaw21110c72010-04-22 10:14:53 -07005015<h2 id="Run_time_panics">Run-time panics</h2>
Rob Pike5bb29fb2010-03-25 17:59:59 -07005016
5017<p>
5018Execution errors such as attempting to index an array out
5019of bounds trigger a <i>run-time panic</i> equivalent to a call of
5020the built-in function <a href="#Handling_panics"><code>panic</code></a>
5021with a value of the implementation-defined interface type <code>runtime.Error</code>.
5022That type defines at least the method
5023<code>String() string</code>. The exact error values that
5024represent distinct run-time error conditions are unspecified,
5025at least for now.
5026</p>
5027
5028<pre>
5029package runtime
5030
5031type Error interface {
5032 String() string
5033 // and perhaps others
5034}
5035</pre>
5036
Russ Cox7c4f7cc2009-08-20 11:11:03 -07005037<h2 id="System_considerations">System considerations</h2>
Robert Griesemer52c02c22009-02-11 13:46:30 -08005038
Russ Cox7c4f7cc2009-08-20 11:11:03 -07005039<h3 id="Package_unsafe">Package <code>unsafe</code></h3>
Robert Griesemer52c02c22009-02-11 13:46:30 -08005040
Robert Griesemerc2d55862009-02-19 16:49:10 -08005041<p>
Rob Pike96750f12009-02-27 16:47:48 -08005042The built-in package <code>unsafe</code>, known to the compiler,
5043provides facilities for low-level programming including operations
5044that violate the type system. A package using <code>unsafe</code>
5045must be vetted manually for type safety. The package provides the
5046following interface:
Rob Pikef27e9f02009-02-23 19:22:05 -08005047</p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08005048
Rob Pikeff70f092009-02-20 13:36:14 -08005049<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08005050package unsafe
Robert Griesemer52c02c22009-02-11 13:46:30 -08005051
Robert Griesemer98b4f6a2009-05-12 21:37:46 -07005052type ArbitraryType int // shorthand for an arbitrary Go type; it is not a real type
5053type Pointer *ArbitraryType
Robert Griesemer52c02c22009-02-11 13:46:30 -08005054
Robert Griesemer98b4f6a2009-05-12 21:37:46 -07005055func Alignof(variable ArbitraryType) int
5056func Offsetof(selector ArbitraryType) int
Robert Griesemer98b4f6a2009-05-12 21:37:46 -07005057func Sizeof(variable ArbitraryType) int
Rob Pike678625d2009-09-15 09:54:22 -07005058
Gustavo Niemeyerf8404ee2011-02-04 09:29:08 -08005059func Reflect(val interface{}) (typ runtime.Type, addr uintptr)
5060func Typeof(val interface{}) (typ interface{})
Rob Pike678625d2009-09-15 09:54:22 -07005061func Unreflect(typ runtime.Type, addr uintptr) interface{}
Robert Griesemerc2d55862009-02-19 16:49:10 -08005062</pre>
Robert Griesemer52c02c22009-02-11 13:46:30 -08005063
Robert Griesemerc2d55862009-02-19 16:49:10 -08005064<p>
Rob Pikef27e9f02009-02-23 19:22:05 -08005065Any pointer or value of type <code>uintptr</code> can be converted into
5066a <code>Pointer</code> and vice versa.
5067</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08005068<p>
Rob Pikef27e9f02009-02-23 19:22:05 -08005069The function <code>Sizeof</code> takes an expression denoting a
Robert Griesemer4023dce2009-08-14 17:41:52 -07005070variable of any type and returns the size of the variable in bytes.
Rob Pikef27e9f02009-02-23 19:22:05 -08005071</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08005072<p>
Russ Cox7c4f7cc2009-08-20 11:11:03 -07005073The function <code>Offsetof</code> takes a selector (§<a href="#Selectors">Selectors</a>) denoting a struct
Robert Griesemer52c02c22009-02-11 13:46:30 -08005074field of any type and returns the field offset in bytes relative to the
Rob Pike678625d2009-09-15 09:54:22 -07005075struct's address.
5076For a struct <code>s</code> with field <code>f</code>:
Rob Pikef27e9f02009-02-23 19:22:05 -08005077</p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08005078
Robert Griesemerc2d55862009-02-19 16:49:10 -08005079<pre>
Rob Pikef27e9f02009-02-23 19:22:05 -08005080uintptr(unsafe.Pointer(&amp;s)) + uintptr(unsafe.Offsetof(s.f)) == uintptr(unsafe.Pointer(&amp;s.f))
Robert Griesemerc2d55862009-02-19 16:49:10 -08005081</pre>
Robert Griesemer52c02c22009-02-11 13:46:30 -08005082
Robert Griesemerc2d55862009-02-19 16:49:10 -08005083<p>
Rob Pikef27e9f02009-02-23 19:22:05 -08005084Computer architectures may require memory addresses to be <i>aligned</i>;
5085that is, for addresses of a variable to be a multiple of a factor,
5086the variable's type's <i>alignment</i>. The function <code>Alignof</code>
5087takes an expression denoting a variable of any type and returns the
5088alignment of the (type of the) variable in bytes. For a variable
5089<code>x</code>:
5090</p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08005091
Robert Griesemerc2d55862009-02-19 16:49:10 -08005092<pre>
5093uintptr(unsafe.Pointer(&amp;x)) % uintptr(unsafe.Alignof(x)) == 0
5094</pre>
Robert Griesemer533dfd62009-05-13 16:56:00 -07005095
Rob Pikef27e9f02009-02-23 19:22:05 -08005096<p>
5097Calls to <code>Alignof</code>, <code>Offsetof</code>, and
Rob Pike678625d2009-09-15 09:54:22 -07005098<code>Sizeof</code> are compile-time constant expressions of type <code>int</code>.
Rob Pikef27e9f02009-02-23 19:22:05 -08005099</p>
Robert Griesemer98b4f6a2009-05-12 21:37:46 -07005100<p>
Rob Pike678625d2009-09-15 09:54:22 -07005101The functions <code>unsafe.Typeof</code>,
5102<code>unsafe.Reflect</code>,
Russ Cox13dac652009-09-28 14:16:33 -07005103and <code>unsafe.Unreflect</code> allow access at run time to the dynamic
Rob Pike678625d2009-09-15 09:54:22 -07005104types and values stored in interfaces.
5105<code>Typeof</code> returns a representation of
5106<code>val</code>'s
5107dynamic type as a <code>runtime.Type</code>.
5108<code>Reflect</code> allocates a copy of
5109<code>val</code>'s dynamic
5110value and returns both the type and the address of the copy.
5111<code>Unreflect</code> inverts <code>Reflect</code>,
5112creating an
5113interface value from a type and address.
Russ Cox16205a32010-01-18 15:59:14 -08005114The <a href="/pkg/reflect/"><code>reflect</code> package</a> built on these primitives
Rob Pike678625d2009-09-15 09:54:22 -07005115provides a safe, more convenient way to inspect interface values.
Robert Griesemer98b4f6a2009-05-12 21:37:46 -07005116</p>
Robert Griesemer6f8df7a2009-02-11 21:57:15 -08005117
Robert Griesemer52c02c22009-02-11 13:46:30 -08005118
Russ Cox7c4f7cc2009-08-20 11:11:03 -07005119<h3 id="Size_and_alignment_guarantees">Size and alignment guarantees</h3>
Robert Griesemer52c02c22009-02-11 13:46:30 -08005120
Robert Griesemer997851e2009-09-25 15:36:25 -07005121<p>
Russ Cox7c4f7cc2009-08-20 11:11:03 -07005122For the numeric types (§<a href="#Numeric_types">Numeric types</a>), the following sizes are guaranteed:
Robert Griesemer997851e2009-09-25 15:36:25 -07005123</p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08005124
Rob Pikeff70f092009-02-20 13:36:14 -08005125<pre class="grammar">
Robert Griesemer777a96a2010-12-02 12:32:14 -08005126type size in bytes
Robert Griesemer52c02c22009-02-11 13:46:30 -08005127
Robert Griesemer777a96a2010-12-02 12:32:14 -08005128byte, uint8, int8 1
5129uint16, int16 2
5130uint32, int32, float32 4
5131uint64, int64, float64, complex64 8
5132complex128 16
Robert Griesemerc2d55862009-02-19 16:49:10 -08005133</pre>
Robert Griesemer52c02c22009-02-11 13:46:30 -08005134
Robert Griesemerc2d55862009-02-19 16:49:10 -08005135<p>
Rob Pikef27e9f02009-02-23 19:22:05 -08005136The following minimal alignment properties are guaranteed:
Rob Pike4501d342009-02-19 17:31:36 -08005137</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08005138<ol>
Robert Griesemerdd916be2011-01-10 14:25:17 -08005139<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 -07005140</li>
Robert Griesemer52c02c22009-02-11 13:46:30 -08005141
Rob Pikef27e9f02009-02-23 19:22:05 -08005142<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 -08005143 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 -07005144</li>
Robert Griesemer52c02c22009-02-11 13:46:30 -08005145
Rob Pikef27e9f02009-02-23 19:22:05 -08005146<li>For a variable <code>x</code> of array type: <code>unsafe.Alignof(x)</code> is the same as
5147 <code>unsafe.Alignof(x[0])</code>, but at least 1.
Robert Griesemer3af480372010-05-14 13:11:48 -07005148</li>
Robert Griesemerc2d55862009-02-19 16:49:10 -08005149</ol>
Robert Griesemer52c02c22009-02-11 13:46:30 -08005150
Robert Griesemer90cc4a52009-10-22 09:41:38 -07005151<h2 id="Implementation_differences"><span class="alert">Implementation differences - TODO</span></h2>
Robert Griesemer237c8ab2009-09-01 14:07:30 -07005152<ul>
Robert Griesemer90cc4a52009-10-22 09:41:38 -07005153 <li><span class="alert">Implementation does not honor the restriction on goto statements and targets (no intervening declarations).</span></li>
Robert Griesemer11684682010-10-29 11:44:48 -07005154 <li><span class="alert">Gccgo: The <code>append</code> built-in function is not yet implemented.</span></li>
Russ Cox00ffd592010-09-28 13:43:50 -04005155 <li><span class="alert">Gccgo: Method expressions are partially implemented.</span></li>
Russ Cox6aad4192010-04-13 20:55:57 -07005156 <li><span class="alert">Gccgo: allows only one init() function per source file.</span></li>
Robert Griesemer237c8ab2009-09-01 14:07:30 -07005157</ul>