blob: 84480f6e8a9f224a976bf0288cd906460a48c0b2 [file] [log] [blame]
Robert Griesemere8e49872010-03-30 17:37:42 -07001<!-- title The Go Programming Language Specification -->
2<!-- subtitle Version of March 25, 2010 -->
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003
Robert Griesemerc59d2f12008-09-09 10:48:14 -07004<!--
Robert Griesemer19b1d352009-09-24 19:36:48 -07005Todo
6[ ] clarify: two equal lowercase identifiers from different packages denote different objects
Robert Griesemer4b908332009-08-07 17:05:41 -07007[ ] need language about function/method calls and parameter passing rules
Rob Pike72970872010-03-04 12:35:16 -08008[ ] last paragraph of #Assignments (constant promotion) should be elsewhere
9 and mention assignment to empty interface.
Robert Griesemer0a162a12009-08-19 16:44:04 -070010[ ] need to say something about "scope" of selectors?
Robert Griesemer4b908332009-08-07 17:05:41 -070011[ ] clarify what a field name is in struct declarations
12 (struct{T} vs struct {T T} vs struct {t T})
Robert Griesemer7539c852009-07-31 18:05:07 -070013[ ] need explicit language about the result type of operations
14[ ] may want to have some examples for the types of shift operations
Russ Cox60ff8cc2009-10-20 08:27:14 -070015[ ] should string(1<<s) and float(1<<s) be valid?
Robert Griesemer40d6bb52009-04-20 15:32:20 -070016[ ] should probably write something about evaluation order of statements even
17 though obvious
Robert Griesemere1b8cb82009-07-16 20:31:41 -070018[ ] specify iteration direction for range clause
19[ ] review language on implicit dereferencing
Robert Griesemerc59d2f12008-09-09 10:48:14 -070020-->
21
Robert Griesemer40d6bb52009-04-20 15:32:20 -070022
Russ Cox7c4f7cc2009-08-20 11:11:03 -070023<h2 id="Introduction">Introduction</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -070024
Robert Griesemerc2d55862009-02-19 16:49:10 -080025<p>
Rob Pike4501d342009-02-19 17:31:36 -080026This is a reference manual for the Go programming language. For
Rob Pike9339e072009-11-02 15:28:41 -080027more information and other documents, see <a href="http://golang.org/">http://golang.org</a>.
Rob Pike4501d342009-02-19 17:31:36 -080028</p>
Robert Griesemer67153582008-12-16 14:45:09 -080029
Robert Griesemerc2d55862009-02-19 16:49:10 -080030<p>
Rob Pike4501d342009-02-19 17:31:36 -080031Go is a general-purpose language designed with systems programming
Rob Pike678625d2009-09-15 09:54:22 -070032in mind. It is strongly typed and garbage-collected and has explicit
Rob Pike4501d342009-02-19 17:31:36 -080033support for concurrent programming. Programs are constructed from
34<i>packages</i>, whose properties allow efficient management of
35dependencies. The existing implementations use a traditional
36compile/link model to generate executable binaries.
37</p>
38
Robert Griesemerc2d55862009-02-19 16:49:10 -080039<p>
Rob Pike4501d342009-02-19 17:31:36 -080040The grammar is compact and regular, allowing for easy analysis by
41automatic tools such as integrated development environments.
42</p>
Larry Hosken698c6c02009-09-17 08:05:12 -070043
Russ Cox7c4f7cc2009-08-20 11:11:03 -070044<h2 id="Notation">Notation</h2>
Rob Pike4501d342009-02-19 17:31:36 -080045<p>
Robert Griesemer4d230302008-12-17 15:39:15 -080046The syntax is specified using Extended Backus-Naur Form (EBNF):
Rob Pike4501d342009-02-19 17:31:36 -080047</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -070048
Rob Pikeff70f092009-02-20 13:36:14 -080049<pre class="grammar">
Robert Griesemer88a0c402009-04-23 14:42:21 -070050Production = production_name "=" Expression "." .
Rob Pike4501d342009-02-19 17:31:36 -080051Expression = Alternative { "|" Alternative } .
Robert Griesemerc2d55862009-02-19 16:49:10 -080052Alternative = Term { Term } .
Rob Pike4501d342009-02-19 17:31:36 -080053Term = production_name | token [ "..." token ] | Group | Option | Repetition .
54Group = "(" Expression ")" .
Rob Pikec956e902009-04-14 20:10:49 -070055Option = "[" Expression "]" .
Rob Pike4501d342009-02-19 17:31:36 -080056Repetition = "{" Expression "}" .
Robert Griesemerc2d55862009-02-19 16:49:10 -080057</pre>
Robert Griesemerbbfe3122008-10-09 17:12:09 -070058
Rob Pike4501d342009-02-19 17:31:36 -080059<p>
60Productions are expressions constructed from terms and the following
61operators, in increasing precedence:
62</p>
Rob Pikeff70f092009-02-20 13:36:14 -080063<pre class="grammar">
Rob Pike4501d342009-02-19 17:31:36 -080064| alternation
65() grouping
66[] option (0 or 1 times)
67{} repetition (0 to n times)
Robert Griesemerc2d55862009-02-19 16:49:10 -080068</pre>
Robert Griesemer4d230302008-12-17 15:39:15 -080069
Robert Griesemerc2d55862009-02-19 16:49:10 -080070<p>
Rob Pike4501d342009-02-19 17:31:36 -080071Lower-case production names are used to identify lexical tokens.
72Non-terminals are in CamelCase. Lexical symbols are enclosed in
Rob Pike678625d2009-09-15 09:54:22 -070073double quotes <code>""</code> or back quotes <code>``</code>.
Rob Pike4501d342009-02-19 17:31:36 -080074</p>
75
Robert Griesemerc2d55862009-02-19 16:49:10 -080076<p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -070077The form <code>a ... b</code> represents the set of characters from
Rob Pikef27e9f02009-02-23 19:22:05 -080078<code>a</code> through <code>b</code> as alternatives.
Rob Pike4501d342009-02-19 17:31:36 -080079</p>
80
Russ Cox7c4f7cc2009-08-20 11:11:03 -070081<h2 id="Source_code_representation">Source code representation</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -070082
Robert Griesemerc2d55862009-02-19 16:49:10 -080083<p>
Robert Griesemere9192752009-12-01 16:15:53 -080084Source code is Unicode text encoded in
85<a href="http://en.wikipedia.org/wiki/UTF-8">UTF-8</a>. The text is not
Rob Pikeff70f092009-02-20 13:36:14 -080086canonicalized, so a single accented code point is distinct from the
87same character constructed from combining an accent and a letter;
88those are treated as two code points. For simplicity, this document
89will use the term <i>character</i> to refer to a Unicode code point.
90</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -080091<p>
Rob Pikeff70f092009-02-20 13:36:14 -080092Each code point is distinct; for instance, upper and lower case letters
93are different characters.
94</p>
Russ Coxb7d9ffe2010-02-16 16:47:18 -080095<p>
Robert Griesemerf42e8832010-02-17 15:50:34 -080096Implementation restriction: For compatibility with other tools, a
97compiler may disallow the NUL character (U+0000) in the source text.
Russ Coxb7d9ffe2010-02-16 16:47:18 -080098</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -070099
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700100<h3 id="Characters">Characters</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700101
Robert Griesemerc2d55862009-02-19 16:49:10 -0800102<p>
Rob Pike4501d342009-02-19 17:31:36 -0800103The following terms are used to denote specific Unicode character classes:
104</p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700105<pre class="ebnf">
106unicode_char = /* an arbitrary Unicode code point */ .
107unicode_letter = /* a Unicode code point classified as "Letter" */ .
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700108unicode_digit = /* a Unicode code point classified as "Digit" */ .
109</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700110
Rob Pike678625d2009-09-15 09:54:22 -0700111<p>
Robert Griesemere9192752009-12-01 16:15:53 -0800112In <a href="http://www.unicode.org/versions/Unicode5.2.0/">The Unicode Standard 5.2</a>,
Rob Pike678625d2009-09-15 09:54:22 -0700113Section 4.5 General Category-Normative
114defines a set of character categories. Go treats
115those characters in category Lu, Ll, Lt, Lm, or Lo as Unicode letters,
116and those in category Nd as Unicode digits.
117</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700118
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700119<h3 id="Letters_and_digits">Letters and digits</h3>
Rob Pikeff70f092009-02-20 13:36:14 -0800120
121<p>
Rob Pikef27e9f02009-02-23 19:22:05 -0800122The underscore character <code>_</code> (U+005F) is considered a letter.
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700123</p>
124<pre class="ebnf">
Robert Griesemerc2d55862009-02-19 16:49:10 -0800125letter = unicode_letter | "_" .
126decimal_digit = "0" ... "9" .
127octal_digit = "0" ... "7" .
128hex_digit = "0" ... "9" | "A" ... "F" | "a" ... "f" .
129</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700130
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700131<h2 id="Lexical_elements">Lexical elements</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700132
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700133<h3 id="Comments">Comments</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700134
Rob Pikeff70f092009-02-20 13:36:14 -0800135<p>
Robert Griesemer130ac742009-12-10 16:43:01 -0800136There are two forms of comments:
Rob Pikeff70f092009-02-20 13:36:14 -0800137</p>
138
Robert Griesemer130ac742009-12-10 16:43:01 -0800139<ol>
140<li>
141<i>Line comments</i> start with the character sequence <code>//</code>
142and continue through the next newline. A line comment acts like a newline.
143</li>
144<li>
145<i>General comments</i> start with the character sequence <code>/*</code>
146and continue through the character sequence <code>*/</code>. A general
147comment that spans multiple lines acts like a newline, otherwise it acts
148like a space.
149</li>
150</ol>
151
152<p>
153Comments do not nest.
154</p>
155
156
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700157<h3 id="Tokens">Tokens</h3>
Rob Pikeff70f092009-02-20 13:36:14 -0800158
159<p>
160Tokens form the vocabulary of the Go language.
Robert Griesemereb109a72009-12-28 14:40:42 -0800161There are four classes: <i>identifiers</i>, <i>keywords</i>, <i>operators
162and delimiters</i>, and <i>literals</i>. <i>White space</i>, formed from
Rob Pike4fe41922009-11-07 22:00:59 -0800163spaces (U+0020), horizontal tabs (U+0009),
164carriage returns (U+000D), and newlines (U+000A),
165is ignored except as it separates tokens
Robert Griesemereb109a72009-12-28 14:40:42 -0800166that would otherwise combine into a single token. Also, a newline
167may trigger the insertion of a <a href="#Semicolons">semicolon</a>.
Robert Griesemer130ac742009-12-10 16:43:01 -0800168While breaking the input into tokens,
Rob Pikeff70f092009-02-20 13:36:14 -0800169the next token is the longest sequence of characters that form a
170valid token.
171</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700172
Robert Griesemer130ac742009-12-10 16:43:01 -0800173<h3 id="Semicolons">Semicolons</h3>
174
175<p>
176The formal grammar uses semicolons <code>";"</code> as terminators in
177a number of productions. Go programs may omit most of these semicolons
178using the following two rules:
179</p>
180
181<ol>
182<li>
183<p>
184When the input is broken into tokens, a semicolon is automatically inserted
185into the token stream at the end of a non-blank line if the line's final
186token is
187</p>
188<ul>
Robert Griesemer30e5ed22010-01-04 17:28:02 -0800189 <li>an identifier
190 <li>an integer, floating-point, character, or string literal
Robert Griesemer130ac742009-12-10 16:43:01 -0800191 <li>one of the keywords
192 <code>break</code>, <code>continue</code>, <code>fallthrough</code>,
193 or <code>return</code>
194 </li>
195 <li>one of the operators and delimiters
196 <code>++</code>, <code>--</code>, <code>)</code>, <code>]</code>,
197 or <code>}</code>
198 </li>
199</ul>
200</li>
201
202<li>
203To allow complex statements to occupy a single line, a semicolon
204may be omitted before a closing <code>")"</code> or <code>"}"</code>.
205</li>
206</ol>
207
208<p>
209To reflect idiomatic use, code examples in this document elide semicolons
210using these rules.
211</p>
212
213
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700214<h3 id="Identifiers">Identifiers</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700215
Rob Pikeff70f092009-02-20 13:36:14 -0800216<p>
217Identifiers name program entities such as variables and types.
218An identifier is a sequence of one or more letters and digits.
219The first character in an identifier must be a letter.
220</p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700221<pre class="ebnf">
Robert Griesemerd36d1912009-09-18 11:58:35 -0700222identifier = letter { letter | unicode_digit } .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800223</pre>
Robert Griesemerc2d55862009-02-19 16:49:10 -0800224<pre>
225a
226_x9
227ThisVariableIsExported
228αβ
229</pre>
Robert Griesemer130ac742009-12-10 16:43:01 -0800230
231<p>
Robert Griesemer4e56b332009-09-10 10:14:00 -0700232Some identifiers are <a href="#Predeclared_identifiers">predeclared</a>.
Robert Griesemer130ac742009-12-10 16:43:01 -0800233</p>
234
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -0700235
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700236<h3 id="Keywords">Keywords</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700237
Rob Pikeff70f092009-02-20 13:36:14 -0800238<p>
239The following keywords are reserved and may not be used as identifiers.
240</p>
241<pre class="grammar">
242break default func interface select
243case defer go map struct
244chan else goto package switch
245const fallthrough if range type
246continue for import return var
247</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700248
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700249<h3 id="Operators_and_Delimiters">Operators and Delimiters</h3>
Rob Pikeff70f092009-02-20 13:36:14 -0800250
251<p>
Robert Griesemerd36d1912009-09-18 11:58:35 -0700252The following character sequences represent <a href="#Operators">operators</a>, delimiters, and other special tokens:
Rob Pikeff70f092009-02-20 13:36:14 -0800253</p>
254<pre class="grammar">
255+ &amp; += &amp;= &amp;&amp; == != ( )
256- | -= |= || &lt; &lt;= [ ]
257* ^ *= ^= &lt;- &gt; &gt;= { }
Robert Griesemer4ed666e2009-08-27 16:45:42 -0700258/ &lt;&lt; /= &lt;&lt;= ++ = := , ;
259% &gt;&gt; %= &gt;&gt;= -- ! ... . :
Rob Pikecd04ec92009-03-11 21:59:05 -0700260 &amp;^ &amp;^=
Rob Pikeff70f092009-02-20 13:36:14 -0800261</pre>
262
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700263<h3 id="Integer_literals">Integer literals</h3>
Rob Pikeff70f092009-02-20 13:36:14 -0800264
265<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700266An integer literal is a sequence of digits representing an
267<a href="#Constants">integer constant</a>.
268An optional prefix sets a non-decimal base: <code>0</code> for octal, <code>0x</code> or
Rob Pikef27e9f02009-02-23 19:22:05 -0800269<code>0X</code> for hexadecimal. In hexadecimal literals, letters
270<code>a-f</code> and <code>A-F</code> represent values 10 through 15.
Rob Pikeff70f092009-02-20 13:36:14 -0800271</p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700272<pre class="ebnf">
Robert Griesemerd36d1912009-09-18 11:58:35 -0700273int_lit = decimal_lit | octal_lit | hex_lit .
274decimal_lit = ( "1" ... "9" ) { decimal_digit } .
275octal_lit = "0" { octal_digit } .
276hex_lit = "0" ( "x" | "X" ) hex_digit { hex_digit } .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800277</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700278
Robert Griesemerc2d55862009-02-19 16:49:10 -0800279<pre>
28042
2810600
2820xBadFace
283170141183460469231731687303715884105727
284</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700285
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700286<h3 id="Floating-point_literals">Floating-point literals</h3>
Rob Pikeff70f092009-02-20 13:36:14 -0800287<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700288A floating-point literal is a decimal representation of a
289<a href="#Constants">floating-point constant</a>.
290It has an integer part, a decimal point, a fractional part,
Rob Pikeff70f092009-02-20 13:36:14 -0800291and an exponent part. The integer and fractional part comprise
Rob Pikef27e9f02009-02-23 19:22:05 -0800292decimal digits; the exponent part is an <code>e</code> or <code>E</code>
Rob Pikeff70f092009-02-20 13:36:14 -0800293followed by an optionally signed decimal exponent. One of the
294integer part or the fractional part may be elided; one of the decimal
295point or the exponent may be elided.
296</p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700297<pre class="ebnf">
Robert Griesemerd36d1912009-09-18 11:58:35 -0700298float_lit = decimals "." [ decimals ] [ exponent ] |
299 decimals exponent |
300 "." decimals [ exponent ] .
301decimals = decimal_digit { decimal_digit } .
302exponent = ( "e" | "E" ) [ "+" | "-" ] decimals .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800303</pre>
Robert Griesemerad711102008-09-11 17:48:20 -0700304
Robert Griesemerc2d55862009-02-19 16:49:10 -0800305<pre>
3060.
Rob Pike72970872010-03-04 12:35:16 -080030772.40
308072.40 // == 72.40
Robert Griesemerc2d55862009-02-19 16:49:10 -08003092.71828
3101.e+0
3116.67428e-11
3121E6
313.25
314.12345E+5
315</pre>
Robert Griesemerad711102008-09-11 17:48:20 -0700316
Rob Pike72970872010-03-04 12:35:16 -0800317<h3 id="Imaginary_literals">Imaginary literals</h3>
318<p>
319An imaginary literal is a decimal representation of the imaginary part of a
320<a href="#Constants">complex constant</a>.
321It consists of a
322<a href="#Floating-point_literals">floating-point literal</a>
323or decimal integer followed
324by the lower-case letter <code>i</code>.
325</p>
326<pre class="ebnf">
327imaginary_lit = (decimals | float_lit) "i" .
328</pre>
329
330<pre>
3310i
332011i // == 11i
3330.i
3342.71828i
3351.e+0i
3366.67428e-11i
3371E6i
338.25i
339.12345E+5i
340</pre>
341
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700342
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700343<h3 id="Character_literals">Character literals</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700344
Rob Pike4501d342009-02-19 17:31:36 -0800345<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700346A character literal represents an <a href="#Constants">integer constant</a>,
347typically a Unicode code point, as one or more characters enclosed in single
Rob Pikeff70f092009-02-20 13:36:14 -0800348quotes. Within the quotes, any character may appear except single
349quote and newline. A single quoted character represents itself,
350while multi-character sequences beginning with a backslash encode
351values in various formats.
Rob Pike4501d342009-02-19 17:31:36 -0800352</p>
Rob Pikeff70f092009-02-20 13:36:14 -0800353<p>
354The simplest form represents the single character within the quotes;
355since Go source text is Unicode characters encoded in UTF-8, multiple
356UTF-8-encoded bytes may represent a single integer value. For
Rob Pikef27e9f02009-02-23 19:22:05 -0800357instance, the literal <code>'a'</code> holds a single byte representing
358a literal <code>a</code>, Unicode U+0061, value <code>0x61</code>, while
359<code>'ä'</code> holds two bytes (<code>0xc3</code> <code>0xa4</code>) representing
360a literal <code>a</code>-dieresis, U+00E4, value <code>0xe4</code>.
Rob Pikeff70f092009-02-20 13:36:14 -0800361</p>
362<p>
363Several backslash escapes allow arbitrary values to be represented
364as ASCII text. There are four ways to represent the integer value
Rob Pikef27e9f02009-02-23 19:22:05 -0800365as a numeric constant: <code>\x</code> followed by exactly two hexadecimal
366digits; <code>\u</code> followed by exactly four hexadecimal digits;
367<code>\U</code> followed by exactly eight hexadecimal digits, and a
368plain backslash <code>\</code> followed by exactly three octal digits.
Rob Pikeff70f092009-02-20 13:36:14 -0800369In each case the value of the literal is the value represented by
370the digits in the corresponding base.
371</p>
372<p>
373Although these representations all result in an integer, they have
374different valid ranges. Octal escapes must represent a value between
Rob Pike678625d2009-09-15 09:54:22 -07003750 and 255 inclusive. Hexadecimal escapes satisfy this condition
376by construction. The escapes <code>\u</code> and <code>\U</code>
Rob Pikeff70f092009-02-20 13:36:14 -0800377represent Unicode code points so within them some values are illegal,
Rob Pikef27e9f02009-02-23 19:22:05 -0800378in particular those above <code>0x10FFFF</code> and surrogate halves.
Rob Pikeff70f092009-02-20 13:36:14 -0800379</p>
380<p>
381After a backslash, certain single-character escapes represent special values:
382</p>
383<pre class="grammar">
384\a U+0007 alert or bell
385\b U+0008 backspace
386\f U+000C form feed
387\n U+000A line feed or newline
388\r U+000D carriage return
389\t U+0009 horizontal tab
390\v U+000b vertical tab
391\\ U+005c backslash
392\' U+0027 single quote (valid escape only within character literals)
393\" U+0022 double quote (valid escape only within string literals)
Robert Griesemerc2d55862009-02-19 16:49:10 -0800394</pre>
Robert Griesemerc2d55862009-02-19 16:49:10 -0800395<p>
Rob Pike4fe41922009-11-07 22:00:59 -0800396All other sequences starting with a backslash are illegal inside character literals.
Rob Pike4501d342009-02-19 17:31:36 -0800397</p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700398<pre class="ebnf">
Rob Pikeff70f092009-02-20 13:36:14 -0800399char_lit = "'" ( unicode_value | byte_value ) "'" .
400unicode_value = unicode_char | little_u_value | big_u_value | escaped_char .
401byte_value = octal_byte_value | hex_byte_value .
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700402octal_byte_value = `\` octal_digit octal_digit octal_digit .
403hex_byte_value = `\` "x" hex_digit hex_digit .
404little_u_value = `\` "u" hex_digit hex_digit hex_digit hex_digit .
405big_u_value = `\` "U" hex_digit hex_digit hex_digit hex_digit
Rob Pikeff70f092009-02-20 13:36:14 -0800406 hex_digit hex_digit hex_digit hex_digit .
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700407escaped_char = `\` ( "a" | "b" | "f" | "n" | "r" | "t" | "v" | `\` | "'" | `"` ) .
Rob Pikeff70f092009-02-20 13:36:14 -0800408</pre>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700409
Robert Griesemerc2d55862009-02-19 16:49:10 -0800410<pre>
411'a'
412'ä'
413'本'
414'\t'
415'\000'
416'\007'
417'\377'
418'\x07'
419'\xff'
420'\u12e4'
421'\U00101234'
422</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700423
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700424
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700425<h3 id="String_literals">String literals</h3>
Rob Pikeff70f092009-02-20 13:36:14 -0800426
427<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700428A string literal represents a <a href="#Constants">string constant</a>
429obtained from concatenating a sequence of characters. There are two forms:
430raw string literals and interpreted string literals.
Rob Pikeff70f092009-02-20 13:36:14 -0800431</p>
432<p>
433Raw string literals are character sequences between back quotes
Rob Pikef27e9f02009-02-23 19:22:05 -0800434<code>``</code>. Within the quotes, any character is legal except
Robert Griesemerdb7a6222009-06-18 13:51:14 -0700435back quote. The value of a raw string literal is the
436string composed of the uninterpreted characters between the quotes;
437in particular, backslashes have no special meaning and the string may
438span multiple lines.
Rob Pikeff70f092009-02-20 13:36:14 -0800439</p>
440<p>
441Interpreted string literals are character sequences between double
Rob Pike4fe41922009-11-07 22:00:59 -0800442quotes <code>&quot;&quot;</code>. The text between the quotes,
443which may not span multiple lines, forms the
Rob Pikeff70f092009-02-20 13:36:14 -0800444value of the literal, with backslash escapes interpreted as they
Rob Pikef27e9f02009-02-23 19:22:05 -0800445are in character literals (except that <code>\'</code> is illegal and
Robert Griesemere9192752009-12-01 16:15:53 -0800446<code>\"</code> is legal). The three-digit octal (<code>\</code><i>nnn</i>)
447and two-digit hexadecimal (<code>\x</code><i>nn</i>) escapes represent individual
Rob Pikeff70f092009-02-20 13:36:14 -0800448<i>bytes</i> of the resulting string; all other escapes represent
449the (possibly multi-byte) UTF-8 encoding of individual <i>characters</i>.
Rob Pikef27e9f02009-02-23 19:22:05 -0800450Thus inside a string literal <code>\377</code> and <code>\xFF</code> represent
451a single byte of value <code>0xFF</code>=255, while <code>ÿ</code>,
452<code>\u00FF</code>, <code>\U000000FF</code> and <code>\xc3\xbf</code> represent
Robert Griesemere1e76192009-09-25 14:11:03 -0700453the two bytes <code>0xc3</code> <code>0xbf</code> of the UTF-8 encoding of character
Rob Pikeff70f092009-02-20 13:36:14 -0800454U+00FF.
455</p>
456
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700457<pre class="ebnf">
Rob Pikeff70f092009-02-20 13:36:14 -0800458string_lit = raw_string_lit | interpreted_string_lit .
459raw_string_lit = "`" { unicode_char } "`" .
Robert Griesemerd4d4ff02009-10-19 13:13:59 -0700460interpreted_string_lit = `"` { unicode_value | byte_value } `"` .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800461</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700462
Robert Griesemerc2d55862009-02-19 16:49:10 -0800463<pre>
Robert Griesemerdb7a6222009-06-18 13:51:14 -0700464`abc` // same as "abc"
465`\n
466\n` // same as "\\n\n\\n"
Robert Griesemerc2d55862009-02-19 16:49:10 -0800467"\n"
468""
469"Hello, world!\n"
470"日本語"
471"\u65e5本\U00008a9e"
472"\xff\u00FF"
473</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700474
Rob Pikeff70f092009-02-20 13:36:14 -0800475<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700476These examples all represent the same string:
Rob Pikeff70f092009-02-20 13:36:14 -0800477</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700478
Robert Griesemerc2d55862009-02-19 16:49:10 -0800479<pre>
Rob Pikeff70f092009-02-20 13:36:14 -0800480"日本語" // UTF-8 input text
481`日本語` // UTF-8 input text as a raw literal
482"\u65e5\u672c\u8a9e" // The explicit Unicode code points
483"\U000065e5\U0000672c\U00008a9e" // The explicit Unicode code points
Robert Griesemerc2d55862009-02-19 16:49:10 -0800484"\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e" // The explicit UTF-8 bytes
485</pre>
Robert Griesemercd499272008-09-29 12:09:00 -0700486
Robert Griesemerc2d55862009-02-19 16:49:10 -0800487<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700488If the source code represents a character as two code points, such as
489a combining form involving an accent and a letter, the result will be
490an error if placed in a character literal (it is not a single code
491point), and will appear as two code points if placed in a string
492literal.
Rob Pikeff70f092009-02-20 13:36:14 -0800493</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700494
Robert Griesemer19b1d352009-09-24 19:36:48 -0700495
496<h2 id="Constants">Constants</h2>
497
Rob Pike72970872010-03-04 12:35:16 -0800498<p>There are <i>boolean constants</i>, <i>integer constants</i>,
499<i>floating-point constants</i>, <i>complex constants</i>,
500and <i>string constants</i>. Integer, floating-point,
501and complex constants are
Robert Griesemer19b1d352009-09-24 19:36:48 -0700502collectively called <i>numeric constants</i>.
503</p>
Rob Pike678625d2009-09-15 09:54:22 -0700504
505<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700506A constant value is represented by an
507<a href="#Integer_literals">integer</a>,
508<a href="#Floating-point_literals">floating-point</a>,
Rob Pike72970872010-03-04 12:35:16 -0800509<a href="#Imaginary_literals">imaginary</a>,
Robert Griesemer19b1d352009-09-24 19:36:48 -0700510<a href="#Character_literals">character</a>, or
511<a href="#String_literals">string</a> literal,
512an identifier denoting a constant,
513a <a href="#Constant_expressions">constant expression</a>, or
514the result value of some built-in functions such as <code>unsafe.Sizeof</code>
515and <code>cap</code> or <code>len</code> applied to an array,
Rob Pike72970872010-03-04 12:35:16 -0800516<code>len</code> applied to a string constant,
517<code>real</code> and <code>imag</code> applied to a complex constant
518and <code>cmplx</code> applied to numeric constants.
Robert Griesemer19b1d352009-09-24 19:36:48 -0700519The boolean truth values are represented by the predeclared constants
520<code>true</code> and <code>false</code>. The predeclared identifier
521<a href="#Iota">iota</a> denotes an integer constant.
Rob Pike678625d2009-09-15 09:54:22 -0700522</p>
523
Robert Griesemer19b1d352009-09-24 19:36:48 -0700524<p>
Rob Pike72970872010-03-04 12:35:16 -0800525In general, complex constants are a form of
526<a href="#Constant_expressions">constant expression</a>
527and are discussed in that section.
528</p>
529
530<p>
Robert Griesemere9192752009-12-01 16:15:53 -0800531Numeric constants represent values of arbitrary precision and do not overflow.
Robert Griesemer19b1d352009-09-24 19:36:48 -0700532</p>
533
534<p>
535Constants may be <a href="#Types">typed</a> or untyped.
536Literal constants, <code>true</code>, <code>false</code>, <code>iota</code>,
537and certain <a href="#Constant_expressions">constant expressions</a>
538containing only untyped constant operands are untyped.
539</p>
540
541<p>
542A constant may be given a type explicitly by a <a href="#Constant_declarations">constant declaration</a>
543or <a href="#Conversions">conversion</a>, or implicitly when used in a
544<a href="#Variable_declarations">variable declaration</a> or an
545<a href="#Assignments">assignment</a> or as an
546operand in an <a href="#Expressions">expression</a>.
547It is an error if the constant value
548cannot be accurately represented as a value of the respective type.
Robert Griesemere9192752009-12-01 16:15:53 -0800549For instance, <code>3.0</code> can be given any integer or any
Rob Pike4fe41922009-11-07 22:00:59 -0800550floating-point type, while <code>2147483648.0</code> (equal to <code>1&lt;&lt;31</code>)
551can be given the types <code>float32</code>, <code>float64</code>, or <code>uint32</code> but
552not <code>int32</code> or <code>string</code>.
Robert Griesemer19b1d352009-09-24 19:36:48 -0700553</p>
554
555<p>
Russ Cox16205a32010-01-18 15:59:14 -0800556There are no constants denoting the IEEE-754 infinity and not-a-number values,
557but the <a href="/pkg/math/"><code>math</code> package</a>'s
558<a href="/pkg/math/#Inf">Inf</a>,
559<a href="/pkg/math/#NaN">NaN</a>,
560<a href="/pkg/math/#IsInf">IsInf</a>, and
561<a href="/pkg/math/#IsNaN">IsNaN</a>
562functions return and test for those values at run time.
563</p>
564
565<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700566Implementation restriction: A compiler may implement numeric constants by choosing
567an internal representation with at least twice as many bits as any machine type;
568for floating-point values, both the mantissa and exponent must be twice as large.
569</p>
570
571
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700572<h2 id="Types">Types</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700573
Robert Griesemerc2d55862009-02-19 16:49:10 -0800574<p>
Robert Griesemer56809d02009-05-20 11:02:48 -0700575A type determines the set of values and operations specific to values of that
576type. A type may be specified by a (possibly qualified) <i>type name</i>
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700577<a href="#Qualified_identifier">Qualified identifier</a>, §<a href="#Type_declarations">Type declarations</a>) or a <i>type literal</i>,
Robert Griesemer56809d02009-05-20 11:02:48 -0700578which composes a new type from previously declared types.
Rob Pike5af7de32009-02-24 15:17:59 -0800579</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700580
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700581<pre class="ebnf">
Rob Pike8f2330d2009-02-25 16:20:44 -0800582Type = TypeName | TypeLit | "(" Type ")" .
583TypeName = QualifiedIdent.
584TypeLit = ArrayType | StructType | PointerType | FunctionType | InterfaceType |
585 SliceType | MapType | ChannelType .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800586</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -0800587
Robert Griesemerc2d55862009-02-19 16:49:10 -0800588<p>
Robert Griesemerfc61b772009-09-28 14:10:20 -0700589Named instances of the boolean, numeric, and string types are
590<a href="#Predeclared_identifiers">predeclared</a>.
591<i>Composite types</i>&mdash;array, struct, pointer, function,
592interface, slice, map, and channel types&mdash;may be constructed using
593type literals.
Rob Pike5af7de32009-02-24 15:17:59 -0800594</p>
Robert Griesemer7abfcd92008-10-07 17:14:30 -0700595
Rob Pike5af7de32009-02-24 15:17:59 -0800596<p>
Robert Griesemer3b576a72009-06-17 14:31:33 -0700597A type may have a <i>method set</i> associated with it
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700598<a href="#Interface_types">Interface types</a>, §<a href="#Method_declarations">Method declarations</a>).
Robert Griesemer19b1d352009-09-24 19:36:48 -0700599The method set of an <a href="#Interface_types">interface type</a> is its interface.
Robert Griesemer56809d02009-05-20 11:02:48 -0700600The method set of any other named type <code>T</code>
Robert Griesemerfc61b772009-09-28 14:10:20 -0700601consists of all methods with receiver type <code>T</code>.
Robert Griesemer56809d02009-05-20 11:02:48 -0700602The method set of the corresponding pointer type <code>*T</code>
603is the set of all methods with receiver <code>*T</code> or <code>T</code>
604(that is, it also contains the method set of <code>T</code>).
605Any other type has an empty method set.
Robert Griesemerd4d4ff02009-10-19 13:13:59 -0700606In a method set, each method must have a unique name.
Rob Pike5af7de32009-02-24 15:17:59 -0800607</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -0800608<p>
Rob Pike5af7de32009-02-24 15:17:59 -0800609The <i>static type</i> (or just <i>type</i>) of a variable is the
610type defined by its declaration. Variables of interface type
Robert Griesemer19b1d352009-09-24 19:36:48 -0700611also have a distinct <i>dynamic type</i>, which
Rob Pike5af7de32009-02-24 15:17:59 -0800612is the actual type of the value stored in the variable at run-time.
Robert Griesemer19b1d352009-09-24 19:36:48 -0700613The dynamic type may vary during execution but is always assignment compatible
614to the static type of the interface variable. For non-interface
Rob Pike5af7de32009-02-24 15:17:59 -0800615types, the dynamic type is always the static type.
616</p>
Robert Griesemer1f3e8422008-09-29 18:41:30 -0700617
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700618
Robert Griesemer19b1d352009-09-24 19:36:48 -0700619<h3 id="Boolean_types">Boolean types</h3>
620
621A <i>boolean type</i> represents the set of Boolean truth values
622denoted by the predeclared constants <code>true</code>
623and <code>false</code>. The predeclared boolean type is <code>bool</code>.
624
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700625
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700626<h3 id="Numeric_types">Numeric types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700627
Rob Pike5af7de32009-02-24 15:17:59 -0800628<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700629A <i>numeric type</i> represents sets of integer or floating-point values.
630The predeclared architecture-independent numeric types are:
Rob Pike5af7de32009-02-24 15:17:59 -0800631</p>
Robert Griesemerebf14c62008-10-30 14:50:23 -0700632
Rob Pikeff70f092009-02-20 13:36:14 -0800633<pre class="grammar">
Rob Pike72970872010-03-04 12:35:16 -0800634uint8 the set of all unsigned 8-bit integers (0 to 255)
635uint16 the set of all unsigned 16-bit integers (0 to 65535)
636uint32 the set of all unsigned 32-bit integers (0 to 4294967295)
637uint64 the set of all unsigned 64-bit integers (0 to 18446744073709551615)
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700638
Rob Pike72970872010-03-04 12:35:16 -0800639int8 the set of all signed 8-bit integers (-128 to 127)
640int16 the set of all signed 16-bit integers (-32768 to 32767)
641int32 the set of all signed 32-bit integers (-2147483648 to 2147483647)
642int64 the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700643
Rob Pike72970872010-03-04 12:35:16 -0800644float32 the set of all IEEE-754 32-bit floating-point numbers
645float64 the set of all IEEE-754 64-bit floating-point numbers
646
647complex64 the set of all complex numbers with float32 real and imaginary parts
648complex128 the set of all complex numbers with float64 real and imaginary parts
Rob Pike5af7de32009-02-24 15:17:59 -0800649
Robert Griesemeref4c2b82010-03-10 15:29:36 -0800650byte familiar alias for uint8
Robert Griesemerc2d55862009-02-19 16:49:10 -0800651</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700652
Rob Pike5af7de32009-02-24 15:17:59 -0800653<p>
Robert Griesemere9192752009-12-01 16:15:53 -0800654The value of an <i>n</i>-bit integer is <i>n</i> bits wide and represented using
655<a href="http://en.wikipedia.org/wiki/Two's_complement">two's complement arithmetic</a>.
Rob Pike5af7de32009-02-24 15:17:59 -0800656</p>
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -0800657
Rob Pike5af7de32009-02-24 15:17:59 -0800658<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700659There is also a set of predeclared numeric types with implementation-specific sizes:
Rob Pike5af7de32009-02-24 15:17:59 -0800660</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700661
Rob Pikeff70f092009-02-20 13:36:14 -0800662<pre class="grammar">
Robert Griesemercfe92112009-06-18 13:29:40 -0700663uint either 32 or 64 bits
664int either 32 or 64 bits
665float either 32 or 64 bits
Rob Pike72970872010-03-04 12:35:16 -0800666complex real and imaginary parts have type float
Robert Griesemercfe92112009-06-18 13:29:40 -0700667uintptr an unsigned integer large enough to store the uninterpreted bits of a pointer value
Robert Griesemerc2d55862009-02-19 16:49:10 -0800668</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700669
Robert Griesemerc2d55862009-02-19 16:49:10 -0800670<p>
Rob Pikeda389742009-03-02 19:13:40 -0800671To avoid portability issues all numeric types are distinct except
672<code>byte</code>, which is an alias for <code>uint8</code>.
673Conversions
Robert Griesemer533dfd62009-05-13 16:56:00 -0700674are required when incompatible numeric types are mixed in an expression
Rob Pike5af7de32009-02-24 15:17:59 -0800675or assignment. For instance, <code>int32</code> and <code>int</code>
Russ Cox5958dd62009-03-04 17:19:21 -0800676are not the same type even though they may have the same size on a
Rob Pike5af7de32009-02-24 15:17:59 -0800677particular architecture.
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700678
679
Robert Griesemer19b1d352009-09-24 19:36:48 -0700680<h3 id="String_types">String types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700681
Rob Pike4501d342009-02-19 17:31:36 -0800682<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700683A <i>string type</i> represents the set of string values.
Rob Pike5af7de32009-02-24 15:17:59 -0800684Strings behave like arrays of bytes but are immutable: once created,
685it is impossible to change the contents of a string.
Robert Griesemer19b1d352009-09-24 19:36:48 -0700686The predeclared string type is <code>string</code>.
Rob Pike5af7de32009-02-24 15:17:59 -0800687
688<p>
689The elements of strings have type <code>byte</code> and may be
Robert Griesemerfc61b772009-09-28 14:10:20 -0700690accessed using the usual <a href="#Indexes">indexing operations</a>. It is
Rob Pike678625d2009-09-15 09:54:22 -0700691illegal to take the address of such an element; if
692<code>s[i]</code> is the <i>i</i>th byte of a
Robert Griesemercfe92112009-06-18 13:29:40 -0700693string, <code>&amp;s[i]</code> is invalid. The length of string
694<code>s</code> can be discovered using the built-in function
Robert Griesemer19b1d352009-09-24 19:36:48 -0700695<code>len</code>. The length is a compile-time constant if <code>s</code>
Robert Griesemercfe92112009-06-18 13:29:40 -0700696is a string literal.
Rob Pike4501d342009-02-19 17:31:36 -0800697</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700698
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700699
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700700<h3 id="Array_types">Array types</h3>
Russ Cox461dd912009-03-04 14:44:51 -0800701
702<p>
703An array is a numbered sequence of elements of a single
Robert Griesemer4023dce2009-08-14 17:41:52 -0700704type, called the element type.
705The number of elements is called the length and is never
Russ Cox461dd912009-03-04 14:44:51 -0800706negative.
707</p>
708
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700709<pre class="ebnf">
Russ Cox461dd912009-03-04 14:44:51 -0800710ArrayType = "[" ArrayLength "]" ElementType .
711ArrayLength = Expression .
Robert Griesemer4023dce2009-08-14 17:41:52 -0700712ElementType = Type .
Russ Cox461dd912009-03-04 14:44:51 -0800713</pre>
714
715<p>
Robert Griesemere9192752009-12-01 16:15:53 -0800716The length is part of the array's type and must be a
Robert Griesemer4ed666e2009-08-27 16:45:42 -0700717<a href="#Constant_expressions">constant expression</a> that evaluates to a non-negative
Russ Cox461dd912009-03-04 14:44:51 -0800718integer value. The length of array <code>a</code> can be discovered
719using the built-in function <code>len(a)</code>, which is a
720compile-time constant. The elements can be indexed by integer
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700721indices 0 through the <code>len(a)-1</code><a href="#Indexes">Indexes</a>).
Rob Pikeff6a8fd2009-11-20 15:47:15 -0800722Array types are always one-dimensional but may be composed to form
723multi-dimensional types.
Russ Cox461dd912009-03-04 14:44:51 -0800724</p>
725
726<pre>
727[32]byte
728[2*N] struct { x, y int32 }
729[1000]*float64
Rob Pikeff6a8fd2009-11-20 15:47:15 -0800730[3][5]int
731[2][2][2]float64 // same as [2]([2]([2]float64))
Russ Cox461dd912009-03-04 14:44:51 -0800732</pre>
733
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700734<h3 id="Slice_types">Slice types</h3>
Russ Cox461dd912009-03-04 14:44:51 -0800735
736<p>
737A slice is a reference to a contiguous segment of an array and
738contains a numbered sequence of elements from that array. A slice
739type denotes the set of all slices of arrays of its element type.
740A slice value may be <code>nil</code>.
741</p>
742
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700743<pre class="ebnf">
Russ Cox461dd912009-03-04 14:44:51 -0800744SliceType = "[" "]" ElementType .
745</pre>
746
747<p>
748Like arrays, slices are indexable and have a length. The length of a
749slice <code>s</code> can be discovered by the built-in function
750<code>len(s)</code>; unlike with arrays it may change during
751execution. The elements can be addressed by integer indices 0
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700752through <code>len(s)-1</code><a href="#Indexes">Indexes</a>). The slice index of a
Russ Cox461dd912009-03-04 14:44:51 -0800753given element may be less than the index of the same element in the
754underlying array.
755</p>
756<p>
757A slice, once initialized, is always associated with an underlying
Rob Pike7ec08562010-01-09 07:32:26 +1100758array that holds its elements. A slice therefore shares storage
Russ Cox461dd912009-03-04 14:44:51 -0800759with its array and with other slices of the same array; by contrast,
760distinct arrays always represent distinct storage.
761</p>
762<p>
763The array underlying a slice may extend past the end of the slice.
Russ Cox5958dd62009-03-04 17:19:21 -0800764The <i>capacity</i> is a measure of that extent: it is the sum of
Russ Cox461dd912009-03-04 14:44:51 -0800765the length of the slice and the length of the array beyond the slice;
766a slice of length up to that capacity can be created by `slicing' a new
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700767one from the original slice (§<a href="#Slices">Slices</a>).
Russ Cox461dd912009-03-04 14:44:51 -0800768The capacity of a slice <code>a</code> can be discovered using the
Robert Griesemercfe92112009-06-18 13:29:40 -0700769built-in function <code>cap(a)</code> and the relationship between
770<code>len()</code> and <code>cap()</code> is:
Russ Cox461dd912009-03-04 14:44:51 -0800771</p>
772
773<pre>
7740 <= len(a) <= cap(a)
775</pre>
776
777<p>
778The value of an uninitialized slice is <code>nil</code>.
779The length and capacity of a <code>nil</code> slice
780are 0. A new, initialized slice value for a given element type <code>T</code> is
781made using the built-in function <code>make</code>, which takes a slice type
782and parameters specifying the length and optionally the capacity:
783</p>
784
785<pre>
786make([]T, length)
787make([]T, length, capacity)
788</pre>
Russ Cox5958dd62009-03-04 17:19:21 -0800789
Russ Cox461dd912009-03-04 14:44:51 -0800790<p>
791The <code>make()</code> call allocates a new, hidden array to which the returned
Rob Pike678625d2009-09-15 09:54:22 -0700792slice value refers. That is, executing
Russ Cox461dd912009-03-04 14:44:51 -0800793</p>
794
795<pre>
796make([]T, length, capacity)
797</pre>
798
799<p>
800produces the same slice as allocating an array and slicing it, so these two examples
801result in the same slice:
802</p>
803
804<pre>
805make([]int, 50, 100)
806new([100]int)[0:50]
807</pre>
808
Rob Pikeff6a8fd2009-11-20 15:47:15 -0800809<p>
810Like arrays, slices are always one-dimensional but may be composed to construct
811higher-dimensional objects.
812With arrays of arrays, the inner arrays are, by construction, always the same length;
813however with slices of slices (or arrays of slices), the lengths may vary dynamically.
814Moreover, the inner slices must be allocated individually (with <code>make</code>).
815</p>
Russ Cox461dd912009-03-04 14:44:51 -0800816
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700817<h3 id="Struct_types">Struct types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700818
Rob Pike5af7de32009-02-24 15:17:59 -0800819<p>
Robert Griesemerd3b15652009-11-16 08:58:55 -0800820A struct is a sequence of named elements, called fields, each of which has a
821name and a type. Field names may be specified explicitly (IdentifierList) or
822implicitly (AnonymousField).
823Within a struct, non-<a href="#Blank_identifier">blank</a> field names must
824be unique.
Rob Pike5af7de32009-02-24 15:17:59 -0800825</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700826
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700827<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -0800828StructType = "struct" "{" { FieldDecl ";" } "}" .
Robert Griesemerd3b15652009-11-16 08:58:55 -0800829FieldDecl = (IdentifierList Type | AnonymousField) [ Tag ] .
830AnonymousField = [ "*" ] TypeName .
Robert Griesemer130ac742009-12-10 16:43:01 -0800831Tag = string_lit .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800832</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700833
Robert Griesemerc2d55862009-02-19 16:49:10 -0800834<pre>
835// An empty struct.
836struct {}
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700837
Robert Griesemer4e56b332009-09-10 10:14:00 -0700838// A struct with 6 fields.
Robert Griesemerc2d55862009-02-19 16:49:10 -0800839struct {
Robert Griesemer130ac742009-12-10 16:43:01 -0800840 x, y int
841 u float
842 _ float // padding
843 A *[]int
844 F func()
Robert Griesemerc2d55862009-02-19 16:49:10 -0800845}
846</pre>
Robert Griesemer1f3e8422008-09-29 18:41:30 -0700847
Rob Pike5af7de32009-02-24 15:17:59 -0800848<p>
Robert Griesemerd3b15652009-11-16 08:58:55 -0800849A field declared with a type but no explicit field name is an <i>anonymous field</i>.
Rob Pike5af7de32009-02-24 15:17:59 -0800850Such a field type must be specified as
Rob Pikeda389742009-03-02 19:13:40 -0800851a type name <code>T</code> or as a pointer to a type name <code>*T</code>,
Ian Lance Taylor3e804ba2009-08-17 11:40:57 -0700852and <code>T</code> itself may not be
Robert Griesemerd3b15652009-11-16 08:58:55 -0800853a pointer type. The unqualified type name acts as the field name.
Rob Pike5af7de32009-02-24 15:17:59 -0800854</p>
Robert Griesemer1f3e8422008-09-29 18:41:30 -0700855
Robert Griesemerc2d55862009-02-19 16:49:10 -0800856<pre>
857// A struct with four anonymous fields of type T1, *T2, P.T3 and *P.T4
858struct {
Robert Griesemer130ac742009-12-10 16:43:01 -0800859 T1 // field name is T1
860 *T2 // field name is T2
861 P.T3 // field name is T3
862 *P.T4 // field name is T4
863 x, y int // field names are x and y
Robert Griesemerc2d55862009-02-19 16:49:10 -0800864}
865</pre>
Robert Griesemer1f3e8422008-09-29 18:41:30 -0700866
Rob Pike5af7de32009-02-24 15:17:59 -0800867<p>
Robert Griesemerd3b15652009-11-16 08:58:55 -0800868The following declaration is illegal because field names must be unique
869in a struct type:
Rob Pike5af7de32009-02-24 15:17:59 -0800870</p>
Robert Griesemer071c91b2008-10-23 12:04:45 -0700871
Robert Griesemerc2d55862009-02-19 16:49:10 -0800872<pre>
873struct {
Robert Griesemer130ac742009-12-10 16:43:01 -0800874 T // conflicts with anonymous field *T and *P.T
875 *T // conflicts with anonymous field T and *P.T
876 *P.T // conflicts with anonymous field T and *T
Robert Griesemerc2d55862009-02-19 16:49:10 -0800877}
878</pre>
Robert Griesemer1f3e8422008-09-29 18:41:30 -0700879
Robert Griesemerc2d55862009-02-19 16:49:10 -0800880<p>
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700881Fields and methods (§<a href="#Method_declarations">Method declarations</a>) of an anonymous field are
882promoted to be ordinary fields and methods of the struct (§<a href="#Selectors">Selectors</a>).
Robert Griesemer56809d02009-05-20 11:02:48 -0700883The following rules apply for a struct type named <code>S</code> and
884a type named <code>T</code>:
Rob Pike5af7de32009-02-24 15:17:59 -0800885</p>
Robert Griesemer56809d02009-05-20 11:02:48 -0700886<ul>
887 <li>If <code>S</code> contains an anonymous field <code>T</code>, the
888 method set of <code>S</code> includes the method set of <code>T</code>.
889 </li>
890
891 <li>If <code>S</code> contains an anonymous field <code>*T</code>, the
892 method set of <code>S</code> includes the method set of <code>*T</code>
893 (which itself includes the method set of <code>T</code>).
894 </li>
895
896 <li>If <code>S</code> contains an anonymous field <code>T</code> or
897 <code>*T</code>, the method set of <code>*S</code> includes the
898 method set of <code>*T</code> (which itself includes the method
899 set of <code>T</code>).
900 </li>
901</ul>
Rob Pike5af7de32009-02-24 15:17:59 -0800902<p>
Robert Griesemer56809d02009-05-20 11:02:48 -0700903A field declaration may be followed by an optional string literal <i>tag</i>,
Robert Griesemerd3b15652009-11-16 08:58:55 -0800904which becomes an attribute for all the fields in the corresponding
Rob Pike5af7de32009-02-24 15:17:59 -0800905field declaration. The tags are made
Rob Pike8cb91842009-09-15 11:56:39 -0700906visible through a <a href="#Package_unsafe">reflection interface</a>
Rob Pike5af7de32009-02-24 15:17:59 -0800907but are otherwise ignored.
908</p>
Robert Griesemer2e90e542008-10-30 15:52:37 -0700909
Robert Griesemerc2d55862009-02-19 16:49:10 -0800910<pre>
Rob Pike678625d2009-09-15 09:54:22 -0700911// A struct corresponding to the TimeStamp protocol buffer.
Rob Pikecdbf6192009-02-24 17:47:45 -0800912// The tag strings define the protocol buffer field numbers.
Robert Griesemerc2d55862009-02-19 16:49:10 -0800913struct {
Robert Griesemer130ac742009-12-10 16:43:01 -0800914 microsec uint64 "field 1"
915 serverIP6 uint64 "field 2"
916 process string "field 3"
Robert Griesemerc2d55862009-02-19 16:49:10 -0800917}
918</pre>
Robert Griesemer2e90e542008-10-30 15:52:37 -0700919
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700920<h3 id="Pointer_types">Pointer types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700921
Rob Pike5af7de32009-02-24 15:17:59 -0800922<p>
Robert Griesemer4dc25282008-09-09 10:37:19 -0700923A pointer type denotes the set of all pointers to variables of a given
Rob Pikeda389742009-03-02 19:13:40 -0800924type, called the <i>base type</i> of the pointer.
Rob Pike8f2330d2009-02-25 16:20:44 -0800925A pointer value may be <code>nil</code>.
Rob Pike5af7de32009-02-24 15:17:59 -0800926</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700927
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700928<pre class="ebnf">
Robert Griesemerc2d55862009-02-19 16:49:10 -0800929PointerType = "*" BaseType .
930BaseType = Type .
931</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700932
Robert Griesemerc2d55862009-02-19 16:49:10 -0800933<pre>
934*int
Rob Pike8f2330d2009-02-25 16:20:44 -0800935*map[string] *chan int
Robert Griesemerc2d55862009-02-19 16:49:10 -0800936</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700937
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700938<h3 id="Function_types">Function types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700939
Rob Pike8f2330d2009-02-25 16:20:44 -0800940<p>
Robert Griesemer7231ceb2008-09-08 15:01:04 -0700941A function type denotes the set of all functions with the same parameter
Rob Pike8f2330d2009-02-25 16:20:44 -0800942and result types.
943A function value may be <code>nil</code>.
944</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700945
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700946<pre class="ebnf">
Rob Pike8f2330d2009-02-25 16:20:44 -0800947FunctionType = "func" Signature .
948Signature = Parameters [ Result ] .
Robert Griesemer4023dce2009-08-14 17:41:52 -0700949Result = Parameters | Type .
Robert Griesemer130ac742009-12-10 16:43:01 -0800950Parameters = "(" [ ParameterList [ "," ] ] ")" .
Rob Pike8f2330d2009-02-25 16:20:44 -0800951ParameterList = ParameterDecl { "," ParameterDecl } .
Rob Pikeb81065d2010-01-27 13:14:40 -0800952ParameterDecl = [ IdentifierList ] ( Type | "..." [ Type ] ) .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800953</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700954
Robert Griesemerc2d55862009-02-19 16:49:10 -0800955<p>
Rob Pike8f2330d2009-02-25 16:20:44 -0800956Within a list of parameters or results, the names (IdentifierList)
957must either all be present or all be absent. If present, each name
958stands for one item (parameter or result) of the specified type; if absent, each
959type stands for one item of that type. Parameter and result
960lists are always parenthesized except that if there is exactly
Russ Cox46871692010-01-26 10:25:56 -0800961one unnamed result it may written as an unparenthesized type.
Rob Pike8f2330d2009-02-25 16:20:44 -0800962</p>
963<p>
964For the last parameter only, instead of a type one may write
Rob Pikeb81065d2010-01-27 13:14:40 -0800965<code>...</code> or <code>... T</code> to indicate that the function
966may be invoked with zero or more additional arguments. If the type
967<code>T</code> is present in the parameter declaration, the additional
968arguments must all be
969<a href="#Assignment_compatibility">assignment compatible</a>
970with type <code>T</code>; otherwise they may be of any type.
Rob Pike8f2330d2009-02-25 16:20:44 -0800971</p>
Robert Griesemer2bfa9572008-10-24 13:13:12 -0700972
Robert Griesemerc2d55862009-02-19 16:49:10 -0800973<pre>
Russ Cox46871692010-01-26 10:25:56 -0800974func()
975func(x int)
976func() int
977func(string, float, ...)
Rob Pikeb81065d2010-01-27 13:14:40 -0800978func(prefix string, values ... int)
Russ Cox46871692010-01-26 10:25:56 -0800979func(a, b int, z float) bool
980func(a, b int, z float) (bool)
981func(a, b int, z float, opt ...) (success bool)
982func(int, int, float) (float, *[]int)
983func(n int) func(p *T)
Robert Griesemerc2d55862009-02-19 16:49:10 -0800984</pre>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -0800985
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700986
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700987<h3 id="Interface_types">Interface types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700988
Rob Pike8f2330d2009-02-25 16:20:44 -0800989<p>
Robert Griesemerd4d4ff02009-10-19 13:13:59 -0700990An interface type specifies a <a href="#Types">method set</a> called its <i>interface</i>.
Robert Griesemer56809d02009-05-20 11:02:48 -0700991A variable of interface type can store a value of any type with a method set
992that is any superset of the interface. Such a type is said to
993<i>implement the interface</i>. An interface value may be <code>nil</code>.
Rob Pike8f2330d2009-02-25 16:20:44 -0800994</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700995
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700996<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -0800997InterfaceType = "interface" "{" { MethodSpec ";" } "}" .
Robert Griesemerd4d4ff02009-10-19 13:13:59 -0700998MethodSpec = MethodName Signature | InterfaceTypeName .
999MethodName = identifier .
Rob Pike8f2330d2009-02-25 16:20:44 -08001000InterfaceTypeName = TypeName .
Robert Griesemerc2d55862009-02-19 16:49:10 -08001001</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001002
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07001003<p>
1004As with all method sets, in an interface type, each method must have a unique name.
1005</p>
1006
Robert Griesemerc2d55862009-02-19 16:49:10 -08001007<pre>
Rob Pike8f2330d2009-02-25 16:20:44 -08001008// A simple File interface
Robert Griesemerc2d55862009-02-19 16:49:10 -08001009interface {
Robert Griesemer130ac742009-12-10 16:43:01 -08001010 Read(b Buffer) bool
1011 Write(b Buffer) bool
1012 Close()
Robert Griesemerc2d55862009-02-19 16:49:10 -08001013}
1014</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001015
Rob Pike8f2330d2009-02-25 16:20:44 -08001016<p>
Robert Griesemer56809d02009-05-20 11:02:48 -07001017More than one type may implement an interface.
Rob Pike8f2330d2009-02-25 16:20:44 -08001018For instance, if two types <code>S1</code> and <code>S2</code>
Robert Griesemer56809d02009-05-20 11:02:48 -07001019have the method set
Rob Pike8f2330d2009-02-25 16:20:44 -08001020</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001021
Robert Griesemerc2d55862009-02-19 16:49:10 -08001022<pre>
1023func (p T) Read(b Buffer) bool { return ... }
1024func (p T) Write(b Buffer) bool { return ... }
1025func (p T) Close() { ... }
1026</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001027
Rob Pike8f2330d2009-02-25 16:20:44 -08001028<p>
1029(where <code>T</code> stands for either <code>S1</code> or <code>S2</code>)
1030then the <code>File</code> interface is implemented by both <code>S1</code> and
1031<code>S2</code>, regardless of what other methods
1032<code>S1</code> and <code>S2</code> may have or share.
1033</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001034
Rob Pike8f2330d2009-02-25 16:20:44 -08001035<p>
1036A type implements any interface comprising any subset of its methods
1037and may therefore implement several distinct interfaces. For
1038instance, all types implement the <i>empty interface</i>:
1039</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001040
Robert Griesemerc2d55862009-02-19 16:49:10 -08001041<pre>
Robert Griesemer19b1d352009-09-24 19:36:48 -07001042interface{}
Robert Griesemerc2d55862009-02-19 16:49:10 -08001043</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001044
Rob Pike8f2330d2009-02-25 16:20:44 -08001045<p>
1046Similarly, consider this interface specification,
Robert Griesemer4ed666e2009-08-27 16:45:42 -07001047which appears within a <a href="#Type_declarations">type declaration</a>
Rob Pike8f2330d2009-02-25 16:20:44 -08001048to define an interface called <code>Lock</code>:
1049</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001050
Robert Griesemerc2d55862009-02-19 16:49:10 -08001051<pre>
1052type Lock interface {
Robert Griesemer130ac742009-12-10 16:43:01 -08001053 Lock()
1054 Unlock()
Robert Griesemerc2d55862009-02-19 16:49:10 -08001055}
1056</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001057
Rob Pike8f2330d2009-02-25 16:20:44 -08001058<p>
1059If <code>S1</code> and <code>S2</code> also implement
1060</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001061
Robert Griesemerc2d55862009-02-19 16:49:10 -08001062<pre>
1063func (p T) Lock() { ... }
1064func (p T) Unlock() { ... }
1065</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001066
Robert Griesemerc2d55862009-02-19 16:49:10 -08001067<p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001068they implement the <code>Lock</code> interface as well
1069as the <code>File</code> interface.
1070</p>
1071<p>
1072An interface may contain an interface type name <code>T</code>
1073in place of a method specification.
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07001074The effect is equivalent to enumerating the methods of <code>T</code> explicitly
Rob Pike8f2330d2009-02-25 16:20:44 -08001075in the interface.
1076</p>
Robert Griesemer38c232f2009-02-11 15:09:15 -08001077
Robert Griesemerc2d55862009-02-19 16:49:10 -08001078<pre>
1079type ReadWrite interface {
Robert Griesemer130ac742009-12-10 16:43:01 -08001080 Read(b Buffer) bool
1081 Write(b Buffer) bool
Robert Griesemerc2d55862009-02-19 16:49:10 -08001082}
Robert Griesemer38c232f2009-02-11 15:09:15 -08001083
Robert Griesemerc2d55862009-02-19 16:49:10 -08001084type File interface {
Robert Griesemer130ac742009-12-10 16:43:01 -08001085 ReadWrite // same as enumerating the methods in ReadWrite
1086 Lock // same as enumerating the methods in Lock
1087 Close()
Robert Griesemerc2d55862009-02-19 16:49:10 -08001088}
1089</pre>
Robert Griesemer38c232f2009-02-11 15:09:15 -08001090
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001091<h3 id="Map_types">Map types</h3>
Robert Griesemera3294712009-01-05 11:17:26 -08001092
Rob Pike8f2330d2009-02-25 16:20:44 -08001093<p>
1094A map is an unordered group of elements of one type, called the
Robert Griesemer0660d242009-11-15 17:42:27 -08001095element type, indexed by a set of unique <i>keys</i> of another type,
Robert Griesemer4023dce2009-08-14 17:41:52 -07001096called the key type.
Rob Pike8f2330d2009-02-25 16:20:44 -08001097A map value may be <code>nil</code>.
1098
1099</p>
Robert Griesemera3294712009-01-05 11:17:26 -08001100
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001101<pre class="ebnf">
Robert Griesemer19b1d352009-09-24 19:36:48 -07001102MapType = "map" "[" KeyType "]" ElementType .
Robert Griesemer4023dce2009-08-14 17:41:52 -07001103KeyType = Type .
Robert Griesemerc2d55862009-02-19 16:49:10 -08001104</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001105
Robert Griesemerc2d55862009-02-19 16:49:10 -08001106<p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001107The comparison operators <code>==</code> and <code>!=</code>
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001108<a href="#Comparison_operators">Comparison operators</a>) must be fully defined for operands of the
Robert Griesemer19b1d352009-09-24 19:36:48 -07001109key type; thus the key type must be a boolean, numeric, string, pointer, function, interface,
Rob Pike8f2330d2009-02-25 16:20:44 -08001110map, or channel type. If the key type is an interface type, these
1111comparison operators must be defined for the dynamic key values;
Rob Pike5bb29fb2010-03-25 17:59:59 -07001112failure will cause a <a href="#Run_time_panics">run-time panic</a>.
Rob Pike8f2330d2009-02-25 16:20:44 -08001113
1114</p>
Robert Griesemera3294712009-01-05 11:17:26 -08001115
Robert Griesemerc2d55862009-02-19 16:49:10 -08001116<pre>
1117map [string] int
1118map [*T] struct { x, y float }
1119map [string] interface {}
1120</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001121
Rob Pike5af7de32009-02-24 15:17:59 -08001122<p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001123The number of elements is called the length and is never negative.
1124The length of a map <code>m</code> can be discovered using the
1125built-in function <code>len(m)</code> and may change during execution.
Rob Pike678625d2009-09-15 09:54:22 -07001126Values may be added and removed
1127during execution using special forms of <a href="#Assignments">assignment</a>.
Rob Pike5af7de32009-02-24 15:17:59 -08001128</p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001129<p>
Rob Pike678625d2009-09-15 09:54:22 -07001130The value of an uninitialized map is <code>nil</code>.
Rob Pike8f2330d2009-02-25 16:20:44 -08001131A new, empty map value is made using the built-in
1132function <code>make</code>, which takes the map type and an optional
Rob Pikeda389742009-03-02 19:13:40 -08001133capacity hint as arguments:
Rob Pike8f2330d2009-02-25 16:20:44 -08001134</p>
1135
1136<pre>
Rob Pikeda389742009-03-02 19:13:40 -08001137make(map[string] int)
1138make(map[string] int, 100)
Rob Pike8f2330d2009-02-25 16:20:44 -08001139</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001140
Rob Pikeda389742009-03-02 19:13:40 -08001141<p>
1142The initial capacity does not bound its size:
1143maps grow to accommodate the number of items
1144stored in them.
1145</p>
1146
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001147<h3 id="Channel_types">Channel types</h3>
Robert Griesemera3294712009-01-05 11:17:26 -08001148
Rob Pike8f2330d2009-02-25 16:20:44 -08001149<p>
Robert Griesemera3294712009-01-05 11:17:26 -08001150A channel provides a mechanism for two concurrently executing functions
Rob Pike8f2330d2009-02-25 16:20:44 -08001151to synchronize execution and communicate by passing a value of a
Robert Griesemer4023dce2009-08-14 17:41:52 -07001152specified element type.
Robert Griesemere2cb60b2009-06-19 13:03:01 -07001153A value of channel type may be <code>nil</code>.
Rob Pike8f2330d2009-02-25 16:20:44 -08001154</p>
Robert Griesemera3294712009-01-05 11:17:26 -08001155
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001156<pre class="ebnf">
Rob Pike8f2330d2009-02-25 16:20:44 -08001157ChannelType = Channel | SendChannel | RecvChannel .
Robert Griesemer0660d242009-11-15 17:42:27 -08001158Channel = "chan" ElementType .
1159SendChannel = "chan" "&lt;-" ElementType .
1160RecvChannel = "&lt;-" "chan" ElementType .
Robert Griesemerc2d55862009-02-19 16:49:10 -08001161</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001162
Rob Pike8f2330d2009-02-25 16:20:44 -08001163<p>
Robert Griesemer1c369bd2010-01-27 09:35:39 -08001164To avoid a parsing ambiguity in cases such as <code>chan&lt;- chan int</code>,
1165the Channel production's ElementType cannot be a RecvChannel.
1166To construct such a type, parenthesize the RecvChannel first.
1167</p>
1168
1169<pre>
1170chan&lt;- chan int // same as chan&lt;- (chan int)
1171chan&lt;- &lt;-chan int // same as chan&lt;- (&lt;-chan int)
1172&lt;-chan &lt;-chan int // same as &lt;-chan (&lt;-chan int)
1173chan (&lt;-chan int)
1174</pre>
1175
1176<p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001177Upon creation, a channel can be used both to send and to receive values.
Robert Griesemera3294712009-01-05 11:17:26 -08001178By conversion or assignment, a channel may be constrained only to send or
Rob Pike8f2330d2009-02-25 16:20:44 -08001179to receive. This constraint is called a channel's <i>direction</i>; either
1180<i>send</i>, <i>receive</i>, or <i>bi-directional</i> (unconstrained).
1181</p>
Robert Griesemera3294712009-01-05 11:17:26 -08001182
Robert Griesemerc2d55862009-02-19 16:49:10 -08001183<pre>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07001184chan T // can be used to send and receive values of type T
Robert Griesemere1e76192009-09-25 14:11:03 -07001185chan&lt;- float // can only be used to send floats
Rob Pike46596852009-03-02 16:17:29 -08001186&lt;-chan int // can only be used to receive ints
Robert Griesemerc2d55862009-02-19 16:49:10 -08001187</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001188
Rob Pike8f2330d2009-02-25 16:20:44 -08001189<p>
1190The value of an uninitialized channel is <code>nil</code>. A new, initialized channel
Robert Griesemere9192752009-12-01 16:15:53 -08001191value can be made using the built-in function <code>make</code>,
Robert Griesemer633957b2009-01-06 13:23:20 -08001192which takes the channel type and an optional capacity as arguments:
Rob Pike8f2330d2009-02-25 16:20:44 -08001193</p>
Robert Griesemera3294712009-01-05 11:17:26 -08001194
Robert Griesemerc2d55862009-02-19 16:49:10 -08001195<pre>
Rob Pikeda389742009-03-02 19:13:40 -08001196make(chan int, 100)
Robert Griesemerc2d55862009-02-19 16:49:10 -08001197</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001198
Rob Pike8f2330d2009-02-25 16:20:44 -08001199<p>
1200The capacity, in number of elements, sets the size of the buffer in the channel. If the
Rob Pike678625d2009-09-15 09:54:22 -07001201capacity is greater than zero, the channel is asynchronous: provided the
Rob Pike8f2330d2009-02-25 16:20:44 -08001202buffer is not full, sends can succeed without blocking. If the capacity is zero
1203or absent, the communication succeeds only when both a sender and receiver are ready.
1204</p>
Robert Griesemera3294712009-01-05 11:17:26 -08001205
Rob Pike94b67eb2009-03-24 17:40:47 -07001206<p>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07001207A channel may be closed and tested for closure with the built-in functions
1208<a href="#Close_and_closed"><code>close</code> and <code>closed</code></a>.
Rob Pike94b67eb2009-03-24 17:40:47 -07001209</p>
1210
Rob Pike83cbca52009-08-21 14:18:08 -07001211<h2 id="Properties_of_types_and_values">Properties of types and values</h2>
Robert Griesemer434c6052008-11-07 13:34:37 -08001212
Rob Pike4501d342009-02-19 17:31:36 -08001213<p>
Robert Griesemerfc61b772009-09-28 14:10:20 -07001214Two types are either <i>identical</i> or <i>different</i>, and they are
1215either <i>compatible</i> or <i>incompatible</i>.
1216Identical types are always compatible, but compatible types need not be identical.
Robert Griesemer19b1d352009-09-24 19:36:48 -07001217</p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001218
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001219<h3 id="Type_identity_and_compatibility">Type identity and compatibility</h3>
Robert Griesemer533dfd62009-05-13 16:56:00 -07001220
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001221<h4 id="Type_identity">Type identity</h4>
Rob Pike8f2330d2009-02-25 16:20:44 -08001222
Robert Griesemerc2d55862009-02-19 16:49:10 -08001223<p>
Robert Griesemer533dfd62009-05-13 16:56:00 -07001224Two named types are identical if their type names originate in the same
Robert Griesemeraeaab592009-08-31 17:30:55 -07001225type declaration (§<a href="#Declarations_and_scope">Declarations and scope</a>). A named and an unnamed type
Robert Griesemer533dfd62009-05-13 16:56:00 -07001226are never identical. Two unnamed types are identical if the corresponding
1227type literals have the same literal structure and corresponding components have
1228identical types. In detail:
Rob Pike4501d342009-02-19 17:31:36 -08001229</p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001230
Robert Griesemerc2d55862009-02-19 16:49:10 -08001231<ul>
Robert Griesemer533dfd62009-05-13 16:56:00 -07001232 <li>Two array types are identical if they have identical element types and
1233 the same array length.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001234
Robert Griesemer533dfd62009-05-13 16:56:00 -07001235 <li>Two slice types are identical if they have identical element types.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001236
Robert Griesemer533dfd62009-05-13 16:56:00 -07001237 <li>Two struct types are identical if they have the same sequence of fields,
1238 and if corresponding fields have the same names and identical types.
Robert Griesemer56809d02009-05-20 11:02:48 -07001239 Two anonymous fields are considered to have the same name.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001240
Robert Griesemer533dfd62009-05-13 16:56:00 -07001241 <li>Two pointer types are identical if they have identical base types.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001242
Robert Griesemer533dfd62009-05-13 16:56:00 -07001243 <li>Two function types are identical if they have the same number of parameters
1244 and result values and if corresponding parameter and result types are
Rob Pikeb81065d2010-01-27 13:14:40 -08001245 identical. All "..." parameters without a specified type are defined to have
1246 identical type. All "..." parameters with specified identical type
1247 <code>T</code> are defined to have identical type.
Robert Griesemer533dfd62009-05-13 16:56:00 -07001248 Parameter and result names are not required to match.</li>
Robert Griesemera3294712009-01-05 11:17:26 -08001249
Robert Griesemer533dfd62009-05-13 16:56:00 -07001250 <li>Two interface types are identical if they have the same set of methods
1251 with the same names and identical function types. The order
1252 of the methods is irrelevant.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001253
Robert Griesemer533dfd62009-05-13 16:56:00 -07001254 <li>Two map types are identical if they have identical key and value types.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001255
Robert Griesemer533dfd62009-05-13 16:56:00 -07001256 <li>Two channel types are identical if they have identical value types and
1257 the same direction.</li>
Robert Griesemerc2d55862009-02-19 16:49:10 -08001258</ul>
Robert Griesemer434c6052008-11-07 13:34:37 -08001259
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001260<h4 id="Type_compatibility">Type compatibility</h4>
Robert Griesemer533dfd62009-05-13 16:56:00 -07001261
Robert Griesemerc2d55862009-02-19 16:49:10 -08001262<p>
Robert Griesemer533dfd62009-05-13 16:56:00 -07001263Type compatibility is less stringent than type identity: a named and an unnamed
1264type are compatible if the respective type literals are compatible.
1265In all other respects, the definition of type compatibility is the
1266same as for type identity listed above but with ``compatible''
1267substituted for ``identical''.
Rob Pike4501d342009-02-19 17:31:36 -08001268</p>
Robert Griesemer434c6052008-11-07 13:34:37 -08001269
Robert Griesemerc2d55862009-02-19 16:49:10 -08001270<p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001271Given the declarations
1272</p>
Robert Griesemer434c6052008-11-07 13:34:37 -08001273
Robert Griesemerc2d55862009-02-19 16:49:10 -08001274<pre>
1275type (
Robert Griesemer130ac742009-12-10 16:43:01 -08001276 T0 []string
1277 T1 []string
1278 T2 struct { a, b int }
1279 T3 struct { a, c int }
Russ Cox46871692010-01-26 10:25:56 -08001280 T4 func(int, float) *T0
1281 T5 func(x int, y float) *[]string
Robert Griesemerc2d55862009-02-19 16:49:10 -08001282)
1283</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -08001284
Rob Pike8f2330d2009-02-25 16:20:44 -08001285<p>
Robert Griesemer533dfd62009-05-13 16:56:00 -07001286these types are identical:
Rob Pike8f2330d2009-02-25 16:20:44 -08001287</p>
Robert Griesemer434c6052008-11-07 13:34:37 -08001288
Robert Griesemerc2d55862009-02-19 16:49:10 -08001289<pre>
1290T0 and T0
1291[]int and []int
1292struct { a, b *T5 } and struct { a, b *T5 }
Russ Cox46871692010-01-26 10:25:56 -08001293func(x int, y float) *[]string and func(int, float) (result *[]string)
Robert Griesemerc2d55862009-02-19 16:49:10 -08001294</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -08001295
Rob Pike8f2330d2009-02-25 16:20:44 -08001296<p>
Robert Griesemer533dfd62009-05-13 16:56:00 -07001297<code>T0</code> and <code>T1</code> are neither identical nor compatible
1298because they are named types with distinct declarations.
1299</p>
1300
1301<p>
1302These types are compatible:
1303</p>
1304
1305<pre>
1306T0 and T0
1307T0 and []string
1308T3 and struct { a int; c int }
Russ Cox46871692010-01-26 10:25:56 -08001309T4 and func(x int, y float) *[]string
Robert Griesemer533dfd62009-05-13 16:56:00 -07001310</pre>
1311
1312<p>
1313<code>T2</code> and <code>struct { a, c int }</code> are incompatible because
1314they have different field names.
Rob Pike8f2330d2009-02-25 16:20:44 -08001315</p>
Robert Griesemer434c6052008-11-07 13:34:37 -08001316
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001317<h3 id="Assignment_compatibility">Assignment compatibility</h3>
Rob Pike5af7de32009-02-24 15:17:59 -08001318
Rob Pike5af7de32009-02-24 15:17:59 -08001319<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07001320A value <code>v</code> of static type <code>V</code> is <i>assignment compatible</i>
Rob Pike4fe41922009-11-07 22:00:59 -08001321with a type <code>T</code> if one or more of the following conditions applies:
Rob Pike5af7de32009-02-24 15:17:59 -08001322</p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07001323
Rob Pike5af7de32009-02-24 15:17:59 -08001324<ul>
1325<li>
Robert Griesemer19b1d352009-09-24 19:36:48 -07001326<code>V</code> is compatible with <code>T</code>.
Rob Pike5af7de32009-02-24 15:17:59 -08001327</li>
1328<li>
Robert Griesemer19b1d352009-09-24 19:36:48 -07001329<code>T</code> is an interface type and
1330<code>V</code> <a href="#Interface_types">implements</a> <code>T</code>.
Robert Griesemere2cb60b2009-06-19 13:03:01 -07001331</li>
1332<li>
Robert Griesemer19b1d352009-09-24 19:36:48 -07001333<code>V</code> is a pointer to an array and <code>T</code> is a slice type
1334with compatible element type and at least one of <code>V</code> or <code>T</code> is unnamed.
1335After assignment, the slice variable refers to the original array; the elements are not
1336copied.
Rob Pike5af7de32009-02-24 15:17:59 -08001337</li>
Robert Griesemer4e56b332009-09-10 10:14:00 -07001338<li>
Robert Griesemer19b1d352009-09-24 19:36:48 -07001339<code>V</code> is a bidirectional channel and <code>T</code> is a channel type
1340with compatible element type and at least one of <code>V</code> or <code>T</code> is unnamed.
Robert Griesemer4e56b332009-09-10 10:14:00 -07001341</li>
Rob Pike5af7de32009-02-24 15:17:59 -08001342</ul>
1343
Robert Griesemer19b1d352009-09-24 19:36:48 -07001344<p>
Robert Griesemer326ef132009-09-28 19:21:15 -07001345If <code>T</code> is a struct type, either all fields of <code>T</code>
1346must be <a href="#Exported_identifiers">exported</a>, or the assignment must be in
1347the same package in which <code>T</code> is declared.
1348In other words, a struct value can be assigned to a struct variable only if
1349every field of the struct may be legally assigned individually by the program.
1350</p>
1351
1352<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07001353An untyped <a href="#Constants">constant</a> <code>v</code>
1354is assignment compatible with type <code>T</code> if <code>v</code>
1355can be represented accurately as a value of type <code>T</code>.
1356</p>
1357
1358<p>
1359The predeclared identifier <code>nil</code> is assignment compatible with any
1360pointer, function, slice, map, channel, or interface type and
Robert Griesemer997851e2009-09-25 15:36:25 -07001361represents the <a href="#The_zero_value">zero value</a> for that type.
Robert Griesemer19b1d352009-09-24 19:36:48 -07001362</p>
1363
1364<p>
1365Any value may be assigned to the <a href="#Blank_identifier">blank identifier</a>.
1366</p>
1367
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001368<h3 id="Comparison_compatibility">Comparison compatibility</h3>
Rob Pike5af7de32009-02-24 15:17:59 -08001369
1370<p>
Rob Pike678625d2009-09-15 09:54:22 -07001371Except as noted, values of any type may be compared to other values of
1372<a href="#Type_compatibility">compatible static type</a>.
Ian Lance Taylor18187e72010-03-08 14:05:46 -08001373Values of integer, floating-point, and string type may be compared using the
Rob Pike678625d2009-09-15 09:54:22 -07001374full range of <a href="#Comparison_operators;">comparison operators</a>;
Ian Lance Taylor18187e72010-03-08 14:05:46 -08001375booleans and complex values may be compared only for equality or inequality.
Rob Pike5af7de32009-02-24 15:17:59 -08001376</p>
1377
1378<p>
1379Values of composite type may be
1380compared for equality or inequality using the <code>==</code> and
1381<code>!=</code> operators, with the following provisos:
1382</p>
1383<ul>
1384<li>
1385Arrays and structs may not be compared to anything.
1386</li>
1387<li>
Rob Pikeda389742009-03-02 19:13:40 -08001388A slice value may only be compared explicitly against <code>nil</code>.
1389A slice value is equal to <code>nil</code> if it has been assigned the explicit
Rob Pike678625d2009-09-15 09:54:22 -07001390value <code>nil</code>, if it is uninitialized, or if it has
1391been assigned another slice value equal to <code>nil</code>·
Rob Pike5af7de32009-02-24 15:17:59 -08001392</li>
1393<li>
Rob Pike4fe41922009-11-07 22:00:59 -08001394An interface value is equal to <code>nil</code> if it has
Rob Pike678625d2009-09-15 09:54:22 -07001395been assigned the explicit value <code>nil</code>, if it is uninitialized,
1396or if it has been assigned another interface value equal to <code>nil</code>.
Rob Pike5af7de32009-02-24 15:17:59 -08001397</li>
1398<li>
1399For types that can be compared to <code>nil</code>,
1400two values of the same type are equal if they both equal <code>nil</code>,
1401unequal if one equals <code>nil</code> and one does not.
1402</li>
1403<li>
1404Pointer values are equal if they point to the same location.
1405</li>
1406<li>
Rob Pikeda389742009-03-02 19:13:40 -08001407Function values are equal if they refer to the same function.
Rob Pike5af7de32009-02-24 15:17:59 -08001408</li>
1409<li>
Rob Pikeda389742009-03-02 19:13:40 -08001410Channel and map values are equal if they were created by the same call to <code>make</code>
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001411<a href="#Making_slices">Making slices</a>, maps, and channels).
Rob Pike83cbca52009-08-21 14:18:08 -07001412When comparing two values of channel type, the channel value types
1413must be compatible but the channel direction is ignored.
Rob Pike5af7de32009-02-24 15:17:59 -08001414</li>
1415<li>
Robert Griesemer533dfd62009-05-13 16:56:00 -07001416Interface values may be compared if they have compatible static types.
Rob Pikeda389742009-03-02 19:13:40 -08001417They will be equal only if they have the same dynamic type and the underlying values are equal.
Rob Pike5af7de32009-02-24 15:17:59 -08001418</li>
1419</ul>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001420
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001421<h2 id="Blocks">Blocks</h2>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001422
1423<p>
Robert Griesemer0a162a12009-08-19 16:44:04 -07001424A <i>block</i> is a sequence of declarations and statements within matching
1425brace brackets.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001426</p>
1427
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001428<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -08001429Block = "{" { Statement ";" } "}" .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001430</pre>
Russ Cox5958dd62009-03-04 17:19:21 -08001431
Rob Pikea9ed30f2009-02-23 19:26:07 -08001432<p>
Robert Griesemer0a162a12009-08-19 16:44:04 -07001433In addition to explicit blocks in the source code, there are implicit blocks:
Rob Pikea9ed30f2009-02-23 19:26:07 -08001434</p>
1435
1436<ol>
Robert Griesemer0a162a12009-08-19 16:44:04 -07001437 <li>The <i>universe block</i> encompasses all Go source text.</li>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001438
Robert Griesemer4e56b332009-09-10 10:14:00 -07001439 <li>Each <a href="#Packages">package</a> has a <i>package block</i> containing all
Robert Griesemer0a162a12009-08-19 16:44:04 -07001440 Go source text for that package.</li>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001441
Robert Griesemer0a162a12009-08-19 16:44:04 -07001442 <li>Each file has a <i>file block</i> containing all Go source text
1443 in that file.</li>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001444
Robert Griesemer0a162a12009-08-19 16:44:04 -07001445 <li>Each <code>if</code>, <code>for</code>, and <code>switch</code>
1446 statement is considered to be in its own implicit block.</li>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001447
Russ Cox16b95ba2009-08-20 10:22:52 -07001448 <li>Each clause in a <code>switch</code> or <code>select</code> statement
Robert Griesemer0a162a12009-08-19 16:44:04 -07001449 acts as an implicit block.</li>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001450</ol>
1451
Robert Griesemer0a162a12009-08-19 16:44:04 -07001452<p>
Robert Griesemer4e56b332009-09-10 10:14:00 -07001453Blocks nest and influence <a href="#Declarations_and_scope">scoping</a>.
Robert Griesemer0a162a12009-08-19 16:44:04 -07001454</p>
1455
1456
Robert Griesemeraeaab592009-08-31 17:30:55 -07001457<h2 id="Declarations_and_scope">Declarations and scope</h2>
Robert Griesemer0a162a12009-08-19 16:44:04 -07001458
1459<p>
Robert Griesemer4e56b332009-09-10 10:14:00 -07001460A declaration binds a non-<a href="#Blank_identifier">blank</a>
1461identifier to a constant, type, variable, function, or package.
Robert Griesemer0a162a12009-08-19 16:44:04 -07001462Every identifier in a program must be declared.
1463No identifier may be declared twice in the same block, and
1464no identifier may be declared in both the file and package block.
1465</p>
1466
1467<pre class="ebnf">
1468Declaration = ConstDecl | TypeDecl | VarDecl .
1469TopLevelDecl = Declaration | FunctionDecl | MethodDecl .
1470</pre>
1471
1472<p>
1473The <i>scope</i> of a declared identifier is the extent of source text in which
1474the identifier denotes the specified constant, type, variable, function, or package.
1475</p>
1476
1477<p>
1478Go is lexically scoped using blocks:
1479</p>
1480
1481<ol>
1482 <li>The scope of a predeclared identifier is the universe block.</li>
1483
1484 <li>The scope of an identifier denoting a constant, type, variable,
1485 or function declared at top level (outside any function) is the
1486 package block.</li>
1487
1488 <li>The scope of an imported package identifier is the file block
1489 of the file containing the import declaration.</li>
1490
1491 <li>The scope of an identifier denoting a function parameter or
1492 result variable is the function body.</li>
1493
1494 <li>The scope of a constant or variable identifier declared
1495 inside a function begins at the end of the ConstSpec or VarSpec
1496 and ends at the end of the innermost containing block.</li>
1497
1498 <li>The scope of a type identifier declared inside a function
Russ Cox16b95ba2009-08-20 10:22:52 -07001499 begins at the identifier in the TypeSpec
Robert Griesemer0a162a12009-08-19 16:44:04 -07001500 and ends at the end of the innermost containing block.</li>
1501</ol>
1502
1503<p>
1504An identifier declared in a block may be redeclared in an inner block.
1505While the identifier of the inner declaration is in scope, it denotes
1506the entity declared by the inner declaration.
1507</p>
1508
1509<p>
Robert Griesemer506c0082009-09-08 15:41:14 -07001510The <a href="#Package_clause">package clause</a> is not a declaration; the package name
Robert Griesemer0a162a12009-08-19 16:44:04 -07001511does not appear in any scope. Its purpose is to identify the files belonging
Robert Griesemer997851e2009-09-25 15:36:25 -07001512to the same <a href="#Packages">package</a> and to specify the default package name for import
Robert Griesemer0a162a12009-08-19 16:44:04 -07001513declarations.
1514</p>
1515
1516
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001517<h3 id="Label_scopes">Label scopes</h3>
Robert Griesemer0a162a12009-08-19 16:44:04 -07001518
1519<p>
Robert Griesemer506c0082009-09-08 15:41:14 -07001520Labels are declared by <a href="#Labeled_statements">labeled statements</a> and are
Robert Griesemer0a162a12009-08-19 16:44:04 -07001521used in the <code>break</code>, <code>continue</code>, and <code>goto</code>
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001522statements (§<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 -07001523In contrast to other identifiers, labels are not block scoped and do
1524not conflict with identifiers that are not labels. The scope of a label
1525is the body of the function in which it is declared and excludes
1526the body of any nested function.
1527</p>
1528
1529
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001530<h3 id="Predeclared_identifiers">Predeclared identifiers</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001531
1532<p>
Robert Griesemer0a162a12009-08-19 16:44:04 -07001533The following identifiers are implicitly declared in the universe block:
Rob Pikea9ed30f2009-02-23 19:26:07 -08001534</p>
1535<pre class="grammar">
1536Basic types:
Russ Cox5958dd62009-03-04 17:19:21 -08001537 bool byte float32 float64 int8 int16 int32 int64
1538 string uint8 uint16 uint32 uint64
Rob Pikea9ed30f2009-02-23 19:26:07 -08001539
Rob Pike5af7de32009-02-24 15:17:59 -08001540Architecture-specific convenience types:
Rob Pikea9ed30f2009-02-23 19:26:07 -08001541 float int uint uintptr
1542
1543Constants:
Robert Griesemer19b1d352009-09-24 19:36:48 -07001544 true false iota
1545
1546Zero value:
1547 nil
Rob Pikea9ed30f2009-02-23 19:26:07 -08001548
1549Functions:
Rob Pike72970872010-03-04 12:35:16 -08001550 cap close closed cmplx copy imag len make
Rob Pikebf9b8f22010-03-24 15:17:00 -07001551 new panic print println real
Rob Pikea9ed30f2009-02-23 19:26:07 -08001552</pre>
1553
Robert Griesemeraeaab592009-08-31 17:30:55 -07001554
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001555<h3 id="Exported_identifiers">Exported identifiers</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001556
1557<p>
Robert Griesemeraeaab592009-08-31 17:30:55 -07001558An identifier may be <i>exported</i> to permit access to it from another package
1559using a <a href="#Qualified_identifiers">qualified identifier</a>. An identifier
1560is exported if both:
Rob Pikea9ed30f2009-02-23 19:26:07 -08001561</p>
1562<ol>
Robert Griesemeraeaab592009-08-31 17:30:55 -07001563 <li>the first character of the identifier's name is a Unicode upper case letter (Unicode class "Lu"); and
Rob Pike678625d2009-09-15 09:54:22 -07001564 <li>the identifier is declared in the <a href="#Blocks">package block</a> or denotes a field or method of a type
Robert Griesemeraeaab592009-08-31 17:30:55 -07001565 declared in that block.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001566</ol>
1567<p>
Robert Griesemeraeaab592009-08-31 17:30:55 -07001568All other identifiers are not exported.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001569</p>
1570
Robert Griesemeraeaab592009-08-31 17:30:55 -07001571
Robert Griesemer4e56b332009-09-10 10:14:00 -07001572<h3 id="Blank_identifier">Blank identifier</h3>
1573
1574<p>
1575The <i>blank identifier</i>, represented by the underscore character <code>_</code>, may be used in a declaration like
1576any other identifier but the declaration does not introduce a new binding.
1577</p>
1578
1579
Robert Griesemer19b1d352009-09-24 19:36:48 -07001580<h3 id="Constant_declarations">Constant declarations</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001581
1582<p>
1583A constant declaration binds a list of identifiers (the names of
Robert Griesemer506c0082009-09-08 15:41:14 -07001584the constants) to the values of a list of <a href="#Constant_expressions">constant expressions</a>.
1585The number of identifiers must be equal
1586to the number of expressions, and the <i>n</i>th identifier on
1587the left is bound to the value of the <i>n</i>th expression on the
Rob Pikea9ed30f2009-02-23 19:26:07 -08001588right.
1589</p>
1590
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001591<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -08001592ConstDecl = "const" ( ConstSpec | "(" { ConstSpec ";" } ")" ) .
Robert Griesemer4023dce2009-08-14 17:41:52 -07001593ConstSpec = IdentifierList [ [ Type ] "=" ExpressionList ] .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001594
1595IdentifierList = identifier { "," identifier } .
1596ExpressionList = Expression { "," Expression } .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001597</pre>
1598
1599<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07001600If the type is present, all constants take the type specified, and
1601the expressions must be <a href="#Assignment_compatibility">assignment compatible</a> with that type.
Robert Griesemer4023dce2009-08-14 17:41:52 -07001602If the type is omitted, the constants take the
Robert Griesemer19b1d352009-09-24 19:36:48 -07001603individual types of the corresponding expressions.
1604If the expression values are untyped <a href="#Constants">constants</a>,
1605the declared constants remain untyped and the constant identifiers
1606denote the constant values. For instance, if the expression is a
1607floating-point literal, the constant identifier denotes a floating-point
1608constant, even if the literal's fractional part is zero.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001609</p>
1610
1611<pre>
1612const Pi float64 = 3.14159265358979323846
Robert Griesemer19b1d352009-09-24 19:36:48 -07001613const zero = 0.0 // untyped floating-point constant
Rob Pikea9ed30f2009-02-23 19:26:07 -08001614const (
Robert Griesemer130ac742009-12-10 16:43:01 -08001615 size int64 = 1024
1616 eof = -1 // untyped integer constant
Rob Pikea9ed30f2009-02-23 19:26:07 -08001617)
Robert Griesemer19b1d352009-09-24 19:36:48 -07001618const a, b, c = 3, 4, "foo" // a = 3, b = 4, c = "foo", untyped integer and string constants
Rob Pikea9ed30f2009-02-23 19:26:07 -08001619const u, v float = 0, 3 // u = 0.0, v = 3.0
1620</pre>
1621
1622<p>
1623Within a parenthesized <code>const</code> declaration list the
1624expression list may be omitted from any but the first declaration.
1625Such an empty list is equivalent to the textual substitution of the
Rob Pike678625d2009-09-15 09:54:22 -07001626first preceding non-empty expression list and its type if any.
Russ Cox5958dd62009-03-04 17:19:21 -08001627Omitting the list of expressions is therefore equivalent to
1628repeating the previous list. The number of identifiers must be equal
1629to the number of expressions in the previous list.
Robert Griesemer506c0082009-09-08 15:41:14 -07001630Together with the <a href="#Iota"><code>iota</code> constant generator</a>
1631this mechanism permits light-weight declaration of sequential values:
Rob Pikea9ed30f2009-02-23 19:26:07 -08001632</p>
1633
1634<pre>
1635const (
Robert Griesemer130ac742009-12-10 16:43:01 -08001636 Sunday = iota
1637 Monday
1638 Tuesday
1639 Wednesday
1640 Thursday
1641 Friday
1642 Partyday
1643 numberOfDays // this constant is not exported
Rob Pikea9ed30f2009-02-23 19:26:07 -08001644)
1645</pre>
1646
1647
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001648<h3 id="Iota">Iota</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001649
1650<p>
Robert Griesemere9192752009-12-01 16:15:53 -08001651Within a constant declaration, the predeclared identifier
Robert Griesemer19b1d352009-09-24 19:36:48 -07001652<code>iota</code> represents successive untyped integer <a href="#Constants">
1653constants</a>. It is reset to 0 whenever the reserved word <code>const</code>
Robert Griesemer130ac742009-12-10 16:43:01 -08001654appears in the source and increments with each
1655<a href="#Semicolons">semicolon</a>. It can be used to construct a
Rob Pikea9ed30f2009-02-23 19:26:07 -08001656set of related constants:
1657</p>
1658
1659<pre>
Robert Griesemer4e56b332009-09-10 10:14:00 -07001660const ( // iota is reset to 0
Robert Griesemer130ac742009-12-10 16:43:01 -08001661 c0 = iota // c0 == 0
1662 c1 = iota // c1 == 1
1663 c2 = iota // c2 == 2
Rob Pikea9ed30f2009-02-23 19:26:07 -08001664)
1665
1666const (
Robert Griesemer130ac742009-12-10 16:43:01 -08001667 a = 1 &lt;&lt; iota // a == 1 (iota has been reset)
1668 b = 1 &lt;&lt; iota // b == 2
1669 c = 1 &lt;&lt; iota // c == 4
Rob Pikea9ed30f2009-02-23 19:26:07 -08001670)
1671
1672const (
Robert Griesemer130ac742009-12-10 16:43:01 -08001673 u = iota * 42 // u == 0 (untyped integer constant)
1674 v float = iota * 42 // v == 42.0 (float constant)
1675 w = iota * 42 // w == 84 (untyped integer constant)
Rob Pikea9ed30f2009-02-23 19:26:07 -08001676)
1677
Robert Griesemer130ac742009-12-10 16:43:01 -08001678const x = iota // x == 0 (iota has been reset)
1679const y = iota // y == 0 (iota has been reset)
Rob Pikea9ed30f2009-02-23 19:26:07 -08001680</pre>
1681
1682<p>
1683Within an ExpressionList, the value of each <code>iota</code> is the same because
1684it is only incremented at a semicolon:
1685</p>
1686
1687<pre>
1688const (
Robert Griesemer130ac742009-12-10 16:43:01 -08001689 bit0, mask0 = 1 &lt;&lt; iota, 1 &lt;&lt; iota - 1 // bit0 == 1, mask0 == 0
1690 bit1, mask1 // bit1 == 2, mask1 == 1
1691 _, _ // skips iota == 2
1692 bit3, mask3 // bit3 == 8, mask3 == 7
Rob Pikea9ed30f2009-02-23 19:26:07 -08001693)
1694</pre>
1695
1696<p>
1697This last example exploits the implicit repetition of the
1698last non-empty expression list.
1699</p>
1700
1701
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001702<h3 id="Type_declarations">Type declarations</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001703
1704<p>
Robert Griesemerfc61b772009-09-28 14:10:20 -07001705A type declaration binds an identifier, the <i>type name</i>, to a new type
1706that has the same definition (element, fields, channel direction, etc.) as
1707an existing type. The new type is
1708<a href="#Properties_of_types_and_values">compatible</a> with, but
1709<a href="#Properties_of_types_and_values">different</a> from, the existing type.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001710</p>
1711
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001712<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -08001713TypeDecl = "type" ( TypeSpec | "(" { TypeSpec ";" } ")" ) .
Robert Griesemerbdec3302009-08-31 17:57:14 -07001714TypeSpec = identifier Type .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001715</pre>
1716
1717<pre>
Robert Griesemerbdec3302009-08-31 17:57:14 -07001718type IntArray [16]int
Rob Pikea9ed30f2009-02-23 19:26:07 -08001719
1720type (
Robert Griesemer130ac742009-12-10 16:43:01 -08001721 Point struct { x, y float }
Rob Pikea9ed30f2009-02-23 19:26:07 -08001722 Polar Point
1723)
1724
1725type TreeNode struct {
Robert Griesemer130ac742009-12-10 16:43:01 -08001726 left, right *TreeNode
1727 value *Comparable
Rob Pikea9ed30f2009-02-23 19:26:07 -08001728}
1729
Rob Pike678625d2009-09-15 09:54:22 -07001730type Cipher interface {
Robert Griesemer130ac742009-12-10 16:43:01 -08001731 BlockSize() int
1732 Encrypt(src, dst []byte)
1733 Decrypt(src, dst []byte)
Rob Pikea9ed30f2009-02-23 19:26:07 -08001734}
1735</pre>
1736
Robert Griesemerfc61b772009-09-28 14:10:20 -07001737<p>
1738The declared type does not inherit any <a href="#Method_declarations">methods</a>
1739bound to the existing type, but the <a href="#Types">method set</a>
1740of elements of a composite type is not changed:
1741</p>
1742
1743<pre>
1744// A Mutex is a data type with two methods Lock and Unlock.
1745type Mutex struct { /* Mutex fields */ }
1746func (m *Mutex) Lock() { /* Lock implementation */ }
1747func (m *Mutex) Unlock() { /* Unlock implementation */ }
1748
1749// NewMutex has the same composition as Mutex but its method set is empty.
1750type NewMutex Mutex
1751
Rob Pike4fe41922009-11-07 22:00:59 -08001752// PrintableMutex's method set contains the methods
1753// Lock and Unlock bound to its anonymous field Mutex.
Robert Griesemerfc61b772009-09-28 14:10:20 -07001754type PrintableMutex struct {
Robert Griesemer130ac742009-12-10 16:43:01 -08001755 Mutex
Robert Griesemerfc61b772009-09-28 14:10:20 -07001756}
1757</pre>
1758
1759<p>
1760A type declaration may be used to define a different boolean, numeric, or string
1761type and attach methods to it:
1762</p>
1763
1764<pre>
1765type TimeZone int
1766
1767const (
Robert Griesemer130ac742009-12-10 16:43:01 -08001768 EST TimeZone = -(5 + iota)
1769 CST
1770 MST
1771 PST
Robert Griesemerfc61b772009-09-28 14:10:20 -07001772)
1773
1774func (tz TimeZone) String() string {
Robert Griesemer130ac742009-12-10 16:43:01 -08001775 return fmt.Sprintf("GMT+%dh", tz)
Robert Griesemerfc61b772009-09-28 14:10:20 -07001776}
1777</pre>
1778
1779
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001780<h3 id="Variable_declarations">Variable declarations</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001781
1782<p>
1783A variable declaration creates a variable, binds an identifier to it and
1784gives it a type and optionally an initial value.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001785</p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001786<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -08001787VarDecl = "var" ( VarSpec | "(" { VarSpec ";" } ")" ) .
Robert Griesemer4023dce2009-08-14 17:41:52 -07001788VarSpec = IdentifierList ( Type [ "=" ExpressionList ] | "=" ExpressionList ) .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001789</pre>
1790
1791<pre>
1792var i int
1793var U, V, W float
1794var k = 0
Rob Pike678625d2009-09-15 09:54:22 -07001795var x, y float = -1, -2
Rob Pikea9ed30f2009-02-23 19:26:07 -08001796var (
Robert Griesemer130ac742009-12-10 16:43:01 -08001797 i int
Rob Pikea9ed30f2009-02-23 19:26:07 -08001798 u, v, s = 2.0, 3.0, "bar"
1799)
Robert Griesemer4e56b332009-09-10 10:14:00 -07001800var re, im = complexSqrt(-1)
Robert Griesemer130ac742009-12-10 16:43:01 -08001801var _, found = entries[name] // map lookup; only interested in "found"
Rob Pikea9ed30f2009-02-23 19:26:07 -08001802</pre>
1803
1804<p>
Robert Griesemeref45e642009-08-21 11:25:00 -07001805If a list of expressions is given, the variables are initialized
Rob Pike678625d2009-09-15 09:54:22 -07001806by assigning the expressions to the variables (§<a href="#Assignments">Assignments</a>)
1807in order; all expressions must be consumed and all variables initialized from them.
Rob Pike4fe41922009-11-07 22:00:59 -08001808Otherwise, each variable is initialized to its <a href="#The_zero_value">zero value</a>.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001809</p>
Robert Griesemeref45e642009-08-21 11:25:00 -07001810
Rob Pikea9ed30f2009-02-23 19:26:07 -08001811<p>
Robert Griesemeref45e642009-08-21 11:25:00 -07001812If the type is present, each variable is given that type.
1813Otherwise, the types are deduced from the assignment
1814of the expression list.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001815</p>
Robert Griesemeref45e642009-08-21 11:25:00 -07001816
Rob Pikea9ed30f2009-02-23 19:26:07 -08001817<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07001818If the type is absent and the corresponding expression evaluates to an
1819untyped <a href="#Constants">constant</a>, the type of the declared variable
1820is <code>bool</code>, <code>int</code>, <code>float</code>, or <code>string</code>
1821respectively, depending on whether the value is a boolean, integer,
1822floating-point, or string constant:
Rob Pikea9ed30f2009-02-23 19:26:07 -08001823</p>
1824
1825<pre>
Robert Griesemer19b1d352009-09-24 19:36:48 -07001826var b = true // t has type bool
Rob Pikea9ed30f2009-02-23 19:26:07 -08001827var i = 0 // i has type int
Robert Griesemer19b1d352009-09-24 19:36:48 -07001828var f = 3.0 // f has type float
Robert Griesemeref45e642009-08-21 11:25:00 -07001829var s = "OMDB" // s has type string
Rob Pikea9ed30f2009-02-23 19:26:07 -08001830</pre>
1831
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001832<h3 id="Short_variable_declarations">Short variable declarations</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001833
Robert Griesemer997851e2009-09-25 15:36:25 -07001834<p>
Robert Griesemeref45e642009-08-21 11:25:00 -07001835A <i>short variable declaration</i> uses the syntax:
Robert Griesemer997851e2009-09-25 15:36:25 -07001836</p>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001837
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001838<pre class="ebnf">
Robert Griesemere1b8cb82009-07-16 20:31:41 -07001839ShortVarDecl = IdentifierList ":=" ExpressionList .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001840</pre>
1841
Robert Griesemer997851e2009-09-25 15:36:25 -07001842<p>
Robert Griesemeref45e642009-08-21 11:25:00 -07001843It is a shorthand for a regular variable declaration with
1844initializer expressions but no types:
Robert Griesemer997851e2009-09-25 15:36:25 -07001845</p>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001846
1847<pre class="grammar">
1848"var" IdentifierList = ExpressionList .
1849</pre>
1850
1851<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08001852i, j := 0, 10
1853f := func() int { return 7 }
1854ch := make(chan int)
1855r, w := os.Pipe(fd) // os.Pipe() returns two values
1856_, y, _ := coord(p) // coord() returns three values; only interested in y coordinate
Rob Pikea9ed30f2009-02-23 19:26:07 -08001857</pre>
1858
1859<p>
Robert Griesemeref45e642009-08-21 11:25:00 -07001860Unlike regular variable declarations, a short variable declaration may redeclare variables provided they
Rob Pike2a1683a2009-04-19 20:04:15 -07001861were originally declared in the same block with the same type, and at
Robert Griesemer4e56b332009-09-10 10:14:00 -07001862least one of the non-<a href="#Blank_identifier">blank</a> variables is new. As a consequence, redeclaration
Rob Pike2a1683a2009-04-19 20:04:15 -07001863can only appear in a multi-variable short declaration.
1864Redeclaration does not introduce a new
1865variable; it just assigns a new value to the original.
1866</p>
1867
1868<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08001869field1, offset := nextField(str, 0)
1870field2, offset := nextField(str, offset) // redeclares offset
Rob Pike2a1683a2009-04-19 20:04:15 -07001871</pre>
1872
1873<p>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001874Short variable declarations may appear only inside functions.
1875In some contexts such as the initializers for <code>if</code>,
1876<code>for</code>, or <code>switch</code> statements,
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001877they can be used to declare local temporary variables (§<a href="#Statements">Statements</a>).
Rob Pikea9ed30f2009-02-23 19:26:07 -08001878</p>
1879
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001880<h3 id="Function_declarations">Function declarations</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001881
1882<p>
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001883A function declaration binds an identifier to a function (§<a href="#Function_types">Function types</a>).
Rob Pikea9ed30f2009-02-23 19:26:07 -08001884</p>
1885
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001886<pre class="ebnf">
Robert Griesemer0a162a12009-08-19 16:44:04 -07001887FunctionDecl = "func" identifier Signature [ Body ] .
1888Body = Block.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001889</pre>
1890
Robert Griesemer4023dce2009-08-14 17:41:52 -07001891<p>
1892A function declaration may omit the body. Such a declaration provides the
1893signature for a function implemented outside Go, such as an assembly routine.
1894</p>
1895
Rob Pikea9ed30f2009-02-23 19:26:07 -08001896<pre>
1897func min(x int, y int) int {
1898 if x &lt; y {
Robert Griesemer130ac742009-12-10 16:43:01 -08001899 return x
Rob Pikea9ed30f2009-02-23 19:26:07 -08001900 }
Robert Griesemer130ac742009-12-10 16:43:01 -08001901 return y
Rob Pikea9ed30f2009-02-23 19:26:07 -08001902}
Robert Griesemer4023dce2009-08-14 17:41:52 -07001903
1904func flushICache(begin, end uintptr) // implemented externally
Rob Pikea9ed30f2009-02-23 19:26:07 -08001905</pre>
1906
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001907<h3 id="Method_declarations">Method declarations</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001908
1909<p>
Robert Griesemere9192752009-12-01 16:15:53 -08001910A method is a function with a <i>receiver</i>.
1911A method declaration binds an identifier to a method.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001912</p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001913<pre class="ebnf">
Rob Pike4fe41922009-11-07 22:00:59 -08001914MethodDecl = "func" Receiver MethodName Signature [ Body ] .
1915Receiver = "(" [ identifier ] [ "*" ] BaseTypeName ")" .
Robert Griesemerfc61b772009-09-28 14:10:20 -07001916BaseTypeName = identifier .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001917</pre>
1918
1919<p>
Robert Griesemer56809d02009-05-20 11:02:48 -07001920The receiver type must be of the form <code>T</code> or <code>*T</code> where
1921<code>T</code> is a type name. <code>T</code> is called the
1922<i>receiver base type</i> or just <i>base type</i>.
1923The base type must not be a pointer or interface type and must be
Stephen Ma5db1d382009-09-02 20:09:25 -07001924declared in the same package as the method.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001925The method is said to be <i>bound</i> to the base type
1926and is visible only within selectors for that type
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001927<a href="#Type_declarations">Type declarations</a>, §<a href="#Selectors">Selectors</a>).
Rob Pikea9ed30f2009-02-23 19:26:07 -08001928</p>
1929
1930<p>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001931Given type <code>Point</code>, the declarations
1932</p>
1933
1934<pre>
1935func (p *Point) Length() float {
Robert Griesemer130ac742009-12-10 16:43:01 -08001936 return Math.sqrt(p.x * p.x + p.y * p.y)
Rob Pikea9ed30f2009-02-23 19:26:07 -08001937}
1938
1939func (p *Point) Scale(factor float) {
Robert Griesemer130ac742009-12-10 16:43:01 -08001940 p.x = p.x * factor
1941 p.y = p.y * factor
Rob Pikea9ed30f2009-02-23 19:26:07 -08001942}
1943</pre>
1944
1945<p>
Rob Pike678625d2009-09-15 09:54:22 -07001946bind the methods <code>Length</code> and <code>Scale</code>,
1947with receiver type <code>*Point</code>,
Rob Pikea9ed30f2009-02-23 19:26:07 -08001948to the base type <code>Point</code>.
1949</p>
1950
1951<p>
Robert Griesemere9192752009-12-01 16:15:53 -08001952If the receiver's value is not referenced inside the body of the method,
Rob Pikea9ed30f2009-02-23 19:26:07 -08001953its identifier may be omitted in the declaration. The same applies in
1954general to parameters of functions and methods.
1955</p>
1956
1957<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08001958The type of a method is the type of a function with the receiver as first
1959argument. For instance, the method <code>Scale</code> has type
1960</p>
1961
1962<pre>
1963(p *Point, factor float)
1964</pre>
1965
1966<p>
1967However, a function declared this way is not a method.
1968</p>
1969
Rob Pikea9ed30f2009-02-23 19:26:07 -08001970
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001971<h2 id="Expressions">Expressions</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001972
Rob Pikedf3183f2009-02-26 16:37:23 -08001973<p>
1974An expression specifies the computation of a value by applying
Robert Griesemer19b1d352009-09-24 19:36:48 -07001975operators and functions to operands.
Rob Pikedf3183f2009-02-26 16:37:23 -08001976</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001977
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001978<h3 id="Operands">Operands</h3>
Robert Griesemerad711102008-09-11 17:48:20 -07001979
Robert Griesemer997851e2009-09-25 15:36:25 -07001980<p>
Robert Griesemerad711102008-09-11 17:48:20 -07001981Operands denote the elementary values in an expression.
Robert Griesemer997851e2009-09-25 15:36:25 -07001982</p>
Robert Griesemerad711102008-09-11 17:48:20 -07001983
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001984<pre class="ebnf">
Rob Pike01cadde2009-09-15 15:56:44 -07001985Operand = Literal | QualifiedIdent | MethodExpr | "(" Expression ")" .
Rob Pikedf3183f2009-02-26 16:37:23 -08001986Literal = BasicLit | CompositeLit | FunctionLit .
Rob Pike72970872010-03-04 12:35:16 -08001987BasicLit = int_lit | float_lit | imaginary_lit | char_lit | string_lit .
Robert Griesemerc2d55862009-02-19 16:49:10 -08001988</pre>
Robert Griesemerad711102008-09-11 17:48:20 -07001989
1990
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001991<h3 id="Qualified_identifiers">Qualified identifiers</h3>
Robert Griesemerad711102008-09-11 17:48:20 -07001992
Robert Griesemerc2d55862009-02-19 16:49:10 -08001993<p>
Robert Griesemer4e56b332009-09-10 10:14:00 -07001994A 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 -08001995</p>
Robert Griesemer337af312008-11-17 18:11:36 -08001996
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001997<pre class="ebnf">
Russ Cox16b95ba2009-08-20 10:22:52 -07001998QualifiedIdent = [ PackageName "." ] identifier .
Robert Griesemerc2d55862009-02-19 16:49:10 -08001999</pre>
Robert Griesemerad711102008-09-11 17:48:20 -07002000
Rob Pikedf3183f2009-02-26 16:37:23 -08002001<p>
Robert Griesemeraeaab592009-08-31 17:30:55 -07002002A qualified identifier accesses an identifier in a separate package.
2003The identifier must be <a href="#Exported_identifiers">exported</a> by that
2004package, which means that it must begin with a Unicode upper case letter.
Rob Pikedf3183f2009-02-26 16:37:23 -08002005</p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002006
2007<pre>
Rob Pike678625d2009-09-15 09:54:22 -07002008math.Sin
Rob Pikedf3183f2009-02-26 16:37:23 -08002009</pre>
Robert Griesemerad711102008-09-11 17:48:20 -07002010
Rob Pike0b4de7a2009-11-09 16:09:57 -08002011<!---
Robert Griesemer4e56b332009-09-10 10:14:00 -07002012<p>
Robert Griesemer90cc4a52009-10-22 09:41:38 -07002013<span class="alert">TODO: Unify this section with Selectors - it's the same syntax.</span>
Robert Griesemer4e56b332009-09-10 10:14:00 -07002014</p>
Rob Pike0b4de7a2009-11-09 16:09:57 -08002015--->
Robert Griesemer4e56b332009-09-10 10:14:00 -07002016
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002017<h3 id="Composite_literals">Composite literals</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002018
Rob Pikedf3183f2009-02-26 16:37:23 -08002019<p>
2020Composite literals construct values for structs, arrays, slices, and maps
2021and create a new value each time they are evaluated.
2022They consist of the type of the value
Robert Griesemer838cf122009-05-22 10:25:06 -07002023followed by a brace-bound list of composite elements. An element may be
2024a single expression or a key-value pair.
Rob Pikedf3183f2009-02-26 16:37:23 -08002025</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002026
Robert Griesemerf7ac31362009-07-10 16:06:40 -07002027<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -08002028CompositeLit = LiteralType "{" [ ElementList [ "," ] ] "}" .
Rob Pikedf3183f2009-02-26 16:37:23 -08002029LiteralType = StructType | ArrayType | "[" "..." "]" ElementType |
Robert Griesemer164a7bc2009-09-30 12:00:25 -07002030 SliceType | MapType | TypeName | "(" LiteralType ")" .
Robert Griesemer130ac742009-12-10 16:43:01 -08002031ElementList = Element { "," Element } .
Robert Griesemer838cf122009-05-22 10:25:06 -07002032Element = [ Key ":" ] Value .
Robert Griesemerfb5fce52009-11-09 12:35:56 -08002033Key = FieldName | ElementIndex .
Rob Pike678625d2009-09-15 09:54:22 -07002034FieldName = identifier .
Robert Griesemerfb5fce52009-11-09 12:35:56 -08002035ElementIndex = Expression .
Robert Griesemer838cf122009-05-22 10:25:06 -07002036Value = Expression .
Robert Griesemerc2d55862009-02-19 16:49:10 -08002037</pre>
Robert Griesemer0976e342008-09-03 13:37:44 -07002038
Rob Pikedf3183f2009-02-26 16:37:23 -08002039<p>
Robert Griesemer838cf122009-05-22 10:25:06 -07002040The LiteralType must be a struct, array, slice, or map type
2041(the grammar enforces this constraint except when the type is given
2042as a TypeName).
Robert Griesemer326ef132009-09-28 19:21:15 -07002043The types of the expressions must be <a href="#Assignment_compatibility">assignment compatible</a> with
Russ Cox7a5e97b2009-03-03 15:40:30 -08002044the respective field, element, and key types of the LiteralType;
2045there is no additional conversion.
Robert Griesemer838cf122009-05-22 10:25:06 -07002046The key is interpreted as a field name for struct literals,
Rob Pike678625d2009-09-15 09:54:22 -07002047an index expression for array and slice literals, and a key for map literals.
Robert Griesemer838cf122009-05-22 10:25:06 -07002048For map literals, all elements must have a key. It is an error
2049to specify multiple elements with the same field name or
2050constant key value.
Rob Pikedf3183f2009-02-26 16:37:23 -08002051</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002052
Robert Griesemer838cf122009-05-22 10:25:06 -07002053<p>
2054For struct literals the following rules apply:
Robert Griesemercfe92112009-06-18 13:29:40 -07002055</p>
Robert Griesemer838cf122009-05-22 10:25:06 -07002056<ul>
Robert Griesemerd3b15652009-11-16 08:58:55 -08002057 <li>A key must be a field name declared in the LiteralType.
2058 </li>
Rob Pike678625d2009-09-15 09:54:22 -07002059 <li>A literal that does not contain any keys must
Robert Griesemer838cf122009-05-22 10:25:06 -07002060 list an element for each struct field in the
2061 order in which the fields are declared.
2062 </li>
2063 <li>If any element has a key, every element must have a key.
2064 </li>
Rob Pike678625d2009-09-15 09:54:22 -07002065 <li>A literal that contains keys does not need to
Robert Griesemer838cf122009-05-22 10:25:06 -07002066 have an element for each struct field. Omitted fields
2067 get the zero value for that field.
2068 </li>
2069 <li>A literal may omit the element list; such a literal evaluates
2070 to the zero value for its type.
2071 </li>
2072 <li>It is an error to specify an element for a non-exported
2073 field of a struct belonging to a different package.
2074 </li>
2075</ul>
Robert Griesemer838cf122009-05-22 10:25:06 -07002076
2077<p>
2078Given the declarations
2079</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002080<pre>
Robert Griesemer838cf122009-05-22 10:25:06 -07002081type Point struct { x, y, z float }
2082type Line struct { p, q Point }
Robert Griesemerc2d55862009-02-19 16:49:10 -08002083</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002084
Rob Pikedf3183f2009-02-26 16:37:23 -08002085<p>
2086one may write
2087</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002088
Robert Griesemerc2d55862009-02-19 16:49:10 -08002089<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08002090origin := Point{} // zero value for Point
2091line := Line{origin, Point{y: -4, z: 12.3}} // zero value for line.q.x
Rob Pike37ab8382009-03-18 22:58:36 -07002092</pre>
2093
Robert Griesemercfe92112009-06-18 13:29:40 -07002094<p>
2095For array and slice literals the following rules apply:
2096</p>
Robert Griesemer838cf122009-05-22 10:25:06 -07002097<ul>
2098 <li>Each element has an associated integer index marking
2099 its position in the array.
2100 </li>
2101 <li>An element with a key uses the key as its index; the
2102 key must be a constant integer expression.
2103 </li>
2104 <li>An element without a key uses the previous element's index plus one.
2105 If the first element has no key, its index is zero.
2106 </li>
2107</ul>
Robert Griesemer838cf122009-05-22 10:25:06 -07002108
Rob Pike37ab8382009-03-18 22:58:36 -07002109<p>
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002110Taking the address of a composite literal (§<a href="#Address_operators">Address operators</a>)
Rob Pike2a5af742009-03-20 17:03:48 -07002111generates a unique pointer to an instance of the literal's value.
Rob Pike37ab8382009-03-18 22:58:36 -07002112</p>
Rob Pike37ab8382009-03-18 22:58:36 -07002113<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08002114var pointer *Point = &amp;Point{y: 1000}
Robert Griesemerc2d55862009-02-19 16:49:10 -08002115</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002116
Rob Pikedf3183f2009-02-26 16:37:23 -08002117<p>
Robert Griesemera3294712009-01-05 11:17:26 -08002118The length of an array literal is the length specified in the LiteralType.
2119If fewer elements than the length are provided in the literal, the missing
Rob Pike8f2330d2009-02-25 16:20:44 -08002120elements are set to the zero value for the array element type.
Robert Griesemer838cf122009-05-22 10:25:06 -07002121It is an error to provide elements with index values outside the index range
2122of the array. The notation <code>...</code> specifies an array length equal
2123to the maximum element index plus one.
Rob Pikedf3183f2009-02-26 16:37:23 -08002124</p>
Robert Griesemerb90b2132008-09-19 15:49:55 -07002125
Robert Griesemerc2d55862009-02-19 16:49:10 -08002126<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08002127buffer := [10]string{} // len(buffer) == 10
2128intSet := [6]int{1, 2, 3, 5} // len(intSet) == 6
2129days := [...]string{"Sat", "Sun"} // len(days) == 2
Robert Griesemerc2d55862009-02-19 16:49:10 -08002130</pre>
Robert Griesemer91bbd642009-01-07 09:31:35 -08002131
Rob Pikedf3183f2009-02-26 16:37:23 -08002132<p>
2133A slice literal describes the entire underlying array literal.
Rob Pike678625d2009-09-15 09:54:22 -07002134Thus, the length and capacity of a slice literal are the maximum
Robert Griesemer838cf122009-05-22 10:25:06 -07002135element index plus one. A slice literal has the form
Rob Pikedf3183f2009-02-26 16:37:23 -08002136</p>
Robert Griesemer91bbd642009-01-07 09:31:35 -08002137
Robert Griesemerc2d55862009-02-19 16:49:10 -08002138<pre>
Rob Pike426335f2009-03-02 17:52:52 -08002139[]T{x1, x2, ... xn}
Robert Griesemerc2d55862009-02-19 16:49:10 -08002140</pre>
Robert Griesemer91bbd642009-01-07 09:31:35 -08002141
Rob Pikedf3183f2009-02-26 16:37:23 -08002142<p>
2143and is a shortcut for a slice operation applied to an array literal:
2144</p>
Robert Griesemer91bbd642009-01-07 09:31:35 -08002145
Robert Griesemerc2d55862009-02-19 16:49:10 -08002146<pre>
Rob Pike426335f2009-03-02 17:52:52 -08002147[n]T{x1, x2, ... xn}[0 : n]
Robert Griesemerc2d55862009-02-19 16:49:10 -08002148</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002149
Rob Pikedf3183f2009-02-26 16:37:23 -08002150<p>
Russ Cox7a5e97b2009-03-03 15:40:30 -08002151A parsing ambiguity arises when a composite literal using the
2152TypeName form of the LiteralType appears in the condition of an
2153"if", "for", or "switch" statement, because the braces surrounding
2154the expressions in the literal are confused with those introducing
2155a block of statements. To resolve the ambiguity in this rare case,
2156the composite literal must appear within
2157parentheses.
2158</p>
2159
2160<pre>
2161if x == (T{a,b,c}[i]) { ... }
2162if (x == T{a,b,c}[i]) { ... }
2163</pre>
2164
Robert Griesemer838cf122009-05-22 10:25:06 -07002165<p>
2166Examples of valid array, slice, and map literals:
2167</p>
2168
2169<pre>
2170// list of prime numbers
Robert Griesemer130ac742009-12-10 16:43:01 -08002171primes := []int{2, 3, 5, 7, 9, 11, 13, 17, 19, 991}
Robert Griesemer838cf122009-05-22 10:25:06 -07002172
2173// vowels[ch] is true if ch is a vowel
Robert Griesemer130ac742009-12-10 16:43:01 -08002174vowels := [128]bool{'a': true, 'e': true, 'i': true, 'o': true, 'u': true, 'y': true}
Robert Griesemer838cf122009-05-22 10:25:06 -07002175
Robert Griesemer130ac742009-12-10 16:43:01 -08002176// the array [10]float{-1, 0, 0, 0, -0.1, -0.1, 0, 0, 0, -1}
2177filter := [10]float{-1, 4: -0.1, -0.1, 9: -1}
Robert Griesemer838cf122009-05-22 10:25:06 -07002178
2179// frequencies in Hz for equal-tempered scale (A4 = 440Hz)
2180noteFrequency := map[string]float{
2181 "C0": 16.35, "D0": 18.35, "E0": 20.60, "F0": 21.83,
2182 "G0": 24.50, "A0": 27.50, "B0": 30.87,
2183}
2184</pre>
2185
2186
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002187<h3 id="Function_literals">Function literals</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002188
Rob Pikedf3183f2009-02-26 16:37:23 -08002189<p>
2190A function literal represents an anonymous function.
2191It consists of a specification of the function type and a function body.
2192</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002193
Robert Griesemerf7ac31362009-07-10 16:06:40 -07002194<pre class="ebnf">
Robert Griesemer0a162a12009-08-19 16:44:04 -07002195FunctionLit = FunctionType Body .
Robert Griesemerc2d55862009-02-19 16:49:10 -08002196</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002197
Robert Griesemerc2d55862009-02-19 16:49:10 -08002198<pre>
Russ Cox46871692010-01-26 10:25:56 -08002199func(a, b int, z float) bool { return a*b &lt; int(z) }
Robert Griesemerc2d55862009-02-19 16:49:10 -08002200</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002201
Rob Pikedf3183f2009-02-26 16:37:23 -08002202<p>
2203A function literal can be assigned to a variable or invoked directly.
2204</p>
Robert Griesemer7231ceb2008-09-08 15:01:04 -07002205
Robert Griesemerc2d55862009-02-19 16:49:10 -08002206<pre>
Rob Pikedf3183f2009-02-26 16:37:23 -08002207f := func(x, y int) int { return x + y }
2208func(ch chan int) { ch &lt;- ACK } (reply_chan)
Robert Griesemerc2d55862009-02-19 16:49:10 -08002209</pre>
Robert Griesemer7231ceb2008-09-08 15:01:04 -07002210
Rob Pikedf3183f2009-02-26 16:37:23 -08002211<p>
2212Function literals are <i>closures</i>: they may refer to variables
Robert Griesemerd8a764c2009-02-06 17:01:10 -08002213defined in a surrounding function. Those variables are then shared between
2214the surrounding function and the function literal, and they survive as long
Rob Pikedf3183f2009-02-26 16:37:23 -08002215as they are accessible.
2216</p>
Robert Griesemer7231ceb2008-09-08 15:01:04 -07002217
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002218
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002219<h3 id="Primary_expressions">Primary expressions</h3>
Russ Cox5958dd62009-03-04 17:19:21 -08002220
Robert Griesemer164a7bc2009-09-30 12:00:25 -07002221<p>
2222Primary expressions are the operands for unary and binary expressions.
2223</p>
2224
Robert Griesemerf7ac31362009-07-10 16:06:40 -07002225<pre class="ebnf">
Robert Griesemerc2d55862009-02-19 16:49:10 -08002226PrimaryExpr =
2227 Operand |
Robert Griesemer5eb36242009-09-16 11:05:14 -07002228 Conversion |
Robert Griesemer164a7bc2009-09-30 12:00:25 -07002229 BuiltinCall |
Robert Griesemerc2d55862009-02-19 16:49:10 -08002230 PrimaryExpr Selector |
2231 PrimaryExpr Index |
2232 PrimaryExpr Slice |
Russ Cox5958dd62009-03-04 17:19:21 -08002233 PrimaryExpr TypeAssertion |
Robert Griesemerc2d55862009-02-19 16:49:10 -08002234 PrimaryExpr Call .
Robert Griesemer57b34612008-10-10 12:45:44 -07002235
Russ Cox5958dd62009-03-04 17:19:21 -08002236Selector = "." identifier .
2237Index = "[" Expression "]" .
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08002238Slice = "[" Expression ":" [ Expression ] "]" .
Russ Cox5958dd62009-03-04 17:19:21 -08002239TypeAssertion = "." "(" Type ")" .
Robert Griesemer130ac742009-12-10 16:43:01 -08002240Call = "(" [ ExpressionList [ "," ] ] ")" .
Robert Griesemerc2d55862009-02-19 16:49:10 -08002241</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002242
2243
Robert Griesemerc2d55862009-02-19 16:49:10 -08002244<pre>
2245x
22462
2247(s + ".txt")
2248f(3.1415, true)
Rob Pike426335f2009-03-02 17:52:52 -08002249Point{1, 2}
Robert Griesemerc2d55862009-02-19 16:49:10 -08002250m["foo"]
2251s[i : j + 1]
2252obj.color
2253Math.sin
2254f.p[i].x()
2255</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002256
2257
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002258<h3 id="Selectors">Selectors</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002259
Rob Pikedf3183f2009-02-26 16:37:23 -08002260<p>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002261A primary expression of the form
Rob Pikedf3183f2009-02-26 16:37:23 -08002262</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002263
Robert Griesemerc2d55862009-02-19 16:49:10 -08002264<pre>
2265x.f
2266</pre>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002267
Robert Griesemerc2d55862009-02-19 16:49:10 -08002268<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002269denotes the field or method <code>f</code> of the value denoted by <code>x</code>
2270(or of <code>*x</code> if
2271<code>x</code> is of pointer type). The identifier <code>f</code>
2272is called the (field or method)
Robert Griesemer4e56b332009-09-10 10:14:00 -07002273<i>selector</i>; it must not be the <a href="#Blank_identifier">blank identifier</a>.
Rob Pikedf3183f2009-02-26 16:37:23 -08002274The type of the expression is the type of <code>f</code>.
2275</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002276<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002277A selector <code>f</code> may denote a field or method <code>f</code> of
2278a type <code>T</code>, or it may refer
2279to a field or method <code>f</code> of a nested anonymous field of
2280<code>T</code>.
2281The number of anonymous fields traversed
2282to reach <code>f</code> is called its <i>depth</i> in <code>T</code>.
2283The depth of a field or method <code>f</code>
2284declared in <code>T</code> is zero.
2285The depth of a field or method <code>f</code> declared in
2286an anonymous field <code>A</code> in <code>T</code> is the
2287depth of <code>f</code> in <code>A</code> plus one.
2288</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002289<p>
Robert Griesemer071c91b2008-10-23 12:04:45 -07002290The following rules apply to selectors:
Rob Pikedf3183f2009-02-26 16:37:23 -08002291</p>
2292<ol>
2293<li>
2294For a value <code>x</code> of type <code>T</code> or <code>*T</code>
2295where <code>T</code> is not an interface type,
2296<code>x.f</code> denotes the field or method at the shallowest depth
2297in <code>T</code> where there
2298is such an <code>f</code>.
2299If there is not exactly one <code>f</code> with shallowest depth, the selector
Robert Griesemer071c91b2008-10-23 12:04:45 -07002300expression is illegal.
Rob Pikedf3183f2009-02-26 16:37:23 -08002301</li>
2302<li>
2303For a variable <code>x</code> of type <code>I</code> or <code>*I</code>
2304where <code>I</code> is an interface type,
2305<code>x.f</code> denotes the actual method with name <code>f</code> of the value assigned
2306to <code>x</code> if there is such a method.
2307If no value or <code>nil</code> was assigned to <code>x</code>, <code>x.f</code> is illegal.
2308</li>
2309<li>
2310In all other cases, <code>x.f</code> is illegal.
2311</ol>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002312<p>
Rob Pike678625d2009-09-15 09:54:22 -07002313Selectors automatically dereference pointers.
Rob Pikedf3183f2009-02-26 16:37:23 -08002314If <code>x</code> is of pointer type, <code>x.y</code>
2315is shorthand for <code>(*x).y</code>; if <code>y</code>
2316is also of pointer type, <code>x.y.z</code> is shorthand
2317for <code>(*(*x).y).z</code>, and so on.
2318If <code>*x</code> is of pointer type, dereferencing
2319must be explicit;
2320only one level of automatic dereferencing is provided.
2321For an <code>x</code> of type <code>T</code> containing an
2322anonymous field declared as <code>*A</code>,
2323<code>x.f</code> is a shortcut for <code>(*x.A).f</code>.
2324</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002325<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002326For example, given the declarations:
2327</p>
Robert Griesemer071c91b2008-10-23 12:04:45 -07002328
Robert Griesemerc2d55862009-02-19 16:49:10 -08002329<pre>
2330type T0 struct {
Robert Griesemer130ac742009-12-10 16:43:01 -08002331 x int
Robert Griesemerc2d55862009-02-19 16:49:10 -08002332}
Robert Griesemer071c91b2008-10-23 12:04:45 -07002333
Robert Griesemerc2d55862009-02-19 16:49:10 -08002334func (recv *T0) M0()
Robert Griesemer071c91b2008-10-23 12:04:45 -07002335
Robert Griesemerc2d55862009-02-19 16:49:10 -08002336type T1 struct {
Robert Griesemer130ac742009-12-10 16:43:01 -08002337 y int
Robert Griesemerc2d55862009-02-19 16:49:10 -08002338}
Robert Griesemer071c91b2008-10-23 12:04:45 -07002339
Robert Griesemerc2d55862009-02-19 16:49:10 -08002340func (recv T1) M1()
Robert Griesemer071c91b2008-10-23 12:04:45 -07002341
Robert Griesemerc2d55862009-02-19 16:49:10 -08002342type T2 struct {
Robert Griesemer130ac742009-12-10 16:43:01 -08002343 z int
2344 T1
2345 *T0
Robert Griesemerc2d55862009-02-19 16:49:10 -08002346}
Robert Griesemer071c91b2008-10-23 12:04:45 -07002347
Robert Griesemerc2d55862009-02-19 16:49:10 -08002348func (recv *T2) M2()
Robert Griesemer071c91b2008-10-23 12:04:45 -07002349
Robert Griesemer130ac742009-12-10 16:43:01 -08002350var p *T2 // with p != nil and p.T1 != nil
Robert Griesemerc2d55862009-02-19 16:49:10 -08002351</pre>
Robert Griesemer071c91b2008-10-23 12:04:45 -07002352
Rob Pikedf3183f2009-02-26 16:37:23 -08002353<p>
2354one may write:
2355</p>
Robert Griesemer071c91b2008-10-23 12:04:45 -07002356
Robert Griesemerc2d55862009-02-19 16:49:10 -08002357<pre>
2358p.z // (*p).z
2359p.y // ((*p).T1).y
2360p.x // (*(*p).T0).x
Robert Griesemer071c91b2008-10-23 12:04:45 -07002361
Robert Griesemerc2d55862009-02-19 16:49:10 -08002362p.M2 // (*p).M2
2363p.M1 // ((*p).T1).M1
2364p.M0 // ((*p).T0).M0
2365</pre>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002366
2367
Rob Pike0b4de7a2009-11-09 16:09:57 -08002368<!---
Robert Griesemer90cc4a52009-10-22 09:41:38 -07002369<span class="alert">
Robert Griesemer071c91b2008-10-23 12:04:45 -07002370TODO: Specify what happens to receivers.
Robert Griesemer90cc4a52009-10-22 09:41:38 -07002371</span>
Rob Pike0b4de7a2009-11-09 16:09:57 -08002372--->
Robert Griesemer7abfcd92008-10-07 17:14:30 -07002373
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002374
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002375<h3 id="Indexes">Indexes</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002376
Rob Pikedf3183f2009-02-26 16:37:23 -08002377<p>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002378A primary expression of the form
Rob Pikedf3183f2009-02-26 16:37:23 -08002379</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002380
Robert Griesemerc2d55862009-02-19 16:49:10 -08002381<pre>
2382a[x]
2383</pre>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002384
Rob Pike4501d342009-02-19 17:31:36 -08002385<p>
Rob Pike678625d2009-09-15 09:54:22 -07002386denotes the element of the array, slice, string or map <code>a</code> indexed by <code>x</code>.
Rob Pikedf3183f2009-02-26 16:37:23 -08002387The value <code>x</code> is called the
Rob Pike678625d2009-09-15 09:54:22 -07002388<i>index</i> or <i>map key</i>, respectively. The following
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002389rules apply:
Rob Pike4501d342009-02-19 17:31:36 -08002390</p>
Russ Coxeaa92e02009-06-25 14:43:55 -07002391
Robert Griesemerc2d55862009-02-19 16:49:10 -08002392<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002393For <code>a</code> of type <code>A</code> or <code>*A</code>
Robert Griesemer506c0082009-09-08 15:41:14 -07002394where <code>A</code> is an <a href="#Array_types">array type</a>,
2395or 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 -08002396</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002397<ul>
Robert Griesemer29f1ca52010-03-23 14:01:51 -07002398 <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 -08002399 <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 -07002400 <code>a[x]</code> is the element type of <code>A</code></li>
Rob Pike5bb29fb2010-03-25 17:59:59 -07002401 <li>if the index <code>x</code> is out of range,
2402 a <a href="#Run_time_panics">run-time panic</a> occurs</li>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002403</ul>
Russ Coxeaa92e02009-06-25 14:43:55 -07002404
Robert Griesemerc2d55862009-02-19 16:49:10 -08002405<p>
Russ Coxeaa92e02009-06-25 14:43:55 -07002406For <code>a</code> of type <code>T</code>
Robert Griesemer19b1d352009-09-24 19:36:48 -07002407where <code>T</code> is a <a href="#String_types">string type</a>:
Russ Coxeaa92e02009-06-25 14:43:55 -07002408</p>
2409<ul>
Robert Griesemer29f1ca52010-03-23 14:01:51 -07002410 <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 -07002411 <li><code>a[x]</code> is the byte at index <code>x</code> and the type of
Robert Griesemer29f1ca52010-03-23 14:01:51 -07002412 <code>a[x]</code> is <code>byte</code></li>
Russ Coxeaa92e02009-06-25 14:43:55 -07002413 <li><code>a[x]</code> may not be assigned to
Rob Pike5bb29fb2010-03-25 17:59:59 -07002414 <li>if the index <code>x</code> is out of range,
2415 a <a href="#Run_time_panics">run-time panic</a> occurs</li>
Russ Coxeaa92e02009-06-25 14:43:55 -07002416</ul>
2417
2418<p>
2419For <code>a</code> of type <code>M</code>
Robert Griesemer506c0082009-09-08 15:41:14 -07002420where <code>M</code> is a <a href="#Map_types">map type</a>:
Rob Pike4501d342009-02-19 17:31:36 -08002421</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002422<ul>
Robert Griesemer29f1ca52010-03-23 14:01:51 -07002423 <li><code>x</code>'s type must be
2424 <a href="#Assignment_compatibility">assignment compatible</a>
2425 with the key type of <code>M</code></li>
2426 <li>if the map contains an entry with key <code>x</code>,
2427 <code>a[x]</code> is the map value with key <code>x</code>
2428 and the type of <code>a[x]</code> is the value type of <code>M</code></li>
2429 <li>if the map does not contain such an entry,
2430 <code>a[x]</code> is the <a href="#The_zero_value">zero value</a>
2431 for the value type of <code>M</code></li>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002432</ul>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002433
Robert Griesemerc2d55862009-02-19 16:49:10 -08002434<p>
Robert Griesemer29f1ca52010-03-23 14:01:51 -07002435Otherwise <code>a[x]</code> is illegal.
Rob Pikedf3183f2009-02-26 16:37:23 -08002436</p>
Robert Griesemer7abfcd92008-10-07 17:14:30 -07002437
Rob Pikedf3183f2009-02-26 16:37:23 -08002438<p>
Robert Griesemer29f1ca52010-03-23 14:01:51 -07002439An index expression on a map <code>a</code> of type <code>map[K]V</code>
2440may be used in an assignment or initialization of the special form
Rob Pikedf3183f2009-02-26 16:37:23 -08002441</p>
2442
2443<pre>
Robert Griesemer29f1ca52010-03-23 14:01:51 -07002444v, ok = a[x]
2445v, ok := a[x]
2446var v, ok = a[x]
Rob Pikedf3183f2009-02-26 16:37:23 -08002447</pre>
2448
2449<p>
Robert Griesemer29f1ca52010-03-23 14:01:51 -07002450where the result of the index expression is a pair of values with types
2451<code>(V, bool)</code>. In this form, the value of <code>ok</code> is
2452<code>true</code> if the key <code>x</code> is present in the map, and
2453<code>false</code> otherwise. The value of <code>v</code> is the value
2454<code>a[x]</code> as in the single-result form.
Rob Pikedf3183f2009-02-26 16:37:23 -08002455</p>
2456
2457<p>
2458Similarly, if an assignment to a map has the special form
2459</p>
2460
2461<pre>
Robert Griesemer29f1ca52010-03-23 14:01:51 -07002462a[x] = v, ok
Rob Pikedf3183f2009-02-26 16:37:23 -08002463</pre>
2464
2465<p>
2466and boolean <code>ok</code> has the value <code>false</code>,
2467the entry for key <code>x</code> is deleted from the map; if
2468<code>ok</code> is <code>true</code>, the construct acts like
2469a regular assignment to an element of the map.
2470</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002471
Robert Griesemer29f1ca52010-03-23 14:01:51 -07002472
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002473<h3 id="Slices">Slices</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002474
Rob Pikedf3183f2009-02-26 16:37:23 -08002475<p>
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08002476For a string, array, or slice <code>a</code>, the primary expression
Rob Pikedf3183f2009-02-26 16:37:23 -08002477</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002478
Robert Griesemerc2d55862009-02-19 16:49:10 -08002479<pre>
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08002480a[lo : hi]
Robert Griesemerc2d55862009-02-19 16:49:10 -08002481</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002482
Rob Pikedf3183f2009-02-26 16:37:23 -08002483<p>
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08002484constructs a substring or slice. The index expressions <code>lo</code> and
2485<code>hi</code> select which elements appear in the result. The result has
2486indexes starting at 0 and length equal to
2487<code>hi</code>&nbsp;-&nbsp;<code>lo</code>.
2488After slicing the array <code>a</code>
2489</p>
2490
2491<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08002492a := [5]int{1, 2, 3, 4, 5}
2493s := a[1:4]
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08002494</pre>
2495
2496<p>
2497the slice <code>s</code> has type <code>[]int</code>, length 3, capacity 4, and elements
Rob Pikedf3183f2009-02-26 16:37:23 -08002498</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002499
Robert Griesemerc2d55862009-02-19 16:49:10 -08002500<pre>
2501s[0] == 2
2502s[1] == 3
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08002503s[2] == 4
Robert Griesemerc2d55862009-02-19 16:49:10 -08002504</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002505
Rob Pikedf3183f2009-02-26 16:37:23 -08002506<p>
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08002507For convenience, the <code>hi</code> expression may be omitted; the notation
2508<code>a[lo :]</code> is shorthand for <code>a[lo : len(a)]</code>.
Russ Cox5958dd62009-03-04 17:19:21 -08002509For arrays or strings, the indexes
Rob Pike811dd252009-03-04 20:39:39 -08002510<code>lo</code> and <code>hi</code> must satisfy
25110 &lt;= <code>lo</code> &lt;= <code>hi</code> &lt;= length;
Russ Cox5958dd62009-03-04 17:19:21 -08002512for slices, the upper bound is the capacity rather than the length.
Robert Griesemer19b1d352009-09-24 19:36:48 -07002513</p>
2514
Robert Griesemerc2d55862009-02-19 16:49:10 -08002515<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07002516If the sliced operand is a string or slice, the result of the slice operation
2517is a string or slice of the same type.
2518If the sliced operand is an array, the result of the slice operation is a slice
2519with the same element type as the array.
Rob Pikedf3183f2009-02-26 16:37:23 -08002520</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002521
2522
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002523<h3 id="Type_assertions">Type assertions</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002524
Rob Pikedf3183f2009-02-26 16:37:23 -08002525<p>
Robert Griesemere9192752009-12-01 16:15:53 -08002526For an expression <code>x</code> of <a href="#Interface_types">interface type</a>
2527and a type <code>T</code>, the primary expression
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>
2531x.(T)
2532</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -08002533
Robert Griesemerc2d55862009-02-19 16:49:10 -08002534<p>
Robert Griesemere9192752009-12-01 16:15:53 -08002535asserts that <code>x</code> is not <code>nil</code>
Russ Coxb89a54e2009-05-20 18:16:04 -07002536and that the value stored in <code>x</code> is of type <code>T</code>.
Russ Cox5958dd62009-03-04 17:19:21 -08002537The notation <code>x.(T)</code> is called a <i>type assertion</i>.
Rob Pikedf3183f2009-02-26 16:37:23 -08002538</p>
2539<p>
Russ Cox5958dd62009-03-04 17:19:21 -08002540More precisely, if <code>T</code> is not an interface type, <code>x.(T)</code> asserts
Rob Pikedf3183f2009-02-26 16:37:23 -08002541that the dynamic type of <code>x</code> is identical to the type <code>T</code>
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002542<a href="#Type_identity_and_compatibility">Type identity and compatibility</a>).
Russ Cox5958dd62009-03-04 17:19:21 -08002543If <code>T</code> is an interface type, <code>x.(T)</code> asserts that the dynamic type
Rob Pike632a9852010-01-13 12:06:33 +11002544of <code>x</code> implements the interface <code>T</code><a href="#Interface_types">Interface types</a>).
Rob Pikedf3183f2009-02-26 16:37:23 -08002545</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002546<p>
Russ Cox5958dd62009-03-04 17:19:21 -08002547If the type assertion holds, the value of the expression is the value
Rob Pike5bb29fb2010-03-25 17:59:59 -07002548stored in <code>x</code> and its type is <code>T</code>. If the type assertion is false,
2549a <a href="#Run_time_panics">run-time panic</a> occurs.
2550In other words, even though the dynamic type of <code>x</code>
Russ Cox5958dd62009-03-04 17:19:21 -08002551is known only at run-time, the type of <code>x.(T)</code> is
Rob Pikedf3183f2009-02-26 16:37:23 -08002552known to be <code>T</code> in a correct program.
2553</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002554<p>
Rob Piked5537072009-08-22 00:04:04 -07002555If a type assertion is used in an assignment or initialization of the form
Rob Pikedf3183f2009-02-26 16:37:23 -08002556</p>
Robert Griesemer434c6052008-11-07 13:34:37 -08002557
Robert Griesemerc2d55862009-02-19 16:49:10 -08002558<pre>
2559v, ok = x.(T)
2560v, ok := x.(T)
Rob Piked5537072009-08-22 00:04:04 -07002561var v, ok = x.(T)
Robert Griesemerc2d55862009-02-19 16:49:10 -08002562</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -08002563
Robert Griesemerc2d55862009-02-19 16:49:10 -08002564<p>
Russ Cox5958dd62009-03-04 17:19:21 -08002565the result of the assertion is a pair of values with types <code>(T, bool)</code>.
2566If the assertion holds, the expression returns the pair <code>(x.(T), true)</code>;
Rob Pikedf3183f2009-02-26 16:37:23 -08002567otherwise, the expression returns <code>(Z, false)</code> where <code>Z</code>
Robert Griesemer506c0082009-09-08 15:41:14 -07002568is the <a href="#The_zero_value">zero value</a> for type <code>T</code>.
Rob Pike5bb29fb2010-03-25 17:59:59 -07002569No run-time panic occurs in this case.
Russ Cox5958dd62009-03-04 17:19:21 -08002570The type assertion in this construct thus acts like a function call
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002571returning a value and a boolean indicating success. (§<a href="#Assignments">Assignments</a>)
Rob Pikedf3183f2009-02-26 16:37:23 -08002572</p>
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -07002573
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002574
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002575<h3 id="Calls">Calls</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002576
Robert Griesemerc2d55862009-02-19 16:49:10 -08002577<p>
Rob Pike96750f12009-02-27 16:47:48 -08002578Given an expression <code>f</code> of function type
2579<code>F</code>,
Rob Pikedf3183f2009-02-26 16:37:23 -08002580</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002581
Robert Griesemerc2d55862009-02-19 16:49:10 -08002582<pre>
Rob Pike96750f12009-02-27 16:47:48 -08002583f(a1, a2, ... an)
Robert Griesemerc2d55862009-02-19 16:49:10 -08002584</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002585
Robert Griesemerc2d55862009-02-19 16:49:10 -08002586<p>
Rob Pike96750f12009-02-27 16:47:48 -08002587calls <code>f</code> with arguments <code>a1, a2, ... an</code>.
Rob Pike4fe41922009-11-07 22:00:59 -08002588Except for one special case, arguments must be single-valued expressions
Robert Griesemer326ef132009-09-28 19:21:15 -07002589<a href="#Assignment_compatibility">assignment compatible</a> with the parameter types of
Rob Pikedf3183f2009-02-26 16:37:23 -08002590<code>F</code> and are evaluated before the function is called.
2591The type of the expression is the result type
2592of <code>F</code>.
2593A method invocation is similar but the method itself
2594is specified as a selector upon a value of the receiver type for
2595the method.
2596</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002597
Robert Griesemerc2d55862009-02-19 16:49:10 -08002598<pre>
Robert Griesemere9192752009-12-01 16:15:53 -08002599math.Atan2(x, y) // function call
Robert Griesemer130ac742009-12-10 16:43:01 -08002600var pt *Point
Rob Pikedf3183f2009-02-26 16:37:23 -08002601pt.Scale(3.5) // method call with receiver pt
Robert Griesemerc2d55862009-02-19 16:49:10 -08002602</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002603
Rob Pikedf3183f2009-02-26 16:37:23 -08002604<p>
Rob Pike4fe41922009-11-07 22:00:59 -08002605As a special case, if the return parameters of a function or method
2606<code>g</code> are equal in number and individually assignment
2607compatible with the parameters of another function or method
2608<code>f</code>, then the call <code>f(g(<i>parameters_of_g</i>))</code>
2609will invoke <code>f</code> after binding the return values of
2610<code>g</code> to the parameters of <code>f</code> in order. The call
2611of <code>f</code> must contain no parameters other than the call of <code>g</code>.
2612If <code>f</code> has a final <code>...</code> parameter, it is
2613assigned the return values of <code>g</code> that remain after
2614assignment of regular parameters.
2615</p>
2616
2617<pre>
2618func Split(s string, pos int) (string, string) {
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08002619 return s[0:pos], s[pos:]
Rob Pike4fe41922009-11-07 22:00:59 -08002620}
2621
2622func Join(s, t string) string {
2623 return s + t
2624}
2625
2626if Join(Split(value, len(value)/2)) != value {
Rob Pike3909b6b2010-01-25 07:48:31 +11002627 log.Crash("test fails")
Rob Pike4fe41922009-11-07 22:00:59 -08002628}
2629</pre>
2630
2631<p>
Robert Griesemer56809d02009-05-20 11:02:48 -07002632A method call <code>x.m()</code> is valid if the method set of
Rob Pike678625d2009-09-15 09:54:22 -07002633(the type of) <code>x</code> contains <code>m</code> and the
2634argument list is compatible with the parameter list of <code>m</code>.
2635If <code>x</code> is <a href="#Address_operators">addressable</a> and <code>&amp;x</code>'s method
Robert Griesemer56809d02009-05-20 11:02:48 -07002636set contains <code>m</code>, <code>x.m()</code> is shorthand
2637for <code>(&amp;x).m()</code>:
Rob Pikedf3183f2009-02-26 16:37:23 -08002638</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002639
Robert Griesemerc2d55862009-02-19 16:49:10 -08002640<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08002641var p Point
Rob Pikedf3183f2009-02-26 16:37:23 -08002642p.Scale(3.5)
Robert Griesemerc2d55862009-02-19 16:49:10 -08002643</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002644
Robert Griesemerc2d55862009-02-19 16:49:10 -08002645<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002646There is no distinct method type and there are no method literals.
Rob Pikedf3183f2009-02-26 16:37:23 -08002647</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002648
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002649<h3 id="Passing_arguments_to_..._parameters">Passing arguments to <code>...</code> parameters</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002650
Robert Griesemerc2d55862009-02-19 16:49:10 -08002651<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002652When a function <code>f</code> has a <code>...</code> parameter,
2653it is always the last formal parameter. Within calls to <code>f</code>,
2654the arguments before the <code>...</code> are treated normally.
2655After those, an arbitrary number (including zero) of trailing
2656arguments may appear in the call and are bound to the <code>...</code>
2657parameter.
2658</p>
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002659
Rob Pikedf3183f2009-02-26 16:37:23 -08002660<p>
Rob Pikeb81065d2010-01-27 13:14:40 -08002661Within <code>f</code>, a <code>...</code> parameter with no
2662specified type has static type <code>interface{}</code> (the empty
2663interface). For each call, its dynamic type is a structure whose
2664sequential fields are the trailing arguments of the call. That is,
2665the actual arguments provided for a <code>...</code> parameter are
2666wrapped into a struct that is passed to the function instead of the
2667actual arguments. Using the <a href="#Package_unsafe">reflection</a>
2668interface, <code>f</code> may unpack the elements of the dynamic
2669type to recover the actual arguments.
Rob Pikedf3183f2009-02-26 16:37:23 -08002670</p>
2671
2672<p>
2673Given the function and call
2674</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002675<pre>
Rob Pikefb24d792009-05-08 11:21:20 -07002676func Fprintf(f io.Writer, format string, args ...)
Robert Griesemer130ac742009-12-10 16:43:01 -08002677Fprintf(os.Stdout, "%s %d", "hello", 23)
Robert Griesemerc2d55862009-02-19 16:49:10 -08002678</pre>
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002679
Rob Pikedf3183f2009-02-26 16:37:23 -08002680<p>
2681Within <code>Fprintf</code>, the dynamic type of <code>args</code> for this
2682call will be, schematically,
2683<code> struct { string; int }</code>.
2684</p>
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002685
Rob Pikeb81065d2010-01-27 13:14:40 -08002686<p>
2687If the final parameter of <code>f</code> has type <code>... T</code>,
2688within the function it is equivalent to a parameter of type
2689<code>[]T</code>. At each call of <code>f</code>, the actual
2690arguments provided for the <code>... T</code> parameter are placed
2691into a new slice of type <code>[]T</code> whose successive elements are
2692the actual arguments. The length of the slice is therefore the
2693number of arguments bound to the <code>... T</code> parameter and
2694may differ for each call site.
2695</p>
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002696
Rob Pikedf3183f2009-02-26 16:37:23 -08002697<p>
Rob Pikeb81065d2010-01-27 13:14:40 -08002698Given the function and call
2699</p>
2700<pre>
2701func Greeting(prefix string, who ... string)
2702Greeting("hello:", "Joe", "Anna", "Eileen")
2703</pre>
2704
2705<p>
2706Within <code>Greeting</code>, <code>who</code> will have value
2707<code>[]string{"Joe", "Anna", "Eileen")</code>
2708</p>
2709
2710
2711<p>
2712As a special case, if a function passes its own <code>...</code> parameter,
2713with or without specified type, as the argument
2714for a <code>...</code> in a call to another function with a <code>...</code> parameter
2715of identical type,
Rob Pikedf3183f2009-02-26 16:37:23 -08002716the parameter is not wrapped again but passed directly. In short, a formal <code>...</code>
Rob Pikeb81065d2010-01-27 13:14:40 -08002717parameter is passed unchanged as an actual <code>...</code> parameter provided the
2718types match.
2719</p>
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002720
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002721<h3 id="Operators">Operators</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002722
Rob Pikedf3183f2009-02-26 16:37:23 -08002723<p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002724Operators combine operands into expressions.
Rob Pikedf3183f2009-02-26 16:37:23 -08002725</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002726
Robert Griesemerf7ac31362009-07-10 16:06:40 -07002727<pre class="ebnf">
Russ Cox5958dd62009-03-04 17:19:21 -08002728Expression = UnaryExpr | Expression binary_op UnaryExpr .
Rob Pikedf3183f2009-02-26 16:37:23 -08002729UnaryExpr = PrimaryExpr | unary_op UnaryExpr .
Robert Griesemer57b34612008-10-10 12:45:44 -07002730
Rob Pikedf3183f2009-02-26 16:37:23 -08002731binary_op = log_op | com_op | rel_op | add_op | mul_op .
2732log_op = "||" | "&amp;&amp;" .
2733com_op = "&lt;-" .
2734rel_op = "==" | "!=" | "&lt;" | "&lt;=" | ">" | ">=" .
2735add_op = "+" | "-" | "|" | "^" .
Robert Griesemer4ed666e2009-08-27 16:45:42 -07002736mul_op = "*" | "/" | "%" | "&lt;&lt;" | "&gt;&gt;" | "&amp;" | "&amp;^" .
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002737
Rob Pikedf3183f2009-02-26 16:37:23 -08002738unary_op = "+" | "-" | "!" | "^" | "*" | "&amp;" | "&lt;-" .
Robert Griesemerc2d55862009-02-19 16:49:10 -08002739</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002740
Robert Griesemerc2d55862009-02-19 16:49:10 -08002741<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07002742Comparisons are discussed <a href="#Comparison_operators">elsewhere</a>.
2743For other binary operators, the operand types must be identical
Rob Pike83cbca52009-08-21 14:18:08 -07002744<a href="#Properties_of_types_and_values">Properties of types and values</a>)
Robert Griesemer19b1d352009-09-24 19:36:48 -07002745unless the operation involves channels, shifts, or untyped <a href="#Constants">constants</a>.
2746For operations involving constants only, see the section on
2747<a href="#Constant_expressions">constant expressions</a>.
Rob Pike4501d342009-02-19 17:31:36 -08002748</p>
Robert Griesemerc8e18762008-09-12 12:26:22 -07002749
Rob Pike83cbca52009-08-21 14:18:08 -07002750<p>
Robert Griesemerb691e082009-10-28 18:17:24 -07002751In a channel send, the first operand is always a channel and the second
2752must be a value <a href="#Assignment_compatibility">assignment compatible</a>
2753with the channel's element type.
Rob Pike83cbca52009-08-21 14:18:08 -07002754</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002755
Rob Pike83cbca52009-08-21 14:18:08 -07002756<p>
2757Except for shift operations,
Robert Griesemer19b1d352009-09-24 19:36:48 -07002758if one operand is an untyped <a href="#Constants">constant</a>
2759and the other operand is not, the constant is <a href="#Conversions">converted</a>
2760to the type of the other operand.
Rob Pike83cbca52009-08-21 14:18:08 -07002761</p>
Robert Griesemer7539c852009-07-31 18:05:07 -07002762
Rob Pike83cbca52009-08-21 14:18:08 -07002763<p>
2764The right operand in a shift operation must have unsigned integer type
Robert Griesemer19b1d352009-09-24 19:36:48 -07002765or be an untyped constant that can be converted to unsigned integer type.
Rob Pike83cbca52009-08-21 14:18:08 -07002766</p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002767
Rob Pike83cbca52009-08-21 14:18:08 -07002768<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07002769If the left operand of a non-constant shift operation is an untyped constant,
2770the type of constant is what it would be if the shift operation were replaced by
2771the left operand alone.
Rob Pike83cbca52009-08-21 14:18:08 -07002772</p>
Robert Griesemera6b546f2008-10-20 11:46:40 -07002773
Rob Pike83cbca52009-08-21 14:18:08 -07002774<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08002775var s uint = 33
2776var i = 1&lt;&lt;s // 1 has type int
2777var j = int32(1&lt;&lt;s) // 1 has type int32; j == 0
2778var u = uint64(1&lt;&lt;s) // 1 has type uint64; u == 1&lt;&lt;33
2779var f = float(1&lt;&lt;s) // illegal: 1 has type float, cannot shift
2780var g = float(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 -07002781</pre>
Robert Griesemer18b05c12009-01-26 09:34:19 -08002782
Robert Griesemerd36d1912009-09-18 11:58:35 -07002783<h3 id="Operator_precedence">Operator precedence</h3>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002784<p>
Russ Coxec9b0422009-07-09 16:44:13 -07002785Unary operators have the highest precedence.
2786As the <code>++</code> and <code>--</code> operators form
Rob Pikedf3183f2009-02-26 16:37:23 -08002787statements, not expressions, they fall
Russ Coxec9b0422009-07-09 16:44:13 -07002788outside the operator hierarchy.
Rob Pikedf3183f2009-02-26 16:37:23 -08002789As a consequence, statement <code>*p++</code> is the same as <code>(*p)++</code>.
2790<p>
2791There are six precedence levels for binary operators.
2792Multiplication operators bind strongest, followed by addition
Robert Griesemerd36d1912009-09-18 11:58:35 -07002793operators, comparison operators, <code>&lt;-</code> (channel send),
Rob Pikedf3183f2009-02-26 16:37:23 -08002794<code>&amp;&amp;</code> (logical and), and finally <code>||</code> (logical or):
2795</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002796
Rob Pikeff70f092009-02-20 13:36:14 -08002797<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08002798Precedence Operator
Robert Griesemer4ed666e2009-08-27 16:45:42 -07002799 6 * / % &lt;&lt; &gt;&gt; &amp; &amp;^
Robert Griesemerc2d55862009-02-19 16:49:10 -08002800 5 + - | ^
2801 4 == != &lt; &lt;= > >=
2802 3 &lt;-
2803 2 &amp;&amp;
2804 1 ||
2805</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002806
Robert Griesemerc2d55862009-02-19 16:49:10 -08002807<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002808Binary operators of the same precedence associate from left to right.
Robert Griesemerd36d1912009-09-18 11:58:35 -07002809For instance, <code>x / y * z</code> is the same as <code>(x / y) * z</code>.
Rob Pikedf3183f2009-02-26 16:37:23 -08002810</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002811
Robert Griesemerc2d55862009-02-19 16:49:10 -08002812<pre>
2813+x
281423 + 3*x[i]
2815x &lt;= f()
Robert Griesemer4ed666e2009-08-27 16:45:42 -07002816^a &gt;&gt; b
Robert Griesemerc2d55862009-02-19 16:49:10 -08002817f() || g()
Robert Griesemerd36d1912009-09-18 11:58:35 -07002818x == y+1 &amp;&amp; &lt;-chan_ptr > 0
Robert Griesemerc2d55862009-02-19 16:49:10 -08002819</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002820
2821
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002822<h3 id="Arithmetic_operators">Arithmetic operators</h3>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002823<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07002824Arithmetic operators apply to numeric values and yield a result of the same
Rob Pikedf3183f2009-02-26 16:37:23 -08002825type as the first operand. The four standard arithmetic operators (<code>+</code>,
Rob Pike72970872010-03-04 12:35:16 -08002826<code>-</code>, <code>*</code>, <code>/</code>) apply to integer,
2827floating-point, and complex types; <code>+</code> also applies
Robert Griesemer19b1d352009-09-24 19:36:48 -07002828to strings. All other arithmetic operators apply to integers only.
Rob Pikedf3183f2009-02-26 16:37:23 -08002829</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002830
Rob Pikeff70f092009-02-20 13:36:14 -08002831<pre class="grammar">
Rob Pike72970872010-03-04 12:35:16 -08002832+ sum integers, floats, complex values, strings
2833- difference integers, floats, complex values
2834* product integers, floats, complex values
2835/ quotient integers, floats, complex values
Rob Pike307ec212009-03-12 15:53:56 -07002836% remainder integers
Robert Griesemerc2d55862009-02-19 16:49:10 -08002837
Rob Pike307ec212009-03-12 15:53:56 -07002838&amp; bitwise and integers
2839| bitwise or integers
2840^ bitwise xor integers
2841&amp;^ bit clear (and not) integers
Robert Griesemerc2d55862009-02-19 16:49:10 -08002842
Robert Griesemer4ed666e2009-08-27 16:45:42 -07002843&lt;&lt; left shift integer &lt;&lt; unsigned integer
2844&gt;&gt; right shift integer &gt;&gt; unsigned integer
Robert Griesemerc2d55862009-02-19 16:49:10 -08002845</pre>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002846
Rob Pikedf3183f2009-02-26 16:37:23 -08002847<p>
2848Strings can be concatenated using the <code>+</code> operator
2849or the <code>+=</code> assignment operator:
2850</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002851
Robert Griesemerc2d55862009-02-19 16:49:10 -08002852<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08002853s := "hi" + string(c)
2854s += " and good bye"
Robert Griesemerc2d55862009-02-19 16:49:10 -08002855</pre>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002856
Robert Griesemerc2d55862009-02-19 16:49:10 -08002857<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002858String addition creates a new string by concatenating the operands.
2859</p>
2860<p>
2861For integer values, <code>/</code> and <code>%</code> satisfy the following relationship:
2862</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002863
Robert Griesemerc2d55862009-02-19 16:49:10 -08002864<pre>
2865(a / b) * b + a % b == a
2866</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002867
Rob Pikedf3183f2009-02-26 16:37:23 -08002868<p>
2869with <code>(a / b)</code> truncated towards zero.
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002870Examples:
Rob Pikedf3183f2009-02-26 16:37:23 -08002871</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002872
Robert Griesemerc2d55862009-02-19 16:49:10 -08002873<pre>
2874 x y x / y x % y
2875 5 3 1 2
2876-5 3 -1 -2
2877 5 -3 -1 2
2878-5 -3 1 -2
2879</pre>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002880
Rob Pikedf3183f2009-02-26 16:37:23 -08002881<p>
2882If the dividend is positive and the divisor is a constant power of 2,
Rob Pikef3a33bc2009-09-14 17:39:17 -07002883the division may be replaced by a right shift, and computing the remainder may
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002884be replaced by a bitwise "and" operation:
Rob Pikedf3183f2009-02-26 16:37:23 -08002885</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002886
Robert Griesemerc2d55862009-02-19 16:49:10 -08002887<pre>
Robert Griesemer4ed666e2009-08-27 16:45:42 -07002888 x x / 4 x % 4 x &gt;&gt; 2 x &amp; 3
Robert Griesemerc2d55862009-02-19 16:49:10 -08002889 11 2 3 2 3
2890-11 -2 -3 -3 1
2891</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002892
Rob Pikedf3183f2009-02-26 16:37:23 -08002893<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002894The shift operators shift the left operand by the shift count specified by the
2895right operand. They implement arithmetic shifts if the left operand is a signed
Rob Pikedf3183f2009-02-26 16:37:23 -08002896integer and logical shifts if it is an unsigned integer. The shift count must
2897be an unsigned integer. There is no upper limit on the shift count. Shifts behave
2898as if the left operand is shifted <code>n</code> times by 1 for a shift
2899count of <code>n</code>.
Robert Griesemer4ed666e2009-08-27 16:45:42 -07002900As a result, <code>x &lt;&lt; 1</code> is the same as <code>x*2</code>
2901and <code>x &gt;&gt; 1</code> is the same as
Robert Griesemere9192752009-12-01 16:15:53 -08002902<code>x/2</code> but truncated towards negative infinity.
Rob Pikedf3183f2009-02-26 16:37:23 -08002903</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002904
Rob Pikedf3183f2009-02-26 16:37:23 -08002905<p>
2906For integer operands, the unary operators
2907<code>+</code>, <code>-</code>, and <code>^</code> are defined as
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002908follows:
Rob Pikedf3183f2009-02-26 16:37:23 -08002909</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002910
Rob Pikeff70f092009-02-20 13:36:14 -08002911<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08002912+x is 0 + x
2913-x negation is 0 - x
Russ Coxd83dc4f2009-05-29 16:04:16 -07002914^x bitwise complement is m ^ x with m = "all bits set to 1" for unsigned x
2915 and m = -1 for signed x
Robert Griesemerc2d55862009-02-19 16:49:10 -08002916</pre>
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002917
Russ Cox5958dd62009-03-04 17:19:21 -08002918<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07002919For floating-point numbers,
Russ Cox5958dd62009-03-04 17:19:21 -08002920<code>+x</code> is the same as <code>x</code>,
2921while <code>-x</code> is the negation of <code>x</code>.
2922</p>
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002923
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002924<h3 id="Integer_overflow">Integer overflow</h3>
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002925
Rob Pikedf3183f2009-02-26 16:37:23 -08002926<p>
2927For unsigned integer values, the operations <code>+</code>,
2928<code>-</code>, <code>*</code>, and <code>&lt;&lt;</code> are
2929computed modulo 2<sup><i>n</i></sup>, where <i>n</i> is the bit width of
2930the unsigned integer's type
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002931<a href="#Numeric_types">Numeric types</a>). Loosely speaking, these unsigned integer operations
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002932discard high bits upon overflow, and programs may rely on ``wrap around''.
Rob Pikedf3183f2009-02-26 16:37:23 -08002933</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002934<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002935For signed integers, the operations <code>+</code>,
2936<code>-</code>, <code>*</code>, and <code>&lt;&lt;</code> may legally
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002937overflow and the resulting value exists and is deterministically defined
2938by the signed integer representation, the operation, and its operands.
Rob Pikedf3183f2009-02-26 16:37:23 -08002939No exception is raised as a result of overflow. A
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002940compiler may not optimize code under the assumption that overflow does
Rob Pikedf3183f2009-02-26 16:37:23 -08002941not occur. For instance, it may not assume that <code>x &lt; x + 1</code> is always true.
2942</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002943
2944
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002945<h3 id="Comparison_operators">Comparison operators</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002946
Rob Pike5af7de32009-02-24 15:17:59 -08002947<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07002948Comparison operators yield a value of type <code>bool</code>.
Rob Pike72970872010-03-04 12:35:16 -08002949The operators <code>==</code> and <code>!=</code> apply
Robert Griesemer19b1d352009-09-24 19:36:48 -07002950to operands of all types except arrays and structs.
Rob Pike72970872010-03-04 12:35:16 -08002951All other comparison operators apply only to integer, floating-point
2952and string values.
Rob Pike5af7de32009-02-24 15:17:59 -08002953</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002954
Rob Pikeff70f092009-02-20 13:36:14 -08002955<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08002956== equal
2957!= not equal
2958< less
2959<= less or equal
2960> greater
2961>= greater or equal
2962</pre>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002963
Rob Pike5af7de32009-02-24 15:17:59 -08002964<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07002965Operands of numeric type are compared in the usual way.
Rob Pike5af7de32009-02-24 15:17:59 -08002966</p>
2967<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07002968Operands of string type are compared byte-wise (lexically).
Rob Pike5af7de32009-02-24 15:17:59 -08002969</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002970<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07002971Operands of boolean type are equal if they are either both <code>true</code>
2972or both <code>false</code>.
Rob Pike5af7de32009-02-24 15:17:59 -08002973</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002974<p>
Rob Pike5af7de32009-02-24 15:17:59 -08002975The rules for comparison of composite types are described in the
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002976section on §<a href="#Comparison_compatibility">Comparison compatibility</a>.
Rob Pike5af7de32009-02-24 15:17:59 -08002977</p>
Robert Griesemera3294712009-01-05 11:17:26 -08002978
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002979
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002980<h3 id="Logical_operators">Logical operators</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002981
Rob Pikedf3183f2009-02-26 16:37:23 -08002982<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07002983Logical operators apply to <a href="#Boolean_types">boolean</a> values
2984and yield a result of the same type as the operands.
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002985The right operand is evaluated conditionally.
Rob Pikedf3183f2009-02-26 16:37:23 -08002986</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002987
Rob Pikeff70f092009-02-20 13:36:14 -08002988<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08002989&amp;&amp; conditional and p &amp;&amp; q is "if p then q else false"
2990|| conditional or p || q is "if p then true else q"
2991! not !p is "not p"
2992</pre>
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -07002993
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002994
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002995<h3 id="Address_operators">Address operators</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002996
Rob Pikedf3183f2009-02-26 16:37:23 -08002997<p>
Rob Pike678625d2009-09-15 09:54:22 -07002998The address-of operator <code>&amp;</code> generates the address of its operand,
2999which must be <i>addressable</i>,
3000that is, either a variable, pointer indirection, array or slice indexing
3001operation,
3002or a field selector of an addressable struct operand.
Rob Pike678625d2009-09-15 09:54:22 -07003003Given an operand of pointer type, the pointer indirection
3004operator <code>*</code> retrieves the value pointed
Rob Pikeafee1c52009-03-20 17:41:25 -07003005to by the operand.
3006</p>
3007
3008<pre>
3009&amp;x
3010&amp;a[f(2)]
3011*p
3012*pf(x)
3013</pre>
3014
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003015<h3 id="Communication_operators">Communication operators</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003016
Robert Griesemerc2d55862009-02-19 16:49:10 -08003017<p>
Rob Pike678625d2009-09-15 09:54:22 -07003018The term <i>channel</i> means "value of <a href="#Channel_types">channel type</a>".
Rob Pikedf3183f2009-02-26 16:37:23 -08003019</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08003020<p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08003021The send operation uses the binary operator "&lt;-", which operates on
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003022a channel and a value (expression):
Rob Pikedf3183f2009-02-26 16:37:23 -08003023</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003024
Robert Griesemerc2d55862009-02-19 16:49:10 -08003025<pre>
Robert Griesemere1e76192009-09-25 14:11:03 -07003026ch &lt;- 3
Robert Griesemerc2d55862009-02-19 16:49:10 -08003027</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003028
Rob Pikedf3183f2009-02-26 16:37:23 -08003029<p>
3030The send operation sends the value on the channel. Both the channel
3031and the expression are evaluated before communication begins.
3032Communication blocks until the send can proceed, at which point the
Rob Pike678625d2009-09-15 09:54:22 -07003033value is transmitted on the channel.
3034A send on an unbuffered channel can proceed if a receiver is ready.
3035A send on a buffered channel can proceed if there is room in the buffer.
Rob Pikedf3183f2009-02-26 16:37:23 -08003036</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08003037<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003038If the send operation appears in an expression context, the value
3039of the expression is a boolean and the operation is non-blocking.
3040The value of the boolean reports true if the communication succeeded,
Rob Pikedf3183f2009-02-26 16:37:23 -08003041false if it did not. (The channel and
3042the expression to be sent are evaluated regardless.)
3043These two examples are equivalent:
3044</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003045
Robert Griesemerc2d55862009-02-19 16:49:10 -08003046<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08003047ok := ch &lt;- 3
Robert Griesemerc2d55862009-02-19 16:49:10 -08003048if ok { print("sent") } else { print("not sent") }
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003049
Robert Griesemere1e76192009-09-25 14:11:03 -07003050if ch &lt;- 3 { print("sent") } else { print("not sent") }
Robert Griesemerc2d55862009-02-19 16:49:10 -08003051</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003052
Rob Pikedf3183f2009-02-26 16:37:23 -08003053<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003054In other words, if the program tests the value of a send operation,
3055the send is non-blocking and the value of the expression is the
3056success of the operation. If the program does not test the value,
3057the operation blocks until it succeeds.
Rob Pikedf3183f2009-02-26 16:37:23 -08003058</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08003059<p>
3060The receive operation uses the prefix unary operator "&lt;-".
Rob Pikedf3183f2009-02-26 16:37:23 -08003061The value of the expression is the value received, whose type
3062is the element type of the channel.
3063</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003064
Robert Griesemerc2d55862009-02-19 16:49:10 -08003065<pre>
Robert Griesemere1e76192009-09-25 14:11:03 -07003066&lt;-ch
Robert Griesemerc2d55862009-02-19 16:49:10 -08003067</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003068
Rob Pikedf3183f2009-02-26 16:37:23 -08003069<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003070The expression blocks until a value is available, which then can
Rob Pikedf3183f2009-02-26 16:37:23 -08003071be assigned to a variable or used like any other expression.
3072If the receive expression does not save the value, the value is
3073discarded.
3074</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003075
Robert Griesemerc2d55862009-02-19 16:49:10 -08003076<pre>
Robert Griesemere1e76192009-09-25 14:11:03 -07003077v1 := &lt;-ch
3078v2 = &lt;-ch
3079f(&lt;-ch)
3080&lt;-strobe // wait until clock pulse
Robert Griesemerc2d55862009-02-19 16:49:10 -08003081</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003082
Rob Pikedf3183f2009-02-26 16:37:23 -08003083<p>
Rob Piked5537072009-08-22 00:04:04 -07003084If a receive expression is used in an assignment or initialization of the form
Rob Pikedf3183f2009-02-26 16:37:23 -08003085</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003086
Robert Griesemerc2d55862009-02-19 16:49:10 -08003087<pre>
Robert Griesemere1e76192009-09-25 14:11:03 -07003088x, ok = &lt;-ch
3089x, ok := &lt;-ch
3090var x, ok = &lt;-ch
Robert Griesemerc2d55862009-02-19 16:49:10 -08003091</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003092
Rob Pikedf3183f2009-02-26 16:37:23 -08003093<p>
3094the receive operation becomes non-blocking.
Rob Pike678625d2009-09-15 09:54:22 -07003095If the operation can proceed, the boolean variable
Rob Pikedf3183f2009-02-26 16:37:23 -08003096<code>ok</code> will be set to <code>true</code>
3097and the value stored in <code>x</code>; otherwise
3098<code>ok</code> is set
3099to <code>false</code> and <code>x</code> is set to the
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003100zero value for its type (§<a href="#The_zero_value">The zero value</a>).
Rob Pikedf3183f2009-02-26 16:37:23 -08003101</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003102
Rob Pike0b4de7a2009-11-09 16:09:57 -08003103<!---
Rob Pikedf3183f2009-02-26 16:37:23 -08003104<p>
Robert Griesemer90cc4a52009-10-22 09:41:38 -07003105<span class="alert">TODO: Probably in a separate section, communication semantics
3106need to be presented regarding send, receive, select, and goroutines.</span>
Rob Pikedf3183f2009-02-26 16:37:23 -08003107</p>
Rob Pike0b4de7a2009-11-09 16:09:57 -08003108--->
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003109
Rob Pike01cadde2009-09-15 15:56:44 -07003110<h3 id="Method_expressions">Method expressions</h3>
3111
3112<p>
3113If <code>M</code> is in the method set of type <code>T</code>,
3114<code>T.M</code> is a function that is callable as a regular function
3115with the same arguments as <code>M</code> prefixed by an additional
3116argument that is the receiver of the method.
3117</p>
3118
Robert Griesemerda961882009-09-17 11:01:50 -07003119<pre class="ebnf">
Rob Pike01cadde2009-09-15 15:56:44 -07003120MethodExpr = ReceiverType "." MethodName .
3121ReceiverType = TypeName | "(" "*" TypeName ")" .
Rob Pike01cadde2009-09-15 15:56:44 -07003122</pre>
3123
3124<p>
3125Consider a struct type <code>T</code> with two methods,
3126<code>Mv</code>, whose receiver is of type <code>T</code>, and
3127<code>Mp</code>, whose receiver is of type <code>*T</code>.
3128</p>
3129
3130<pre>
3131type T struct {
Robert Griesemer130ac742009-12-10 16:43:01 -08003132 a int
Rob Pike01cadde2009-09-15 15:56:44 -07003133}
3134func (tv T) Mv(a int) int { return 0 } // value receiver
3135func (tp *T) Mp(f float) float { return 1 } // pointer receiver
Robert Griesemer130ac742009-12-10 16:43:01 -08003136var t T
Rob Pike01cadde2009-09-15 15:56:44 -07003137</pre>
3138
3139<p>
3140The expression
3141</p>
3142
3143<pre>
3144T.Mv
3145</pre>
3146
3147<p>
3148yields a function equivalent to <code>Mv</code> but
3149with an explicit receiver as its first argument; it has signature
3150</p>
3151
3152<pre>
Russ Cox46871692010-01-26 10:25:56 -08003153func(tv T, a int) int
Rob Pike01cadde2009-09-15 15:56:44 -07003154</pre>
3155
3156<p>
3157That function may be called normally with an explicit receiver, so
3158these three invocations are equivalent:
3159</p>
3160
3161<pre>
3162t.Mv(7)
3163T.Mv(t, 7)
3164f := T.Mv; f(t, 7)
3165</pre>
3166
3167<p>
3168Similarly, the expression
3169</p>
3170
3171<pre>
3172(*T).Mp
3173</pre>
3174
3175<p>
3176yields a function value representing <code>Mp</code> with signature
3177</p>
3178
3179<pre>
Russ Cox46871692010-01-26 10:25:56 -08003180func(tp *T, f float) float
Rob Pike01cadde2009-09-15 15:56:44 -07003181</pre>
3182
3183<p>
3184For a method with a value receiver, one can derive a function
3185with an explicit pointer receiver, so
3186</p>
3187
3188<pre>
3189(*T).Mv
3190</pre>
3191
3192<p>
3193yields a function value representing <code>Mv</code> with signature
3194</p>
3195
3196<pre>
Russ Cox46871692010-01-26 10:25:56 -08003197func(tv *T, a int) int
Rob Pike01cadde2009-09-15 15:56:44 -07003198</pre>
3199
3200<p>
3201Such a function indirects through the receiver to create a value
3202to pass as the receiver to the underlying method;
3203the method does not overwrite the value whose address is passed in
3204the function call.
3205</p>
3206
3207<p>
3208The final case, a value-receiver function for a pointer-receiver method,
3209is illegal because pointer-receiver methods are not in the method set
3210of the value type.
3211</p>
3212
3213<p>
3214Function values derived from methods are called with function call syntax;
3215the receiver is provided as the first argument to the call.
3216That is, given <code>f := T.Mv</code>, <code>f</code> is invoked
3217as <code>f(t, 7)</code> not <code>t.f(7)</code>.
3218To construct a function that binds the receiver, use a
Robert Griesemerd36d1912009-09-18 11:58:35 -07003219<a href="#Function_literals">closure</a>.
Rob Pike01cadde2009-09-15 15:56:44 -07003220</p>
3221
3222<p>
3223It is legal to derive a function value from a method of an interface type.
3224The resulting function takes an explicit receiver of that interface type.
3225</p>
3226
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003227<h3 id="Conversions">Conversions</h3>
3228
3229<p>
3230Conversions are expressions of the form <code>T(x)</code>
3231where <code>T</code> is a type and <code>x</code> is an expression
3232that can be converted to type <code>T</code>.
3233</p>
3234
3235<pre class="ebnf">
3236Conversion = LiteralType "(" Expression ")" .
3237</pre>
3238
3239<p>
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003240In general, a conversion succeeds if the value of <code>x</code> is
3241<a href="#Assignment_compatibility">assignment compatible</a> with type <code>T</code>,
3242or if the value would be assignment compatible with type <code>T</code> if the
3243value's type, or <code>T</code>, or any of their component types were unnamed.
3244Usually, such a conversion changes the type but not the representation of the value
3245of <code>x</code> and thus has no run-time cost.
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003246</p>
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003247
3248<p>
3249Specific rules apply to conversions where <code>T</code> is a numeric or string type.
3250These conversions may change the representation of a value and incur a run-time cost.
3251</p>
3252
3253<h4>Conversions between integer types</h4>
3254<p>
3255If the value is a signed quantity, it is
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003256sign extended to implicit infinite precision; otherwise it is zero
3257extended. It is then truncated to fit in the result type's size.
3258For example, if <code>x := uint16(0x10F0)</code>, then <code>uint32(int8(x)) == 0xFFFFFFF0</code>.
3259The conversion always yields a valid value; there is no indication of overflow.
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003260</p>
3261
Rob Pike72970872010-03-04 12:35:16 -08003262<h4>Conversions involving floating point and complex types</h4>
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003263<ol>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003264<li>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003265When converting a floating-point number to an integer, the fraction is discarded
3266(truncation towards zero).
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003267</li>
3268<li>
Rob Pike72970872010-03-04 12:35:16 -08003269A value of complex type may be converted to a different complex type,
Ian Lance Taylor11a21842010-03-10 20:38:38 -08003270but there is no conversion between complex and any other type.
Rob Pike72970872010-03-04 12:35:16 -08003271<li>
3272When converting a number to a floating-point or complex type,
3273the result value is rounded
3274to the precision specified by the destination type.
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003275For instance, the value of a variable <code>x</code> of type <code>float32</code>
3276may be stored using additional precision beyond that of an IEEE-754 32-bit number,
3277but float32(x) represents the result of rounding <code>x</code>'s value to
327832-bit precision. Similarly, <code>x + 0.1</code> may use more than 32 bits
3279of precision, <code>but float32(x + 0.1)</code> does not.
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003280</li>
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003281</ol>
3282
3283<p>
Rob Pike72970872010-03-04 12:35:16 -08003284In all conversions involving floating-point or complex values,
3285if the result type cannot represent the value the conversion
3286succeeds but the result value is
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003287implementation-dependent.
3288</p>
3289
Rob Pike1811fac2010-02-17 11:26:09 +11003290<h4>Conversions to and from a string type</h4>
3291
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003292<ol>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003293<li>
Rob Pike1811fac2010-02-17 11:26:09 +11003294Converting a signed or unsigned integer value to a string type yields a
3295string containing the UTF-8 representation of the integer.
3296Negative values are converted to <code>"\uFFFD"</code>.
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003297
3298<pre>
Rob Pike1811fac2010-02-17 11:26:09 +11003299string('a') // "a"
3300string(-1) // "\ufffd" == "\xef\xbf\xbd "
3301string(0xf8) // "\u00f8" == "ø" == "\xc3\xb8"
3302type MyString string
3303MyString(0x65e5) // "\u65e5" == "日" == "\xe6\x97\xa5"
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003304</pre>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003305</li>
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003306
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003307<li>
Rob Pike1811fac2010-02-17 11:26:09 +11003308Converting a value of type <code>[]byte</code> (or
3309the equivalent <code>[]uint8</code>) to a string type yields a
3310string whose successive bytes are the elements of the slice. If
3311the slice value is <code>nil</code>, the result is the empty string.
3312
3313<pre>
3314string([]byte{'h', 'e', 'l', 'l', '\xc3', '\xb8'}) // "hellø"
3315</pre>
3316</li>
3317
3318<li>
3319Converting a value of type <code>[]int</code> to a string type yields
3320a string that is the concatenation of the individual integers
3321converted to strings. If the slice value is <code>nil</code>, the
3322result is the empty string.
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003323<pre>
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003324string([]int{0x767d, 0x9d6c, 0x7fd4}) // "\u767d\u9d6c\u7fd4" == "白鵬翔"
3325</pre>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003326</li>
3327
3328<li>
Rob Pike1811fac2010-02-17 11:26:09 +11003329Converting a value of a string type to <code>[]byte</code> (or <code>[]uint8</code>)
3330yields a slice whose successive elements are the bytes of the string.
3331If the string is empty, the result is <code>[]byte(nil)</code>.
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003332
3333<pre>
Rob Pike1811fac2010-02-17 11:26:09 +11003334[]byte("hellø") // []byte{'h', 'e', 'l', 'l', '\xc3', '\xb8'}
3335</pre>
3336</li>
3337
3338<li>
3339Converting a value of a string type to <code>[]int</code> yields a
3340slice containing the individual Unicode code points of the string.
3341If the string is empty, the result is <code>[]int(nil)</code>.
3342<pre>
3343[]int(MyString("白鵬翔")) // []int{0x767d, 0x9d6c, 0x7fd4}
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003344</pre>
3345</li>
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003346</ol>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003347
3348<p>
3349There is no linguistic mechanism to convert between pointers and integers.
3350The package <a href="#Package_unsafe"><code>unsafe</code></a>
3351implements this functionality under
3352restricted circumstances.
3353</p>
3354
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003355<h3 id="Constant_expressions">Constant expressions</h3>
Robert Griesemerb90b2132008-09-19 15:49:55 -07003356
Rob Pikef27e9f02009-02-23 19:22:05 -08003357<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07003358Constant expressions may contain only <a href="#Constants">constant</a>
3359operands and are evaluated at compile-time.
Rob Pikef27e9f02009-02-23 19:22:05 -08003360</p>
3361
3362<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07003363Untyped boolean, numeric, and string constants may be used as operands
3364wherever it is legal to use an operand of boolean, numeric, or string type,
3365respectively. Except for shift operations, if the operands of a binary operation
3366are an untyped integer constant and an untyped floating-point constant,
3367the integer constant is converted to an untyped floating-point constant
3368(relevant for <code>/</code> and <code>%</code>).
Rob Pike72970872010-03-04 12:35:16 -08003369Similarly,
3370untyped integer or floating-point constants may be used as operands
3371wherever it is legal to use an operand of complex type;
3372the integer or floating point constant is converted to a
3373complex constant with a zero imaginary part.
Robert Griesemer19b1d352009-09-24 19:36:48 -07003374</p>
Robert Griesemer997851e2009-09-25 15:36:25 -07003375
3376<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07003377Applying an operator to untyped constants results in an untyped
Rob Pike72970872010-03-04 12:35:16 -08003378constant of the same kind (that is, a boolean, integer, floating-point,
3379complex, or string constant), except for
3380<a href="#Comparison_operators">comparison operators</a>, which result in
Robert Griesemer19b1d352009-09-24 19:36:48 -07003381a constant of type <code>bool</code>.
3382</p>
3383
3384<p>
Rob Pike72970872010-03-04 12:35:16 -08003385Imaginary literals are untyped complex constants (with zero real part)
3386and may be combined in binary
3387operations with untyped integer and floating-point constants; the
3388result is an untyped complex constant.
3389Complex constants are always constructed from
3390constant expressions involving imaginary
Robert Griesemeref4c2b82010-03-10 15:29:36 -08003391literals or constants derived from them, or calls of the built-in function
3392<a href="#Complex_numbers"><code>cmplx</code></a>.
Rob Pike72970872010-03-04 12:35:16 -08003393</p>
3394
3395<pre>
3396const Σ = 1 - 0.707i
Robert Griesemeref4c2b82010-03-10 15:29:36 -08003397const Δ = Σ + 2.0e-4 - 1/1i
Rob Pike72970872010-03-04 12:35:16 -08003398const Φ = iota * 1i
3399const iΓ = cmplx(0, Γ)
3400</pre>
3401
3402<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07003403Constant expressions are always evaluated exactly; intermediate values and the
3404constants themselves may require precision significantly larger than supported
3405by any predeclared type in the language. The following are legal declarations:
Rob Pikef27e9f02009-02-23 19:22:05 -08003406</p>
3407
3408<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08003409const Huge = 1 &lt;&lt; 100
3410const Four int8 = Huge &gt;&gt; 98
Rob Pikef27e9f02009-02-23 19:22:05 -08003411</pre>
3412
3413<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07003414The values of <i>typed</i> constants must always be accurately representable as values
3415of the constant type. The following constant expressions are illegal:
Rob Pike21d03492009-03-24 19:16:42 -07003416</p>
3417
3418<pre>
Robert Griesemere9192752009-12-01 16:15:53 -08003419uint(-1) // -1 cannot be represented as a uint
3420int(3.14) // 3.14 cannot be represented as an int
3421int64(Huge) // 1&lt;&lt;100 cannot be represented as an int64
3422Four * 300 // 300 cannot be represented as an int8
3423Four * 100 // 400 cannot be represented as an int8
Rob Pike21d03492009-03-24 19:16:42 -07003424</pre>
3425
3426<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07003427The mask used by the unary bitwise complement operator <code>^</code> matches
Rob Pike678625d2009-09-15 09:54:22 -07003428the rule for non-constants: the mask is all 1s for unsigned constants
Robert Griesemer19b1d352009-09-24 19:36:48 -07003429and -1 for signed and untyped constants.
Rob Pike21d03492009-03-24 19:16:42 -07003430</p>
3431
3432<pre>
Robert Griesemer19b1d352009-09-24 19:36:48 -07003433^1 // untyped integer constant, equal to -2
Rob Pike21d03492009-03-24 19:16:42 -07003434uint8(^1) // error, same as uint8(-2), out of range
3435^uint8(1) // typed uint8 constant, same as 0xFF ^ uint8(1) = uint8(0xFE)
3436int8(^1) // same as int8(-2)
Russ Coxd83dc4f2009-05-29 16:04:16 -07003437^int8(1) // same as -1 ^ int8(1) = -2
Rob Pike21d03492009-03-24 19:16:42 -07003438</pre>
3439
Rob Pike0b4de7a2009-11-09 16:09:57 -08003440<!---
Rob Pike21d03492009-03-24 19:16:42 -07003441<p>
Robert Griesemer90cc4a52009-10-22 09:41:38 -07003442<span class="alert">
Rob Pike21d03492009-03-24 19:16:42 -07003443TODO: perhaps ^ should be disallowed on non-uints instead of assuming twos complement.
3444Also it may be possible to make typed constants more like variables, at the cost of fewer
3445overflow etc. errors being caught.
Robert Griesemer90cc4a52009-10-22 09:41:38 -07003446</span>
Rob Pike21d03492009-03-24 19:16:42 -07003447</p>
Rob Pike0b4de7a2009-11-09 16:09:57 -08003448--->
Robert Griesemer19b1d352009-09-24 19:36:48 -07003449
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003450<h3 id="Order_of_evaluation">Order of evaluation</h3>
Rob Pikec956e902009-04-14 20:10:49 -07003451
3452<p>
3453When evaluating the elements of an assignment or expression,
3454all function calls, method calls and
3455communication operations are evaluated in lexical left-to-right
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003456order.
3457</p>
3458
3459<p>
Robert Griesemer4f185492009-05-01 17:00:16 -07003460For example, in the assignment
Rob Pikec956e902009-04-14 20:10:49 -07003461</p>
3462<pre>
Robert Griesemere1e76192009-09-25 14:11:03 -07003463y[f()], ok = g(h(), i() + x[j()], &lt;-c), k()
Rob Pikec956e902009-04-14 20:10:49 -07003464</pre>
3465<p>
Robert Griesemer4f185492009-05-01 17:00:16 -07003466the function calls and communication happen in the order
3467<code>f()</code>, <code>h()</code>, <code>i()</code>, <code>j()</code>,
Robert Griesemere1e76192009-09-25 14:11:03 -07003468<code>&lt;-c</code>, <code>g()</code>, and <code>k()</code>.
Robert Griesemer4f185492009-05-01 17:00:16 -07003469However, the order of those events compared to the evaluation
3470and indexing of <code>x</code> and the evaluation
3471of <code>y</code> is not specified.
Rob Pikec956e902009-04-14 20:10:49 -07003472</p>
3473
Rob Pike4fe41922009-11-07 22:00:59 -08003474<p>
3475Floating-point operations within a single expression are evaluated according to
3476the associativity of the operators. Explicit parentheses affect the evaluation
3477by overriding the default associativity.
3478In the expression <code>x + (y + z)</code> the addition <code>y + z</code>
3479is performed before adding <code>x</code>.
3480</p>
3481
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003482<h2 id="Statements">Statements</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003483
Rob Pike96750f12009-02-27 16:47:48 -08003484<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003485Statements control execution.
Rob Pike96750f12009-02-27 16:47:48 -08003486</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003487
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003488<pre class="ebnf">
Robert Griesemerdea43942009-03-16 17:36:52 -07003489Statement =
Rob Pikef3a33bc2009-09-14 17:39:17 -07003490 Declaration | LabeledStmt | SimpleStmt |
3491 GoStmt | ReturnStmt | BreakStmt | ContinueStmt | GotoStmt |
Rob Pike11417162009-03-24 17:45:53 -07003492 FallthroughStmt | Block | IfStmt | SwitchStmt | SelectStmt | ForStmt |
3493 DeferStmt .
Robert Griesemer7abfcd92008-10-07 17:14:30 -07003494
Rob Pikef3a33bc2009-09-14 17:39:17 -07003495SimpleStmt = EmptyStmt | ExpressionStmt | IncDecStmt | Assignment | ShortVarDecl .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003496</pre>
Robert Griesemer7271e042008-10-09 20:05:24 -07003497
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003498
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003499<h3 id="Empty_statements">Empty statements</h3>
Robert Griesemeraed247f2008-10-08 17:05:30 -07003500
Rob Pike96750f12009-02-27 16:47:48 -08003501<p>
Robert Griesemeraed247f2008-10-08 17:05:30 -07003502The empty statement does nothing.
Rob Pike96750f12009-02-27 16:47:48 -08003503</p>
Robert Griesemeraed247f2008-10-08 17:05:30 -07003504
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003505<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003506EmptyStmt = .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003507</pre>
Robert Griesemeraed247f2008-10-08 17:05:30 -07003508
3509
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003510<h3 id="Labeled_statements">Labeled statements</h3>
Robert Griesemerdea43942009-03-16 17:36:52 -07003511
3512<p>
3513A labeled statement may be the target of a <code>goto</code>,
3514<code>break</code> or <code>continue</code> statement.
3515</p>
3516
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003517<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003518LabeledStmt = Label ":" Statement .
Robert Griesemerdea43942009-03-16 17:36:52 -07003519Label = identifier .
3520</pre>
3521
3522<pre>
Rob Pike3909b6b2010-01-25 07:48:31 +11003523Error: log.Crash("error encountered")
Robert Griesemerdea43942009-03-16 17:36:52 -07003524</pre>
3525
3526
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003527<h3 id="Expression_statements">Expression statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003528
Rob Pike96750f12009-02-27 16:47:48 -08003529<p>
3530Function calls, method calls, and channel operations
3531can appear in statement context.
3532</p>
3533
3534
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003535<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003536ExpressionStmt = Expression .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003537</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003538
Robert Griesemerc2d55862009-02-19 16:49:10 -08003539<pre>
3540f(x+y)
Robert Griesemere1e76192009-09-25 14:11:03 -07003541&lt;-ch
Robert Griesemerc2d55862009-02-19 16:49:10 -08003542</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003543
3544
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003545<h3 id="IncDec_statements">IncDec statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003546
Rob Pike96750f12009-02-27 16:47:48 -08003547<p>
Robert Griesemer52a54802008-09-30 13:02:50 -07003548The "++" and "--" statements increment or decrement their operands
Robert Griesemer19b1d352009-09-24 19:36:48 -07003549by the untyped <a href="#Constants">constant</a> <code>1</code>.
3550As with an assignment, the operand must be a variable, pointer indirection,
3551field selector or index expression.
Rob Pike96750f12009-02-27 16:47:48 -08003552</p>
Robert Griesemer52a54802008-09-30 13:02:50 -07003553
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003554<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003555IncDecStmt = Expression ( "++" | "--" ) .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003556</pre>
Russ Cox5958dd62009-03-04 17:19:21 -08003557
Rob Pike96750f12009-02-27 16:47:48 -08003558<p>
Robert Griesemer506c0082009-09-08 15:41:14 -07003559The following <a href="#Assignments">assignment statements</a> are semantically
Robert Griesemer52a54802008-09-30 13:02:50 -07003560equivalent:
Rob Pike96750f12009-02-27 16:47:48 -08003561</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003562
Rob Pikeff70f092009-02-20 13:36:14 -08003563<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08003564IncDec statement Assignment
3565x++ x += 1
3566x-- x -= 1
3567</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003568
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003569<h3 id="Assignments">Assignments</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003570
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003571<pre class="ebnf">
Robert Griesemerc2d55862009-02-19 16:49:10 -08003572Assignment = ExpressionList assign_op ExpressionList .
Rob Pikeff70f092009-02-20 13:36:14 -08003573
Robert Griesemerc2d55862009-02-19 16:49:10 -08003574assign_op = [ add_op | mul_op ] "=" .
3575</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003576
Rob Pike96750f12009-02-27 16:47:48 -08003577<p>
Rob Pike678625d2009-09-15 09:54:22 -07003578Each left-hand side operand must be <a href="#Address_operators">addressable</a>,
Rob Pike4fe41922009-11-07 22:00:59 -08003579a map index expression,
Rob Pike678625d2009-09-15 09:54:22 -07003580or the <a href="#Blank_identifier">blank identifier</a>.
Rob Pike96750f12009-02-27 16:47:48 -08003581</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003582
Robert Griesemerc2d55862009-02-19 16:49:10 -08003583<pre>
3584x = 1
3585*p = f()
3586a[i] = 23
Robert Griesemere1e76192009-09-25 14:11:03 -07003587k = &lt;-ch
Robert Griesemerc2d55862009-02-19 16:49:10 -08003588</pre>
Rob Pike96750f12009-02-27 16:47:48 -08003589
3590<p>
3591An <i>assignment operation</i> <code>x</code> <i>op</i><code>=</code>
3592<code>y</code> where <i>op</i> is a binary arithmetic operation is equivalent
3593to <code>x</code> <code>=</code> <code>x</code> <i>op</i>
Rob Pike4fe41922009-11-07 22:00:59 -08003594<code>y</code> but evaluates <code>x</code>
Rob Pike96750f12009-02-27 16:47:48 -08003595only once. The <i>op</i><code>=</code> construct is a single token.
Rob Pike678625d2009-09-15 09:54:22 -07003596In assignment operations, both the left- and right-hand expression lists
3597must contain exactly one single-valued expression.
Rob Pike96750f12009-02-27 16:47:48 -08003598</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003599
Robert Griesemerc2d55862009-02-19 16:49:10 -08003600<pre>
Robert Griesemer4ed666e2009-08-27 16:45:42 -07003601a[i] &lt;&lt;= 2
Rob Pike678625d2009-09-15 09:54:22 -07003602i &amp;^= 1&lt;&lt;n
Robert Griesemerc2d55862009-02-19 16:49:10 -08003603</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003604
Rob Pike96750f12009-02-27 16:47:48 -08003605<p>
3606A tuple assignment assigns the individual elements of a multi-valued
3607operation to a list of variables. There are two forms. In the
3608first, the right hand operand is a single multi-valued expression
Robert Griesemer506c0082009-09-08 15:41:14 -07003609such as a function evaluation or <a href="#Channel_types">channel</a> or
3610<a href="#Map_types">map</a> operation or a <a href="#Type_assertions">type assertion</a>.
Russ Cox5958dd62009-03-04 17:19:21 -08003611The number of operands on the left
Robert Griesemer4e56b332009-09-10 10:14:00 -07003612hand side must match the number of values. For instance, if
Rob Pike96750f12009-02-27 16:47:48 -08003613<code>f</code> is a function returning two values,
3614</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003615
Robert Griesemerc2d55862009-02-19 16:49:10 -08003616<pre>
3617x, y = f()
3618</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003619
Rob Pike96750f12009-02-27 16:47:48 -08003620<p>
3621assigns the first value to <code>x</code> and the second to <code>y</code>.
Rob Pike678625d2009-09-15 09:54:22 -07003622The <a href="#Blank_identifier">blank identifier</a> provides a
Robert Griesemer4e56b332009-09-10 10:14:00 -07003623way to ignore values returned by a multi-valued expression:
Rob Pike96750f12009-02-27 16:47:48 -08003624</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003625
Robert Griesemer4e56b332009-09-10 10:14:00 -07003626<pre>
3627x, _ = f() // ignore second value returned by f()
3628</pre>
3629
Rob Pike96750f12009-02-27 16:47:48 -08003630<p>
3631In the second form, the number of operands on the left must equal the number
Robert Griesemeref45e642009-08-21 11:25:00 -07003632of expressions on the right, each of which must be single-valued, and the
3633<i>n</i>th expression on the right is assigned to the <i>n</i>th
3634operand on the left.
Russ Cox5958dd62009-03-04 17:19:21 -08003635The expressions on the right are evaluated before assigning to
3636any of the operands on the left, but otherwise the evaluation
Rob Pike678625d2009-09-15 09:54:22 -07003637order is unspecified beyond <a href="#Order_of_evaluation">the usual rules</a>.
Rob Pike96750f12009-02-27 16:47:48 -08003638</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003639
Robert Griesemerc2d55862009-02-19 16:49:10 -08003640<pre>
Rob Pike96750f12009-02-27 16:47:48 -08003641a, b = b, a // exchange a and b
Robert Griesemerc2d55862009-02-19 16:49:10 -08003642</pre>
Rob Pike96750f12009-02-27 16:47:48 -08003643
3644<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07003645In assignments, each value must be
Robert Griesemer4ed666e2009-08-27 16:45:42 -07003646<a href="#Assignment_compatibility">assignment compatible</a> with the type of the
Robert Griesemer19b1d352009-09-24 19:36:48 -07003647operand to which it is assigned. If an untyped <a href="#Constants">constant</a>
3648is assigned to a variable of interface type, the constant is <a href="#Conversions">converted</a>
Rob Pike72970872010-03-04 12:35:16 -08003649to type <code>bool</code>, <code>int</code>, <code>float</code>,
3650<code>complex</code> or <code>string</code>
Robert Griesemer19b1d352009-09-24 19:36:48 -07003651respectively, depending on whether the value is a boolean, integer, floating-point,
Rob Pike72970872010-03-04 12:35:16 -08003652complex, or string constant.
Rob Pike96750f12009-02-27 16:47:48 -08003653</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003654
3655
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003656<h3 id="If_statements">If statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003657
Rob Pike96750f12009-02-27 16:47:48 -08003658<p>
3659"If" statements specify the conditional execution of two branches
3660according to the value of a boolean expression. If the expression
3661evaluates to true, the "if" branch is executed, otherwise, if
3662present, the "else" branch is executed. A missing condition
3663is equivalent to <code>true</code>.
3664</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003665
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003666<pre class="ebnf">
Rob Pikef3a33bc2009-09-14 17:39:17 -07003667IfStmt = "if" [ SimpleStmt ";" ] [ Expression ] Block [ "else" Statement ] .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003668</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003669
Robert Griesemerc2d55862009-02-19 16:49:10 -08003670<pre>
3671if x > 0 {
3672 return true;
3673}
3674</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003675
Russ Cox5958dd62009-03-04 17:19:21 -08003676<p>
Russ Cox16b95ba2009-08-20 10:22:52 -07003677The expression may be preceded by a simple statement, which
3678executes before the expression is evaluated.
Russ Cox5958dd62009-03-04 17:19:21 -08003679</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003680
Robert Griesemerc2d55862009-02-19 16:49:10 -08003681<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08003682if x := f(); x &lt; y {
3683 return x
Robert Griesemerc2d55862009-02-19 16:49:10 -08003684} else if x > z {
Robert Griesemer130ac742009-12-10 16:43:01 -08003685 return z
Robert Griesemerc2d55862009-02-19 16:49:10 -08003686} else {
Robert Griesemer130ac742009-12-10 16:43:01 -08003687 return y
Robert Griesemerc2d55862009-02-19 16:49:10 -08003688}
3689</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003690
3691
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003692<h3 id="Switch_statements">Switch statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003693
Rob Pike96750f12009-02-27 16:47:48 -08003694<p>
3695"Switch" statements provide multi-way execution.
Rob Pike5a578492009-03-17 16:48:35 -07003696An expression or type specifier is compared to the "cases"
3697inside the "switch" to determine which branch
3698to execute.
Rob Pikeafee1c52009-03-20 17:41:25 -07003699</p>
Robert Griesemer091cba82009-03-19 08:39:40 -07003700
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003701<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003702SwitchStmt = ExprSwitchStmt | TypeSwitchStmt .
Robert Griesemer091cba82009-03-19 08:39:40 -07003703</pre>
3704
Rob Pikeafee1c52009-03-20 17:41:25 -07003705<p>
Rob Pike5a578492009-03-17 16:48:35 -07003706There are two forms: expression switches and type switches.
Rob Pike5a578492009-03-17 16:48:35 -07003707In an expression switch, the cases contain expressions that are compared
3708against the value of the switch expression.
3709In a type switch, the cases contain types that are compared against the
3710type of a specially annotated switch expression.
Rob Pike96750f12009-02-27 16:47:48 -08003711</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003712
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003713<h4 id="Expression_switches">Expression switches</h4>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003714
Rob Pike96750f12009-02-27 16:47:48 -08003715<p>
Rob Pike5a578492009-03-17 16:48:35 -07003716In an expression switch,
3717the switch expression is evaluated and
3718the case expressions, which need not be constants,
Robert Griesemer4f185492009-05-01 17:00:16 -07003719are evaluated left-to-right and top-to-bottom; the first one that equals the
Rob Pike5a578492009-03-17 16:48:35 -07003720switch expression
Rob Pike96750f12009-02-27 16:47:48 -08003721triggers execution of the statements of the associated case;
3722the other cases are skipped.
Rob Pike5a578492009-03-17 16:48:35 -07003723If no case matches and there is a "default" case,
3724its statements are executed.
Rob Pike96750f12009-02-27 16:47:48 -08003725There can be at most one default case and it may appear anywhere in the
3726"switch" statement.
Robert Griesemere9192752009-12-01 16:15:53 -08003727A missing switch expression is equivalent to
Rob Pike70c1a102009-03-18 19:23:59 -07003728the expression <code>true</code>.
Rob Pike96750f12009-02-27 16:47:48 -08003729</p>
Rob Pike70c1a102009-03-18 19:23:59 -07003730
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003731<pre class="ebnf">
Rob Pikef3a33bc2009-09-14 17:39:17 -07003732ExprSwitchStmt = "switch" [ SimpleStmt ";" ] [ Expression ] "{" { ExprCaseClause } "}" .
Robert Griesemer130ac742009-12-10 16:43:01 -08003733ExprCaseClause = ExprSwitchCase ":" { Statement ";" } .
Robert Griesemer091cba82009-03-19 08:39:40 -07003734ExprSwitchCase = "case" ExpressionList | "default" .
Rob Pike70c1a102009-03-18 19:23:59 -07003735</pre>
3736
Rob Pike96750f12009-02-27 16:47:48 -08003737<p>
3738In a case or default clause,
3739the last statement only may be a "fallthrough" statement
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003740<a href="#Fallthrough_statement">Fallthrough statement</a>) to
Rob Pike96750f12009-02-27 16:47:48 -08003741indicate that control should flow from the end of this clause to
Robert Griesemeraed247f2008-10-08 17:05:30 -07003742the first statement of the next clause.
Rob Pike96750f12009-02-27 16:47:48 -08003743Otherwise control flows to the end of the "switch" statement.
3744</p>
Robert Griesemer0a162a12009-08-19 16:44:04 -07003745
Robert Griesemerc2d55862009-02-19 16:49:10 -08003746<p>
Russ Cox16b95ba2009-08-20 10:22:52 -07003747The expression may be preceded by a simple statement, which
3748executes before the expression is evaluated.
Rob Pike96750f12009-02-27 16:47:48 -08003749</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003750
Robert Griesemerc2d55862009-02-19 16:49:10 -08003751<pre>
3752switch tag {
Rob Pike65ec16b2009-05-29 15:46:03 -07003753default: s3()
3754case 0, 1, 2, 3: s1()
3755case 4, 5, 6, 7: s2()
Robert Griesemerc2d55862009-02-19 16:49:10 -08003756}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003757
Andrew Gerrand10b77f72010-03-29 13:12:08 +11003758switch x := f(); { // missing switch expression means "true"
Rob Pike65ec16b2009-05-29 15:46:03 -07003759case x &lt; 0: return -x
3760default: return x
Robert Griesemerc2d55862009-02-19 16:49:10 -08003761}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003762
Robert Griesemere9192752009-12-01 16:15:53 -08003763switch {
Robert Griesemer130ac742009-12-10 16:43:01 -08003764case x &lt; y: f1()
3765case x &lt; z: f2()
3766case x == 4: f3()
Robert Griesemerc2d55862009-02-19 16:49:10 -08003767}
3768</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003769
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003770<h4 id="Type_switches">Type switches</h4>
Rob Pike70c1a102009-03-18 19:23:59 -07003771
Rob Pike5a578492009-03-17 16:48:35 -07003772<p>
Rob Pike70c1a102009-03-18 19:23:59 -07003773A type switch compares types rather than values. It is otherwise similar
Robert Griesemer1f95f0d2009-08-27 16:44:17 -07003774to an expression switch. It is marked by a special switch expression that
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003775has the form of a <a href="#Type_assertions">type assertion</a>
Rob Pike70c1a102009-03-18 19:23:59 -07003776using the reserved word <code>type</code> rather than an actual type.
3777Cases then match literal types against the dynamic type of the expression
Rob Pikef5387602009-03-30 16:08:41 -07003778in the type assertion.
Rob Pike5a578492009-03-17 16:48:35 -07003779</p>
3780
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003781<pre class="ebnf">
Rob Pikef3a33bc2009-09-14 17:39:17 -07003782TypeSwitchStmt = "switch" [ SimpleStmt ";" ] TypeSwitchGuard "{" { TypeCaseClause } "}" .
Russ Coxc1045db2009-12-23 13:48:44 -08003783TypeSwitchGuard = [ identifier ":=" ] PrimaryExpr "." "(" "type" ")" .
Robert Griesemer130ac742009-12-10 16:43:01 -08003784TypeCaseClause = TypeSwitchCase ":" { Statement ";" } .
Rob Pike678625d2009-09-15 09:54:22 -07003785TypeSwitchCase = "case" TypeList | "default" .
3786TypeList = Type { "," Type } .
Rob Pike5a578492009-03-17 16:48:35 -07003787</pre>
3788
3789<p>
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003790The TypeSwitchGuard may include a
3791<a href="#Short_variable_declarations">short variable declaration</a>.
3792When that form is used, the variable is declared in each clause.
3793In clauses with a case listing exactly one type, the variable
3794has that type; otherwise, the variable has the type of the expression
3795in the TypeSwitchGuard.
Rob Pike94b67eb2009-03-24 17:40:47 -07003796</p>
3797
3798<p>
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003799The type in a case may be <code>nil</code>
3800<a href="#Predeclared_identifiers">Predeclared identifiers</a>);
3801that case is used when the expression in the TypeSwitchGuard
Robert Griesemer1f95f0d2009-08-27 16:44:17 -07003802is a <code>nil</code> interface value.
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003803</p>
3804
3805<p>
Robert Griesemere9192752009-12-01 16:15:53 -08003806Given an expression <code>x</code> of type <code>interface{}</code>,
Rob Pike70c1a102009-03-18 19:23:59 -07003807the following type switch:
Rob Pike5a578492009-03-17 16:48:35 -07003808</p>
3809
3810<pre>
Robert Griesemere9192752009-12-01 16:15:53 -08003811switch i := x.(type) {
Rob Pike94b67eb2009-03-24 17:40:47 -07003812case nil:
Robert Griesemer130ac742009-12-10 16:43:01 -08003813 printString("x is nil")
Rob Pike5a578492009-03-17 16:48:35 -07003814case int:
Robert Griesemer130ac742009-12-10 16:43:01 -08003815 printInt(i) // i is an int
Rob Pike5a578492009-03-17 16:48:35 -07003816case float:
Robert Griesemer130ac742009-12-10 16:43:01 -08003817 printFloat(i) // i is a float
Rob Pike70c1a102009-03-18 19:23:59 -07003818case func(int) float:
Robert Griesemer130ac742009-12-10 16:43:01 -08003819 printFunction(i) // i is a function
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003820case bool, string:
Robert Griesemer130ac742009-12-10 16:43:01 -08003821 printString("type is bool or string") // i is an interface{}
Rob Pike5a578492009-03-17 16:48:35 -07003822default:
Robert Griesemer130ac742009-12-10 16:43:01 -08003823 printString("don't know the type")
Rob Pike5a578492009-03-17 16:48:35 -07003824}
3825</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003826
Rob Pike70c1a102009-03-18 19:23:59 -07003827<p>
3828could be rewritten:
3829</p>
3830
3831<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08003832v := x // x is evaluated exactly once
Rob Pike94b67eb2009-03-24 17:40:47 -07003833if v == nil {
Robert Griesemer130ac742009-12-10 16:43:01 -08003834 printString("x is nil")
Rob Pike94b67eb2009-03-24 17:40:47 -07003835} else if i, is_int := v.(int); is_int {
Robert Griesemer130ac742009-12-10 16:43:01 -08003836 printInt(i) // i is an int
Rob Pike70c1a102009-03-18 19:23:59 -07003837} else if i, is_float := v.(float); is_float {
Robert Griesemer130ac742009-12-10 16:43:01 -08003838 printFloat(i) // i is a float
Rob Pike70c1a102009-03-18 19:23:59 -07003839} else if i, is_func := v.(func(int) float); is_func {
Robert Griesemer130ac742009-12-10 16:43:01 -08003840 printFunction(i) // i is a function
Rob Pike70c1a102009-03-18 19:23:59 -07003841} else {
Robert Griesemer130ac742009-12-10 16:43:01 -08003842 i1, is_bool := v.(bool)
3843 i2, is_string := v.(string)
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003844 if is_bool || is_string {
Robert Griesemer130ac742009-12-10 16:43:01 -08003845 i := v
3846 printString("type is bool or string") // i is an interface{}
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003847 } else {
Robert Griesemer130ac742009-12-10 16:43:01 -08003848 i := v
3849 printString("don't know the type") // i is an interface{}
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003850 }
Rob Pike70c1a102009-03-18 19:23:59 -07003851}
3852</pre>
3853
Robert Griesemeraeaab592009-08-31 17:30:55 -07003854<p>
Russ Cox16b95ba2009-08-20 10:22:52 -07003855The type switch guard may be preceded by a simple statement, which
3856executes before the guard is evaluated.
Robert Griesemer1f95f0d2009-08-27 16:44:17 -07003857</p>
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003858
3859<p>
3860The "fallthrough" statement is not permitted in a type switch.
Russ Cox16b95ba2009-08-20 10:22:52 -07003861</p>
3862
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003863<h3 id="For_statements">For statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003864
Rob Pike96750f12009-02-27 16:47:48 -08003865<p>
3866A "for" statement specifies repeated execution of a block. The iteration is
3867controlled by a condition, a "for" clause, or a "range" clause.
3868</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003869
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003870<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003871ForStmt = "for" [ Condition | ForClause | RangeClause ] Block .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003872Condition = Expression .
3873</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003874
Rob Pike96750f12009-02-27 16:47:48 -08003875<p>
3876In its simplest form, a "for" statement specifies the repeated execution of
3877a block as long as a boolean condition evaluates to true.
3878The condition is evaluated before each iteration.
3879If the condition is absent, it is equivalent to <code>true</code>.
3880</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003881
Robert Griesemerc2d55862009-02-19 16:49:10 -08003882<pre>
3883for a &lt; b {
3884 a *= 2
3885}
3886</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003887
Rob Pike96750f12009-02-27 16:47:48 -08003888<p>
Rob Pike678625d2009-09-15 09:54:22 -07003889A "for" statement with a ForClause is also controlled by its condition, but
Rob Pike96750f12009-02-27 16:47:48 -08003890additionally it may specify an <i>init</i>
3891and a <i>post</i> statement, such as an assignment,
Russ Cox16b95ba2009-08-20 10:22:52 -07003892an increment or decrement statement. The init statement may be a
Robert Griesemer4ed666e2009-08-27 16:45:42 -07003893<a href="#Short_variable_declarations">short variable declaration</a>, but the post statement must not.
Rob Pike96750f12009-02-27 16:47:48 -08003894</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003895
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003896<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -08003897ForClause = [ InitStmt ] ";" [ Condition ] ";" [ PostStmt ] .
Rob Pike11417162009-03-24 17:45:53 -07003898InitStmt = SimpleStmt .
3899PostStmt = SimpleStmt .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003900</pre>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003901
Robert Griesemerc2d55862009-02-19 16:49:10 -08003902<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08003903for i := 0; i &lt; 10; i++ {
Robert Griesemerc2d55862009-02-19 16:49:10 -08003904 f(i)
3905}
3906</pre>
Russ Cox5958dd62009-03-04 17:19:21 -08003907
Robert Griesemerc2d55862009-02-19 16:49:10 -08003908<p>
Rob Pike96750f12009-02-27 16:47:48 -08003909If non-empty, the init statement is executed once before evaluating the
3910condition for the first iteration;
3911the post statement is executed after each execution of the block (and
3912only if the block was executed).
Robert Griesemer130ac742009-12-10 16:43:01 -08003913Any element of the ForClause may be empty but the
3914<a href="#Semicolons">semicolons</a> are
Rob Pike96750f12009-02-27 16:47:48 -08003915required unless there is only a condition.
3916If the condition is absent, it is equivalent to <code>true</code>.
3917</p>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003918
Robert Griesemerc2d55862009-02-19 16:49:10 -08003919<pre>
Rob Pike678625d2009-09-15 09:54:22 -07003920for cond { S() } is the same as for ; cond ; { S() }
3921for { S() } is the same as for true { S() }
Robert Griesemerc2d55862009-02-19 16:49:10 -08003922</pre>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003923
Rob Pike96750f12009-02-27 16:47:48 -08003924<p>
3925A "for" statement with a "range" clause
Rob Pike7aee71b2009-04-15 20:28:25 -07003926iterates through all entries of an array, slice, string or map,
3927or values received on a channel.
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003928For each entry it first assigns the current index or key to an iteration
3929variable - or the current (index, element) or (key, value) pair to a pair
Rob Pike96750f12009-02-27 16:47:48 -08003930of iteration variables - and then executes the block.
3931</p>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003932
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003933<pre class="ebnf">
Rob Pikeb3408792009-04-15 20:51:17 -07003934RangeClause = ExpressionList ( "=" | ":=" ) "range" Expression .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003935</pre>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003936
Robert Griesemerc2d55862009-02-19 16:49:10 -08003937<p>
Rob Pike7aee71b2009-04-15 20:28:25 -07003938The type of the right-hand expression in the "range" clause must be an
Rob Pike678625d2009-09-15 09:54:22 -07003939array, slice, string or map, or a pointer to an array;
Rob Pike94b67eb2009-03-24 17:40:47 -07003940or it may be a channel.
Rob Pike7aee71b2009-04-15 20:28:25 -07003941Except for channels,
Rob Pikeb3408792009-04-15 20:51:17 -07003942the identifier list must contain one or two expressions
3943(as in assignments, these must be a
3944variable, pointer indirection, field selector, or index expression)
3945denoting the
Rob Pike96750f12009-02-27 16:47:48 -08003946iteration variables. On each iteration,
Rob Pike7aee71b2009-04-15 20:28:25 -07003947the first variable is set to the string, array or slice index or
Rob Pike96750f12009-02-27 16:47:48 -08003948map key, and the second variable, if present, is set to the corresponding
Rob Pike7aee71b2009-04-15 20:28:25 -07003949string or array element or map value.
Rob Pike96750f12009-02-27 16:47:48 -08003950The types of the array or slice index (always <code>int</code>)
3951and element, or of the map key and value respectively,
Robert Griesemer326ef132009-09-28 19:21:15 -07003952must be <a href="#Assignment_compatibility">assignment compatible</a> with
Ian Lance Taylorae13f432010-01-13 12:50:45 -08003953the type of the iteration variables. The expression on the right hand
3954side is evaluated once before beginning the loop. At each iteration
3955of the loop, the values produced by the range clause are assigned to
3956the left hand side as in an <a href="#Assignments">assignment
3957statement</a>. Function calls on the left hand side will be evaluated
3958exactly once per iteration.
Rob Pike96750f12009-02-27 16:47:48 -08003959</p>
3960<p>
Rob Pike1811fac2010-02-17 11:26:09 +11003961For a value of a string type, the "range" clause iterates over the Unicode code points
Rob Pike7aee71b2009-04-15 20:28:25 -07003962in the string. On successive iterations, the index variable will be the
Rob Pike678625d2009-09-15 09:54:22 -07003963index of the first byte of successive UTF-8-encoded code points in the string, and
Rob Pike7aee71b2009-04-15 20:28:25 -07003964the second variable, of type <code>int</code>, will be the value of
3965the corresponding code point. If the iteration encounters an invalid
3966UTF-8 sequence, the second variable will be <code>0xFFFD</code>,
3967the Unicode replacement character, and the next iteration will advance
3968a single byte in the string.
3969</p>
3970<p>
Rob Pike94b67eb2009-03-24 17:40:47 -07003971For channels, the identifier list must contain one identifier.
Robert Griesemerce9fbdb2009-04-29 11:45:08 -07003972The iteration receives values sent on the channel until the channel is closed;
Rob Pike94b67eb2009-03-24 17:40:47 -07003973it does not process the zero value sent before the channel is closed.
3974</p>
3975<p>
Rob Pike96750f12009-02-27 16:47:48 -08003976The iteration variables may be declared by the "range" clause (":="), in which
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003977case their scope ends at the end of the "for" statement (§<a href="#Declarations_and">Declarations and</a>
Rob Pike96750f12009-02-27 16:47:48 -08003978scope rules). In this case their types are set to
Russ Cox5958dd62009-03-04 17:19:21 -08003979<code>int</code> and the array element type, or the map key and value types, respectively.
Rob Pike96750f12009-02-27 16:47:48 -08003980If the iteration variables are declared outside the "for" statement,
3981after execution their values will be those of the last iteration.
3982</p>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003983
Robert Griesemerc2d55862009-02-19 16:49:10 -08003984<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08003985var a [10]string
3986m := map[string]int{"mon":0, "tue":1, "wed":2, "thu":3, "fri":4, "sat":5, "sun":6}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003987
Robert Griesemerc2d55862009-02-19 16:49:10 -08003988for i, s := range a {
3989 // type of i is int
3990 // type of s is string
3991 // s == a[i]
3992 g(i, s)
3993}
3994
Robert Griesemer130ac742009-12-10 16:43:01 -08003995var key string
3996var val interface {} // value type of m is assignment compatible with val
Rob Pike678625d2009-09-15 09:54:22 -07003997for key, val = range m {
3998 h(key, val)
Robert Griesemerc2d55862009-02-19 16:49:10 -08003999}
4000// key == last map key encountered in iteration
4001// val == map[key]
4002</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004003
Rob Pike96750f12009-02-27 16:47:48 -08004004<p>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08004005If map entries that have not yet been processed are deleted during iteration,
4006they will not be processed. If map entries are inserted during iteration, the
Russ Cox5958dd62009-03-04 17:19:21 -08004007behavior is implementation-dependent, but each entry will be processed at most once.
Rob Pike96750f12009-02-27 16:47:48 -08004008</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004009
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004010<h3 id="Go_statements">Go statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004011
Rob Pike96750f12009-02-27 16:47:48 -08004012<p>
Russ Cox5958dd62009-03-04 17:19:21 -08004013A "go" statement starts the execution of a function or method call
Rob Pike96750f12009-02-27 16:47:48 -08004014as an independent concurrent thread of control, or <i>goroutine</i>,
4015within the same address space.
4016</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004017
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004018<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07004019GoStmt = "go" Expression .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004020</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004021
Rob Pike96750f12009-02-27 16:47:48 -08004022<p>
4023The expression must be a call, and
4024unlike with a regular call, program execution does not wait
Robert Griesemerb9f8b9c2008-09-26 13:38:38 -07004025for the invoked function to complete.
Rob Pike96750f12009-02-27 16:47:48 -08004026</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004027
Robert Griesemerc2d55862009-02-19 16:49:10 -08004028<pre>
4029go Server()
Robert Griesemere1e76192009-09-25 14:11:03 -07004030go func(ch chan&lt;- bool) { for { sleep(10); ch &lt;- true; }} (c)
Robert Griesemerc2d55862009-02-19 16:49:10 -08004031</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004032
4033
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004034<h3 id="Select_statements">Select statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004035
Rob Pike96750f12009-02-27 16:47:48 -08004036<p>
4037A "select" statement chooses which of a set of possible communications
4038will proceed. It looks similar to a "switch" statement but with the
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004039cases all referring to communication operations.
Rob Pike96750f12009-02-27 16:47:48 -08004040</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004041
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004042<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07004043SelectStmt = "select" "{" { CommClause } "}" .
Robert Griesemer130ac742009-12-10 16:43:01 -08004044CommClause = CommCase ":" { Statement ";" } .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004045CommCase = "case" ( SendExpr | RecvExpr) | "default" .
4046SendExpr = Expression "&lt;-" Expression .
4047RecvExpr = [ Expression ( "=" | ":=" ) ] "&lt;-" Expression .
4048</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004049
Rob Pike96750f12009-02-27 16:47:48 -08004050<p>
Rob Pike96750f12009-02-27 16:47:48 -08004051For all the send and receive expressions in the "select"
Rob Pike9d7538b2009-09-16 11:49:35 -07004052statement, the channel expressions are evaluated, along with
4053any expressions that appear on the right hand side of send expressions,
4054in top-to-bottom order.
4055If any of the resulting operations can proceed, one is
Rob Pike569a1072008-10-03 11:18:45 -07004056chosen and the corresponding communication and statements are
4057evaluated. Otherwise, if there is a default case, that executes;
Rob Pikecd368a22008-10-02 10:37:12 -07004058if not, the statement blocks until one of the communications can
Rob Pike569a1072008-10-03 11:18:45 -07004059complete. The channels and send expressions are not re-evaluated.
Rob Pike96750f12009-02-27 16:47:48 -08004060A channel pointer may be <code>nil</code>,
4061which is equivalent to that case not
4062being present in the select statement
4063except, if a send, its expression is still evaluated.
4064</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004065<p>
Rob Pike569a1072008-10-03 11:18:45 -07004066Since all the channels and send expressions are evaluated, any side
4067effects in that evaluation will occur for all the communications
Rob Pike96750f12009-02-27 16:47:48 -08004068in the "select" statement.
4069</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004070<p>
Rob Pike96750f12009-02-27 16:47:48 -08004071If multiple cases can proceed, a uniform fair choice is made to decide
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004072which single communication will execute.
Robert Griesemerc2d55862009-02-19 16:49:10 -08004073<p>
Robert Griesemer4ed666e2009-08-27 16:45:42 -07004074The receive case may declare a new variable using a
4075<a href="#Short_variable_declarations">short variable declaration</a>.
Rob Pike96750f12009-02-27 16:47:48 -08004076</p>
Robert Griesemer2902a822008-09-17 13:57:11 -07004077
Robert Griesemerc2d55862009-02-19 16:49:10 -08004078<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08004079var c, c1, c2 chan int
4080var i1, i2 int
Robert Griesemerc2d55862009-02-19 16:49:10 -08004081select {
4082case i1 = &lt;-c1:
Robert Griesemer130ac742009-12-10 16:43:01 -08004083 print("received ", i1, " from c1\n")
Robert Griesemerc2d55862009-02-19 16:49:10 -08004084case c2 &lt;- i2:
Robert Griesemer130ac742009-12-10 16:43:01 -08004085 print("sent ", i2, " to c2\n")
Robert Griesemerc2d55862009-02-19 16:49:10 -08004086default:
Robert Griesemer130ac742009-12-10 16:43:01 -08004087 print("no communication\n")
Robert Griesemerc2d55862009-02-19 16:49:10 -08004088}
4089
4090for { // send random sequence of bits to c
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004091 select {
Robert Griesemerc2d55862009-02-19 16:49:10 -08004092 case c &lt;- 0: // note: no statement, no fallthrough, no folding of cases
4093 case c &lt;- 1:
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004094 }
Robert Griesemerc2d55862009-02-19 16:49:10 -08004095}
Robert Griesemerc2d55862009-02-19 16:49:10 -08004096</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004097
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004098
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004099<h3 id="Return_statements">Return statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004100
Rob Pike96750f12009-02-27 16:47:48 -08004101<p>
4102A "return" statement terminates execution of the containing function
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004103and optionally provides a result value or values to the caller.
Rob Pike96750f12009-02-27 16:47:48 -08004104</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004105
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004106<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07004107ReturnStmt = "return" [ ExpressionList ] .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004108</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004109
Robert Griesemer4b908332009-08-07 17:05:41 -07004110<p>
4111In a function without a result type, a "return" statement must not
4112specify any result values.
4113</p>
Rob Pike96750f12009-02-27 16:47:48 -08004114<pre>
Robert Griesemer4b908332009-08-07 17:05:41 -07004115func no_result() {
Rob Pike96750f12009-02-27 16:47:48 -08004116 return
4117}
4118</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004119
Rob Pike96750f12009-02-27 16:47:48 -08004120<p>
Robert Griesemer4b908332009-08-07 17:05:41 -07004121There are three ways to return values from a function with a result
4122type:
Rob Pike96750f12009-02-27 16:47:48 -08004123</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004124
Robert Griesemer4b908332009-08-07 17:05:41 -07004125<ol>
4126 <li>The return value or values may be explicitly listed
4127 in the "return" statement. Each expression must be single-valued
Robert Griesemer326ef132009-09-28 19:21:15 -07004128 and <a href="#Assignment_compatibility">assignment compatible</a>
4129 with the type of the corresponding element of the function's
4130 result type.
Robert Griesemerc2d55862009-02-19 16:49:10 -08004131<pre>
4132func simple_f() int {
Rob Pike96750f12009-02-27 16:47:48 -08004133 return 2
Robert Griesemerc2d55862009-02-19 16:49:10 -08004134}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004135
Robert Griesemerc2d55862009-02-19 16:49:10 -08004136func complex_f1() (re float, im float) {
Rob Pike96750f12009-02-27 16:47:48 -08004137 return -7.0, -4.0
Robert Griesemerc2d55862009-02-19 16:49:10 -08004138}
4139</pre>
Robert Griesemer4b908332009-08-07 17:05:41 -07004140 </li>
4141 <li>The expression list in the "return" statement may be a single
4142 call to a multi-valued function. The effect is as if each value
4143 returned from that function were assigned to a temporary
4144 variable with the type of the respective value, followed by a
4145 "return" statement listing these variables, at which point the
4146 rules of the previous case apply.
Robert Griesemerc2d55862009-02-19 16:49:10 -08004147<pre>
4148func complex_f2() (re float, im float) {
Rob Pike96750f12009-02-27 16:47:48 -08004149 return complex_f1()
4150}
4151</pre>
Robert Griesemer4b908332009-08-07 17:05:41 -07004152 </li>
4153 <li>The expression list may be empty if the functions's result
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004154 type specifies names for its result parameters (§<a href="#Function_Types">Function Types</a>).
Robert Griesemer4b908332009-08-07 17:05:41 -07004155 The result parameters act as ordinary local variables that are
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004156 initialized to the zero values for their type (§<a href="#The_zero_value">The zero value</a>)
Robert Griesemer4b908332009-08-07 17:05:41 -07004157 and the function may assign values to them as necessary.
4158 The "return" statement returns the values of these variables.
Rob Pike96750f12009-02-27 16:47:48 -08004159<pre>
4160func complex_f3() (re float, im float) {
Robert Griesemer130ac742009-12-10 16:43:01 -08004161 re = 7.0
4162 im = 4.0
4163 return
Robert Griesemerc2d55862009-02-19 16:49:10 -08004164}
4165</pre>
Robert Griesemer4b908332009-08-07 17:05:41 -07004166 </li>
4167</ol>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004168
Rob Pike0b4de7a2009-11-09 16:09:57 -08004169<!---
Russ Cox5958dd62009-03-04 17:19:21 -08004170<p>
Robert Griesemer90cc4a52009-10-22 09:41:38 -07004171<span class="alert">
Robert Griesemer4b908332009-08-07 17:05:41 -07004172TODO: Define when return is required.<br />
4173TODO: Language about result parameters needs to go into a section on
4174 function/method invocation<br />
Robert Griesemer90cc4a52009-10-22 09:41:38 -07004175</span>
Russ Cox5958dd62009-03-04 17:19:21 -08004176</p>
Rob Pike0b4de7a2009-11-09 16:09:57 -08004177--->
Russ Cox5958dd62009-03-04 17:19:21 -08004178
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004179<h3 id="Break_statements">Break statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004180
Rob Pike96750f12009-02-27 16:47:48 -08004181<p>
4182A "break" statement terminates execution of the innermost
4183"for", "switch" or "select" statement.
4184</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004185
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004186<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -08004187BreakStmt = "break" [ Label ] .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004188</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004189
Rob Pike96750f12009-02-27 16:47:48 -08004190<p>
4191If there is a label, it must be that of an enclosing
4192"for", "switch" or "select" statement, and that is the one whose execution
4193terminates
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004194<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 -08004195</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004196
Robert Griesemerc2d55862009-02-19 16:49:10 -08004197<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08004198L: for i &lt; n {
Robert Griesemerc2d55862009-02-19 16:49:10 -08004199 switch i {
Rob Pike96750f12009-02-27 16:47:48 -08004200 case 5: break L
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004201 }
Robert Griesemerc2d55862009-02-19 16:49:10 -08004202}
4203</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004204
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004205<h3 id="Continue_statements">Continue statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004206
Rob Pike96750f12009-02-27 16:47:48 -08004207<p>
4208A "continue" statement begins the next iteration of the
Rob Pike678625d2009-09-15 09:54:22 -07004209innermost "for" loop at its post statement (§<a href="#For_statements">For statements</a>).
Rob Pike96750f12009-02-27 16:47:48 -08004210</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004211
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004212<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -08004213ContinueStmt = "continue" [ Label ] .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004214</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004215
Rob Pike96750f12009-02-27 16:47:48 -08004216<p>
4217The optional label is analogous to that of a "break" statement.
4218</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004219
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004220<h3 id="Goto_statements">Goto statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004221
Rob Pike96750f12009-02-27 16:47:48 -08004222<p>
4223A "goto" statement transfers control to the statement with the corresponding label.
4224</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004225
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004226<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07004227GotoStmt = "goto" Label .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004228</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004229
Robert Griesemerc2d55862009-02-19 16:49:10 -08004230<pre>
4231goto Error
4232</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004233
Rob Pike96750f12009-02-27 16:47:48 -08004234<p>
4235Executing the "goto" statement must not cause any variables to come into
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004236scope that were not already in scope at the point of the goto. For
4237instance, this example:
Rob Pike96750f12009-02-27 16:47:48 -08004238</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004239
Robert Griesemerc2d55862009-02-19 16:49:10 -08004240<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08004241goto L // BAD
4242v := 3
Robert Griesemerc2d55862009-02-19 16:49:10 -08004243L:
4244</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004245
Rob Pike96750f12009-02-27 16:47:48 -08004246<p>
4247is erroneous because the jump to label <code>L</code> skips
4248the creation of <code>v</code>.
Rob Pike0b4de7a2009-11-09 16:09:57 -08004249<!---
Robert Griesemer90cc4a52009-10-22 09:41:38 -07004250(<span class="alert">TODO: Eliminate in favor of used and not set errors?</span>)
Rob Pike0b4de7a2009-11-09 16:09:57 -08004251--->
Rob Pike96750f12009-02-27 16:47:48 -08004252</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004253
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004254<h3 id="Fallthrough_statements">Fallthrough statements</h3>
Robert Griesemeraed247f2008-10-08 17:05:30 -07004255
Rob Pike96750f12009-02-27 16:47:48 -08004256<p>
4257A "fallthrough" statement transfers control to the first statement of the
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004258next case clause in a expression "switch" statement (§<a href="#Expression_switches">Expression switches</a>). It may
Robert Griesemer091cba82009-03-19 08:39:40 -07004259be used only as the final non-empty statement in a case or default clause in an
4260expression "switch" statement.
Rob Pike96750f12009-02-27 16:47:48 -08004261</p>
Robert Griesemeraed247f2008-10-08 17:05:30 -07004262
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004263<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07004264FallthroughStmt = "fallthrough" .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004265</pre>
Robert Griesemeraed247f2008-10-08 17:05:30 -07004266
4267
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004268<h3 id="Defer_statements">Defer statements</h3>
Robert Griesemer4a903e02009-01-27 09:29:40 -08004269
Rob Pike96750f12009-02-27 16:47:48 -08004270<p>
4271A "defer" statement invokes a function whose execution is deferred to the moment
4272the surrounding function returns.
4273</p>
Robert Griesemer4a903e02009-01-27 09:29:40 -08004274
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004275<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07004276DeferStmt = "defer" Expression .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004277</pre>
Robert Griesemer4a903e02009-01-27 09:29:40 -08004278
Rob Pike96750f12009-02-27 16:47:48 -08004279<p>
4280The expression must be a function or method call.
4281Each time the "defer" statement
Robert Griesemer7471eab2009-01-27 14:51:24 -08004282executes, the parameters to the function call are evaluated and saved anew but the
Rob Pike678625d2009-09-15 09:54:22 -07004283function is not invoked.
4284Deferred function calls are executed in LIFO order
4285immediately before the surrounding function returns,
Robert Griesemer48f0cd22010-03-23 17:30:14 -07004286after the return values, if any, have been evaluated, but before they
4287are returned to the caller. For instance, if the deferred function is
Rob Pike5bb29fb2010-03-25 17:59:59 -07004288a <a href="#Function_literals">function literal</a> and the surrounding
Robert Griesemer48f0cd22010-03-23 17:30:14 -07004289function has <a href="#Function_types">named result parameters</a> that
4290are in scope within the literal, the deferred function may access and modify
4291the result parameters before they are returned.
Rob Pike96750f12009-02-27 16:47:48 -08004292</p>
Robert Griesemer4a903e02009-01-27 09:29:40 -08004293
Robert Griesemerc2d55862009-02-19 16:49:10 -08004294<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08004295lock(l)
4296defer unlock(l) // unlocking happens before surrounding function returns
Robert Griesemer4a903e02009-01-27 09:29:40 -08004297
Robert Griesemerc2d55862009-02-19 16:49:10 -08004298// prints 3 2 1 0 before surrounding function returns
4299for i := 0; i &lt;= 3; i++ {
Robert Griesemer130ac742009-12-10 16:43:01 -08004300 defer fmt.Print(i)
Robert Griesemerc2d55862009-02-19 16:49:10 -08004301}
Robert Griesemer48f0cd22010-03-23 17:30:14 -07004302
4303// f returns 1
4304func f() (result int) {
4305 defer func() {
4306 result++
4307 }()
4308 return 0
4309}
Robert Griesemerc2d55862009-02-19 16:49:10 -08004310</pre>
Robert Griesemer4a903e02009-01-27 09:29:40 -08004311
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004312<h2 id="Built-in_functions">Built-in functions</h2>
4313
4314<p>
Rob Pike72970872010-03-04 12:35:16 -08004315Built-in functions are
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004316<a href="#Predeclared_identifiers">predeclared</a>.
4317They are called like any other function but some of them
4318accept a type instead of an expression as the first argument.
4319</p>
4320
Russ Cox2a5f0c62009-12-04 10:23:12 -08004321<p>
4322The built-in functions do not have standard Go types,
4323so they can only appear in <a href="#Calls">call expressions</a>;
4324they cannot be used as function values.
4325</p>
4326
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004327<pre class="ebnf">
4328BuiltinCall = identifier "(" [ BuiltinArgs ] ")" .
4329BuiltinArgs = Type [ "," ExpressionList ] | ExpressionList .
4330</pre>
4331
4332<h3 id="Close_and_closed">Close and closed</h3>
4333
4334<p>
4335For a channel <code>c</code>, the predefined function <code>close(c)</code>
4336marks the channel as unable to accept more
4337values through a send operation. After any previously
4338sent values have been received, receive operations will return
4339the zero value for the channel's type. After at least one such zero value has been
4340received, <code>closed(c)</code> returns true.
4341</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004342
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004343<h3 id="Length_and_capacity">Length and capacity</h3>
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -07004344
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004345<p>
4346The built-in functions <code>len</code> and <code>cap</code> take arguments
4347of various types and return a result of type <code>int</code>.
4348The implementation guarantees that the result always fits into an <code>int</code>.
4349</p>
4350
Rob Pikeff70f092009-02-20 13:36:14 -08004351<pre class="grammar">
Robert Griesemerd3ffc5e2009-09-03 10:35:09 -07004352Call Argument type Result
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -07004353
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004354len(s) string type string length in bytes
Robert Griesemere9192752009-12-01 16:15:53 -08004355 [n]T, *[n]T array length (== constant n)
Russ Coxfe537952009-08-20 10:47:40 -07004356 []T slice length
Rob Pike678625d2009-09-15 09:54:22 -07004357 map[K]T map length (number of defined keys)
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004358 chan T number of elements queued in channel buffer
Robert Griesemer4dc25282008-09-09 10:37:19 -07004359
Robert Griesemere9192752009-12-01 16:15:53 -08004360cap(s) [n]T, *[n]T array length (== constant n)
Russ Coxfe537952009-08-20 10:47:40 -07004361 []T slice capacity
Rob Pikedf3183f2009-02-26 16:37:23 -08004362 chan T channel buffer capacity
Robert Griesemerc2d55862009-02-19 16:49:10 -08004363</pre>
Robert Griesemer4dc25282008-09-09 10:37:19 -07004364
Robert Griesemerc2d55862009-02-19 16:49:10 -08004365<p>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004366The capacity of a slice is the number of elements for which there is
4367space allocated in the underlying array.
4368At any time the following relationship holds:
4369</p>
Robert Griesemer633957b2009-01-06 13:23:20 -08004370
Robert Griesemerc2d55862009-02-19 16:49:10 -08004371<pre>
43720 <= len(s) <= cap(s)
4373</pre>
Robert Griesemer667ef6c2008-09-10 13:00:32 -07004374
Robert Griesemer4dc25282008-09-09 10:37:19 -07004375
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004376<h3 id="Allocation">Allocation</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004377
Rob Pike96750f12009-02-27 16:47:48 -08004378<p>
4379The built-in function <code>new</code> takes a type <code>T</code> and
4380returns a value of type <code>*T</code>.
Robert Griesemera3294712009-01-05 11:17:26 -08004381The memory is initialized as described in the section on initial values
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004382<a href="#The_zero_value">The zero value</a>).
Rob Pike96750f12009-02-27 16:47:48 -08004383</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004384
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08004385<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08004386new(T)
4387</pre>
Robert Griesemer4dc25282008-09-09 10:37:19 -07004388
Rob Pike96750f12009-02-27 16:47:48 -08004389<p>
Robert Griesemer4dc25282008-09-09 10:37:19 -07004390For instance
Rob Pike96750f12009-02-27 16:47:48 -08004391</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004392
Robert Griesemerc2d55862009-02-19 16:49:10 -08004393<pre>
4394type S struct { a int; b float }
4395new(S)
4396</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004397
Rob Pike96750f12009-02-27 16:47:48 -08004398<p>
4399dynamically allocates memory for a variable of type <code>S</code>,
4400initializes it (<code>a=0</code>, <code>b=0.0</code>),
4401and returns a value of type <code>*S</code> containing the address
4402of the memory.
4403</p>
4404
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004405<h3 id="Making_slices_maps_and_channels">Making slices, maps and channels</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004406
Robert Griesemerc2d55862009-02-19 16:49:10 -08004407<p>
Rob Pike96750f12009-02-27 16:47:48 -08004408Slices, maps and channels are reference types that do not require the
4409extra indirection of an allocation with <code>new</code>.
4410The built-in function <code>make</code> takes a type <code>T</code>,
4411which must be a slice, map or channel type,
4412optionally followed by a type-specific list of expressions.
4413It returns a value of type <code>T</code> (not <code>*T</code>).
Robert Griesemer633957b2009-01-06 13:23:20 -08004414The memory is initialized as described in the section on initial values
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004415<a href="#The_zero_value">The zero value</a>).
Rob Pike96750f12009-02-27 16:47:48 -08004416</p>
Robert Griesemer633957b2009-01-06 13:23:20 -08004417
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08004418<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08004419make(T [, optional list of expressions])
4420</pre>
Robert Griesemer633957b2009-01-06 13:23:20 -08004421
Rob Pike96750f12009-02-27 16:47:48 -08004422<p>
Robert Griesemer633957b2009-01-06 13:23:20 -08004423For instance
Rob Pike96750f12009-02-27 16:47:48 -08004424</p>
Robert Griesemer633957b2009-01-06 13:23:20 -08004425
Robert Griesemerc2d55862009-02-19 16:49:10 -08004426<pre>
4427make(map[string] int)
4428</pre>
Robert Griesemer633957b2009-01-06 13:23:20 -08004429
Rob Pike96750f12009-02-27 16:47:48 -08004430<p>
Robert Griesemer633957b2009-01-06 13:23:20 -08004431creates a new map value and initializes it to an empty map.
Rob Pike96750f12009-02-27 16:47:48 -08004432</p>
Robert Griesemer633957b2009-01-06 13:23:20 -08004433
Rob Pike96750f12009-02-27 16:47:48 -08004434<p>
4435The parameters affect sizes for allocating slices, maps, and
Robert Griesemer633957b2009-01-06 13:23:20 -08004436buffered channels:
Rob Pike96750f12009-02-27 16:47:48 -08004437</p>
Robert Griesemer633957b2009-01-06 13:23:20 -08004438
Robert Griesemerc2d55862009-02-19 16:49:10 -08004439<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08004440s := make([]int, 10, 100) // slice with len(s) == 10, cap(s) == 100
4441s := make([]int, 10) // slice with len(s) == cap(s) == 10
4442c := make(chan int, 10) // channel with a buffer size of 10
4443m := make(map[string] int, 100) // map with initial space for 100 elements
Robert Griesemerc2d55862009-02-19 16:49:10 -08004444</pre>
Robert Griesemer633957b2009-01-06 13:23:20 -08004445
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004446
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08004447<h3 id="Copying_slices">Copying slices</h3>
4448
4449<p>
4450The built-in function <code>copy</code> copies array or slice elements from
4451a source <code>src</code> to a destination <code>dst</code> and returns the
4452number of elements copied. Source and destination may overlap.
4453Both arguments must have the same element type <code>T</code> and must be
4454<a href="#Assignment_compatibility">assignment compatible</a> to a slice
4455of type <code>[]T</code>. The number of arguments copied is the minimum of
4456<code>len(src)</code> and <code>len(dst)</code>.
4457</p>
4458
4459<pre class="grammar">
4460copy(dst, src []T) int
4461</pre>
4462
4463<p>
4464Examples:
4465</p>
4466
4467<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08004468var a = [...]int{0, 1, 2, 3, 4, 5, 6, 7}
4469var s = make([]int, 6)
4470n1 := copy(s, &amp;a) // n1 == 6, s == []int{0, 1, 2, 3, 4, 5}
4471n2 := copy(s, s[2:]) // n2 == 4, s == []int{2, 3, 4, 5, 4, 5}
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08004472</pre>
4473
Rob Pike72970872010-03-04 12:35:16 -08004474<h3 id="Complex_numbers">Assembling and disassembling complex numbers</h3>
4475
4476<p>
4477Three functions assemble and disassemble complex numbers.
4478The built-in function <code>cmplx</code> constructs a complex
4479value from a floating-point real and imaginary part, while
4480<code>real</code> and <code>imag</code>
4481extract the real and imaginary parts of a complex value.
4482</p>
4483
4484<pre class="grammar">
4485cmplx(realPart, imaginaryPart floatT) complexT
4486real(complexT) floatT
4487imag(complexT) floatT
4488</pre>
4489
4490<p>
4491The type of the arguments and return value correspond.
4492For <code>cmplx</code>, the two arguments must be of the same
4493floating-point type and the return type is the complex type
4494with the corresponding floating-point constituents:
4495<code>complex</code> for <code>float</code>,
4496<code>complex64</code> for <code>float32</code>,
4497<code>complex128</code> for <code>float64</code>.
4498The <code>real</code> and <code>imag</code> functions
4499together form the inverse, so for a complex value <code>z</code>,
4500<code>z</code> <code>==</code> <code>cmplx(real(z),</code> <code>imag(z))</code>.
4501</p>
4502
4503<p>
4504If the operands of these functions are all constants, the return
4505value is a constant.
4506</p>
4507
4508<pre>
4509var a = cmplx(2, -2) // has type complex
4510var b = cmplx(1.0, -1.4) // has type complex
4511x := float32(math.Cos(math.Pi/2))
4512var c64 = cmplx(5, -x) // has type complex64
4513var im = imag(b) // has type float
4514var rl = real(c64) // type float32
4515</pre>
4516
Rob Pike5bb29fb2010-03-25 17:59:59 -07004517<h3 id="Handling_panics">Handling panics</h3>
4518
4519<p> Two built-in functions, <code>panic</code> and <code>recover</code>,
4520assist in reporting and handling <a href="#Run_time_panics">run-time panics</a>
4521and program-defined error conditions.
4522</p>
4523
4524<pre class="grammar">
4525func panic(interface{})
4526func recover() interface{}
4527</pre>
4528
4529<p>
4530<font color=red>TODO: Most of this text could move to the respective
4531comments in <code>runtime.go</code> once the functions are implemented.
4532They are here, at least for now, for reference and discussion.
4533</font>
4534</p>
4535
4536<p>
4537When a function <code>F</code> calls <code>panic</code>, normal
4538execution of <code>F</code> stops immediately. Any functions whose
4539execution was <a href="#Defer_statements">deferred</a> by the
4540invocation of <code>F</code> are run in the usual way, and then
4541<code>F</code> returns to its caller. To the caller, <code>F</code>
4542then behaves like a call to <code>panic</code>, terminating its own
4543execution and running deferred functions. This continues until all
4544functions in the goroutine have ceased execution, in reverse order.
4545At that point, the program is
4546terminated and the error condition is reported, including the value of
4547the argument to <code>panic</code>. This termination sequence is
4548called <i>panicking</i>.
4549</p>
4550
4551<p>
4552The <code>recover</code> function allows a program to manage behavior
4553of a panicking goroutine. Executing a <code>recover</code> call
4554inside a deferred function (but not any function called by it) stops
4555the panicking sequence by restoring normal execution, and retrieves
4556the error value passed to the call of <code>panic</code>. If
4557<code>recover</code> is called outside the deferred function it will
4558not stop a panicking sequence. In this case, and when the goroutine
4559is not panicking, <code>recover</code> returns <code>nil</code>.
4560</p>
4561
4562<p>
4563If the function defined here,
4564</p>
4565
4566<pre>
4567func f(hideErrors bool) {
4568 defer func() {
4569 if x := recover(); x != nil {
4570 println("panicking with value", v)
4571 if !hideErrors {
4572 panic(x) // go back to panicking
4573 }
4574 }
4575 println("function returns normally") // executes only when hideErrors==true
4576 }()
4577 println("before")
4578 p()
4579 println("after") // never executes
4580}
4581
4582func p() {
4583 panic(3)
4584}
4585</pre>
4586
4587<p>
4588is called with <code>hideErrors=true</code>, it prints
4589</p>
4590
4591<pre>
4592before
4593panicking with value 3
4594function returns normally
4595</pre>
4596
4597<p>
4598and resumes normal execution in the function that called <code>f</code>. Otherwise, it prints
4599</p>
4600
4601<pre>
4602before
4603panicking with value 3
4604</pre>
4605
4606<p>
4607and, absent further <code>recover</code> calls, terminates the program.
4608</p>
4609
4610<p>
4611Since deferred functions run before assigning the return values to the caller
4612of the deferring function, a deferred invocation of a function literal may modify the
4613invoking function's return values in the event of a panic. This permits a function to protect its
4614caller from panics that occur in functions it calls.
4615</p>
4616
4617<pre>
4618func IsPrintable(s string) (ok bool) {
4619 ok = true
4620 defer func() {
4621 if recover() != nil {
4622 println("input is not printable")
4623 ok = false
4624 }
4625 // Panicking has stopped; execution will resume normally in caller.
4626 // The return value will be true normally, false if a panic occurred.
4627 }
4628 panicIfNotPrintable(s) // will panic if validations fails.
4629}
4630</pre>
4631
4632<!---
4633<p>
4634A deferred function that calls <code>recover</code> will see the
4635argument passed to <code>panic</code>. However, functions called
4636<i>from</i> the deferred function run normally, without behaving as
4637though they are panicking. This allows deferred code to run normally
4638in case recovery is necessary and guarantees that functions that manage
4639their own panics will not fail incorrectly. The function
4640</p>
4641
4642<pre>
4643func g() {
4644 s := ReadString()
4645 defer func() {
4646 if IsPrintable(s) {
4647 println("finished processing", s)
4648 } else {
4649 println("finished processing unprintable string")
4650 }
4651 }()
4652 Analyze(s)
4653}
4654</pre>
4655
4656<p>
4657will not cause <code>IsPrintable</code> to print <code>"input is not printable"</code>
4658due to a <code>panic</code> triggered by the call to <code>Analyze</code>.
4659</p>
4660-->
Robert Griesemer1a8ebcc2009-11-18 19:15:25 -08004661
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004662<h3 id="Bootstrapping">Bootstrapping</h3>
4663
Robert Griesemer5eb36242009-09-16 11:05:14 -07004664<p>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004665Current implementations provide several built-in functions useful during
4666bootstrapping. These functions are documented for completeness but are not
4667guaranteed to stay in the language. They do not return a result.
Robert Griesemer5eb36242009-09-16 11:05:14 -07004668</p>
4669
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004670<pre class="grammar">
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07004671Function Behavior
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004672
4673print prints all arguments; formatting of arguments is implementation-specific
4674println like print but prints spaces between arguments and a newline at the end
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004675</pre>
4676
4677
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004678<h2 id="Packages">Packages</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004679
Rob Pike46596852009-03-02 16:17:29 -08004680<p>
4681Go programs are constructed by linking together <i>packages</i>.
Robert Griesemer326ef132009-09-28 19:21:15 -07004682A package in turn is constructed from one or more source files
4683that together declare constants, types, variables and functions
4684belonging to the package and which are accessible in all files
4685of the same package. Those elements may be
4686<a href="#Exported_identifiers">exported</a> and used in another package.
Rob Pike46596852009-03-02 16:17:29 -08004687</p>
4688
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004689<h3 id="Source_file_organization">Source file organization</h3>
Rob Pike46596852009-03-02 16:17:29 -08004690
4691<p>
4692Each source file consists of a package clause defining the package
4693to which it belongs, followed by a possibly empty set of import
4694declarations that declare packages whose contents it wishes to use,
4695followed by a possibly empty set of declarations of functions,
Robert Griesemer0a162a12009-08-19 16:44:04 -07004696types, variables, and constants.
Rob Pike46596852009-03-02 16:17:29 -08004697</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004698
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004699<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -08004700SourceFile = PackageClause ";" { ImportDecl ";" } { TopLevelDecl ";" } .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004701</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004702
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004703<h3 id="Package_clause">Package clause</h3>
Rob Pike46596852009-03-02 16:17:29 -08004704
Robert Griesemerc2d55862009-02-19 16:49:10 -08004705<p>
Rob Pike46596852009-03-02 16:17:29 -08004706A package clause begins each source file and defines the package
4707to which the file belongs.
4708</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004709
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004710<pre class="ebnf">
Robert Griesemer4e56b332009-09-10 10:14:00 -07004711PackageClause = "package" PackageName .
4712PackageName = identifier .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004713</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004714
Robert Griesemer4e56b332009-09-10 10:14:00 -07004715<p>
4716The PackageName must not be the <a href="#Blank_identifier">blank identifier</a>.
4717</p>
4718
Rob Pike46596852009-03-02 16:17:29 -08004719<pre>
4720package math
4721</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004722
Rob Pike46596852009-03-02 16:17:29 -08004723<p>
4724A set of files sharing the same PackageName form the implementation of a package.
4725An implementation may require that all source files for a package inhabit the same directory.
4726</p>
4727
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004728<h3 id="Import_declarations">Import declarations</h3>
Rob Pike46596852009-03-02 16:17:29 -08004729
4730<p>
Rob Pike3aec2e42009-09-25 17:00:22 -07004731An import declaration states that the source file containing the
4732declaration uses identifiers
4733<a href="#Exported_identifiers">exported</a> by the <i>imported</i>
4734package and enables access to them. The import names an
4735identifier (PackageName) to be used for access and an ImportPath
4736that specifies the package to be imported.
Rob Pike46596852009-03-02 16:17:29 -08004737</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004738
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004739<pre class="ebnf">
Robert Griesemer130ac742009-12-10 16:43:01 -08004740ImportDecl = "import" ( ImportSpec | "(" { ImportSpec ";" } ")" ) .
Robert Griesemer997851e2009-09-25 15:36:25 -07004741ImportSpec = [ "." | PackageName ] ImportPath .
Robert Griesemer130ac742009-12-10 16:43:01 -08004742ImportPath = string_lit .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004743</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004744
Robert Griesemerc2d55862009-02-19 16:49:10 -08004745<p>
Rob Pike3aec2e42009-09-25 17:00:22 -07004746The PackageName is used in <a href="#Qualified_identifiers">qualified identifiers</a>
4747to access the exported identifiers of the package within the importing source file.
4748It is declared in the <a href="#Blocks">file block</a>.
4749If the PackageName is omitted, it defaults to the identifier specified in the
4750<a href="#Package_clauses">package clause</a> of the imported package.
4751If an explicit period (<code>.</code>) appears instead of a name, all the
4752package's exported identifiers will be declared in the current file's
4753file block and can be accessed without a qualifier.
Rob Pike46596852009-03-02 16:17:29 -08004754</p>
Russ Cox16b95ba2009-08-20 10:22:52 -07004755
Robert Griesemerc2d55862009-02-19 16:49:10 -08004756<p>
Rob Pike3aec2e42009-09-25 17:00:22 -07004757The interpretation of the ImportPath is implementation-dependent but
4758it is typically a substring of the full file name of the compiled
4759package and may be relative to a repository of installed packages.
4760</p>
4761
4762<p>
4763Assume we have compiled a package containing the package clause
4764<code>package math</code>, which exports function <code>Sin</code>, and
4765installed the compiled package in the file identified by
Rob Pike46596852009-03-02 16:17:29 -08004766<code>"lib/math"</code>.
Rob Pike3aec2e42009-09-25 17:00:22 -07004767This table illustrates how <code>Sin</code> may be accessed in files
4768that import the package after the
4769various types of import declaration.
Rob Pike46596852009-03-02 16:17:29 -08004770</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004771
Rob Pike46596852009-03-02 16:17:29 -08004772<pre class="grammar">
Robert Griesemer997851e2009-09-25 15:36:25 -07004773Import declaration Local name of Sin
Rob Pike46596852009-03-02 16:17:29 -08004774
Rob Pike46596852009-03-02 16:17:29 -08004775import "lib/math" math.Sin
Robert Griesemer997851e2009-09-25 15:36:25 -07004776import M "lib/math" M.Sin
Rob Pike46596852009-03-02 16:17:29 -08004777import . "lib/math" Sin
4778</pre>
4779
Robert Griesemer997851e2009-09-25 15:36:25 -07004780<p>
Rob Pike3aec2e42009-09-25 17:00:22 -07004781An import declaration declares a dependency relation between
4782the importing and imported package.
Robert Griesemer997851e2009-09-25 15:36:25 -07004783It is illegal for a package to import itself or to import a package without
4784referring to any of its exported identifiers. To import a package solely for
4785its side-effects (initialization), use the <a href="#Blank_identifier">blank</a>
4786identifier as explicit package name:
4787</p>
4788
4789<pre>
4790import _ "lib/math"
4791</pre>
4792
4793
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004794<h3 id="An_example_package">An example package</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004795
Robert Griesemerc2d55862009-02-19 16:49:10 -08004796<p>
Rob Pike46596852009-03-02 16:17:29 -08004797Here is a complete Go package that implements a concurrent prime sieve.
4798</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004799
Robert Griesemerc2d55862009-02-19 16:49:10 -08004800<pre>
4801package main
4802
Rob Pike46596852009-03-02 16:17:29 -08004803import "fmt"
4804
Robert Griesemerc2d55862009-02-19 16:49:10 -08004805// Send the sequence 2, 3, 4, ... to channel 'ch'.
Robert Griesemere1e76192009-09-25 14:11:03 -07004806func generate(ch chan&lt;- int) {
Robert Griesemerc2d55862009-02-19 16:49:10 -08004807 for i := 2; ; i++ {
Robert Griesemer130ac742009-12-10 16:43:01 -08004808 ch &lt;- i // Send 'i' to channel 'ch'.
Robert Griesemerc2d55862009-02-19 16:49:10 -08004809 }
4810}
4811
Fazlul Shahriar330139e2009-11-30 21:23:58 -08004812// Copy the values from channel 'src' to channel 'dst',
Robert Griesemerc2d55862009-02-19 16:49:10 -08004813// removing those divisible by 'prime'.
Robert Griesemere1e76192009-09-25 14:11:03 -07004814func filter(src &lt;-chan int, dst chan&lt;- int, prime int) {
4815 for i := range src { // Loop over values received from 'src'.
4816 if i%prime != 0 {
Robert Griesemer130ac742009-12-10 16:43:01 -08004817 dst &lt;- i // Send 'i' to channel 'dst'.
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004818 }
4819 }
Robert Griesemerc2d55862009-02-19 16:49:10 -08004820}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004821
Robert Griesemerc2d55862009-02-19 16:49:10 -08004822// The prime sieve: Daisy-chain filter processes together.
4823func sieve() {
Robert Griesemer130ac742009-12-10 16:43:01 -08004824 ch := make(chan int) // Create a new channel.
4825 go generate(ch) // Start generate() as a subprocess.
Robert Griesemerc2d55862009-02-19 16:49:10 -08004826 for {
Robert Griesemer130ac742009-12-10 16:43:01 -08004827 prime := &lt;-ch
4828 fmt.Print(prime, "\n")
4829 ch1 := make(chan int)
4830 go filter(ch, ch1, prime)
4831 ch = ch1
Robert Griesemerc2d55862009-02-19 16:49:10 -08004832 }
4833}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004834
Robert Griesemerc2d55862009-02-19 16:49:10 -08004835func main() {
Robert Griesemer130ac742009-12-10 16:43:01 -08004836 sieve()
Robert Griesemerc2d55862009-02-19 16:49:10 -08004837}
4838</pre>
Robert Griesemer67153582008-12-16 14:45:09 -08004839
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004840<h2 id="Program_initialization_and_execution">Program initialization and execution</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004841
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004842<h3 id="The_zero_value">The zero value</h3>
Rob Pike8f2330d2009-02-25 16:20:44 -08004843<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004844When memory is allocated to store a value, either through a declaration
Rob Pike678625d2009-09-15 09:54:22 -07004845or <code>make()</code> or <code>new()</code> call,
4846and no explicit initialization is provided, the memory is
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004847given a default initialization. Each element of such a value is
Robert Griesemeref45e642009-08-21 11:25:00 -07004848set to the <i>zero value</i> for its type: <code>false</code> for booleans,
Rob Pike8f2330d2009-02-25 16:20:44 -08004849<code>0</code> for integers, <code>0.0</code> for floats, <code>""</code>
Robert Griesemer19b1d352009-09-24 19:36:48 -07004850for strings, and <code>nil</code> for pointers, functions, interfaces, slices, channels, and maps.
Rob Pikeff70f092009-02-20 13:36:14 -08004851This initialization is done recursively, so for instance each element of an
Rob Pike8f2330d2009-02-25 16:20:44 -08004852array of structs will have its fields zeroed if no value is specified.
4853</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004854<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004855These two simple declarations are equivalent:
Rob Pike8f2330d2009-02-25 16:20:44 -08004856</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004857
Robert Griesemerc2d55862009-02-19 16:49:10 -08004858<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08004859var i int
4860var i int = 0
Robert Griesemerc2d55862009-02-19 16:49:10 -08004861</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004862
Rob Pike8f2330d2009-02-25 16:20:44 -08004863<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004864After
Rob Pike8f2330d2009-02-25 16:20:44 -08004865</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004866
Robert Griesemerc2d55862009-02-19 16:49:10 -08004867<pre>
Robert Griesemer130ac742009-12-10 16:43:01 -08004868type T struct { i int; f float; next *T }
4869t := new(T)
Robert Griesemerc2d55862009-02-19 16:49:10 -08004870</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004871
Rob Pike8f2330d2009-02-25 16:20:44 -08004872<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004873the following holds:
Rob Pike8f2330d2009-02-25 16:20:44 -08004874</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004875
Robert Griesemerc2d55862009-02-19 16:49:10 -08004876<pre>
4877t.i == 0
4878t.f == 0.0
4879t.next == nil
4880</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004881
Rob Pike96750f12009-02-27 16:47:48 -08004882<p>
4883The same would also be true after
4884</p>
4885
4886<pre>
4887var t T
4888</pre>
4889
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004890<h3 id="Program_execution">Program execution</h3>
Rob Pike8f2330d2009-02-25 16:20:44 -08004891<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004892A package with no imports is initialized by assigning initial values to
Rob Pike8cb91842009-09-15 11:56:39 -07004893all its package-level variables
Rob Pike678625d2009-09-15 09:54:22 -07004894and then calling any
Rob Pike96750f12009-02-27 16:47:48 -08004895package-level function with the name and signature of
4896</p>
4897<pre>
4898func init()
4899</pre>
4900<p>
Rob Pike4fe41922009-11-07 22:00:59 -08004901defined in its source.
4902A package may contain multiple
4903<code>init()</code> functions, even
4904within a single source file; they execute
4905in unspecified order.
Rob Pike8f2330d2009-02-25 16:20:44 -08004906</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004907<p>
Rob Pike8cb91842009-09-15 11:56:39 -07004908Within a package, package-level variables are initialized,
4909and constant values are determined, in
4910data-dependent order: if the initializer of <code>A</code>
4911depends on the value of <code>B</code>, <code>A</code>
4912will be set after <code>B</code>.
4913It is an error if such dependencies form a cycle.
4914Dependency analysis is done lexically: <code>A</code>
4915depends on <code>B</code> if the value of <code>A</code>
4916contains a mention of <code>B</code>, contains a value
4917whose initializer
4918mentions <code>B</code>, or mentions a function that
4919mentions <code>B</code>, recursively.
4920If two items are not interdependent, they will be initialized
4921in the order they appear in the source.
Rob Pike01cadde2009-09-15 15:56:44 -07004922Since the dependency analysis is done per package, it can produce
4923unspecified results if <code>A</code>'s initializer calls a function defined
Rob Pike8cb91842009-09-15 11:56:39 -07004924in another package that refers to <code>B</code>.
4925</p>
4926<p>
Robert Griesemer566e3b22008-09-26 16:41:50 -07004927Initialization code may contain "go" statements, but the functions
Robert Griesemerd8a764c2009-02-06 17:01:10 -08004928they invoke do not begin execution until initialization of the entire
4929program is complete. Therefore, all initialization code is run in a single
Rob Pike96750f12009-02-27 16:47:48 -08004930goroutine.
Rob Pike8f2330d2009-02-25 16:20:44 -08004931</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004932<p>
Rob Pike96750f12009-02-27 16:47:48 -08004933An <code>init()</code> function cannot be referred to from anywhere
4934in a program. In particular, <code>init()</code> cannot be called explicitly,
4935nor can a pointer to <code>init</code> be assigned to a function variable.
Rob Pike8f2330d2009-02-25 16:20:44 -08004936</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004937<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004938If a package has imports, the imported packages are initialized
Robert Griesemer566e3b22008-09-26 16:41:50 -07004939before initializing the package itself. If multiple packages import
Rob Pike96750f12009-02-27 16:47:48 -08004940a package <code>P</code>, <code>P</code> will be initialized only once.
Rob Pike8f2330d2009-02-25 16:20:44 -08004941</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004942<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004943The importing of packages, by construction, guarantees that there can
4944be no cyclic dependencies in initialization.
Rob Pike8f2330d2009-02-25 16:20:44 -08004945</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004946<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004947A complete program, possibly created by linking multiple packages,
Rob Pike96750f12009-02-27 16:47:48 -08004948must have one package called <code>main</code>, with a function
Rob Pike8f2330d2009-02-25 16:20:44 -08004949</p>
Robert Griesemer4dc25282008-09-09 10:37:19 -07004950
Robert Griesemerc2d55862009-02-19 16:49:10 -08004951<pre>
Rob Pike96750f12009-02-27 16:47:48 -08004952func main() { ... }
Robert Griesemerc2d55862009-02-19 16:49:10 -08004953</pre>
Robert Griesemer4dc25282008-09-09 10:37:19 -07004954
Rob Pike8f2330d2009-02-25 16:20:44 -08004955<p>
Rob Pike96750f12009-02-27 16:47:48 -08004956defined.
4957The function <code>main.main()</code> takes no arguments and returns no value.
Rob Pike8f2330d2009-02-25 16:20:44 -08004958</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004959<p>
Rob Pike96750f12009-02-27 16:47:48 -08004960Program execution begins by initializing the <code>main</code> package and then
Rob Pike8f2330d2009-02-25 16:20:44 -08004961invoking <code>main.main()</code>.
4962</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004963<p>
Rob Pike4fe41922009-11-07 22:00:59 -08004964When <code>main.main()</code> returns, the program exits. It does not wait for
4965other (non-<code>main</code>) goroutines to complete.
Rob Pike8f2330d2009-02-25 16:20:44 -08004966</p>
Rob Pike811dd252009-03-04 20:39:39 -08004967<p>
4968Implementation restriction: The compiler assumes package <code>main</code>
Robert Griesemer4023dce2009-08-14 17:41:52 -07004969is not imported by any other package.
Rob Pike811dd252009-03-04 20:39:39 -08004970</p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004971
Rob Pike5bb29fb2010-03-25 17:59:59 -07004972<h2 id="Run_time_errors">Run-time panics</h2>
4973
4974<p>
4975Execution errors such as attempting to index an array out
4976of bounds trigger a <i>run-time panic</i> equivalent to a call of
4977the built-in function <a href="#Handling_panics"><code>panic</code></a>
4978with a value of the implementation-defined interface type <code>runtime.Error</code>.
4979That type defines at least the method
4980<code>String() string</code>. The exact error values that
4981represent distinct run-time error conditions are unspecified,
4982at least for now.
4983</p>
4984
4985<pre>
4986package runtime
4987
4988type Error interface {
4989 String() string
4990 // and perhaps others
4991}
4992</pre>
4993
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004994<h2 id="System_considerations">System considerations</h2>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004995
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004996<h3 id="Package_unsafe">Package <code>unsafe</code></h3>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004997
Robert Griesemerc2d55862009-02-19 16:49:10 -08004998<p>
Rob Pike96750f12009-02-27 16:47:48 -08004999The built-in package <code>unsafe</code>, known to the compiler,
5000provides facilities for low-level programming including operations
5001that violate the type system. A package using <code>unsafe</code>
5002must be vetted manually for type safety. The package provides the
5003following interface:
Rob Pikef27e9f02009-02-23 19:22:05 -08005004</p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08005005
Rob Pikeff70f092009-02-20 13:36:14 -08005006<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08005007package unsafe
Robert Griesemer52c02c22009-02-11 13:46:30 -08005008
Robert Griesemer98b4f6a2009-05-12 21:37:46 -07005009type ArbitraryType int // shorthand for an arbitrary Go type; it is not a real type
5010type Pointer *ArbitraryType
Robert Griesemer52c02c22009-02-11 13:46:30 -08005011
Robert Griesemer98b4f6a2009-05-12 21:37:46 -07005012func Alignof(variable ArbitraryType) int
5013func Offsetof(selector ArbitraryType) int
Robert Griesemer98b4f6a2009-05-12 21:37:46 -07005014func Sizeof(variable ArbitraryType) int
Rob Pike678625d2009-09-15 09:54:22 -07005015
5016func Reflect(val interface {}) (typ runtime.Type, addr uintptr)
5017func Typeof(val interface {}) reflect.Type
5018func Unreflect(typ runtime.Type, addr uintptr) interface{}
Robert Griesemerc2d55862009-02-19 16:49:10 -08005019</pre>
Robert Griesemer52c02c22009-02-11 13:46:30 -08005020
Robert Griesemerc2d55862009-02-19 16:49:10 -08005021<p>
Rob Pikef27e9f02009-02-23 19:22:05 -08005022Any pointer or value of type <code>uintptr</code> can be converted into
5023a <code>Pointer</code> and vice versa.
5024</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08005025<p>
Rob Pikef27e9f02009-02-23 19:22:05 -08005026The function <code>Sizeof</code> takes an expression denoting a
Robert Griesemer4023dce2009-08-14 17:41:52 -07005027variable of any type and returns the size of the variable in bytes.
Rob Pikef27e9f02009-02-23 19:22:05 -08005028</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08005029<p>
Russ Cox7c4f7cc2009-08-20 11:11:03 -07005030The function <code>Offsetof</code> takes a selector (§<a href="#Selectors">Selectors</a>) denoting a struct
Robert Griesemer52c02c22009-02-11 13:46:30 -08005031field of any type and returns the field offset in bytes relative to the
Rob Pike678625d2009-09-15 09:54:22 -07005032struct's address.
5033For a struct <code>s</code> with field <code>f</code>:
Rob Pikef27e9f02009-02-23 19:22:05 -08005034</p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08005035
Robert Griesemerc2d55862009-02-19 16:49:10 -08005036<pre>
Rob Pikef27e9f02009-02-23 19:22:05 -08005037uintptr(unsafe.Pointer(&amp;s)) + uintptr(unsafe.Offsetof(s.f)) == uintptr(unsafe.Pointer(&amp;s.f))
Robert Griesemerc2d55862009-02-19 16:49:10 -08005038</pre>
Robert Griesemer52c02c22009-02-11 13:46:30 -08005039
Robert Griesemerc2d55862009-02-19 16:49:10 -08005040<p>
Rob Pikef27e9f02009-02-23 19:22:05 -08005041Computer architectures may require memory addresses to be <i>aligned</i>;
5042that is, for addresses of a variable to be a multiple of a factor,
5043the variable's type's <i>alignment</i>. The function <code>Alignof</code>
5044takes an expression denoting a variable of any type and returns the
5045alignment of the (type of the) variable in bytes. For a variable
5046<code>x</code>:
5047</p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08005048
Robert Griesemerc2d55862009-02-19 16:49:10 -08005049<pre>
5050uintptr(unsafe.Pointer(&amp;x)) % uintptr(unsafe.Alignof(x)) == 0
5051</pre>
Robert Griesemer533dfd62009-05-13 16:56:00 -07005052
Rob Pikef27e9f02009-02-23 19:22:05 -08005053<p>
5054Calls to <code>Alignof</code>, <code>Offsetof</code>, and
Rob Pike678625d2009-09-15 09:54:22 -07005055<code>Sizeof</code> are compile-time constant expressions of type <code>int</code>.
Rob Pikef27e9f02009-02-23 19:22:05 -08005056</p>
Robert Griesemer98b4f6a2009-05-12 21:37:46 -07005057<p>
Rob Pike678625d2009-09-15 09:54:22 -07005058The functions <code>unsafe.Typeof</code>,
5059<code>unsafe.Reflect</code>,
Russ Cox13dac652009-09-28 14:16:33 -07005060and <code>unsafe.Unreflect</code> allow access at run time to the dynamic
Rob Pike678625d2009-09-15 09:54:22 -07005061types and values stored in interfaces.
5062<code>Typeof</code> returns a representation of
5063<code>val</code>'s
5064dynamic type as a <code>runtime.Type</code>.
5065<code>Reflect</code> allocates a copy of
5066<code>val</code>'s dynamic
5067value and returns both the type and the address of the copy.
5068<code>Unreflect</code> inverts <code>Reflect</code>,
5069creating an
5070interface value from a type and address.
Russ Cox16205a32010-01-18 15:59:14 -08005071The <a href="/pkg/reflect/"><code>reflect</code> package</a> built on these primitives
Rob Pike678625d2009-09-15 09:54:22 -07005072provides a safe, more convenient way to inspect interface values.
Robert Griesemer98b4f6a2009-05-12 21:37:46 -07005073</p>
Robert Griesemer6f8df7a2009-02-11 21:57:15 -08005074
Robert Griesemer52c02c22009-02-11 13:46:30 -08005075
Russ Cox7c4f7cc2009-08-20 11:11:03 -07005076<h3 id="Size_and_alignment_guarantees">Size and alignment guarantees</h3>
Robert Griesemer52c02c22009-02-11 13:46:30 -08005077
Robert Griesemer997851e2009-09-25 15:36:25 -07005078<p>
Russ Cox7c4f7cc2009-08-20 11:11:03 -07005079For the numeric types (§<a href="#Numeric_types">Numeric types</a>), the following sizes are guaranteed:
Robert Griesemer997851e2009-09-25 15:36:25 -07005080</p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08005081
Rob Pikeff70f092009-02-20 13:36:14 -08005082<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08005083type size in bytes
Robert Griesemer52c02c22009-02-11 13:46:30 -08005084
Robert Griesemerc2d55862009-02-19 16:49:10 -08005085byte, uint8, int8 1
5086uint16, int16 2
5087uint32, int32, float32 4
5088uint64, int64, float64 8
5089</pre>
Robert Griesemer52c02c22009-02-11 13:46:30 -08005090
Robert Griesemerc2d55862009-02-19 16:49:10 -08005091<p>
Rob Pikef27e9f02009-02-23 19:22:05 -08005092The following minimal alignment properties are guaranteed:
Rob Pike4501d342009-02-19 17:31:36 -08005093</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08005094<ol>
Rob Pikef27e9f02009-02-23 19:22:05 -08005095<li>For a variable <code>x</code> of any type: <code>1 <= unsafe.Alignof(x) <= unsafe.Maxalign</code>.
Robert Griesemer52c02c22009-02-11 13:46:30 -08005096
Russ Cox5958dd62009-03-04 17:19:21 -08005097<li>For a variable <code>x</code> of numeric type: <code>unsafe.Alignof(x)</code> is the smaller
Rob Pikef27e9f02009-02-23 19:22:05 -08005098 of <code>unsafe.Sizeof(x)</code> and <code>unsafe.Maxalign</code>, but at least 1.
Robert Griesemer52c02c22009-02-11 13:46:30 -08005099
Rob Pikef27e9f02009-02-23 19:22:05 -08005100<li>For a variable <code>x</code> of struct type: <code>unsafe.Alignof(x)</code> is the largest of
5101 all the values <code>unsafe.Alignof(x.f)</code> for each field <code>f</code> of x, but at least 1.
Robert Griesemer52c02c22009-02-11 13:46:30 -08005102
Rob Pikef27e9f02009-02-23 19:22:05 -08005103<li>For a variable <code>x</code> of array type: <code>unsafe.Alignof(x)</code> is the same as
5104 <code>unsafe.Alignof(x[0])</code>, but at least 1.
Robert Griesemerc2d55862009-02-19 16:49:10 -08005105</ol>
Robert Griesemer52c02c22009-02-11 13:46:30 -08005106
Robert Griesemer90cc4a52009-10-22 09:41:38 -07005107<h2 id="Implementation_differences"><span class="alert">Implementation differences - TODO</span></h2>
Robert Griesemer237c8ab2009-09-01 14:07:30 -07005108<ul>
Robert Griesemer90cc4a52009-10-22 09:41:38 -07005109 <li><span class="alert">Implementation does not honor the restriction on goto statements and targets (no intervening declarations).</span></li>
Robert Griesemer48f0cd22010-03-23 17:30:14 -07005110 <li><span class="alert">Method expressions are partially implemented.</span></li>
Rob Pike4fe41922009-11-07 22:00:59 -08005111 <li><span class="alert">Gccgo allows only one init() function per source file.</span></li>
Robert Griesemer48f0cd22010-03-23 17:30:14 -07005112 <li><span class="alert">Deferred functions cannot access the surrounding function's result parameters.</span></li>
Russ Cox97d0e8f2010-03-26 18:01:02 -07005113 <li><span class="alert">Function results are not addressable in gccgo.</span></li>
Rob Pike5bb29fb2010-03-25 17:59:59 -07005114 <li><span class="alert">Recover is not implemented.</span></li>
5115 <li><span class="alert">The implemented version of panic differs from its specification.</span></li>
Robert Griesemer237c8ab2009-09-01 14:07:30 -07005116</ul>