blob: 3f78f63cabb865462c07b464189dc231fbceacbc [file] [log] [blame]
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002
Robert Griesemerc59d2f12008-09-09 10:48:14 -07003<!--
Robert Griesemer40d6bb52009-04-20 15:32:20 -07004Open issues:
Robert Griesemer7471eab2009-01-27 14:51:24 -08005[ ] Semantics of type declaration:
6 - creating a new type (status quo), or only a new type name?
Robert Griesemer40d6bb52009-04-20 15:32:20 -07007 - declaration "type T S" strips methods of S. why/why not?
8 - no mechanism to declare a local type name: type T P.T
Robert Griesemer57b34612008-10-10 12:45:44 -07009
Robert Griesemer3b576a72009-06-17 14:31:33 -070010
Robert Griesemer57b34612008-10-10 12:45:44 -070011Todo's:
Robert Griesemer4b908332009-08-07 17:05:41 -070012[ ] need language about function/method calls and parameter passing rules
Robert Griesemer0a162a12009-08-19 16:44:04 -070013[ ] need to say something about "scope" of selectors?
Robert Griesemer4b908332009-08-07 17:05:41 -070014[ ] clarify what a field name is in struct declarations
15 (struct{T} vs struct {T T} vs struct {t T})
Robert Griesemer7539c852009-07-31 18:05:07 -070016[ ] need explicit language about the result type of operations
17[ ] may want to have some examples for the types of shift operations
Robert Griesemer9f4a27c2009-01-16 15:44:08 -080018[ ] document illegality of package-external tuple assignments to structs
Robert Griesemer6f8df7a2009-02-11 21:57:15 -080019 w/ private fields: P.T(1, 2) illegal since same as P.T(a: 1, b: 2) for
Robert Griesemer9f4a27c2009-01-16 15:44:08 -080020 a T struct { a b int }.
Robert Griesemer40d6bb52009-04-20 15:32:20 -070021[ ] should probably write something about evaluation order of statements even
22 though obvious
Robert Griesemere1b8cb82009-07-16 20:31:41 -070023[ ] specify iteration direction for range clause
24[ ] review language on implicit dereferencing
Robert Griesemer1a304e12009-05-08 10:25:06 -070025[ ] document T.m mechanism to obtain a function from a method
Robert Griesemerc59d2f12008-09-09 10:48:14 -070026-->
27
Robert Griesemer40d6bb52009-04-20 15:32:20 -070028
Russ Cox7c4f7cc2009-08-20 11:11:03 -070029<h2 id="Introduction">Introduction</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -070030
Robert Griesemerc2d55862009-02-19 16:49:10 -080031<p>
Rob Pike4501d342009-02-19 17:31:36 -080032This is a reference manual for the Go programming language. For
33more information and other documents, see <a
34href="/">the Go home page</a>.
35</p>
Robert Griesemer67153582008-12-16 14:45:09 -080036
Robert Griesemerc2d55862009-02-19 16:49:10 -080037<p>
Rob Pike4501d342009-02-19 17:31:36 -080038Go is a general-purpose language designed with systems programming
39in mind. It is strongly typed and garbage-collected, and has explicit
40support for concurrent programming. Programs are constructed from
41<i>packages</i>, whose properties allow efficient management of
42dependencies. The existing implementations use a traditional
43compile/link model to generate executable binaries.
44</p>
45
Robert Griesemerc2d55862009-02-19 16:49:10 -080046<p>
Rob Pike4501d342009-02-19 17:31:36 -080047The grammar is compact and regular, allowing for easy analysis by
48automatic tools such as integrated development environments.
49</p>
Rob Pikeff70f092009-02-20 13:36:14 -080050<hr/>
Russ Cox7c4f7cc2009-08-20 11:11:03 -070051<h2 id="Notation">Notation</h2>
Rob Pike4501d342009-02-19 17:31:36 -080052<p>
Robert Griesemer4d230302008-12-17 15:39:15 -080053The syntax is specified using Extended Backus-Naur Form (EBNF):
Rob Pike4501d342009-02-19 17:31:36 -080054</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -070055
Rob Pikeff70f092009-02-20 13:36:14 -080056<pre class="grammar">
Robert Griesemer88a0c402009-04-23 14:42:21 -070057Production = production_name "=" Expression "." .
Rob Pike4501d342009-02-19 17:31:36 -080058Expression = Alternative { "|" Alternative } .
Robert Griesemerc2d55862009-02-19 16:49:10 -080059Alternative = Term { Term } .
Rob Pike4501d342009-02-19 17:31:36 -080060Term = production_name | token [ "..." token ] | Group | Option | Repetition .
61Group = "(" Expression ")" .
Rob Pikec956e902009-04-14 20:10:49 -070062Option = "[" Expression "]" .
Rob Pike4501d342009-02-19 17:31:36 -080063Repetition = "{" Expression "}" .
Robert Griesemerc2d55862009-02-19 16:49:10 -080064</pre>
Robert Griesemerbbfe3122008-10-09 17:12:09 -070065
Rob Pike4501d342009-02-19 17:31:36 -080066<p>
67Productions are expressions constructed from terms and the following
68operators, in increasing precedence:
69</p>
Rob Pikeff70f092009-02-20 13:36:14 -080070<pre class="grammar">
Rob Pike4501d342009-02-19 17:31:36 -080071| alternation
72() grouping
73[] option (0 or 1 times)
74{} repetition (0 to n times)
Robert Griesemerc2d55862009-02-19 16:49:10 -080075</pre>
Robert Griesemer4d230302008-12-17 15:39:15 -080076
Robert Griesemerc2d55862009-02-19 16:49:10 -080077<p>
Rob Pike4501d342009-02-19 17:31:36 -080078Lower-case production names are used to identify lexical tokens.
79Non-terminals are in CamelCase. Lexical symbols are enclosed in
Robert Griesemerf7ac31362009-07-10 16:06:40 -070080double <code>""</code> or back quotes <code>``</code>.
Rob Pike4501d342009-02-19 17:31:36 -080081</p>
82
Robert Griesemerc2d55862009-02-19 16:49:10 -080083<p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -070084The form <code>a ... b</code> represents the set of characters from
Rob Pikef27e9f02009-02-23 19:22:05 -080085<code>a</code> through <code>b</code> as alternatives.
Rob Pike4501d342009-02-19 17:31:36 -080086</p>
87
Rob Pikeff70f092009-02-20 13:36:14 -080088<hr/>
Robert Griesemer7abfcd92008-10-07 17:14:30 -070089
Russ Cox7c4f7cc2009-08-20 11:11:03 -070090<h2 id="Source_code_representation">Source code representation</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -070091
Robert Griesemerc2d55862009-02-19 16:49:10 -080092<p>
Rob Pikeff70f092009-02-20 13:36:14 -080093Source code is Unicode text encoded in UTF-8. The text is not
94canonicalized, so a single accented code point is distinct from the
95same character constructed from combining an accent and a letter;
96those are treated as two code points. For simplicity, this document
97will use the term <i>character</i> to refer to a Unicode code point.
98</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -080099<p>
Rob Pikeff70f092009-02-20 13:36:14 -0800100Each code point is distinct; for instance, upper and lower case letters
101are different characters.
102</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700103
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700104<h3 id="Characters">Characters</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700105
Robert Griesemerc2d55862009-02-19 16:49:10 -0800106<p>
Rob Pike4501d342009-02-19 17:31:36 -0800107The following terms are used to denote specific Unicode character classes:
108</p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700109<pre class="ebnf">
110unicode_char = /* an arbitrary Unicode code point */ .
111unicode_letter = /* a Unicode code point classified as "Letter" */ .
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700112unicode_digit = /* a Unicode code point classified as "Digit" */ .
113</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700114
Robert Griesemer83c17602009-01-16 14:12:50 -0800115(The Unicode Standard, Section 4.5 General Category - Normative.)
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700116
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700117<h3 id="Letters_and_digits">Letters and digits</h3>
Rob Pikeff70f092009-02-20 13:36:14 -0800118
119<p>
Rob Pikef27e9f02009-02-23 19:22:05 -0800120The underscore character <code>_</code> (U+005F) is considered a letter.
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700121</p>
122<pre class="ebnf">
Robert Griesemerc2d55862009-02-19 16:49:10 -0800123letter = unicode_letter | "_" .
124decimal_digit = "0" ... "9" .
125octal_digit = "0" ... "7" .
126hex_digit = "0" ... "9" | "A" ... "F" | "a" ... "f" .
127</pre>
Rob Pikeff70f092009-02-20 13:36:14 -0800128<hr/>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700129
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700130<h2 id="Lexical_elements">Lexical elements</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700131
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700132<h3 id="Comments">Comments</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700133
Rob Pikeff70f092009-02-20 13:36:14 -0800134<p>
135There are two forms of comments. The first starts at the character
Rob Pikef27e9f02009-02-23 19:22:05 -0800136sequence <code>//</code> and continues through the next newline. The
137second starts at the character sequence <code>/*</code> and continues
138through the character sequence <code>*/</code>. Comments do not nest.
Rob Pikeff70f092009-02-20 13:36:14 -0800139</p>
140
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700141<h3 id="Tokens">Tokens</h3>
Rob Pikeff70f092009-02-20 13:36:14 -0800142
143<p>
144Tokens form the vocabulary of the Go language.
145There are four classes: identifiers, keywords, operators
146and delimiters, and literals. <i>White space</i>, formed from
147blanks, tabs, and newlines, is ignored except as it separates tokens
148that would otherwise combine into a single token. Comments
149behave as white space. While breaking the input into tokens,
150the next token is the longest sequence of characters that form a
151valid token.
152</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700153
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700154<h3 id="Identifiers">Identifiers</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700155
Rob Pikeff70f092009-02-20 13:36:14 -0800156<p>
157Identifiers name program entities such as variables and types.
158An identifier is a sequence of one or more letters and digits.
159The first character in an identifier must be a letter.
160</p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700161<pre class="ebnf">
Rob Pikeff70f092009-02-20 13:36:14 -0800162identifier = letter { letter | unicode_digit } .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800163</pre>
Robert Griesemerc2d55862009-02-19 16:49:10 -0800164<pre>
165a
166_x9
167ThisVariableIsExported
168αβ
169</pre>
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700170Some identifiers are predeclared (§<a href="#Predeclared_identifiers">Predeclared identifiers</a>).
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -0700171
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700172<h3 id="Keywords">Keywords</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700173
Rob Pikeff70f092009-02-20 13:36:14 -0800174<p>
175The following keywords are reserved and may not be used as identifiers.
176</p>
177<pre class="grammar">
178break default func interface select
179case defer go map struct
180chan else goto package switch
181const fallthrough if range type
182continue for import return var
183</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700184
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700185<h3 id="Operators_and_Delimiters">Operators and Delimiters</h3>
Rob Pikeff70f092009-02-20 13:36:14 -0800186
187<p>
188The following character sequences represent operators, delimiters, and other special tokens:
189</p>
190<pre class="grammar">
191+ &amp; += &amp;= &amp;&amp; == != ( )
192- | -= |= || &lt; &lt;= [ ]
193* ^ *= ^= &lt;- &gt; &gt;= { }
Robert Griesemer4ed666e2009-08-27 16:45:42 -0700194/ &lt;&lt; /= &lt;&lt;= ++ = := , ;
195% &gt;&gt; %= &gt;&gt;= -- ! ... . :
Rob Pikecd04ec92009-03-11 21:59:05 -0700196 &amp;^ &amp;^=
Rob Pikeff70f092009-02-20 13:36:14 -0800197</pre>
198
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700199<h3 id="Integer_literals">Integer literals</h3>
Rob Pikeff70f092009-02-20 13:36:14 -0800200
201<p>
202An integer literal is a sequence of one or more digits in the
203corresponding base, which may be 8, 10, or 16. An optional prefix
Rob Pikef27e9f02009-02-23 19:22:05 -0800204sets a non-decimal base: <code>0</code> for octal, <code>0x</code> or
205<code>0X</code> for hexadecimal. In hexadecimal literals, letters
206<code>a-f</code> and <code>A-F</code> represent values 10 through 15.
Rob Pikeff70f092009-02-20 13:36:14 -0800207</p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700208<pre class="ebnf">
Rob Pikeff70f092009-02-20 13:36:14 -0800209int_lit = decimal_lit | octal_lit | hex_lit .
210decimal_lit = ( "1" ... "9" ) { decimal_digit } .
211octal_lit = "0" { octal_digit } .
212hex_lit = "0" ( "x" | "X" ) hex_digit { hex_digit } .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800213</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700214
Robert Griesemerc2d55862009-02-19 16:49:10 -0800215<pre>
21642
2170600
2180xBadFace
219170141183460469231731687303715884105727
220</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700221
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700222<h3 id="Floating-point_literals">Floating-point literals</h3>
Rob Pikeff70f092009-02-20 13:36:14 -0800223<p>
224A floating-point literal is a decimal representation of a floating-point
225number. It has an integer part, a decimal point, a fractional part,
226and an exponent part. The integer and fractional part comprise
Rob Pikef27e9f02009-02-23 19:22:05 -0800227decimal digits; the exponent part is an <code>e</code> or <code>E</code>
Rob Pikeff70f092009-02-20 13:36:14 -0800228followed by an optionally signed decimal exponent. One of the
229integer part or the fractional part may be elided; one of the decimal
230point or the exponent may be elided.
231</p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700232<pre class="ebnf">
Rob Pikeff70f092009-02-20 13:36:14 -0800233float_lit = decimals "." [ decimals ] [ exponent ] |
234 decimals exponent |
235 "." decimals [ exponent ] .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800236decimals = decimal_digit { decimal_digit } .
237exponent = ( "e" | "E" ) [ "+" | "-" ] decimals .
238</pre>
Robert Griesemerad711102008-09-11 17:48:20 -0700239
Robert Griesemerc2d55862009-02-19 16:49:10 -0800240<pre>
2410.
2422.71828
2431.e+0
2446.67428e-11
2451E6
246.25
247.12345E+5
248</pre>
Robert Griesemerad711102008-09-11 17:48:20 -0700249
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700250<h3 id="Ideal_numbers">Ideal numbers</h3>
Rob Pikeff70f092009-02-20 13:36:14 -0800251
Robert Griesemerc2d55862009-02-19 16:49:10 -0800252<p>
Rob Pikeff70f092009-02-20 13:36:14 -0800253Integer literals represent values of arbitrary precision, or <i>ideal
254integers</i>. Similarly, floating-point literals represent values
255of arbitrary precision, or <i>ideal floats</i>. These <i>ideal
Robert Griesemer533dfd62009-05-13 16:56:00 -0700256numbers</i> have no size or named type and cannot overflow. However,
Rob Pikeff70f092009-02-20 13:36:14 -0800257when (used in an expression) assigned to a variable or typed constant,
258the destination must be able to represent the assigned value.
259</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -0800260<p>
Robert Griesemerad711102008-09-11 17:48:20 -0700261Implementation restriction: A compiler may implement ideal numbers
Rob Pike8f2330d2009-02-25 16:20:44 -0800262by choosing an internal representation with at least twice the precision
263of any machine type.
Rob Pikeff70f092009-02-20 13:36:14 -0800264</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700265
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700266<h3 id="Character_literals">Character literals</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700267
Rob Pike4501d342009-02-19 17:31:36 -0800268<p>
Rob Pikeff70f092009-02-20 13:36:14 -0800269A character literal represents an integer value, typically a
270Unicode code point, as one or more characters enclosed in single
271quotes. Within the quotes, any character may appear except single
272quote and newline. A single quoted character represents itself,
273while multi-character sequences beginning with a backslash encode
274values in various formats.
Rob Pike4501d342009-02-19 17:31:36 -0800275</p>
Rob Pikeff70f092009-02-20 13:36:14 -0800276<p>
277The simplest form represents the single character within the quotes;
278since Go source text is Unicode characters encoded in UTF-8, multiple
279UTF-8-encoded bytes may represent a single integer value. For
Rob Pikef27e9f02009-02-23 19:22:05 -0800280instance, the literal <code>'a'</code> holds a single byte representing
281a literal <code>a</code>, Unicode U+0061, value <code>0x61</code>, while
282<code>'ä'</code> holds two bytes (<code>0xc3</code> <code>0xa4</code>) representing
283a literal <code>a</code>-dieresis, U+00E4, value <code>0xe4</code>.
Rob Pikeff70f092009-02-20 13:36:14 -0800284</p>
285<p>
286Several backslash escapes allow arbitrary values to be represented
287as ASCII text. There are four ways to represent the integer value
Rob Pikef27e9f02009-02-23 19:22:05 -0800288as a numeric constant: <code>\x</code> followed by exactly two hexadecimal
289digits; <code>\u</code> followed by exactly four hexadecimal digits;
290<code>\U</code> followed by exactly eight hexadecimal digits, and a
291plain backslash <code>\</code> followed by exactly three octal digits.
Rob Pikeff70f092009-02-20 13:36:14 -0800292In each case the value of the literal is the value represented by
293the digits in the corresponding base.
294</p>
295<p>
296Although these representations all result in an integer, they have
297different valid ranges. Octal escapes must represent a value between
2980 and 255 inclusive. (Hexadecimal escapes satisfy this condition
Rob Pikef27e9f02009-02-23 19:22:05 -0800299by construction). The `Unicode' escapes <code>\u</code> and <code>\U</code>
Rob Pikeff70f092009-02-20 13:36:14 -0800300represent Unicode code points so within them some values are illegal,
Rob Pikef27e9f02009-02-23 19:22:05 -0800301in particular those above <code>0x10FFFF</code> and surrogate halves.
Rob Pikeff70f092009-02-20 13:36:14 -0800302</p>
303<p>
304After a backslash, certain single-character escapes represent special values:
305</p>
306<pre class="grammar">
307\a U+0007 alert or bell
308\b U+0008 backspace
309\f U+000C form feed
310\n U+000A line feed or newline
311\r U+000D carriage return
312\t U+0009 horizontal tab
313\v U+000b vertical tab
314\\ U+005c backslash
315\' U+0027 single quote (valid escape only within character literals)
316\" U+0022 double quote (valid escape only within string literals)
Robert Griesemerc2d55862009-02-19 16:49:10 -0800317</pre>
Robert Griesemerc2d55862009-02-19 16:49:10 -0800318<p>
Rob Pikeff70f092009-02-20 13:36:14 -0800319All other sequences are illegal inside character literals.
Rob Pike4501d342009-02-19 17:31:36 -0800320</p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700321<pre class="ebnf">
Rob Pikeff70f092009-02-20 13:36:14 -0800322char_lit = "'" ( unicode_value | byte_value ) "'" .
323unicode_value = unicode_char | little_u_value | big_u_value | escaped_char .
324byte_value = octal_byte_value | hex_byte_value .
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700325octal_byte_value = `\` octal_digit octal_digit octal_digit .
326hex_byte_value = `\` "x" hex_digit hex_digit .
327little_u_value = `\` "u" hex_digit hex_digit hex_digit hex_digit .
328big_u_value = `\` "U" hex_digit hex_digit hex_digit hex_digit
Rob Pikeff70f092009-02-20 13:36:14 -0800329 hex_digit hex_digit hex_digit hex_digit .
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700330escaped_char = `\` ( "a" | "b" | "f" | "n" | "r" | "t" | "v" | `\` | "'" | `"` ) .
Rob Pikeff70f092009-02-20 13:36:14 -0800331</pre>
Robert Griesemerc2d55862009-02-19 16:49:10 -0800332<pre>
333'a'
334'ä'
335'本'
336'\t'
337'\000'
338'\007'
339'\377'
340'\x07'
341'\xff'
342'\u12e4'
343'\U00101234'
344</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700345
Rob Pikeff70f092009-02-20 13:36:14 -0800346<p>
347The value of a character literal is an ideal integer, just as with
348integer literals.
349</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700350
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700351<h3 id="String_literals">String literals</h3>
Rob Pikeff70f092009-02-20 13:36:14 -0800352
353<p>
Rob Pike83cbca52009-08-21 14:18:08 -0700354String literals represent <i>ideal string</i> values. Ideal strings do not
Robert Griesemer533dfd62009-05-13 16:56:00 -0700355have a named type but they are compatible with type <code>string</code>
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700356<a href="#Type_identity_and_compatibility">Type identity and compatibility</a>).
Rob Pikeff70f092009-02-20 13:36:14 -0800357There are two forms: raw string literals and interpreted string
358literals.
359</p>
360<p>
361Raw string literals are character sequences between back quotes
Rob Pikef27e9f02009-02-23 19:22:05 -0800362<code>``</code>. Within the quotes, any character is legal except
Robert Griesemerdb7a6222009-06-18 13:51:14 -0700363back quote. The value of a raw string literal is the
364string composed of the uninterpreted characters between the quotes;
365in particular, backslashes have no special meaning and the string may
366span multiple lines.
Rob Pikeff70f092009-02-20 13:36:14 -0800367</p>
368<p>
369Interpreted string literals are character sequences between double
Rob Pikef27e9f02009-02-23 19:22:05 -0800370quotes <code>&quot;&quot;</code>. The text between the quotes forms the
Rob Pikeff70f092009-02-20 13:36:14 -0800371value of the literal, with backslash escapes interpreted as they
Rob Pikef27e9f02009-02-23 19:22:05 -0800372are in character literals (except that <code>\'</code> is illegal and
373<code>\"</code> is legal). The three-digit octal (<code>\000</code>)
374and two-digit hexadecimal (<code>\x00</code>) escapes represent individual
Rob Pikeff70f092009-02-20 13:36:14 -0800375<i>bytes</i> of the resulting string; all other escapes represent
376the (possibly multi-byte) UTF-8 encoding of individual <i>characters</i>.
Rob Pikef27e9f02009-02-23 19:22:05 -0800377Thus inside a string literal <code>\377</code> and <code>\xFF</code> represent
378a single byte of value <code>0xFF</code>=255, while <code>ÿ</code>,
379<code>\u00FF</code>, <code>\U000000FF</code> and <code>\xc3\xbf</code> represent
380the two bytes <code>0xc3 0xbf</code> of the UTF-8 encoding of character
Rob Pikeff70f092009-02-20 13:36:14 -0800381U+00FF.
382</p>
383
Robert Griesemercfe92112009-06-18 13:29:40 -0700384<p>
385A sequence of string literals is concatenated to form a single string.
386</p>
387
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700388<pre class="ebnf">
Robert Griesemercfe92112009-06-18 13:29:40 -0700389StringLit = string_lit { string_lit } .
Rob Pikeff70f092009-02-20 13:36:14 -0800390string_lit = raw_string_lit | interpreted_string_lit .
391raw_string_lit = "`" { unicode_char } "`" .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800392interpreted_string_lit = """ { unicode_value | byte_value } """ .
393</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700394
Robert Griesemerc2d55862009-02-19 16:49:10 -0800395<pre>
Robert Griesemerdb7a6222009-06-18 13:51:14 -0700396`abc` // same as "abc"
397`\n
398\n` // same as "\\n\n\\n"
Robert Griesemerc2d55862009-02-19 16:49:10 -0800399"\n"
400""
401"Hello, world!\n"
402"日本語"
403"\u65e5本\U00008a9e"
404"\xff\u00FF"
Robert Griesemercfe92112009-06-18 13:29:40 -0700405"Alea iacta est."
406"Alea " /* The die */ `iacta est` /* is cast */ "." // same as "Alea iacta est."
Robert Griesemerc2d55862009-02-19 16:49:10 -0800407</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700408
Rob Pikeff70f092009-02-20 13:36:14 -0800409<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700410These examples all represent the same string:
Rob Pikeff70f092009-02-20 13:36:14 -0800411</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700412
Robert Griesemerc2d55862009-02-19 16:49:10 -0800413<pre>
Rob Pikeff70f092009-02-20 13:36:14 -0800414"日本語" // UTF-8 input text
415`日本語` // UTF-8 input text as a raw literal
416"\u65e5\u672c\u8a9e" // The explicit Unicode code points
417"\U000065e5\U0000672c\U00008a9e" // The explicit Unicode code points
Robert Griesemerc2d55862009-02-19 16:49:10 -0800418"\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e" // The explicit UTF-8 bytes
419</pre>
Robert Griesemercd499272008-09-29 12:09:00 -0700420
Robert Griesemerc2d55862009-02-19 16:49:10 -0800421<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700422If the source code represents a character as two code points, such as
423a combining form involving an accent and a letter, the result will be
424an error if placed in a character literal (it is not a single code
425point), and will appear as two code points if placed in a string
426literal.
Rob Pikeff70f092009-02-20 13:36:14 -0800427</p>
428<hr/>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700429
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700430<h2 id="Types">Types</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700431
Robert Griesemerc2d55862009-02-19 16:49:10 -0800432<p>
Robert Griesemer56809d02009-05-20 11:02:48 -0700433A type determines the set of values and operations specific to values of that
434type. A type may be specified by a (possibly qualified) <i>type name</i>
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700435<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 -0700436which composes a new type from previously declared types.
Rob Pike5af7de32009-02-24 15:17:59 -0800437</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700438
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700439<pre class="ebnf">
Rob Pike8f2330d2009-02-25 16:20:44 -0800440Type = TypeName | TypeLit | "(" Type ")" .
441TypeName = QualifiedIdent.
442TypeLit = ArrayType | StructType | PointerType | FunctionType | InterfaceType |
443 SliceType | MapType | ChannelType .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800444</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -0800445
Robert Griesemerc2d55862009-02-19 16:49:10 -0800446<p>
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700447<i>Basic types</i> such as <code>int</code> are predeclared (§<a href="#Predeclared_identifiers">Predeclared identifiers</a>).
Rob Pike5af7de32009-02-24 15:17:59 -0800448Other types may be constructed from these, recursively,
449including arrays, structs, pointers, functions, interfaces, slices, maps, and
Robert Griesemera3294712009-01-05 11:17:26 -0800450channels.
Rob Pike5af7de32009-02-24 15:17:59 -0800451</p>
Robert Griesemer7abfcd92008-10-07 17:14:30 -0700452
Rob Pike5af7de32009-02-24 15:17:59 -0800453<p>
Robert Griesemer3b576a72009-06-17 14:31:33 -0700454A type may have a <i>method set</i> associated with it
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700455<a href="#Interface_types">Interface types</a>, §<a href="#Method_declarations">Method declarations</a>).
456The method set of an interface type (§<a href="#Interface_types">Interface types</a>) is its interface.
Robert Griesemer56809d02009-05-20 11:02:48 -0700457The method set of any other named type <code>T</code>
458consists of all methods with receiver
459type <code>T</code>.
460The method set of the corresponding pointer type <code>*T</code>
461is the set of all methods with receiver <code>*T</code> or <code>T</code>
462(that is, it also contains the method set of <code>T</code>).
463Any other type has an empty method set.
Rob Pike5af7de32009-02-24 15:17:59 -0800464</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -0800465<p>
Rob Pike5af7de32009-02-24 15:17:59 -0800466The <i>static type</i> (or just <i>type</i>) of a variable is the
467type defined by its declaration. Variables of interface type
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700468<a href="#Interface_types">Interface types</a>) also have a distinct <i>dynamic type</i>, which
Rob Pike5af7de32009-02-24 15:17:59 -0800469is the actual type of the value stored in the variable at run-time.
470The dynamic type may vary during execution but is always compatible
Robert Griesemer56809d02009-05-20 11:02:48 -0700471with the static type of the interface variable. For non-interface
Rob Pike5af7de32009-02-24 15:17:59 -0800472types, the dynamic type is always the static type.
473</p>
Robert Griesemer1f3e8422008-09-29 18:41:30 -0700474
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700475<h3 id="Basic_types">Basic types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700476
Rob Pike5af7de32009-02-24 15:17:59 -0800477<p>
Russ Cox5958dd62009-03-04 17:19:21 -0800478Basic types include traditional numeric types, booleans, and strings. All are predeclared.
Rob Pike5af7de32009-02-24 15:17:59 -0800479</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700480
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700481<h3 id="Numeric_types">Numeric types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700482
Rob Pike5af7de32009-02-24 15:17:59 -0800483<p>
484The architecture-independent numeric types are:
485</p>
Robert Griesemerebf14c62008-10-30 14:50:23 -0700486
Rob Pikeff70f092009-02-20 13:36:14 -0800487<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -0800488uint8 the set of all unsigned 8-bit integers (0 to 255)
489uint16 the set of all unsigned 16-bit integers (0 to 65535)
490uint32 the set of all unsigned 32-bit integers (0 to 4294967295)
491uint64 the set of all unsigned 64-bit integers (0 to 18446744073709551615)
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700492
Robert Griesemerc2d55862009-02-19 16:49:10 -0800493int8 the set of all signed 8-bit integers (-128 to 127)
494int16 the set of all signed 16-bit integers (-32768 to 32767)
495int32 the set of all signed 32-bit integers (-2147483648 to 2147483647)
496int64 the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700497
Robert Griesemerc2d55862009-02-19 16:49:10 -0800498float32 the set of all valid IEEE-754 32-bit floating point numbers
499float64 the set of all valid IEEE-754 64-bit floating point numbers
Rob Pike5af7de32009-02-24 15:17:59 -0800500
501byte familiar alias for uint8
Robert Griesemerc2d55862009-02-19 16:49:10 -0800502</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700503
Rob Pike5af7de32009-02-24 15:17:59 -0800504<p>
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -0800505Integer types are represented in the usual binary format; the value of
506an n-bit integer is n bits wide. A negative signed integer is represented
507as the two's complement of its absolute value.
Rob Pike5af7de32009-02-24 15:17:59 -0800508</p>
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -0800509
Rob Pike5af7de32009-02-24 15:17:59 -0800510<p>
Robert Griesemercfe92112009-06-18 13:29:40 -0700511There is also a set of numeric types with implementation-specific sizes:
Rob Pike5af7de32009-02-24 15:17:59 -0800512</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700513
Rob Pikeff70f092009-02-20 13:36:14 -0800514<pre class="grammar">
Robert Griesemercfe92112009-06-18 13:29:40 -0700515uint either 32 or 64 bits
516int either 32 or 64 bits
517float either 32 or 64 bits
518uintptr an unsigned integer large enough to store the uninterpreted bits of a pointer value
Robert Griesemerc2d55862009-02-19 16:49:10 -0800519</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700520
Robert Griesemerc2d55862009-02-19 16:49:10 -0800521<p>
Rob Pikeda389742009-03-02 19:13:40 -0800522To avoid portability issues all numeric types are distinct except
523<code>byte</code>, which is an alias for <code>uint8</code>.
524Conversions
Robert Griesemer533dfd62009-05-13 16:56:00 -0700525are required when incompatible numeric types are mixed in an expression
Rob Pike5af7de32009-02-24 15:17:59 -0800526or assignment. For instance, <code>int32</code> and <code>int</code>
Russ Cox5958dd62009-03-04 17:19:21 -0800527are not the same type even though they may have the same size on a
Rob Pike5af7de32009-02-24 15:17:59 -0800528particular architecture.
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700529
530
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700531<h3 id="Booleans">Booleans</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700532
Rob Pike5af7de32009-02-24 15:17:59 -0800533The type <code>bool</code> comprises the Boolean truth values
534represented by the predeclared constants <code>true</code>
535and <code>false</code>.
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700536
537
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700538<h3 id="Strings">Strings</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700539
Rob Pike4501d342009-02-19 17:31:36 -0800540<p>
Robert Griesemer533dfd62009-05-13 16:56:00 -0700541The <code>string</code> type represents the set of string values.
Rob Pike5af7de32009-02-24 15:17:59 -0800542Strings behave like arrays of bytes but are immutable: once created,
543it is impossible to change the contents of a string.
544
545<p>
546The elements of strings have type <code>byte</code> and may be
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700547accessed using the usual indexing operations (§<a href="#Indexes">Indexes</a>). It is
Rob Pike5af7de32009-02-24 15:17:59 -0800548illegal to take the address of such an element, that is, even if
549<code>s[i]</code> is the <code>i</code><sup>th</sup> byte of a
Robert Griesemercfe92112009-06-18 13:29:40 -0700550string, <code>&amp;s[i]</code> is invalid. The length of string
551<code>s</code> can be discovered using the built-in function
552<code>len(s)</code>. It is a compile-time constant if <code>s</code>
553is a string literal.
Rob Pike4501d342009-02-19 17:31:36 -0800554</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700555
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700556
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700557<h3 id="Array_types">Array types</h3>
Russ Cox461dd912009-03-04 14:44:51 -0800558
559<p>
560An array is a numbered sequence of elements of a single
Robert Griesemer4023dce2009-08-14 17:41:52 -0700561type, called the element type.
562The number of elements is called the length and is never
Russ Cox461dd912009-03-04 14:44:51 -0800563negative.
564</p>
565
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700566<pre class="ebnf">
Russ Cox461dd912009-03-04 14:44:51 -0800567ArrayType = "[" ArrayLength "]" ElementType .
568ArrayLength = Expression .
Robert Griesemer4023dce2009-08-14 17:41:52 -0700569ElementType = Type .
Russ Cox461dd912009-03-04 14:44:51 -0800570</pre>
571
572<p>
Robert Griesemer4ed666e2009-08-27 16:45:42 -0700573The length is part of the array's type and must must be a
574<a href="#Constant_expressions">constant expression</a> that evaluates to a non-negative
Russ Cox461dd912009-03-04 14:44:51 -0800575integer value. The length of array <code>a</code> can be discovered
576using the built-in function <code>len(a)</code>, which is a
577compile-time constant. The elements can be indexed by integer
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700578indices 0 through the <code>len(a)-1</code><a href="#Indexes">Indexes</a>).
Russ Cox461dd912009-03-04 14:44:51 -0800579</p>
580
581<pre>
582[32]byte
583[2*N] struct { x, y int32 }
584[1000]*float64
585</pre>
586
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700587<h3 id="Slice_types">Slice types</h3>
Russ Cox461dd912009-03-04 14:44:51 -0800588
589<p>
590A slice is a reference to a contiguous segment of an array and
591contains a numbered sequence of elements from that array. A slice
592type denotes the set of all slices of arrays of its element type.
593A slice value may be <code>nil</code>.
594</p>
595
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700596<pre class="ebnf">
Russ Cox461dd912009-03-04 14:44:51 -0800597SliceType = "[" "]" ElementType .
598</pre>
599
600<p>
601Like arrays, slices are indexable and have a length. The length of a
602slice <code>s</code> can be discovered by the built-in function
603<code>len(s)</code>; unlike with arrays it may change during
604execution. The elements can be addressed by integer indices 0
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700605through <code>len(s)-1</code><a href="#Indexes">Indexes</a>). The slice index of a
Russ Cox461dd912009-03-04 14:44:51 -0800606given element may be less than the index of the same element in the
607underlying array.
608</p>
609<p>
610A slice, once initialized, is always associated with an underlying
611array that holds its elements. A slice therfore shares storage
612with its array and with other slices of the same array; by contrast,
613distinct arrays always represent distinct storage.
614</p>
615<p>
616The array underlying a slice may extend past the end of the slice.
Russ Cox5958dd62009-03-04 17:19:21 -0800617The <i>capacity</i> is a measure of that extent: it is the sum of
Russ Cox461dd912009-03-04 14:44:51 -0800618the length of the slice and the length of the array beyond the slice;
619a slice of length up to that capacity can be created by `slicing' a new
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700620one from the original slice (§<a href="#Slices">Slices</a>).
Russ Cox461dd912009-03-04 14:44:51 -0800621The capacity of a slice <code>a</code> can be discovered using the
Robert Griesemercfe92112009-06-18 13:29:40 -0700622built-in function <code>cap(a)</code> and the relationship between
623<code>len()</code> and <code>cap()</code> is:
Russ Cox461dd912009-03-04 14:44:51 -0800624</p>
625
626<pre>
6270 <= len(a) <= cap(a)
628</pre>
629
630<p>
631The value of an uninitialized slice is <code>nil</code>.
632The length and capacity of a <code>nil</code> slice
633are 0. A new, initialized slice value for a given element type <code>T</code> is
634made using the built-in function <code>make</code>, which takes a slice type
635and parameters specifying the length and optionally the capacity:
636</p>
637
638<pre>
639make([]T, length)
640make([]T, length, capacity)
641</pre>
Russ Cox5958dd62009-03-04 17:19:21 -0800642
Russ Cox461dd912009-03-04 14:44:51 -0800643<p>
644The <code>make()</code> call allocates a new, hidden array to which the returned
645slice value refers. That is, calling <code>make</code>
646</p>
647
648<pre>
649make([]T, length, capacity)
650</pre>
651
652<p>
653produces the same slice as allocating an array and slicing it, so these two examples
654result in the same slice:
655</p>
656
657<pre>
658make([]int, 50, 100)
659new([100]int)[0:50]
660</pre>
661
662
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700663<h3 id="Struct_types">Struct types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700664
Rob Pike5af7de32009-02-24 15:17:59 -0800665<p>
666A struct is a sequence of named
667elements, called fields, with various types. A struct type declares
Rob Pikeda389742009-03-02 19:13:40 -0800668an identifier and type for each field. Within a struct, field identifiers
Robert Griesemer4023dce2009-08-14 17:41:52 -0700669must be unique.
Rob Pike5af7de32009-02-24 15:17:59 -0800670</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700671
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700672<pre class="ebnf">
Russ Cox461dd912009-03-04 14:44:51 -0800673StructType = "struct" "{" [ FieldDeclList ] "}" .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800674FieldDeclList = FieldDecl { ";" FieldDecl } [ ";" ] .
Robert Griesemer4023dce2009-08-14 17:41:52 -0700675FieldDecl = (IdentifierList Type | [ "*" ] TypeName) [ Tag ] .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800676Tag = StringLit .
677</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700678
Robert Griesemerc2d55862009-02-19 16:49:10 -0800679<pre>
680// An empty struct.
681struct {}
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700682
Robert Griesemerc2d55862009-02-19 16:49:10 -0800683// A struct with 5 fields.
684struct {
685 x, y int;
686 u float;
687 A *[]int;
688 F func();
689}
690</pre>
Robert Griesemer1f3e8422008-09-29 18:41:30 -0700691
Rob Pike5af7de32009-02-24 15:17:59 -0800692<p>
693A field declared with a type but no field identifier is an <i>anonymous field</i>.
694Such a field type must be specified as
Rob Pikeda389742009-03-02 19:13:40 -0800695a type name <code>T</code> or as a pointer to a type name <code>*T</code>,
Ian Lance Taylor3e804ba2009-08-17 11:40:57 -0700696and <code>T</code> itself may not be
697a pointer type. The unqualified type name acts as the field identifier.
Rob Pike5af7de32009-02-24 15:17:59 -0800698</p>
Robert Griesemer1f3e8422008-09-29 18:41:30 -0700699
Robert Griesemerc2d55862009-02-19 16:49:10 -0800700<pre>
701// A struct with four anonymous fields of type T1, *T2, P.T3 and *P.T4
702struct {
703 T1; // the field name is T1
704 *T2; // the field name is T2
Rob Pikeda389742009-03-02 19:13:40 -0800705 P.T3; // the field name is T3
706 *P.T4; // the field name is T4
Russ Cox5958dd62009-03-04 17:19:21 -0800707 x, y int;
Robert Griesemerc2d55862009-02-19 16:49:10 -0800708}
709</pre>
Robert Griesemer1f3e8422008-09-29 18:41:30 -0700710
Rob Pike5af7de32009-02-24 15:17:59 -0800711<p>
Robert Griesemer7abfcd92008-10-07 17:14:30 -0700712The unqualified type name of an anonymous field must not conflict with the
713field identifier (or unqualified type name for an anonymous field) of any
Robert Griesemer071c91b2008-10-23 12:04:45 -0700714other field within the struct. The following declaration is illegal:
Rob Pike5af7de32009-02-24 15:17:59 -0800715</p>
Robert Griesemer071c91b2008-10-23 12:04:45 -0700716
Robert Griesemerc2d55862009-02-19 16:49:10 -0800717<pre>
718struct {
719 T; // conflicts with anonymous field *T and *P.T
720 *T; // conflicts with anonymous field T and *P.T
721 *P.T; // conflicts with anonymous field T and *T
722}
723</pre>
Robert Griesemer1f3e8422008-09-29 18:41:30 -0700724
Robert Griesemerc2d55862009-02-19 16:49:10 -0800725<p>
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700726Fields and methods (§<a href="#Method_declarations">Method declarations</a>) of an anonymous field are
727promoted to be ordinary fields and methods of the struct (§<a href="#Selectors">Selectors</a>).
Robert Griesemer56809d02009-05-20 11:02:48 -0700728The following rules apply for a struct type named <code>S</code> and
729a type named <code>T</code>:
Rob Pike5af7de32009-02-24 15:17:59 -0800730</p>
Robert Griesemer56809d02009-05-20 11:02:48 -0700731<ul>
732 <li>If <code>S</code> contains an anonymous field <code>T</code>, the
733 method set of <code>S</code> includes the method set of <code>T</code>.
734 </li>
735
736 <li>If <code>S</code> contains an anonymous field <code>*T</code>, the
737 method set of <code>S</code> includes the method set of <code>*T</code>
738 (which itself includes the method set of <code>T</code>).
739 </li>
740
741 <li>If <code>S</code> contains an anonymous field <code>T</code> or
742 <code>*T</code>, the method set of <code>*S</code> includes the
743 method set of <code>*T</code> (which itself includes the method
744 set of <code>T</code>).
745 </li>
746</ul>
Rob Pike5af7de32009-02-24 15:17:59 -0800747<p>
Robert Griesemer56809d02009-05-20 11:02:48 -0700748A field declaration may be followed by an optional string literal <i>tag</i>,
749which becomes an attribute for all the identifiers in the corresponding
Rob Pike5af7de32009-02-24 15:17:59 -0800750field declaration. The tags are made
751visible through a reflection library (TODO: reference?)
752but are otherwise ignored.
753</p>
Robert Griesemer2e90e542008-10-30 15:52:37 -0700754
Robert Griesemerc2d55862009-02-19 16:49:10 -0800755<pre>
756// A struct corresponding to the EventIdMessage protocol buffer.
Rob Pikecdbf6192009-02-24 17:47:45 -0800757// The tag strings define the protocol buffer field numbers.
Robert Griesemerc2d55862009-02-19 16:49:10 -0800758struct {
Rob Pike5af7de32009-02-24 15:17:59 -0800759 time_usec uint64 "field 1";
760 server_ip uint32 "field 2";
761 process_id uint32 "field 3";
Robert Griesemerc2d55862009-02-19 16:49:10 -0800762}
763</pre>
Robert Griesemer2e90e542008-10-30 15:52:37 -0700764
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700765<h3 id="Pointer_types">Pointer types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700766
Rob Pike5af7de32009-02-24 15:17:59 -0800767<p>
Robert Griesemer4dc25282008-09-09 10:37:19 -0700768A pointer type denotes the set of all pointers to variables of a given
Rob Pikeda389742009-03-02 19:13:40 -0800769type, called the <i>base type</i> of the pointer.
Rob Pike8f2330d2009-02-25 16:20:44 -0800770A pointer value may be <code>nil</code>.
Rob Pike5af7de32009-02-24 15:17:59 -0800771</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700772
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700773<pre class="ebnf">
Robert Griesemerc2d55862009-02-19 16:49:10 -0800774PointerType = "*" BaseType .
775BaseType = Type .
776</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700777
Robert Griesemerc2d55862009-02-19 16:49:10 -0800778<pre>
779*int
Rob Pike8f2330d2009-02-25 16:20:44 -0800780*map[string] *chan int
Robert Griesemerc2d55862009-02-19 16:49:10 -0800781</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700782
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700783<h3 id="Function_types">Function types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700784
Rob Pike8f2330d2009-02-25 16:20:44 -0800785<p>
Robert Griesemer7231ceb2008-09-08 15:01:04 -0700786A function type denotes the set of all functions with the same parameter
Rob Pike8f2330d2009-02-25 16:20:44 -0800787and result types.
788A function value may be <code>nil</code>.
789</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700790
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700791<pre class="ebnf">
Rob Pike8f2330d2009-02-25 16:20:44 -0800792FunctionType = "func" Signature .
793Signature = Parameters [ Result ] .
Robert Griesemer4023dce2009-08-14 17:41:52 -0700794Result = Parameters | Type .
Rob Pike8f2330d2009-02-25 16:20:44 -0800795Parameters = "(" [ ParameterList ] ")" .
796ParameterList = ParameterDecl { "," ParameterDecl } .
Robert Griesemer4023dce2009-08-14 17:41:52 -0700797ParameterDecl = [ IdentifierList ] ( Type | "..." ) .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800798</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700799
Robert Griesemerc2d55862009-02-19 16:49:10 -0800800<p>
Rob Pike8f2330d2009-02-25 16:20:44 -0800801Within a list of parameters or results, the names (IdentifierList)
802must either all be present or all be absent. If present, each name
803stands for one item (parameter or result) of the specified type; if absent, each
804type stands for one item of that type. Parameter and result
805lists are always parenthesized except that if there is exactly
806one unnamed result that is not a function type it may writen as an unparenthesized type.
Rob Pike8f2330d2009-02-25 16:20:44 -0800807</p>
808<p>
809For the last parameter only, instead of a type one may write
810<code>...</code> to indicate that the function may be invoked with
Rob Pikeda389742009-03-02 19:13:40 -0800811zero or more additional arguments of any
Rob Pike8f2330d2009-02-25 16:20:44 -0800812type. If parameters of such a function are named, the final identifier
813list must be a single name, that of the <code>...</code> parameter.
814</p>
Robert Griesemer2bfa9572008-10-24 13:13:12 -0700815
Robert Griesemerc2d55862009-02-19 16:49:10 -0800816<pre>
817func ()
818func (x int)
819func () int
820func (string, float, ...)
821func (a, b int, z float) bool
822func (a, b int, z float) (bool)
823func (a, b int, z float, opt ...) (success bool)
824func (int, int, float) (float, *[]int)
Robert Griesemerc2d55862009-02-19 16:49:10 -0800825func (n int) (func (p* T))
826</pre>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -0800827
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700828
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700829<h3 id="Interface_types">Interface types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700830
Rob Pike8f2330d2009-02-25 16:20:44 -0800831<p>
Robert Griesemer56809d02009-05-20 11:02:48 -0700832An interface type specifies a method set called its <i>interface</i>.
833A variable of interface type can store a value of any type with a method set
834that is any superset of the interface. Such a type is said to
835<i>implement the interface</i>. An interface value may be <code>nil</code>.
Rob Pike8f2330d2009-02-25 16:20:44 -0800836</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700837
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700838<pre class="ebnf">
Russ Cox461dd912009-03-04 14:44:51 -0800839InterfaceType = "interface" "{" [ MethodSpecList ] "}" .
Rob Pike8f2330d2009-02-25 16:20:44 -0800840MethodSpecList = MethodSpec { ";" MethodSpec } [ ";" ] .
841MethodSpec = IdentifierList Signature | InterfaceTypeName .
842InterfaceTypeName = TypeName .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800843</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700844
Robert Griesemerc2d55862009-02-19 16:49:10 -0800845<pre>
Rob Pike8f2330d2009-02-25 16:20:44 -0800846// A simple File interface
Robert Griesemerc2d55862009-02-19 16:49:10 -0800847interface {
848 Read, Write (b Buffer) bool;
849 Close ();
850}
851</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700852
Rob Pike8f2330d2009-02-25 16:20:44 -0800853<p>
Robert Griesemer56809d02009-05-20 11:02:48 -0700854More than one type may implement an interface.
Rob Pike8f2330d2009-02-25 16:20:44 -0800855For instance, if two types <code>S1</code> and <code>S2</code>
Robert Griesemer56809d02009-05-20 11:02:48 -0700856have the method set
Rob Pike8f2330d2009-02-25 16:20:44 -0800857</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700858
Robert Griesemerc2d55862009-02-19 16:49:10 -0800859<pre>
860func (p T) Read(b Buffer) bool { return ... }
861func (p T) Write(b Buffer) bool { return ... }
862func (p T) Close() { ... }
863</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700864
Rob Pike8f2330d2009-02-25 16:20:44 -0800865<p>
866(where <code>T</code> stands for either <code>S1</code> or <code>S2</code>)
867then the <code>File</code> interface is implemented by both <code>S1</code> and
868<code>S2</code>, regardless of what other methods
869<code>S1</code> and <code>S2</code> may have or share.
870</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700871
Rob Pike8f2330d2009-02-25 16:20:44 -0800872<p>
873A type implements any interface comprising any subset of its methods
874and may therefore implement several distinct interfaces. For
875instance, all types implement the <i>empty interface</i>:
876</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700877
Robert Griesemerc2d55862009-02-19 16:49:10 -0800878<pre>
Rob Pike8f2330d2009-02-25 16:20:44 -0800879interface { }
Robert Griesemerc2d55862009-02-19 16:49:10 -0800880</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700881
Rob Pike8f2330d2009-02-25 16:20:44 -0800882<p>
883Similarly, consider this interface specification,
Robert Griesemer4ed666e2009-08-27 16:45:42 -0700884which appears within a <a href="#Type_declarations">type declaration</a>
Rob Pike8f2330d2009-02-25 16:20:44 -0800885to define an interface called <code>Lock</code>:
886</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700887
Robert Griesemerc2d55862009-02-19 16:49:10 -0800888<pre>
889type Lock interface {
890 Lock, Unlock ();
891}
892</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700893
Rob Pike8f2330d2009-02-25 16:20:44 -0800894<p>
895If <code>S1</code> and <code>S2</code> also implement
896</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700897
Robert Griesemerc2d55862009-02-19 16:49:10 -0800898<pre>
899func (p T) Lock() { ... }
900func (p T) Unlock() { ... }
901</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700902
Robert Griesemerc2d55862009-02-19 16:49:10 -0800903<p>
Rob Pike8f2330d2009-02-25 16:20:44 -0800904they implement the <code>Lock</code> interface as well
905as the <code>File</code> interface.
906</p>
907<p>
908An interface may contain an interface type name <code>T</code>
909in place of a method specification.
Robert Griesemer4023dce2009-08-14 17:41:52 -0700910In this notation, <code>T</code> must denote a different interface type
Rob Pike8f2330d2009-02-25 16:20:44 -0800911and the effect is equivalent to enumerating the methods of <code>T</code> explicitly
912in the interface.
913</p>
Robert Griesemer38c232f2009-02-11 15:09:15 -0800914
Robert Griesemerc2d55862009-02-19 16:49:10 -0800915<pre>
916type ReadWrite interface {
917 Read, Write (b Buffer) bool;
918}
Robert Griesemer38c232f2009-02-11 15:09:15 -0800919
Robert Griesemerc2d55862009-02-19 16:49:10 -0800920type File interface {
921 ReadWrite; // same as enumerating the methods in ReadWrite
922 Lock; // same as enumerating the methods in Lock
923 Close();
924}
925</pre>
Robert Griesemer38c232f2009-02-11 15:09:15 -0800926
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700927<h3 id="Map_types">Map types</h3>
Robert Griesemera3294712009-01-05 11:17:26 -0800928
Rob Pike8f2330d2009-02-25 16:20:44 -0800929<p>
930A map is an unordered group of elements of one type, called the
931value type, indexed by a set of unique <i>keys</i> of another type,
Robert Griesemer4023dce2009-08-14 17:41:52 -0700932called the key type.
Rob Pike8f2330d2009-02-25 16:20:44 -0800933A map value may be <code>nil</code>.
934
935</p>
Robert Griesemera3294712009-01-05 11:17:26 -0800936
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700937<pre class="ebnf">
Rob Pike8f2330d2009-02-25 16:20:44 -0800938MapType = "map" "[" KeyType "]" ValueType .
Robert Griesemer4023dce2009-08-14 17:41:52 -0700939KeyType = Type .
940ValueType = Type .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800941</pre>
Robert Griesemera3294712009-01-05 11:17:26 -0800942
Robert Griesemerc2d55862009-02-19 16:49:10 -0800943<p>
Rob Pike8f2330d2009-02-25 16:20:44 -0800944The comparison operators <code>==</code> and <code>!=</code>
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700945<a href="#Comparison_operators">Comparison operators</a>) must be fully defined for operands of the
Rob Pike8f2330d2009-02-25 16:20:44 -0800946key type; thus the key type must be a basic, pointer, interface,
947map, or channel type. If the key type is an interface type, these
948comparison operators must be defined for the dynamic key values;
949failure will cause a run-time error.
950
951</p>
Robert Griesemera3294712009-01-05 11:17:26 -0800952
Robert Griesemerc2d55862009-02-19 16:49:10 -0800953<pre>
954map [string] int
955map [*T] struct { x, y float }
956map [string] interface {}
957</pre>
Robert Griesemera3294712009-01-05 11:17:26 -0800958
Rob Pike5af7de32009-02-24 15:17:59 -0800959<p>
Rob Pike8f2330d2009-02-25 16:20:44 -0800960The number of elements is called the length and is never negative.
961The length of a map <code>m</code> can be discovered using the
962built-in function <code>len(m)</code> and may change during execution.
Rob Pikeda389742009-03-02 19:13:40 -0800963The value of an uninitialized map is <code>nil</code>.
Rob Pike5af7de32009-02-24 15:17:59 -0800964</p>
Rob Pike8f2330d2009-02-25 16:20:44 -0800965<p>
966Upon creation, a map is empty. Values may be added and removed
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700967during execution using special forms of assignment (§<a href="#Assignments">Assignments</a>).
Rob Pike8f2330d2009-02-25 16:20:44 -0800968A new, empty map value is made using the built-in
969function <code>make</code>, which takes the map type and an optional
Rob Pikeda389742009-03-02 19:13:40 -0800970capacity hint as arguments:
Rob Pike8f2330d2009-02-25 16:20:44 -0800971</p>
972
973<pre>
Rob Pikeda389742009-03-02 19:13:40 -0800974make(map[string] int)
975make(map[string] int, 100)
Rob Pike8f2330d2009-02-25 16:20:44 -0800976</pre>
Robert Griesemera3294712009-01-05 11:17:26 -0800977
Rob Pikeda389742009-03-02 19:13:40 -0800978<p>
979The initial capacity does not bound its size:
980maps grow to accommodate the number of items
981stored in them.
982</p>
983
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700984<h3 id="Channel_types">Channel types</h3>
Robert Griesemera3294712009-01-05 11:17:26 -0800985
Rob Pike8f2330d2009-02-25 16:20:44 -0800986<p>
Robert Griesemera3294712009-01-05 11:17:26 -0800987A channel provides a mechanism for two concurrently executing functions
Rob Pike8f2330d2009-02-25 16:20:44 -0800988to synchronize execution and communicate by passing a value of a
Robert Griesemer4023dce2009-08-14 17:41:52 -0700989specified element type.
Robert Griesemere2cb60b2009-06-19 13:03:01 -0700990A value of channel type may be <code>nil</code>.
Rob Pike8f2330d2009-02-25 16:20:44 -0800991</p>
Robert Griesemera3294712009-01-05 11:17:26 -0800992
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700993<pre class="ebnf">
Rob Pike8f2330d2009-02-25 16:20:44 -0800994ChannelType = Channel | SendChannel | RecvChannel .
995Channel = "chan" ValueType .
996SendChannel = "chan" "&lt;-" ValueType .
997RecvChannel = "&lt;-" "chan" ValueType .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800998</pre>
Robert Griesemera3294712009-01-05 11:17:26 -0800999
Rob Pike8f2330d2009-02-25 16:20:44 -08001000<p>
1001Upon creation, a channel can be used both to send and to receive values.
Robert Griesemera3294712009-01-05 11:17:26 -08001002By conversion or assignment, a channel may be constrained only to send or
Rob Pike8f2330d2009-02-25 16:20:44 -08001003to receive. This constraint is called a channel's <i>direction</i>; either
1004<i>send</i>, <i>receive</i>, or <i>bi-directional</i> (unconstrained).
1005</p>
Robert Griesemera3294712009-01-05 11:17:26 -08001006
Robert Griesemerc2d55862009-02-19 16:49:10 -08001007<pre>
Rob Pike46596852009-03-02 16:17:29 -08001008chan T // can be used to send and receive values of type T
Robert Griesemerc2d55862009-02-19 16:49:10 -08001009chan &lt;- float // can only be used to send floats
Rob Pike46596852009-03-02 16:17:29 -08001010&lt;-chan int // can only be used to receive ints
Robert Griesemerc2d55862009-02-19 16:49:10 -08001011</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001012
Rob Pike8f2330d2009-02-25 16:20:44 -08001013<p>
1014The value of an uninitialized channel is <code>nil</code>. A new, initialized channel
1015value is made using the built-in function <code>make</code>,
Robert Griesemer633957b2009-01-06 13:23:20 -08001016which takes the channel type and an optional capacity as arguments:
Rob Pike8f2330d2009-02-25 16:20:44 -08001017</p>
Robert Griesemera3294712009-01-05 11:17:26 -08001018
Rob Pike94b67eb2009-03-24 17:40:47 -07001019
Robert Griesemerc2d55862009-02-19 16:49:10 -08001020<pre>
Rob Pikeda389742009-03-02 19:13:40 -08001021make(chan int, 100)
Robert Griesemerc2d55862009-02-19 16:49:10 -08001022</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001023
Rob Pike8f2330d2009-02-25 16:20:44 -08001024<p>
1025The capacity, in number of elements, sets the size of the buffer in the channel. If the
Robert Griesemera3294712009-01-05 11:17:26 -08001026capacity is greater than zero, the channel is asynchronous and, provided the
Rob Pike8f2330d2009-02-25 16:20:44 -08001027buffer is not full, sends can succeed without blocking. If the capacity is zero
1028or absent, the communication succeeds only when both a sender and receiver are ready.
1029</p>
Robert Griesemera3294712009-01-05 11:17:26 -08001030
Rob Pike94b67eb2009-03-24 17:40:47 -07001031<p>
1032For a channel <code>c</code>, the predefined function <code>close(c)</code>
1033marks the channel as unable to accept more
1034values through a send operation. After any previously
1035sent values have been received, receives will return
1036the zero value for the channel's type. After at least one such zero value has been
1037received, <code>closed(c)</code> returns true.
1038</p>
1039
Rob Pike83cbca52009-08-21 14:18:08 -07001040<h2 id="Properties_of_types_and_values">Properties of types and values</h2>
Robert Griesemer434c6052008-11-07 13:34:37 -08001041
Rob Pike4501d342009-02-19 17:31:36 -08001042<p>
Robert Griesemer533dfd62009-05-13 16:56:00 -07001043Two types may be <i>identical</i>, <i>compatible</i>, or <i>incompatible</i>.
1044Two identical types are always compatible, but two compatible types may not be identical.
1045Go is <i>type safe</i>: a value of one type cannot be assigned to a variable of an
1046incompatible type, and two values of incompatible types cannot be mixed in
1047binary operations.</p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001048
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001049<h3 id="Type_identity_and_compatibility">Type identity and compatibility</h3>
Robert Griesemer533dfd62009-05-13 16:56:00 -07001050
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001051<h4 id="Type_identity">Type identity</h4>
Rob Pike8f2330d2009-02-25 16:20:44 -08001052
Robert Griesemerc2d55862009-02-19 16:49:10 -08001053<p>
Robert Griesemer533dfd62009-05-13 16:56:00 -07001054Two named types are identical if their type names originate in the same
Robert Griesemeraeaab592009-08-31 17:30:55 -07001055type declaration (§<a href="#Declarations_and_scope">Declarations and scope</a>). A named and an unnamed type
Robert Griesemer533dfd62009-05-13 16:56:00 -07001056are never identical. Two unnamed types are identical if the corresponding
1057type literals have the same literal structure and corresponding components have
1058identical types. In detail:
Rob Pike4501d342009-02-19 17:31:36 -08001059</p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001060
Robert Griesemerc2d55862009-02-19 16:49:10 -08001061<ul>
Robert Griesemer533dfd62009-05-13 16:56:00 -07001062 <li>Two array types are identical if they have identical element types and
1063 the same array length.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001064
Robert Griesemer533dfd62009-05-13 16:56:00 -07001065 <li>Two slice types are identical if they have identical element types.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001066
Robert Griesemer533dfd62009-05-13 16:56:00 -07001067 <li>Two struct types are identical if they have the same sequence of fields,
1068 and if corresponding fields have the same names and identical types.
Robert Griesemer56809d02009-05-20 11:02:48 -07001069 Two anonymous fields are considered to have the same name.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001070
Robert Griesemer533dfd62009-05-13 16:56:00 -07001071 <li>Two pointer types are identical if they have identical base types.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001072
Robert Griesemer533dfd62009-05-13 16:56:00 -07001073 <li>Two function types are identical if they have the same number of parameters
1074 and result values and if corresponding parameter and result types are
1075 identical. All "..." parameters have identical type.
1076 Parameter and result names are not required to match.</li>
Robert Griesemera3294712009-01-05 11:17:26 -08001077
Robert Griesemer533dfd62009-05-13 16:56:00 -07001078 <li>Two interface types are identical if they have the same set of methods
1079 with the same names and identical function types. The order
1080 of the methods is irrelevant.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001081
Robert Griesemer533dfd62009-05-13 16:56:00 -07001082 <li>Two map types are identical if they have identical key and value types.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001083
Robert Griesemer533dfd62009-05-13 16:56:00 -07001084 <li>Two channel types are identical if they have identical value types and
1085 the same direction.</li>
Robert Griesemerc2d55862009-02-19 16:49:10 -08001086</ul>
Robert Griesemer434c6052008-11-07 13:34:37 -08001087
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001088<h4 id="Type_compatibility">Type compatibility</h4>
Robert Griesemer533dfd62009-05-13 16:56:00 -07001089
Robert Griesemerc2d55862009-02-19 16:49:10 -08001090<p>
Robert Griesemer533dfd62009-05-13 16:56:00 -07001091Type compatibility is less stringent than type identity: a named and an unnamed
1092type are compatible if the respective type literals are compatible.
1093In all other respects, the definition of type compatibility is the
1094same as for type identity listed above but with ``compatible''
1095substituted for ``identical''.
Rob Pike4501d342009-02-19 17:31:36 -08001096</p>
Robert Griesemer434c6052008-11-07 13:34:37 -08001097
Robert Griesemerc2d55862009-02-19 16:49:10 -08001098<p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001099Given the declarations
1100</p>
Robert Griesemer434c6052008-11-07 13:34:37 -08001101
Robert Griesemerc2d55862009-02-19 16:49:10 -08001102<pre>
1103type (
1104 T0 []string;
1105 T1 []string
1106 T2 struct { a, b int };
1107 T3 struct { a, c int };
1108 T4 func (int, float) *T0
1109 T5 func (x int, y float) *[]string
1110)
1111</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -08001112
Rob Pike8f2330d2009-02-25 16:20:44 -08001113<p>
Robert Griesemer533dfd62009-05-13 16:56:00 -07001114these types are identical:
Rob Pike8f2330d2009-02-25 16:20:44 -08001115</p>
Robert Griesemer434c6052008-11-07 13:34:37 -08001116
Robert Griesemerc2d55862009-02-19 16:49:10 -08001117<pre>
1118T0 and T0
1119[]int and []int
1120struct { a, b *T5 } and struct { a, b *T5 }
Robert Griesemer533dfd62009-05-13 16:56:00 -07001121func (x int, y float) *[]string and func (int, float) (result *[]string)
Robert Griesemerc2d55862009-02-19 16:49:10 -08001122</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -08001123
Rob Pike8f2330d2009-02-25 16:20:44 -08001124<p>
Robert Griesemer533dfd62009-05-13 16:56:00 -07001125<code>T0</code> and <code>T1</code> are neither identical nor compatible
1126because they are named types with distinct declarations.
1127</p>
1128
1129<p>
1130These types are compatible:
1131</p>
1132
1133<pre>
1134T0 and T0
1135T0 and []string
1136T3 and struct { a int; c int }
1137T4 and func (x int, y float) *[]string
1138</pre>
1139
1140<p>
1141<code>T2</code> and <code>struct { a, c int }</code> are incompatible because
1142they have different field names.
Rob Pike8f2330d2009-02-25 16:20:44 -08001143</p>
Robert Griesemer434c6052008-11-07 13:34:37 -08001144
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001145<h3 id="Assignment_compatibility">Assignment compatibility</h3>
Rob Pike5af7de32009-02-24 15:17:59 -08001146
Rob Pike5af7de32009-02-24 15:17:59 -08001147<p>
1148Values of any type may always be assigned to variables
Robert Griesemer533dfd62009-05-13 16:56:00 -07001149of compatible static type. Some types and values have conditions under which they may
1150be assigned to otherwise incompatible types:
Rob Pike5af7de32009-02-24 15:17:59 -08001151</p>
1152<ul>
1153<li>
Rob Pikeda389742009-03-02 19:13:40 -08001154A value can be assigned to an interface variable if the static
Rob Pike5af7de32009-02-24 15:17:59 -08001155type of the value implements the interface.
1156</li>
1157<li>
Robert Griesemere2cb60b2009-06-19 13:03:01 -07001158The predeclared constant <code>nil</code> can be assigned to any
1159pointer, function, slice, map, channel, or interface variable.
1160<li>
1161A pointer <code>p</code> to an array can be assigned to a slice variable
1162<code>v</code> with compatible element type
1163if the type of <code>p</code> or <code>v</code> is unnamed.
1164The slice variable then refers to the original array; the data is not copied.
1165</li>
1166<li>
1167A bidirectional channel <code>c</code> can be assigned to a channel variable
1168<code>v</code> with compatible channel value type
1169if the type of <code>c</code> or <code>v</code> is unnamed.
Rob Pike5af7de32009-02-24 15:17:59 -08001170</li>
1171</ul>
1172
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001173<h3 id="Comparison_compatibility">Comparison compatibility</h3>
Rob Pike5af7de32009-02-24 15:17:59 -08001174
1175<p>
Robert Griesemer533dfd62009-05-13 16:56:00 -07001176Values of any type may be compared to other values of compatible static
Rob Pike5af7de32009-02-24 15:17:59 -08001177type. Values of numeric and string type may be compared using the
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001178full range of comparison operators as described in §<a href="#Comparison_operators;">Comparison operators;</a>
Rob Pike5af7de32009-02-24 15:17:59 -08001179booleans may be compared only for equality or inequality.
1180</p>
1181
1182<p>
1183Values of composite type may be
1184compared for equality or inequality using the <code>==</code> and
1185<code>!=</code> operators, with the following provisos:
1186</p>
1187<ul>
1188<li>
1189Arrays and structs may not be compared to anything.
1190</li>
1191<li>
Rob Pikeda389742009-03-02 19:13:40 -08001192A slice value may only be compared explicitly against <code>nil</code>.
1193A slice value is equal to <code>nil</code> if it has been assigned the explicit
Rob Pike5af7de32009-02-24 15:17:59 -08001194value <code>nil</code> or if it is a variable (or array element,
1195field, etc.) that has not been modified since it was created
1196uninitialized.
1197</li>
1198<li>
1199Similarly, an interface value is equal to <code>nil</code> if it has
1200been assigned the explicit value <code>nil</code> or if it is a
1201variable (or array element, field, etc.) that has not been modified
1202since it was created uninitialized.
1203</li>
1204<li>
1205For types that can be compared to <code>nil</code>,
1206two values of the same type are equal if they both equal <code>nil</code>,
1207unequal if one equals <code>nil</code> and one does not.
1208</li>
1209<li>
1210Pointer values are equal if they point to the same location.
1211</li>
1212<li>
Rob Pikeda389742009-03-02 19:13:40 -08001213Function values are equal if they refer to the same function.
Rob Pike5af7de32009-02-24 15:17:59 -08001214</li>
1215<li>
Rob Pikeda389742009-03-02 19:13:40 -08001216Channel and map values are equal if they were created by the same call to <code>make</code>
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001217<a href="#Making_slices">Making slices</a>, maps, and channels).
Rob Pike83cbca52009-08-21 14:18:08 -07001218When comparing two values of channel type, the channel value types
1219must be compatible but the channel direction is ignored.
Rob Pike5af7de32009-02-24 15:17:59 -08001220</li>
1221<li>
Robert Griesemer533dfd62009-05-13 16:56:00 -07001222Interface values may be compared if they have compatible static types.
Rob Pikeda389742009-03-02 19:13:40 -08001223They will be equal only if they have the same dynamic type and the underlying values are equal.
Rob Pike5af7de32009-02-24 15:17:59 -08001224</li>
1225</ul>
Rob Pikeff70f092009-02-20 13:36:14 -08001226<hr/>
Robert Griesemer434c6052008-11-07 13:34:37 -08001227
Rob Pikea9ed30f2009-02-23 19:26:07 -08001228
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001229<h2 id="Blocks">Blocks</h2>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001230
1231<p>
Robert Griesemer0a162a12009-08-19 16:44:04 -07001232A <i>block</i> is a sequence of declarations and statements within matching
1233brace brackets.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001234</p>
1235
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001236<pre class="ebnf">
Robert Griesemer0a162a12009-08-19 16:44:04 -07001237Block = "{" StatementList "}" .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001238</pre>
Russ Cox5958dd62009-03-04 17:19:21 -08001239
Rob Pikea9ed30f2009-02-23 19:26:07 -08001240<p>
Robert Griesemer0a162a12009-08-19 16:44:04 -07001241In addition to explicit blocks in the source code, there are implicit blocks:
Rob Pikea9ed30f2009-02-23 19:26:07 -08001242</p>
1243
1244<ol>
Robert Griesemer0a162a12009-08-19 16:44:04 -07001245 <li>The <i>universe block</i> encompasses all Go source text.</li>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001246
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001247 <li>Each package (§<a href="#Packages">Packages</a>) has a <i>package block</i> containing all
Robert Griesemer0a162a12009-08-19 16:44:04 -07001248 Go source text for that package.</li>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001249
Robert Griesemer0a162a12009-08-19 16:44:04 -07001250 <li>Each file has a <i>file block</i> containing all Go source text
1251 in that file.</li>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001252
Robert Griesemer0a162a12009-08-19 16:44:04 -07001253 <li>Each <code>if</code>, <code>for</code>, and <code>switch</code>
1254 statement is considered to be in its own implicit block.</li>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001255
Russ Cox16b95ba2009-08-20 10:22:52 -07001256 <li>Each clause in a <code>switch</code> or <code>select</code> statement
Robert Griesemer0a162a12009-08-19 16:44:04 -07001257 acts as an implicit block.</li>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001258</ol>
1259
Robert Griesemer0a162a12009-08-19 16:44:04 -07001260<p>
Robert Griesemeraeaab592009-08-31 17:30:55 -07001261Blocks nest and influence scoping (§<a href="#Declarations_and_scope">Declarations and scope</a>).
Robert Griesemer0a162a12009-08-19 16:44:04 -07001262</p>
1263
1264
Robert Griesemeraeaab592009-08-31 17:30:55 -07001265<h2 id="Declarations_and_scope">Declarations and scope</h2>
Robert Griesemer0a162a12009-08-19 16:44:04 -07001266
1267<p>
1268A declaration binds an identifier to a constant, type, variable, function, or package.
1269Every identifier in a program must be declared.
1270No identifier may be declared twice in the same block, and
1271no identifier may be declared in both the file and package block.
1272</p>
1273
1274<pre class="ebnf">
1275Declaration = ConstDecl | TypeDecl | VarDecl .
1276TopLevelDecl = Declaration | FunctionDecl | MethodDecl .
1277</pre>
1278
1279<p>
1280The <i>scope</i> of a declared identifier is the extent of source text in which
1281the identifier denotes the specified constant, type, variable, function, or package.
1282</p>
1283
1284<p>
1285Go is lexically scoped using blocks:
1286</p>
1287
1288<ol>
1289 <li>The scope of a predeclared identifier is the universe block.</li>
1290
1291 <li>The scope of an identifier denoting a constant, type, variable,
1292 or function declared at top level (outside any function) is the
1293 package block.</li>
1294
1295 <li>The scope of an imported package identifier is the file block
1296 of the file containing the import declaration.</li>
1297
1298 <li>The scope of an identifier denoting a function parameter or
1299 result variable is the function body.</li>
1300
1301 <li>The scope of a constant or variable identifier declared
1302 inside a function begins at the end of the ConstSpec or VarSpec
1303 and ends at the end of the innermost containing block.</li>
1304
1305 <li>The scope of a type identifier declared inside a function
Russ Cox16b95ba2009-08-20 10:22:52 -07001306 begins at the identifier in the TypeSpec
Robert Griesemer0a162a12009-08-19 16:44:04 -07001307 and ends at the end of the innermost containing block.</li>
1308</ol>
1309
1310<p>
1311An identifier declared in a block may be redeclared in an inner block.
1312While the identifier of the inner declaration is in scope, it denotes
1313the entity declared by the inner declaration.
1314</p>
1315
1316<p>
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001317The package clause (§<a href="#Package_clause">Package clause</a>) is not a declaration; the package name
Robert Griesemer0a162a12009-08-19 16:44:04 -07001318does not appear in any scope. Its purpose is to identify the files belonging
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001319to the same package (§<a href="#Packages">Packages</a>) and to specify the default name for import
Robert Griesemer0a162a12009-08-19 16:44:04 -07001320declarations.
1321</p>
1322
1323
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001324<h3 id="Label_scopes">Label scopes</h3>
Robert Griesemer0a162a12009-08-19 16:44:04 -07001325
1326<p>
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001327Labels are declared by labeled statements (§<a href="#Labeled_statements">Labeled statements</a>) and are
Robert Griesemer0a162a12009-08-19 16:44:04 -07001328used in the <code>break</code>, <code>continue</code>, and <code>goto</code>
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001329statements (§<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 -07001330In contrast to other identifiers, labels are not block scoped and do
1331not conflict with identifiers that are not labels. The scope of a label
1332is the body of the function in which it is declared and excludes
1333the body of any nested function.
1334</p>
1335
1336
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001337<h3 id="Predeclared_identifiers">Predeclared identifiers</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001338
1339<p>
Robert Griesemer0a162a12009-08-19 16:44:04 -07001340The following identifiers are implicitly declared in the universe block:
Rob Pikea9ed30f2009-02-23 19:26:07 -08001341</p>
1342<pre class="grammar">
1343Basic types:
Russ Cox5958dd62009-03-04 17:19:21 -08001344 bool byte float32 float64 int8 int16 int32 int64
1345 string uint8 uint16 uint32 uint64
Rob Pikea9ed30f2009-02-23 19:26:07 -08001346
Rob Pike5af7de32009-02-24 15:17:59 -08001347Architecture-specific convenience types:
Rob Pikea9ed30f2009-02-23 19:26:07 -08001348 float int uint uintptr
1349
1350Constants:
1351 true false iota nil
1352
1353Functions:
Robert Griesemer4ed666e2009-08-27 16:45:42 -07001354 cap close closed len make new panic panicln print println
Rob Pikea9ed30f2009-02-23 19:26:07 -08001355
1356Packages:
Robert Griesemer98b4f6a2009-05-12 21:37:46 -07001357 unsafe
Rob Pikea9ed30f2009-02-23 19:26:07 -08001358</pre>
1359
Robert Griesemeraeaab592009-08-31 17:30:55 -07001360
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001361<h3 id="Exported_identifiers">Exported identifiers</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001362
1363<p>
Robert Griesemeraeaab592009-08-31 17:30:55 -07001364An identifier may be <i>exported</i> to permit access to it from another package
1365using a <a href="#Qualified_identifiers">qualified identifier</a>. An identifier
1366is exported if both:
Rob Pikea9ed30f2009-02-23 19:26:07 -08001367</p>
1368<ol>
Robert Griesemeraeaab592009-08-31 17:30:55 -07001369 <li>the first character of the identifier's name is a Unicode upper case letter (Unicode class "Lu"); and
1370 <li>the identifier is declared in the <a href="#Blocks">package block</a> or is a field or method of a type
1371 declared in that block.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001372</ol>
1373<p>
Robert Griesemeraeaab592009-08-31 17:30:55 -07001374All other identifiers are not exported.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001375</p>
1376
Robert Griesemeraeaab592009-08-31 17:30:55 -07001377
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001378<h3 id="Const_declarations">Const declarations</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001379
1380<p>
1381A constant declaration binds a list of identifiers (the names of
1382the constants) to the values of a list of constant expressions
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001383<a href="#Constant_expressions">Constant expressions</a>). The number of identifiers must be equal
Rob Pikea9ed30f2009-02-23 19:26:07 -08001384to the number of expressions, and the n<sup>th</sup> identifier on
1385the left is bound to value of the n<sup>th</sup> expression on the
1386right.
1387</p>
1388
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001389<pre class="ebnf">
Rob Pikea9ed30f2009-02-23 19:26:07 -08001390ConstDecl = "const" ( ConstSpec | "(" [ ConstSpecList ] ")" ) .
1391ConstSpecList = ConstSpec { ";" ConstSpec } [ ";" ] .
Robert Griesemer4023dce2009-08-14 17:41:52 -07001392ConstSpec = IdentifierList [ [ Type ] "=" ExpressionList ] .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001393
1394IdentifierList = identifier { "," identifier } .
1395ExpressionList = Expression { "," Expression } .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001396</pre>
1397
1398<p>
Robert Griesemer4023dce2009-08-14 17:41:52 -07001399If the type is omitted, the constants take the
Rob Pikea9ed30f2009-02-23 19:26:07 -08001400individual types of the corresponding expressions, which may be
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001401<i>ideal integer</i> or <i>ideal float</i><a href="#Ideal_number">Ideal number</a>). If the type
Rob Pikea9ed30f2009-02-23 19:26:07 -08001402is present, all constants take the type specified, and the types
1403of all the expressions must be assignment-compatible
1404with that type.
1405</p>
1406
1407<pre>
1408const Pi float64 = 3.14159265358979323846
1409const E = 2.718281828
1410const (
1411 size int64 = 1024;
1412 eof = -1;
1413)
1414const a, b, c = 3, 4, "foo" // a = 3, b = 4, c = "foo"
1415const u, v float = 0, 3 // u = 0.0, v = 3.0
1416</pre>
1417
1418<p>
1419Within a parenthesized <code>const</code> declaration list the
1420expression list may be omitted from any but the first declaration.
1421Such an empty list is equivalent to the textual substitution of the
Russ Coxf8ba0f42009-03-12 19:04:56 -07001422first preceding non-empty expression list, and its type if any.
Russ Cox5958dd62009-03-04 17:19:21 -08001423Omitting the list of expressions is therefore equivalent to
1424repeating the previous list. The number of identifiers must be equal
1425to the number of expressions in the previous list.
1426Together with the <code>iota</code> constant generator
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001427<a href="#Iota">Iota</a>) this mechanism permits light-weight declaration of sequential values:
Rob Pikea9ed30f2009-02-23 19:26:07 -08001428</p>
1429
1430<pre>
1431const (
1432 Sunday = iota;
1433 Monday;
1434 Tuesday;
1435 Wednesday;
1436 Thursday;
1437 Friday;
1438 Partyday;
1439 numberOfDays; // this constant is not exported
1440)
1441</pre>
1442
1443
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001444<h3 id="Iota">Iota</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001445
1446<p>
1447Within a constant declaration, the predeclared pseudo-constant
1448<code>iota</code> represents successive integers. It is reset to 0
1449whenever the reserved word <code>const</code> appears in the source
1450and increments with each semicolon. It can be used to construct a
1451set of related constants:
1452</p>
1453
1454<pre>
1455const ( // iota is reset to 0
1456 c0 = iota; // c0 == 0
1457 c1 = iota; // c1 == 1
1458 c2 = iota // c2 == 2
1459)
1460
1461const (
Robert Griesemer4ed666e2009-08-27 16:45:42 -07001462 a = 1 &lt;&lt; iota; // a == 1 (iota has been reset)
1463 b = 1 &lt;&lt; iota; // b == 2
1464 c = 1 &lt;&lt; iota; // c == 4
Rob Pikea9ed30f2009-02-23 19:26:07 -08001465)
1466
1467const (
1468 u = iota * 42; // u == 0 (ideal integer)
1469 v float = iota * 42; // v == 42.0 (float)
1470 w = iota * 42; // w == 84 (ideal integer)
1471)
1472
1473const x = iota; // x == 0 (iota has been reset)
1474const y = iota; // y == 0 (iota has been reset)
1475</pre>
1476
1477<p>
1478Within an ExpressionList, the value of each <code>iota</code> is the same because
1479it is only incremented at a semicolon:
1480</p>
1481
1482<pre>
1483const (
Robert Griesemer4ed666e2009-08-27 16:45:42 -07001484 bit0, mask0 = 1 &lt;&lt; iota, 1 &lt;&lt; iota - 1; // bit0 == 1, mask0 == 0
Rob Pikea9ed30f2009-02-23 19:26:07 -08001485 bit1, mask1; // bit1 == 2, mask1 == 1
1486 bit2, mask2; // bit2 == 4, mask2 == 3
1487)
1488</pre>
1489
1490<p>
1491This last example exploits the implicit repetition of the
1492last non-empty expression list.
1493</p>
1494
1495
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001496<h3 id="Type_declarations">Type declarations</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001497
1498<p>
1499A type declaration binds an identifier, the <i>type name</i>,
1500to a new type. <font color=red>TODO: what exactly is a "new type"?</font>
1501</p>
1502
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001503<pre class="ebnf">
Rob Pikea9ed30f2009-02-23 19:26:07 -08001504TypeDecl = "type" ( TypeSpec | "(" [ TypeSpecList ] ")" ) .
1505TypeSpecList = TypeSpec { ";" TypeSpec } [ ";" ] .
Robert Griesemerbdec3302009-08-31 17:57:14 -07001506TypeSpec = identifier Type .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001507</pre>
1508
1509<pre>
Robert Griesemerbdec3302009-08-31 17:57:14 -07001510type IntArray [16]int
Rob Pikea9ed30f2009-02-23 19:26:07 -08001511
1512type (
1513 Point struct { x, y float };
1514 Polar Point
1515)
1516
1517type TreeNode struct {
1518 left, right *TreeNode;
Russ Cox461dd912009-03-04 14:44:51 -08001519 value *Comparable;
Rob Pikea9ed30f2009-02-23 19:26:07 -08001520}
1521
1522type Comparable interface {
1523 cmp(Comparable) int
1524}
1525</pre>
1526
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001527<h3 id="Variable_declarations">Variable declarations</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001528
1529<p>
1530A variable declaration creates a variable, binds an identifier to it and
1531gives it a type and optionally an initial value.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001532</p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001533<pre class="ebnf">
Rob Pikea9ed30f2009-02-23 19:26:07 -08001534VarDecl = "var" ( VarSpec | "(" [ VarSpecList ] ")" ) .
1535VarSpecList = VarSpec { ";" VarSpec } [ ";" ] .
Robert Griesemer4023dce2009-08-14 17:41:52 -07001536VarSpec = IdentifierList ( Type [ "=" ExpressionList ] | "=" ExpressionList ) .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001537</pre>
1538
1539<pre>
1540var i int
1541var U, V, W float
1542var k = 0
1543var x, y float = -1.0, -2.0
1544var (
1545 i int;
1546 u, v, s = 2.0, 3.0, "bar"
1547)
1548</pre>
1549
1550<p>
Robert Griesemeref45e642009-08-21 11:25:00 -07001551If a list of expressions is given, the variables are initialized
1552by assigning those expressions to the variables (§<a href="#Assignments">Assignments</a>).
1553Otherwise, each variable is initialized to its <i>zero value</i>
1554<a href="#The_zero_value">The zero value</a>).
Rob Pikea9ed30f2009-02-23 19:26:07 -08001555</p>
Robert Griesemeref45e642009-08-21 11:25:00 -07001556
Rob Pikea9ed30f2009-02-23 19:26:07 -08001557<p>
Robert Griesemeref45e642009-08-21 11:25:00 -07001558If the type is present, each variable is given that type.
1559Otherwise, the types are deduced from the assignment
1560of the expression list.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001561</p>
Robert Griesemeref45e642009-08-21 11:25:00 -07001562
Rob Pikea9ed30f2009-02-23 19:26:07 -08001563<p>
1564If the type is absent and the corresponding expression is a constant
Robert Griesemeref45e642009-08-21 11:25:00 -07001565expression of ideal integer, float, or string type, the type of the
1566declared variable is <code>int</code>, <code>float</code>,
1567or <code>string</code> respectively:
Rob Pikea9ed30f2009-02-23 19:26:07 -08001568</p>
1569
1570<pre>
1571var i = 0 // i has type int
1572var f = 3.1415 // f has type float
Robert Griesemeref45e642009-08-21 11:25:00 -07001573var s = "OMDB" // s has type string
Rob Pikea9ed30f2009-02-23 19:26:07 -08001574</pre>
1575
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001576<h3 id="Short_variable_declarations">Short variable declarations</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001577
Robert Griesemeref45e642009-08-21 11:25:00 -07001578A <i>short variable declaration</i> uses the syntax:
Rob Pikea9ed30f2009-02-23 19:26:07 -08001579
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001580<pre class="ebnf">
Robert Griesemere1b8cb82009-07-16 20:31:41 -07001581ShortVarDecl = IdentifierList ":=" ExpressionList .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001582</pre>
1583
Robert Griesemeref45e642009-08-21 11:25:00 -07001584It is a shorthand for a regular variable declaration with
1585initializer expressions but no types:
Rob Pikea9ed30f2009-02-23 19:26:07 -08001586
1587<pre class="grammar">
1588"var" IdentifierList = ExpressionList .
1589</pre>
1590
1591<pre>
1592i, j := 0, 10;
1593f := func() int { return 7; }
Rob Pikef5387602009-03-30 16:08:41 -07001594ch := make(chan int);
Russ Cox5958dd62009-03-04 17:19:21 -08001595r, w := os.Pipe(fd); // os.Pipe() returns two values
Rob Pikea9ed30f2009-02-23 19:26:07 -08001596</pre>
1597
1598<p>
Robert Griesemeref45e642009-08-21 11:25:00 -07001599Unlike regular variable declarations, a short variable declaration may redeclare variables provided they
Rob Pike2a1683a2009-04-19 20:04:15 -07001600were originally declared in the same block with the same type, and at
1601least one of the variables is new. As a consequence, redeclaration
1602can only appear in a multi-variable short declaration.
1603Redeclaration does not introduce a new
1604variable; it just assigns a new value to the original.
1605</p>
1606
1607<pre>
1608field1, offset := nextField(str, 0);
1609field2, offset := nextField(str, offset); // redeclares offset
1610</pre>
1611
1612<p>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001613Short variable declarations may appear only inside functions.
1614In some contexts such as the initializers for <code>if</code>,
1615<code>for</code>, or <code>switch</code> statements,
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001616they can be used to declare local temporary variables (§<a href="#Statements">Statements</a>).
Rob Pikea9ed30f2009-02-23 19:26:07 -08001617</p>
1618
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001619<h3 id="Function_declarations">Function declarations</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001620
1621<p>
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001622A function declaration binds an identifier to a function (§<a href="#Function_types">Function types</a>).
Rob Pikea9ed30f2009-02-23 19:26:07 -08001623</p>
1624
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001625<pre class="ebnf">
Robert Griesemer0a162a12009-08-19 16:44:04 -07001626FunctionDecl = "func" identifier Signature [ Body ] .
1627Body = Block.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001628</pre>
1629
Robert Griesemer4023dce2009-08-14 17:41:52 -07001630<p>
1631A function declaration may omit the body. Such a declaration provides the
1632signature for a function implemented outside Go, such as an assembly routine.
1633</p>
1634
Rob Pikea9ed30f2009-02-23 19:26:07 -08001635<pre>
1636func min(x int, y int) int {
1637 if x &lt; y {
1638 return x;
1639 }
1640 return y;
1641}
Robert Griesemer4023dce2009-08-14 17:41:52 -07001642
1643func flushICache(begin, end uintptr) // implemented externally
Rob Pikea9ed30f2009-02-23 19:26:07 -08001644</pre>
1645
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001646<h3 id="Method_declarations">Method declarations</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001647
1648<p>
1649A method declaration binds an identifier to a method,
1650which is a function with a <i>receiver</i>.
1651</p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001652<pre class="ebnf">
Robert Griesemer0a162a12009-08-19 16:44:04 -07001653MethodDecl = "func" Receiver identifier Signature [ Body ] .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001654Receiver = "(" [ identifier ] [ "*" ] TypeName ")" .
1655</pre>
1656
1657<p>
Robert Griesemer56809d02009-05-20 11:02:48 -07001658The receiver type must be of the form <code>T</code> or <code>*T</code> where
1659<code>T</code> is a type name. <code>T</code> is called the
1660<i>receiver base type</i> or just <i>base type</i>.
1661The base type must not be a pointer or interface type and must be
Stephen Ma5db1d382009-09-02 20:09:25 -07001662declared in the same package as the method.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001663The method is said to be <i>bound</i> to the base type
1664and is visible only within selectors for that type
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001665<a href="#Type_declarations">Type declarations</a>, §<a href="#Selectors">Selectors</a>).
Rob Pikea9ed30f2009-02-23 19:26:07 -08001666</p>
1667
1668<p>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001669Given type <code>Point</code>, the declarations
1670</p>
1671
1672<pre>
1673func (p *Point) Length() float {
1674 return Math.sqrt(p.x * p.x + p.y * p.y);
1675}
1676
1677func (p *Point) Scale(factor float) {
1678 p.x = p.x * factor;
1679 p.y = p.y * factor;
1680}
1681</pre>
1682
1683<p>
1684bind the methods <code>Length</code> and <code>Scale</code>
1685to the base type <code>Point</code>.
1686</p>
1687
1688<p>
Robert Griesemer56809d02009-05-20 11:02:48 -07001689If the receiver's value is not referenced inside the the body of the method,
Rob Pikea9ed30f2009-02-23 19:26:07 -08001690its identifier may be omitted in the declaration. The same applies in
1691general to parameters of functions and methods.
1692</p>
1693
1694<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08001695The type of a method is the type of a function with the receiver as first
1696argument. For instance, the method <code>Scale</code> has type
1697</p>
1698
1699<pre>
1700(p *Point, factor float)
1701</pre>
1702
1703<p>
1704However, a function declared this way is not a method.
1705</p>
1706
Rob Pikea9ed30f2009-02-23 19:26:07 -08001707
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001708<h2 id="Expressions">Expressions</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001709
Rob Pikedf3183f2009-02-26 16:37:23 -08001710<p>
1711An expression specifies the computation of a value by applying
Robert Griesemer56809d02009-05-20 11:02:48 -07001712operators and functions to operands. An expression has a value
1713and a type.
Rob Pikedf3183f2009-02-26 16:37:23 -08001714</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001715
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001716<h3 id="Operands">Operands</h3>
Robert Griesemerad711102008-09-11 17:48:20 -07001717
1718Operands denote the elementary values in an expression.
1719
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001720<pre class="ebnf">
Rob Pikedf3183f2009-02-26 16:37:23 -08001721Operand = Literal | QualifiedIdent | "(" Expression ")" .
1722Literal = BasicLit | CompositeLit | FunctionLit .
1723BasicLit = int_lit | float_lit | char_lit | StringLit .
Robert Griesemerc2d55862009-02-19 16:49:10 -08001724</pre>
Robert Griesemerad711102008-09-11 17:48:20 -07001725
1726
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001727<h3 id="Constants">Constants</h3>
Robert Griesemerb90b2132008-09-19 15:49:55 -07001728
Rob Pikedf3183f2009-02-26 16:37:23 -08001729<p>
Russ Cox5958dd62009-03-04 17:19:21 -08001730A <i>constant</i> is a literal of a basic type
1731(including the predeclared constants <code>true</code>, <code>false</code>
1732and <code>nil</code>
1733and values denoted by <code>iota</code>)
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001734or a constant expression (§<a href="#Constant_expressions">Constant expressions</a>).
Russ Cox5958dd62009-03-04 17:19:21 -08001735Constants have values that are known at compile time.
Rob Pikedf3183f2009-02-26 16:37:23 -08001736</p>
Robert Griesemerb90b2132008-09-19 15:49:55 -07001737
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001738<h3 id="Qualified_identifiers">Qualified identifiers</h3>
Robert Griesemerad711102008-09-11 17:48:20 -07001739
Robert Griesemerc2d55862009-02-19 16:49:10 -08001740<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08001741A qualified identifier is an identifier qualified by a package name prefix.
1742</p>
Robert Griesemer337af312008-11-17 18:11:36 -08001743
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001744<pre class="ebnf">
Russ Cox16b95ba2009-08-20 10:22:52 -07001745QualifiedIdent = [ PackageName "." ] identifier .
Robert Griesemerc2d55862009-02-19 16:49:10 -08001746PackageName = identifier .
1747</pre>
Robert Griesemerad711102008-09-11 17:48:20 -07001748
Rob Pikedf3183f2009-02-26 16:37:23 -08001749<p>
Robert Griesemeraeaab592009-08-31 17:30:55 -07001750A qualified identifier accesses an identifier in a separate package.
1751The identifier must be <a href="#Exported_identifiers">exported</a> by that
1752package, which means that it must begin with a Unicode upper case letter.
Rob Pikedf3183f2009-02-26 16:37:23 -08001753</p>
Rob Pikedf3183f2009-02-26 16:37:23 -08001754
1755<pre>
1756Math.Sin
Rob Pikedf3183f2009-02-26 16:37:23 -08001757</pre>
Robert Griesemerad711102008-09-11 17:48:20 -07001758
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001759<h3 id="Composite_literals">Composite literals</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001760
Rob Pikedf3183f2009-02-26 16:37:23 -08001761<p>
1762Composite literals construct values for structs, arrays, slices, and maps
1763and create a new value each time they are evaluated.
1764They consist of the type of the value
Robert Griesemer838cf122009-05-22 10:25:06 -07001765followed by a brace-bound list of composite elements. An element may be
1766a single expression or a key-value pair.
Rob Pikedf3183f2009-02-26 16:37:23 -08001767</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001768
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001769<pre class="ebnf">
Robert Griesemer838cf122009-05-22 10:25:06 -07001770CompositeLit = LiteralType "{" [ ElementList ] "}" .
Rob Pikedf3183f2009-02-26 16:37:23 -08001771LiteralType = StructType | ArrayType | "[" "..." "]" ElementType |
1772 SliceType | MapType | TypeName .
Robert Griesemer838cf122009-05-22 10:25:06 -07001773ElementList = Element { "," Element } [ "," ] .
1774Element = [ Key ":" ] Value .
1775Key = Expression .
1776Value = Expression .
Robert Griesemerc2d55862009-02-19 16:49:10 -08001777</pre>
Robert Griesemer0976e342008-09-03 13:37:44 -07001778
Rob Pikedf3183f2009-02-26 16:37:23 -08001779<p>
Robert Griesemer838cf122009-05-22 10:25:06 -07001780The LiteralType must be a struct, array, slice, or map type
1781(the grammar enforces this constraint except when the type is given
1782as a TypeName).
Robert Griesemer4ed666e2009-08-27 16:45:42 -07001783The types of the expressions must be <a href="#Assignment_compatibility">assignment compatible</a> to
Russ Cox7a5e97b2009-03-03 15:40:30 -08001784the respective field, element, and key types of the LiteralType;
1785there is no additional conversion.
Robert Griesemer838cf122009-05-22 10:25:06 -07001786The key is interpreted as a field name for struct literals,
1787an index for array and slice literals, and a key for map literals.
1788For map literals, all elements must have a key. It is an error
1789to specify multiple elements with the same field name or
1790constant key value.
Rob Pikedf3183f2009-02-26 16:37:23 -08001791</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001792
Robert Griesemer838cf122009-05-22 10:25:06 -07001793<p>
1794For struct literals the following rules apply:
Robert Griesemercfe92112009-06-18 13:29:40 -07001795</p>
Robert Griesemer838cf122009-05-22 10:25:06 -07001796<ul>
1797 <li>A literal which does not contain any keys must
1798 list an element for each struct field in the
1799 order in which the fields are declared.
1800 </li>
1801 <li>If any element has a key, every element must have a key.
1802 </li>
1803 <li>A literal which contains keys does not need to
1804 have an element for each struct field. Omitted fields
1805 get the zero value for that field.
1806 </li>
1807 <li>A literal may omit the element list; such a literal evaluates
1808 to the zero value for its type.
1809 </li>
1810 <li>It is an error to specify an element for a non-exported
1811 field of a struct belonging to a different package.
1812 </li>
1813</ul>
Robert Griesemer838cf122009-05-22 10:25:06 -07001814
1815<p>
1816Given the declarations
1817</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08001818<pre>
Robert Griesemer838cf122009-05-22 10:25:06 -07001819type Point struct { x, y, z float }
1820type Line struct { p, q Point }
Robert Griesemerc2d55862009-02-19 16:49:10 -08001821</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001822
Rob Pikedf3183f2009-02-26 16:37:23 -08001823<p>
1824one may write
1825</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001826
Robert Griesemerc2d55862009-02-19 16:49:10 -08001827<pre>
Robert Griesemer838cf122009-05-22 10:25:06 -07001828origin := Point{}; // zero value for Point
1829line := Line{origin, Point{y: -4, z: 12.3}}; // zero value for line.q.x
Rob Pike37ab8382009-03-18 22:58:36 -07001830</pre>
1831
Robert Griesemercfe92112009-06-18 13:29:40 -07001832<p>
1833For array and slice literals the following rules apply:
1834</p>
Robert Griesemer838cf122009-05-22 10:25:06 -07001835<ul>
1836 <li>Each element has an associated integer index marking
1837 its position in the array.
1838 </li>
1839 <li>An element with a key uses the key as its index; the
1840 key must be a constant integer expression.
1841 </li>
1842 <li>An element without a key uses the previous element's index plus one.
1843 If the first element has no key, its index is zero.
1844 </li>
1845</ul>
Robert Griesemer838cf122009-05-22 10:25:06 -07001846
Rob Pike37ab8382009-03-18 22:58:36 -07001847<p>
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001848Taking the address of a composite literal (§<a href="#Address_operators">Address operators</a>)
Rob Pike2a5af742009-03-20 17:03:48 -07001849generates a unique pointer to an instance of the literal's value.
Rob Pike37ab8382009-03-18 22:58:36 -07001850</p>
Rob Pike37ab8382009-03-18 22:58:36 -07001851<pre>
Robert Griesemer838cf122009-05-22 10:25:06 -07001852var pointer *Point = &amp;Point{y: 1000};
Robert Griesemerc2d55862009-02-19 16:49:10 -08001853</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001854
Rob Pikedf3183f2009-02-26 16:37:23 -08001855<p>
Robert Griesemera3294712009-01-05 11:17:26 -08001856The length of an array literal is the length specified in the LiteralType.
1857If fewer elements than the length are provided in the literal, the missing
Rob Pike8f2330d2009-02-25 16:20:44 -08001858elements are set to the zero value for the array element type.
Robert Griesemer838cf122009-05-22 10:25:06 -07001859It is an error to provide elements with index values outside the index range
1860of the array. The notation <code>...</code> specifies an array length equal
1861to the maximum element index plus one.
Rob Pikedf3183f2009-02-26 16:37:23 -08001862</p>
Robert Griesemerb90b2132008-09-19 15:49:55 -07001863
Robert Griesemerc2d55862009-02-19 16:49:10 -08001864<pre>
Rob Pike426335f2009-03-02 17:52:52 -08001865buffer := [10]string{}; // len(buffer) == 10
Robert Griesemer838cf122009-05-22 10:25:06 -07001866intSet := [6]int{1, 2, 3, 5}; // len(intSet) == 6
Rob Pike426335f2009-03-02 17:52:52 -08001867days := [...]string{"Sat", "Sun"}; // len(days) == 2
Robert Griesemerc2d55862009-02-19 16:49:10 -08001868</pre>
Robert Griesemer91bbd642009-01-07 09:31:35 -08001869
Rob Pikedf3183f2009-02-26 16:37:23 -08001870<p>
1871A slice literal describes the entire underlying array literal.
Robert Griesemer838cf122009-05-22 10:25:06 -07001872Thus, the length and capacity of a slice literal is the maximum
1873element index plus one. A slice literal has the form
Rob Pikedf3183f2009-02-26 16:37:23 -08001874</p>
Robert Griesemer91bbd642009-01-07 09:31:35 -08001875
Robert Griesemerc2d55862009-02-19 16:49:10 -08001876<pre>
Rob Pike426335f2009-03-02 17:52:52 -08001877[]T{x1, x2, ... xn}
Robert Griesemerc2d55862009-02-19 16:49:10 -08001878</pre>
Robert Griesemer91bbd642009-01-07 09:31:35 -08001879
Rob Pikedf3183f2009-02-26 16:37:23 -08001880<p>
1881and is a shortcut for a slice operation applied to an array literal:
1882</p>
Robert Griesemer91bbd642009-01-07 09:31:35 -08001883
Robert Griesemerc2d55862009-02-19 16:49:10 -08001884<pre>
Rob Pike426335f2009-03-02 17:52:52 -08001885[n]T{x1, x2, ... xn}[0 : n]
Robert Griesemerc2d55862009-02-19 16:49:10 -08001886</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001887
Rob Pikedf3183f2009-02-26 16:37:23 -08001888<p>
Russ Cox7a5e97b2009-03-03 15:40:30 -08001889A parsing ambiguity arises when a composite literal using the
1890TypeName form of the LiteralType appears in the condition of an
1891"if", "for", or "switch" statement, because the braces surrounding
1892the expressions in the literal are confused with those introducing
1893a block of statements. To resolve the ambiguity in this rare case,
1894the composite literal must appear within
1895parentheses.
1896</p>
1897
1898<pre>
1899if x == (T{a,b,c}[i]) { ... }
1900if (x == T{a,b,c}[i]) { ... }
1901</pre>
1902
Robert Griesemer838cf122009-05-22 10:25:06 -07001903<p>
1904Examples of valid array, slice, and map literals:
1905</p>
1906
1907<pre>
1908// list of prime numbers
1909primes := []int{2, 3, 5, 7, 9, 11, 13, 17, 19, 991};
1910
1911// vowels[ch] is true if ch is a vowel
1912vowels := [128]bool{'a': true, 'e': true, 'i': true, 'o': true, 'u': true, 'y': true};
1913
1914// the array [10]float{-1, 0, 0, 0, -0.1, -0.1, 0, 0, 0, -1};
1915filter := [10]float{-1, 4: -0.1, -0.1, 9: -1};
1916
1917// frequencies in Hz for equal-tempered scale (A4 = 440Hz)
1918noteFrequency := map[string]float{
1919 "C0": 16.35, "D0": 18.35, "E0": 20.60, "F0": 21.83,
1920 "G0": 24.50, "A0": 27.50, "B0": 30.87,
1921}
1922</pre>
1923
1924
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001925<h3 id="Function_literals">Function literals</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001926
Rob Pikedf3183f2009-02-26 16:37:23 -08001927<p>
1928A function literal represents an anonymous function.
1929It consists of a specification of the function type and a function body.
1930</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001931
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001932<pre class="ebnf">
Robert Griesemer0a162a12009-08-19 16:44:04 -07001933FunctionLit = FunctionType Body .
Robert Griesemerc2d55862009-02-19 16:49:10 -08001934</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001935
Robert Griesemerc2d55862009-02-19 16:49:10 -08001936<pre>
Rob Pikedf3183f2009-02-26 16:37:23 -08001937func (a, b int, z float) bool { return a*b &lt; int(z) }
Robert Griesemerc2d55862009-02-19 16:49:10 -08001938</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001939
Rob Pikedf3183f2009-02-26 16:37:23 -08001940<p>
1941A function literal can be assigned to a variable or invoked directly.
1942</p>
Robert Griesemer7231ceb2008-09-08 15:01:04 -07001943
Robert Griesemerc2d55862009-02-19 16:49:10 -08001944<pre>
Rob Pikedf3183f2009-02-26 16:37:23 -08001945f := func(x, y int) int { return x + y }
1946func(ch chan int) { ch &lt;- ACK } (reply_chan)
Robert Griesemerc2d55862009-02-19 16:49:10 -08001947</pre>
Robert Griesemer7231ceb2008-09-08 15:01:04 -07001948
Rob Pikedf3183f2009-02-26 16:37:23 -08001949<p>
1950Function literals are <i>closures</i>: they may refer to variables
Robert Griesemerd8a764c2009-02-06 17:01:10 -08001951defined in a surrounding function. Those variables are then shared between
1952the surrounding function and the function literal, and they survive as long
Rob Pikedf3183f2009-02-26 16:37:23 -08001953as they are accessible.
1954</p>
Robert Griesemer7231ceb2008-09-08 15:01:04 -07001955
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001956
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001957<h3 id="Primary_expressions">Primary expressions</h3>
Russ Cox5958dd62009-03-04 17:19:21 -08001958
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001959<pre class="ebnf">
Robert Griesemerc2d55862009-02-19 16:49:10 -08001960PrimaryExpr =
1961 Operand |
1962 PrimaryExpr Selector |
1963 PrimaryExpr Index |
1964 PrimaryExpr Slice |
Russ Cox5958dd62009-03-04 17:19:21 -08001965 PrimaryExpr TypeAssertion |
Robert Griesemerc2d55862009-02-19 16:49:10 -08001966 PrimaryExpr Call .
Robert Griesemer57b34612008-10-10 12:45:44 -07001967
Russ Cox5958dd62009-03-04 17:19:21 -08001968Selector = "." identifier .
1969Index = "[" Expression "]" .
1970Slice = "[" Expression ":" Expression "]" .
1971TypeAssertion = "." "(" Type ")" .
1972Call = "(" [ ExpressionList ] ")" .
Robert Griesemerc2d55862009-02-19 16:49:10 -08001973</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001974
1975
Robert Griesemerc2d55862009-02-19 16:49:10 -08001976<pre>
1977x
19782
1979(s + ".txt")
1980f(3.1415, true)
Rob Pike426335f2009-03-02 17:52:52 -08001981Point{1, 2}
Robert Griesemerc2d55862009-02-19 16:49:10 -08001982m["foo"]
1983s[i : j + 1]
1984obj.color
1985Math.sin
1986f.p[i].x()
1987</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001988
1989
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001990<h3 id="Selectors">Selectors</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001991
Rob Pikedf3183f2009-02-26 16:37:23 -08001992<p>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07001993A primary expression of the form
Rob Pikedf3183f2009-02-26 16:37:23 -08001994</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001995
Robert Griesemerc2d55862009-02-19 16:49:10 -08001996<pre>
1997x.f
1998</pre>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07001999
Robert Griesemerc2d55862009-02-19 16:49:10 -08002000<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002001denotes the field or method <code>f</code> of the value denoted by <code>x</code>
2002(or of <code>*x</code> if
2003<code>x</code> is of pointer type). The identifier <code>f</code>
2004is called the (field or method)
2005<i>selector</i>.
2006The type of the expression is the type of <code>f</code>.
2007</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002008<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002009A selector <code>f</code> may denote a field or method <code>f</code> of
2010a type <code>T</code>, or it may refer
2011to a field or method <code>f</code> of a nested anonymous field of
2012<code>T</code>.
2013The number of anonymous fields traversed
2014to reach <code>f</code> is called its <i>depth</i> in <code>T</code>.
2015The depth of a field or method <code>f</code>
2016declared in <code>T</code> is zero.
2017The depth of a field or method <code>f</code> declared in
2018an anonymous field <code>A</code> in <code>T</code> is the
2019depth of <code>f</code> in <code>A</code> plus one.
2020</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002021<p>
Robert Griesemer071c91b2008-10-23 12:04:45 -07002022The following rules apply to selectors:
Rob Pikedf3183f2009-02-26 16:37:23 -08002023</p>
2024<ol>
2025<li>
2026For a value <code>x</code> of type <code>T</code> or <code>*T</code>
2027where <code>T</code> is not an interface type,
2028<code>x.f</code> denotes the field or method at the shallowest depth
2029in <code>T</code> where there
2030is such an <code>f</code>.
2031If there is not exactly one <code>f</code> with shallowest depth, the selector
Robert Griesemer071c91b2008-10-23 12:04:45 -07002032expression is illegal.
Rob Pikedf3183f2009-02-26 16:37:23 -08002033</li>
2034<li>
2035For a variable <code>x</code> of type <code>I</code> or <code>*I</code>
2036where <code>I</code> is an interface type,
2037<code>x.f</code> denotes the actual method with name <code>f</code> of the value assigned
2038to <code>x</code> if there is such a method.
2039If no value or <code>nil</code> was assigned to <code>x</code>, <code>x.f</code> is illegal.
2040</li>
2041<li>
2042In all other cases, <code>x.f</code> is illegal.
2043</ol>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002044<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002045Selectors automatically dereference pointers as necessary.
2046If <code>x</code> is of pointer type, <code>x.y</code>
2047is shorthand for <code>(*x).y</code>; if <code>y</code>
2048is also of pointer type, <code>x.y.z</code> is shorthand
2049for <code>(*(*x).y).z</code>, and so on.
2050If <code>*x</code> is of pointer type, dereferencing
2051must be explicit;
2052only one level of automatic dereferencing is provided.
2053For an <code>x</code> of type <code>T</code> containing an
2054anonymous field declared as <code>*A</code>,
2055<code>x.f</code> is a shortcut for <code>(*x.A).f</code>.
2056</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002057<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002058For example, given the declarations:
2059</p>
Robert Griesemer071c91b2008-10-23 12:04:45 -07002060
Robert Griesemerc2d55862009-02-19 16:49:10 -08002061<pre>
2062type T0 struct {
2063 x int;
2064}
Robert Griesemer071c91b2008-10-23 12:04:45 -07002065
Robert Griesemerc2d55862009-02-19 16:49:10 -08002066func (recv *T0) M0()
Robert Griesemer071c91b2008-10-23 12:04:45 -07002067
Robert Griesemerc2d55862009-02-19 16:49:10 -08002068type T1 struct {
2069 y int;
2070}
Robert Griesemer071c91b2008-10-23 12:04:45 -07002071
Robert Griesemerc2d55862009-02-19 16:49:10 -08002072func (recv T1) M1()
Robert Griesemer071c91b2008-10-23 12:04:45 -07002073
Robert Griesemerc2d55862009-02-19 16:49:10 -08002074type T2 struct {
2075 z int;
2076 T1;
2077 *T0;
2078}
Robert Griesemer071c91b2008-10-23 12:04:45 -07002079
Robert Griesemerc2d55862009-02-19 16:49:10 -08002080func (recv *T2) M2()
Robert Griesemer071c91b2008-10-23 12:04:45 -07002081
Robert Griesemerc2d55862009-02-19 16:49:10 -08002082var p *T2; // with p != nil and p.T1 != nil
2083</pre>
Robert Griesemer071c91b2008-10-23 12:04:45 -07002084
Rob Pikedf3183f2009-02-26 16:37:23 -08002085<p>
2086one may write:
2087</p>
Robert Griesemer071c91b2008-10-23 12:04:45 -07002088
Robert Griesemerc2d55862009-02-19 16:49:10 -08002089<pre>
2090p.z // (*p).z
2091p.y // ((*p).T1).y
2092p.x // (*(*p).T0).x
Robert Griesemer071c91b2008-10-23 12:04:45 -07002093
Robert Griesemerc2d55862009-02-19 16:49:10 -08002094p.M2 // (*p).M2
2095p.M1 // ((*p).T1).M1
2096p.M0 // ((*p).T0).M0
2097</pre>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002098
2099
Robert Griesemerc2d55862009-02-19 16:49:10 -08002100<font color=red>
Robert Griesemer071c91b2008-10-23 12:04:45 -07002101TODO: Specify what happens to receivers.
Robert Griesemerc2d55862009-02-19 16:49:10 -08002102</font>
Robert Griesemer7abfcd92008-10-07 17:14:30 -07002103
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002104
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002105<h3 id="Indexes">Indexes</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002106
Rob Pikedf3183f2009-02-26 16:37:23 -08002107<p>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002108A primary expression of the form
Rob Pikedf3183f2009-02-26 16:37:23 -08002109</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002110
Robert Griesemerc2d55862009-02-19 16:49:10 -08002111<pre>
2112a[x]
2113</pre>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002114
Rob Pike4501d342009-02-19 17:31:36 -08002115<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002116denotes the array or map element of <code>a</code> indexed by <code>x</code>.
2117The value <code>x</code> is called the
2118<i>array index</i> or <i>map key</i>, respectively. The following
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002119rules apply:
Rob Pike4501d342009-02-19 17:31:36 -08002120</p>
Russ Coxeaa92e02009-06-25 14:43:55 -07002121
Robert Griesemerc2d55862009-02-19 16:49:10 -08002122<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002123For <code>a</code> of type <code>A</code> or <code>*A</code>
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002124where <code>A</code> is an array type (§<a href="#Array_types">Array types</a>),
2125or for <code>a</code> of type <code>S</code> where <code>S</code> is a slice type (§<a href="#Slice_types">Slice types</a>):
Rob Pike4501d342009-02-19 17:31:36 -08002126</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002127<ul>
Rob Pikedf3183f2009-02-26 16:37:23 -08002128 <li><code>x</code> must be an integer value and <code>0 &lt;= x &lt; len(a)</code>
2129 <li><code>a[x]</code> is the array element at index <code>x</code> and the type of
2130 <code>a[x]</code> is the element type of <code>A</code>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002131</ul>
Russ Coxeaa92e02009-06-25 14:43:55 -07002132
Robert Griesemerc2d55862009-02-19 16:49:10 -08002133<p>
Russ Coxeaa92e02009-06-25 14:43:55 -07002134For <code>a</code> of type <code>T</code>
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002135where <code>T</code> is a string type (§<a href="#Strings">Strings</a>):
Russ Coxeaa92e02009-06-25 14:43:55 -07002136</p>
2137<ul>
2138 <li><code>x</code> must be an integer value and <code>0 &lt;= x &lt; len(a)</code>
2139 <li><code>a[x]</code> is the byte at index <code>x</code> and the type of
2140 <code>a[x]</code> is <code>byte</code>
2141 <li><code>a[x]</code> may not be assigned to
2142</ul>
2143
2144<p>
2145For <code>a</code> of type <code>M</code>
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002146where <code>M</code> is a map type (§<a href="#Map_types">Map types</a>):
Rob Pike4501d342009-02-19 17:31:36 -08002147</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002148<ul>
Robert Griesemer533dfd62009-05-13 16:56:00 -07002149 <li><code>x</code>'s type must be compatible with the key type of <code>M</code>
Rob Pikedf3183f2009-02-26 16:37:23 -08002150 and the map must contain an entry with key <code>x</code> (but see special forms below)
2151 <li><code>a[x]</code> is the map value with key <code>x</code>
2152 and the type of <code>a[x]</code> is the value type of <code>M</code>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002153</ul>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002154
Robert Griesemerc2d55862009-02-19 16:49:10 -08002155<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002156Otherwise <code>a[x]</code> is illegal. If the index or key is out of range evaluating
2157an otherwise legal index expression, a run-time exception occurs.
2158</p>
Robert Griesemer7abfcd92008-10-07 17:14:30 -07002159
Rob Pikedf3183f2009-02-26 16:37:23 -08002160<p>
2161However, if an index expression on a map <code>a</code> of type <code>map[K] V</code>
Rob Piked5537072009-08-22 00:04:04 -07002162is used in an assignment or initialization of the form
Rob Pikedf3183f2009-02-26 16:37:23 -08002163</p>
2164
2165<pre>
2166r, ok = a[x]
2167r, ok := a[x]
Rob Piked5537072009-08-22 00:04:04 -07002168var r, ok = a[x]
Rob Pikedf3183f2009-02-26 16:37:23 -08002169</pre>
2170
2171<p>
2172the result of the index expression is a pair of values with types
2173<code>(K, bool)</code>.
2174If the key is present in the map,
2175the expression returns the pair <code>(a[x], true)</code>;
2176otherwise it returns <code>(Z, false)</code> where <code>Z</code> is
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002177the zero value for <code>V</code><a href="#The_zero_value">The zero value</a>).
Rob Pikedf3183f2009-02-26 16:37:23 -08002178No run-time exception occurs in this case.
2179The index expression in this construct thus acts like a function call
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002180returning a value and a boolean indicating success. (§<a href="#Assignments">Assignments</a>)
Rob Pikedf3183f2009-02-26 16:37:23 -08002181</p>
2182
2183<p>
2184Similarly, if an assignment to a map has the special form
2185</p>
2186
2187<pre>
2188a[x] = r, ok
2189</pre>
2190
2191<p>
2192and boolean <code>ok</code> has the value <code>false</code>,
2193the entry for key <code>x</code> is deleted from the map; if
2194<code>ok</code> is <code>true</code>, the construct acts like
2195a regular assignment to an element of the map.
2196</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002197
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002198<h3 id="Slices">Slices</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002199
Rob Pikedf3183f2009-02-26 16:37:23 -08002200<p>
Rob Pike8f2330d2009-02-25 16:20:44 -08002201Strings, arrays, and slices can be <i>sliced</i> to construct substrings or descriptors
Robert Griesemer633957b2009-01-06 13:23:20 -08002202of subarrays. The index expressions in the slice select which elements appear
2203in the result. The result has indexes starting at 0 and length equal to the
Rob Pikedf3183f2009-02-26 16:37:23 -08002204difference in the index values in the slice. After slicing the array <code>a</code>
2205</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002206
Robert Griesemerc2d55862009-02-19 16:49:10 -08002207<pre>
Rob Pike426335f2009-03-02 17:52:52 -08002208a := [4]int{1, 2, 3, 4};
Robert Griesemerc2d55862009-02-19 16:49:10 -08002209s := a[1:3];
2210</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002211
Rob Pikedf3183f2009-02-26 16:37:23 -08002212<p>
Russ Cox5958dd62009-03-04 17:19:21 -08002213the slice <code>s</code> has type <code>[]int</code>, length 2, capacity 3, and elements
Rob Pikedf3183f2009-02-26 16:37:23 -08002214</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002215
Robert Griesemerc2d55862009-02-19 16:49:10 -08002216<pre>
2217s[0] == 2
2218s[1] == 3
2219</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002220
Rob Pikedf3183f2009-02-26 16:37:23 -08002221<p>
Rob Pike8f2330d2009-02-25 16:20:44 -08002222The slice length must be non-negative.
Russ Cox5958dd62009-03-04 17:19:21 -08002223For arrays or strings, the indexes
Rob Pike811dd252009-03-04 20:39:39 -08002224<code>lo</code> and <code>hi</code> must satisfy
22250 &lt;= <code>lo</code> &lt;= <code>hi</code> &lt;= length;
Russ Cox5958dd62009-03-04 17:19:21 -08002226for slices, the upper bound is the capacity rather than the length.
Robert Griesemerc2d55862009-02-19 16:49:10 -08002227<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002228If the sliced operand is a string, the result of the slice operation is another, new
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002229string (§<a href="#Strings">Strings</a>). If the sliced operand is an array or slice, the result
2230of the slice operation is a slice (§<a href="#Slice_types">Slice types</a>).
Rob Pikedf3183f2009-02-26 16:37:23 -08002231</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002232
2233
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002234<h3 id="Type_assertions">Type assertions</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002235
Rob Pikedf3183f2009-02-26 16:37:23 -08002236<p>
2237For an expression <code>x</code> and a type <code>T</code>, the primary expression
2238</p>
Robert Griesemer434c6052008-11-07 13:34:37 -08002239
Robert Griesemerc2d55862009-02-19 16:49:10 -08002240<pre>
2241x.(T)
2242</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -08002243
Robert Griesemerc2d55862009-02-19 16:49:10 -08002244<p>
Russ Coxb89a54e2009-05-20 18:16:04 -07002245asserts that <code>x</code> is not the zero interface value
2246and that the value stored in <code>x</code> is of type <code>T</code>.
Russ Cox5958dd62009-03-04 17:19:21 -08002247The notation <code>x.(T)</code> is called a <i>type assertion</i>.
2248The type of <code>x</code> must be an interface type.
Rob Pikedf3183f2009-02-26 16:37:23 -08002249</p>
2250<p>
Russ Cox5958dd62009-03-04 17:19:21 -08002251More precisely, if <code>T</code> is not an interface type, <code>x.(T)</code> asserts
Rob Pikedf3183f2009-02-26 16:37:23 -08002252that the dynamic type of <code>x</code> is identical to the type <code>T</code>
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002253<a href="#Type_identity_and_compatibility">Type identity and compatibility</a>).
Russ Cox5958dd62009-03-04 17:19:21 -08002254If <code>T</code> is an interface type, <code>x.(T)</code> asserts that the dynamic type
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002255of <code>T</code> implements the interface <code>T</code><a href="#Interface_types">Interface types</a>).
Rob Pikedf3183f2009-02-26 16:37:23 -08002256</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002257<p>
Russ Cox5958dd62009-03-04 17:19:21 -08002258If the type assertion holds, the value of the expression is the value
2259stored in <code>x</code> and its type is <code>T</code>. If the type assertion is false, a run-time
Rob Pikedf3183f2009-02-26 16:37:23 -08002260exception occurs. In other words, even though the dynamic type of <code>x</code>
Russ Cox5958dd62009-03-04 17:19:21 -08002261is known only at run-time, the type of <code>x.(T)</code> is
Rob Pikedf3183f2009-02-26 16:37:23 -08002262known to be <code>T</code> in a correct program.
2263</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002264<p>
Rob Piked5537072009-08-22 00:04:04 -07002265If a type assertion is used in an assignment or initialization of the form
Rob Pikedf3183f2009-02-26 16:37:23 -08002266</p>
Robert Griesemer434c6052008-11-07 13:34:37 -08002267
Robert Griesemerc2d55862009-02-19 16:49:10 -08002268<pre>
2269v, ok = x.(T)
2270v, ok := x.(T)
Rob Piked5537072009-08-22 00:04:04 -07002271var v, ok = x.(T)
Robert Griesemerc2d55862009-02-19 16:49:10 -08002272</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -08002273
Robert Griesemerc2d55862009-02-19 16:49:10 -08002274<p>
Russ Cox5958dd62009-03-04 17:19:21 -08002275the result of the assertion is a pair of values with types <code>(T, bool)</code>.
2276If the assertion holds, the expression returns the pair <code>(x.(T), true)</code>;
Rob Pikedf3183f2009-02-26 16:37:23 -08002277otherwise, the expression returns <code>(Z, false)</code> where <code>Z</code>
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002278is the zero value for type <code>T</code><a href="#The_zero_value">The zero value</a>).
Rob Pikedf3183f2009-02-26 16:37:23 -08002279No run-time exception occurs in this case.
Russ Cox5958dd62009-03-04 17:19:21 -08002280The type assertion in this construct thus acts like a function call
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002281returning a value and a boolean indicating success. (§<a href="#Assignments">Assignments</a>)
Rob Pikedf3183f2009-02-26 16:37:23 -08002282</p>
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -07002283
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002284
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002285<h3 id="Calls">Calls</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002286
Robert Griesemerc2d55862009-02-19 16:49:10 -08002287<p>
Rob Pike96750f12009-02-27 16:47:48 -08002288Given an expression <code>f</code> of function type
2289<code>F</code>,
Rob Pikedf3183f2009-02-26 16:37:23 -08002290</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002291
Robert Griesemerc2d55862009-02-19 16:49:10 -08002292<pre>
Rob Pike96750f12009-02-27 16:47:48 -08002293f(a1, a2, ... an)
Robert Griesemerc2d55862009-02-19 16:49:10 -08002294</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002295
Robert Griesemerc2d55862009-02-19 16:49:10 -08002296<p>
Rob Pike96750f12009-02-27 16:47:48 -08002297calls <code>f</code> with arguments <code>a1, a2, ... an</code>.
2298The arguments must be single-valued expressions
Robert Griesemer4ed666e2009-08-27 16:45:42 -07002299<a href="#Assignment_compatibility">assignment compatible</a> with the parameters of
Rob Pikedf3183f2009-02-26 16:37:23 -08002300<code>F</code> and are evaluated before the function is called.
2301The type of the expression is the result type
2302of <code>F</code>.
2303A method invocation is similar but the method itself
2304is specified as a selector upon a value of the receiver type for
2305the method.
2306</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002307
Robert Griesemerc2d55862009-02-19 16:49:10 -08002308<pre>
Rob Pikedf3183f2009-02-26 16:37:23 -08002309Atan2(x, y) // function call
2310var pt *Point;
2311pt.Scale(3.5) // method call with receiver pt
Robert Griesemerc2d55862009-02-19 16:49:10 -08002312</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002313
Rob Pikedf3183f2009-02-26 16:37:23 -08002314<p>
Robert Griesemer56809d02009-05-20 11:02:48 -07002315A method call <code>x.m()</code> is valid if the method set of
2316(the type of) <code>x</code> contains <code>m</code> (and the
2317argument list is compatible with the parameter list of <code>m</code>).
2318If <code>x</code> is addressable and <code>&amp;x</code>'s method
2319set contains <code>m</code>, <code>x.m()</code> is shorthand
2320for <code>(&amp;x).m()</code>:
Rob Pikedf3183f2009-02-26 16:37:23 -08002321</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002322
Robert Griesemerc2d55862009-02-19 16:49:10 -08002323<pre>
Rob Pikedf3183f2009-02-26 16:37:23 -08002324var p Point;
2325p.Scale(3.5)
Robert Griesemerc2d55862009-02-19 16:49:10 -08002326</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002327
Robert Griesemerc2d55862009-02-19 16:49:10 -08002328<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002329There is no distinct method type and there are no method literals.
Rob Pikedf3183f2009-02-26 16:37:23 -08002330</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002331
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002332<h3 id="Passing_arguments_to_..._parameters">Passing arguments to <code>...</code> parameters</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002333
Robert Griesemerc2d55862009-02-19 16:49:10 -08002334<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002335When a function <code>f</code> has a <code>...</code> parameter,
2336it is always the last formal parameter. Within calls to <code>f</code>,
2337the arguments before the <code>...</code> are treated normally.
2338After those, an arbitrary number (including zero) of trailing
2339arguments may appear in the call and are bound to the <code>...</code>
2340parameter.
2341</p>
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002342
Rob Pikedf3183f2009-02-26 16:37:23 -08002343<p>
2344Within <code>f</code>, the <code>...</code> parameter has static
2345type <code>interface{}</code> (the empty interface). For each call,
2346its dynamic type is a structure whose sequential fields are the
2347trailing arguments of the call. That is, the actual arguments
2348provided for a <code>...</code> parameter are wrapped into a struct
2349that is passed to the function instead of the actual arguments.
2350Using the reflection library (TODO: reference), <code>f</code> may
2351unpack the elements of the dynamic type to recover the actual
2352arguments.
2353</p>
2354
2355<p>
2356Given the function and call
2357</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002358<pre>
Rob Pikefb24d792009-05-08 11:21:20 -07002359func Fprintf(f io.Writer, format string, args ...)
Rob Pikedf3183f2009-02-26 16:37:23 -08002360Fprintf(os.Stdout, "%s %d", "hello", 23);
Robert Griesemerc2d55862009-02-19 16:49:10 -08002361</pre>
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002362
Rob Pikedf3183f2009-02-26 16:37:23 -08002363<p>
2364Within <code>Fprintf</code>, the dynamic type of <code>args</code> for this
2365call will be, schematically,
2366<code> struct { string; int }</code>.
2367</p>
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002368
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002369
Rob Pikedf3183f2009-02-26 16:37:23 -08002370<p>
2371As a special case, if a function passes its own <code>...</code> parameter as the argument
2372for a <code>...</code> in a call to another function with a <code>...</code> parameter,
2373the parameter is not wrapped again but passed directly. In short, a formal <code>...</code>
2374parameter is passed unchanged as an actual <code>...</code> parameter.
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002375
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002376<h3 id="Operators">Operators</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002377
Rob Pikedf3183f2009-02-26 16:37:23 -08002378<p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002379Operators combine operands into expressions.
Rob Pikedf3183f2009-02-26 16:37:23 -08002380</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002381
Robert Griesemerf7ac31362009-07-10 16:06:40 -07002382<pre class="ebnf">
Russ Cox5958dd62009-03-04 17:19:21 -08002383Expression = UnaryExpr | Expression binary_op UnaryExpr .
Rob Pikedf3183f2009-02-26 16:37:23 -08002384UnaryExpr = PrimaryExpr | unary_op UnaryExpr .
Robert Griesemer57b34612008-10-10 12:45:44 -07002385
Rob Pikedf3183f2009-02-26 16:37:23 -08002386binary_op = log_op | com_op | rel_op | add_op | mul_op .
2387log_op = "||" | "&amp;&amp;" .
2388com_op = "&lt;-" .
2389rel_op = "==" | "!=" | "&lt;" | "&lt;=" | ">" | ">=" .
2390add_op = "+" | "-" | "|" | "^" .
Robert Griesemer4ed666e2009-08-27 16:45:42 -07002391mul_op = "*" | "/" | "%" | "&lt;&lt;" | "&gt;&gt;" | "&amp;" | "&amp;^" .
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002392
Rob Pikedf3183f2009-02-26 16:37:23 -08002393unary_op = "+" | "-" | "!" | "^" | "*" | "&amp;" | "&lt;-" .
Robert Griesemerc2d55862009-02-19 16:49:10 -08002394</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002395
Robert Griesemerc2d55862009-02-19 16:49:10 -08002396<p>
Rob Pike83cbca52009-08-21 14:18:08 -07002397Comparisons are discussed elsewhere
2398<a href="#Comparison_compatibility">Comparison compatibility</a>).
2399For other binary operators, the
2400operand types must be identical
2401<a href="#Properties_of_types_and_values">Properties of types and values</a>)
2402unless the operation involves
2403channels, shifts, or ideal constants.
Rob Pike4501d342009-02-19 17:31:36 -08002404</p>
Robert Griesemerc8e18762008-09-12 12:26:22 -07002405
Rob Pike83cbca52009-08-21 14:18:08 -07002406<p>
2407In a channel send, the first operand is always a channel and the
2408second is a value of the channel's element type.
2409</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002410
Rob Pike83cbca52009-08-21 14:18:08 -07002411<p>
2412Except for shift operations,
2413if one operand has ideal type and the other operand does not,
Rob Pike0262f882009-08-24 16:12:59 -07002414the ideal operand is converted to match the type of
Rob Pike83cbca52009-08-21 14:18:08 -07002415the other operand (§<a href="#Expressions">Expressions</a>).
2416If both operands are ideal numbers and one is an
2417ideal float, the other is converted to ideal float
2418(relevant for <code>/</code> and <code>%</code>).
2419</p>
Robert Griesemer7539c852009-07-31 18:05:07 -07002420
Rob Pike83cbca52009-08-21 14:18:08 -07002421<p>
2422The right operand in a shift operation must have unsigned integer type
2423or be an ideal number that can be converted to unsigned integer type
2424<a href="#Arithmetic_operators">Arithmetic operators</a>).
2425</p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002426
Rob Pike83cbca52009-08-21 14:18:08 -07002427<p>
2428If the left operand of a non-constant shift operation is an ideal number,
2429the type of the ideal number
2430is what it would be if the shift operation were replaced by the left operand alone.
2431</p>
Robert Griesemera6b546f2008-10-20 11:46:40 -07002432
Rob Pike83cbca52009-08-21 14:18:08 -07002433<pre>
2434var s uint = 33;
2435var i = 1&lt;&lt;s; // 1 has type int
2436var j = int32(1&lt;&lt;s); // 1 has type int32; j == 0
2437var u = uint64(1&lt;&lt;s); // 1 has type uint64; u == 1&lt;&lt;33
2438var f = float(1&lt;&lt;s); // illegal: 1 has type float, cannot shift
2439var g = float(1&lt;&lt;33); // legal; 1&lt;&lt;33 is a constant shift operation; g == 1&lt;&lt;33
2440</pre>
Robert Griesemer18b05c12009-01-26 09:34:19 -08002441
Robert Griesemerc2d55862009-02-19 16:49:10 -08002442<p>
Russ Coxec9b0422009-07-09 16:44:13 -07002443Unary operators have the highest precedence.
2444As the <code>++</code> and <code>--</code> operators form
Rob Pikedf3183f2009-02-26 16:37:23 -08002445statements, not expressions, they fall
Russ Coxec9b0422009-07-09 16:44:13 -07002446outside the operator hierarchy.
Rob Pikedf3183f2009-02-26 16:37:23 -08002447As a consequence, statement <code>*p++</code> is the same as <code>(*p)++</code>.
2448<p>
2449There are six precedence levels for binary operators.
2450Multiplication operators bind strongest, followed by addition
Robert Griesemerad711102008-09-11 17:48:20 -07002451operators, comparison operators, communication operators,
Rob Pikedf3183f2009-02-26 16:37:23 -08002452<code>&amp;&amp;</code> (logical and), and finally <code>||</code> (logical or):
2453</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002454
Rob Pikeff70f092009-02-20 13:36:14 -08002455<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08002456Precedence Operator
Robert Griesemer4ed666e2009-08-27 16:45:42 -07002457 6 * / % &lt;&lt; &gt;&gt; &amp; &amp;^
Robert Griesemerc2d55862009-02-19 16:49:10 -08002458 5 + - | ^
2459 4 == != &lt; &lt;= > >=
2460 3 &lt;-
2461 2 &amp;&amp;
2462 1 ||
2463</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002464
Robert Griesemerc2d55862009-02-19 16:49:10 -08002465<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002466Binary operators of the same precedence associate from left to right.
2467For instance, <code>x / y / z</code> is the same as <code>(x / y) / z</code>.
2468</p>
2469<p>
2470Examples:
2471</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002472
Robert Griesemerc2d55862009-02-19 16:49:10 -08002473<pre>
2474+x
247523 + 3*x[i]
2476x &lt;= f()
Robert Griesemer4ed666e2009-08-27 16:45:42 -07002477^a &gt;&gt; b
Robert Griesemerc2d55862009-02-19 16:49:10 -08002478f() || g()
2479x == y + 1 &amp;&amp; &lt;-chan_ptr > 0
2480</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002481
2482
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002483<h3 id="Arithmetic_operators">Arithmetic operators</h3>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002484<p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002485Arithmetic operators apply to numeric types and yield a result of the same
Rob Pikedf3183f2009-02-26 16:37:23 -08002486type as the first operand. The four standard arithmetic operators (<code>+</code>,
2487<code>-</code>, <code>*</code>, <code>/</code>) apply both to integer and
2488floating point types, while <code>+</code> applies also
2489to strings; all other arithmetic operators apply to integers only.
2490</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002491
Rob Pikeff70f092009-02-20 13:36:14 -08002492<pre class="grammar">
Rob Pike307ec212009-03-12 15:53:56 -07002493+ sum integers, floats, strings
2494- difference integers, floats
2495* product integers, floats
2496/ quotient integers, floats
2497% remainder integers
Robert Griesemerc2d55862009-02-19 16:49:10 -08002498
Rob Pike307ec212009-03-12 15:53:56 -07002499&amp; bitwise and integers
2500| bitwise or integers
2501^ bitwise xor integers
2502&amp;^ bit clear (and not) integers
Robert Griesemerc2d55862009-02-19 16:49:10 -08002503
Robert Griesemer4ed666e2009-08-27 16:45:42 -07002504&lt;&lt; left shift integer &lt;&lt; unsigned integer
2505&gt;&gt; right shift integer &gt;&gt; unsigned integer
Robert Griesemerc2d55862009-02-19 16:49:10 -08002506</pre>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002507
Rob Pikedf3183f2009-02-26 16:37:23 -08002508<p>
2509Strings can be concatenated using the <code>+</code> operator
2510or the <code>+=</code> assignment operator:
2511</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002512
Robert Griesemerc2d55862009-02-19 16:49:10 -08002513<pre>
Rob Pikedf3183f2009-02-26 16:37:23 -08002514s := "hi" + string(c);
2515s += " and good bye";
Robert Griesemerc2d55862009-02-19 16:49:10 -08002516</pre>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002517
Robert Griesemerc2d55862009-02-19 16:49:10 -08002518<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002519String addition creates a new string by concatenating the operands.
2520</p>
2521<p>
2522For integer values, <code>/</code> and <code>%</code> satisfy the following relationship:
2523</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002524
Robert Griesemerc2d55862009-02-19 16:49:10 -08002525<pre>
2526(a / b) * b + a % b == a
2527</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002528
Rob Pikedf3183f2009-02-26 16:37:23 -08002529<p>
2530with <code>(a / b)</code> truncated towards zero.
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002531Examples:
Rob Pikedf3183f2009-02-26 16:37:23 -08002532</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002533
Robert Griesemerc2d55862009-02-19 16:49:10 -08002534<pre>
2535 x y x / y x % y
2536 5 3 1 2
2537-5 3 -1 -2
2538 5 -3 -1 2
2539-5 -3 1 -2
2540</pre>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002541
Rob Pikedf3183f2009-02-26 16:37:23 -08002542<p>
2543If the dividend is positive and the divisor is a constant power of 2,
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002544the division may be replaced by a left shift, and computing the remainder may
2545be replaced by a bitwise "and" operation:
Rob Pikedf3183f2009-02-26 16:37:23 -08002546</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002547
Robert Griesemerc2d55862009-02-19 16:49:10 -08002548<pre>
Robert Griesemer4ed666e2009-08-27 16:45:42 -07002549 x x / 4 x % 4 x &gt;&gt; 2 x &amp; 3
Robert Griesemerc2d55862009-02-19 16:49:10 -08002550 11 2 3 2 3
2551-11 -2 -3 -3 1
2552</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002553
Rob Pikedf3183f2009-02-26 16:37:23 -08002554<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002555The shift operators shift the left operand by the shift count specified by the
2556right operand. They implement arithmetic shifts if the left operand is a signed
Rob Pikedf3183f2009-02-26 16:37:23 -08002557integer and logical shifts if it is an unsigned integer. The shift count must
2558be an unsigned integer. There is no upper limit on the shift count. Shifts behave
2559as if the left operand is shifted <code>n</code> times by 1 for a shift
2560count of <code>n</code>.
Robert Griesemer4ed666e2009-08-27 16:45:42 -07002561As a result, <code>x &lt;&lt; 1</code> is the same as <code>x*2</code>
2562and <code>x &gt;&gt; 1</code> is the same as
Rob Pikedf3183f2009-02-26 16:37:23 -08002563<code>x/2</code> truncated towards negative infinity.
2564</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002565
Rob Pikedf3183f2009-02-26 16:37:23 -08002566<p>
2567For integer operands, the unary operators
2568<code>+</code>, <code>-</code>, and <code>^</code> are defined as
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002569follows:
Rob Pikedf3183f2009-02-26 16:37:23 -08002570</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002571
Rob Pikeff70f092009-02-20 13:36:14 -08002572<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08002573+x is 0 + x
2574-x negation is 0 - x
Russ Coxd83dc4f2009-05-29 16:04:16 -07002575^x bitwise complement is m ^ x with m = "all bits set to 1" for unsigned x
2576 and m = -1 for signed x
Robert Griesemerc2d55862009-02-19 16:49:10 -08002577</pre>
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002578
Russ Cox5958dd62009-03-04 17:19:21 -08002579<p>
2580For floating point numbers,
2581<code>+x</code> is the same as <code>x</code>,
2582while <code>-x</code> is the negation of <code>x</code>.
2583</p>
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002584
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002585<h3 id="Integer_overflow">Integer overflow</h3>
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002586
Rob Pikedf3183f2009-02-26 16:37:23 -08002587<p>
2588For unsigned integer values, the operations <code>+</code>,
2589<code>-</code>, <code>*</code>, and <code>&lt;&lt;</code> are
2590computed modulo 2<sup><i>n</i></sup>, where <i>n</i> is the bit width of
2591the unsigned integer's type
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002592<a href="#Numeric_types">Numeric types</a>). Loosely speaking, these unsigned integer operations
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002593discard high bits upon overflow, and programs may rely on ``wrap around''.
Rob Pikedf3183f2009-02-26 16:37:23 -08002594</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002595<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002596For signed integers, the operations <code>+</code>,
2597<code>-</code>, <code>*</code>, and <code>&lt;&lt;</code> may legally
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002598overflow and the resulting value exists and is deterministically defined
2599by the signed integer representation, the operation, and its operands.
Rob Pikedf3183f2009-02-26 16:37:23 -08002600No exception is raised as a result of overflow. A
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002601compiler may not optimize code under the assumption that overflow does
Rob Pikedf3183f2009-02-26 16:37:23 -08002602not occur. For instance, it may not assume that <code>x &lt; x + 1</code> is always true.
2603</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002604
2605
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002606<h3 id="Comparison_operators">Comparison operators</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002607
Rob Pike5af7de32009-02-24 15:17:59 -08002608<p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002609Comparison operators yield a boolean result. All comparison operators apply
Rob Pike5af7de32009-02-24 15:17:59 -08002610to basic types except bools.
2611The operators <code>==</code> and <code>!=</code> apply, at least in some cases,
2612to all types except arrays and structs.
2613</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002614
Rob Pikeff70f092009-02-20 13:36:14 -08002615<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08002616== equal
2617!= not equal
2618< less
2619<= less or equal
2620> greater
2621>= greater or equal
2622</pre>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002623
Rob Pike5af7de32009-02-24 15:17:59 -08002624<p>
2625Numeric basic types are compared in the usual way.
2626</p>
2627<p>
Robert Griesemer52a54802008-09-30 13:02:50 -07002628Strings are compared byte-wise (lexically).
Rob Pike5af7de32009-02-24 15:17:59 -08002629</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002630<p>
Robert Griesemer18b05c12009-01-26 09:34:19 -08002631Booleans are equal if they are either both "true" or both "false".
Rob Pike5af7de32009-02-24 15:17:59 -08002632</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002633<p>
Rob Pike5af7de32009-02-24 15:17:59 -08002634The rules for comparison of composite types are described in the
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002635section on §<a href="#Comparison_compatibility">Comparison compatibility</a>.
Rob Pike5af7de32009-02-24 15:17:59 -08002636</p>
Robert Griesemera3294712009-01-05 11:17:26 -08002637
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002638
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002639<h3 id="Logical_operators">Logical operators</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002640
Rob Pikedf3183f2009-02-26 16:37:23 -08002641<p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002642Logical operators apply to boolean operands and yield a boolean result.
2643The right operand is evaluated conditionally.
Rob Pikedf3183f2009-02-26 16:37:23 -08002644</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002645
Rob Pikeff70f092009-02-20 13:36:14 -08002646<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08002647&amp;&amp; conditional and p &amp;&amp; q is "if p then q else false"
2648|| conditional or p || q is "if p then true else q"
2649! not !p is "not p"
2650</pre>
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -07002651
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002652
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002653<h3 id="Address_operators">Address operators</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002654
Rob Pikedf3183f2009-02-26 16:37:23 -08002655<p>
Rob Pikeafee1c52009-03-20 17:41:25 -07002656The unary prefix address-of operator <code>&amp;</code> generates the address of its operand, which must be a variable,
2657pointer indirection, field selector, or array or slice indexing operation. It is illegal to take the address of a function
2658result variable.
2659Given an operand of pointer type, the unary prefix pointer indirection operator <code>*</code> retrieves the value pointed
2660to by the operand.
2661</p>
2662
2663<pre>
2664&amp;x
2665&amp;a[f(2)]
2666*p
2667*pf(x)
2668</pre>
2669
Robert Griesemerc2d55862009-02-19 16:49:10 -08002670<p>
2671<font color=red>TODO: This text needs to be cleaned up and go elsewhere, there are no address
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002672operators involved.
Robert Griesemerc2d55862009-02-19 16:49:10 -08002673</font>
Rob Pikeafee1c52009-03-20 17:41:25 -07002674</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002675<p>
Rob Pikeafee1c52009-03-20 17:41:25 -07002676Methods are a form of function and a method ``value'' has a function type.
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002677Consider the type T with method M:
Rob Pikeafee1c52009-03-20 17:41:25 -07002678</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002679
Robert Griesemerc2d55862009-02-19 16:49:10 -08002680<pre>
2681type T struct {
2682 a int;
2683}
2684func (tp *T) M(a int) int;
2685var t *T;
2686</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002687
Rob Pikeafee1c52009-03-20 17:41:25 -07002688<p>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002689To construct the value of method M, one writes
Rob Pikeafee1c52009-03-20 17:41:25 -07002690</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002691
Robert Griesemerc2d55862009-02-19 16:49:10 -08002692<pre>
2693t.M
2694</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002695
Rob Pikeafee1c52009-03-20 17:41:25 -07002696<p>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002697using the variable t (not the type T).
Robert Griesemerc2d55862009-02-19 16:49:10 -08002698<font color=red>TODO: It makes perfect sense to be able to say T.M (in fact, it makes more
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002699sense then t.M, since only the type T is needed to find the method M, i.e.,
2700its address). TBD.
Robert Griesemerc2d55862009-02-19 16:49:10 -08002701</font>
Rob Pikeafee1c52009-03-20 17:41:25 -07002702</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002703
Rob Pikeafee1c52009-03-20 17:41:25 -07002704<p>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002705The expression t.M is a function value with type
Rob Pikeafee1c52009-03-20 17:41:25 -07002706</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002707
Robert Griesemerc2d55862009-02-19 16:49:10 -08002708<pre>
2709func (t *T, a int) int
2710</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002711
Rob Pikeafee1c52009-03-20 17:41:25 -07002712<p>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002713and may be invoked only as a function, not as a method:
Rob Pikeafee1c52009-03-20 17:41:25 -07002714</p>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002715
Robert Griesemerc2d55862009-02-19 16:49:10 -08002716<pre>
2717var f func (t *T, a int) int;
2718f = t.M;
2719x := f(t, 7);
2720</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002721
Rob Pikeafee1c52009-03-20 17:41:25 -07002722<p>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002723Note that one does not write t.f(7); taking the value of a method demotes
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002724it to a function.
Rob Pikeafee1c52009-03-20 17:41:25 -07002725</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002726
Rob Pikeafee1c52009-03-20 17:41:25 -07002727<p>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002728In general, given type T with method M and variable t of type T,
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002729the method invocation
Rob Pikeafee1c52009-03-20 17:41:25 -07002730</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002731
Robert Griesemerc2d55862009-02-19 16:49:10 -08002732<pre>
2733t.M(args)
2734</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002735
Rob Pikeafee1c52009-03-20 17:41:25 -07002736<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002737is equivalent to the function call
Rob Pikeafee1c52009-03-20 17:41:25 -07002738</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002739
Robert Griesemerc2d55862009-02-19 16:49:10 -08002740<pre>
2741(t.M)(t, args)
2742</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002743
Rob Pikeafee1c52009-03-20 17:41:25 -07002744<p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002745<font color=red>
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002746TODO: should probably describe the effect of (t.m) under §<a href="#Expressions_if_t">Expressions if t</a>.m
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002747denotes a method: Effect is as described above, converts into function.
Robert Griesemerc2d55862009-02-19 16:49:10 -08002748</font>
Rob Pikeafee1c52009-03-20 17:41:25 -07002749</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002750<p>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002751If T is an interface type, the expression t.M does not determine which
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002752underlying type's M is called until the point of the call itself. Thus given
Robert Griesemerd8a764c2009-02-06 17:01:10 -08002753T1 and T2, both implementing interface I with method M, the sequence
Rob Pikeafee1c52009-03-20 17:41:25 -07002754</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002755
Robert Griesemerc2d55862009-02-19 16:49:10 -08002756<pre>
2757var t1 *T1;
2758var t2 *T2;
2759var i I = t1;
2760m := i.M;
2761m(t2, 7);
2762</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002763
Rob Pikeafee1c52009-03-20 17:41:25 -07002764<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002765will invoke t2.M() even though m was constructed with an expression involving
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002766t1. Effectively, the value of m is a function literal
Rob Pikeafee1c52009-03-20 17:41:25 -07002767</p>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002768
Robert Griesemerc2d55862009-02-19 16:49:10 -08002769<pre>
2770func (recv I, a int) {
2771 recv.M(a);
2772}
2773</pre>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002774
Rob Pikeafee1c52009-03-20 17:41:25 -07002775<p>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002776that is automatically created.
Rob Pikeafee1c52009-03-20 17:41:25 -07002777</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002778<p>
2779<font color=red>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002780TODO: Document implementation restriction: It is illegal to take the address
Robert Griesemerc2d55862009-02-19 16:49:10 -08002781of a result parameter (e.g.: func f() (x int, p *int) { return 2, &amp;x }).
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002782(TBD: is it an implementation restriction or fact?)
Robert Griesemerc2d55862009-02-19 16:49:10 -08002783</font>
Rob Pikeafee1c52009-03-20 17:41:25 -07002784</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002785
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002786<h3 id="Communication_operators">Communication operators</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002787
Robert Griesemerc2d55862009-02-19 16:49:10 -08002788<p>
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002789The term <i>channel</i> means "variable of channel type" (§<a href="#Channel_types">Channel types</a>).
Rob Pikedf3183f2009-02-26 16:37:23 -08002790</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002791<p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002792The send operation uses the binary operator "&lt;-", which operates on
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002793a channel and a value (expression):
Rob Pikedf3183f2009-02-26 16:37:23 -08002794</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002795
Robert Griesemerc2d55862009-02-19 16:49:10 -08002796<pre>
2797ch <- 3
2798</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002799
Rob Pikedf3183f2009-02-26 16:37:23 -08002800<p>
2801The send operation sends the value on the channel. Both the channel
2802and the expression are evaluated before communication begins.
2803Communication blocks until the send can proceed, at which point the
2804value is transmitted on the channel. A send can proceed if the
2805channel is asynchronous and there is room in its buffer or the
2806channel is synchronous and a receiver is ready.
2807</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002808<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002809If the send operation appears in an expression context, the value
2810of the expression is a boolean and the operation is non-blocking.
2811The value of the boolean reports true if the communication succeeded,
Rob Pikedf3183f2009-02-26 16:37:23 -08002812false if it did not. (The channel and
2813the expression to be sent are evaluated regardless.)
2814These two examples are equivalent:
2815</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002816
Robert Griesemerc2d55862009-02-19 16:49:10 -08002817<pre>
2818ok := ch <- 3;
2819if ok { print("sent") } else { print("not sent") }
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002820
Robert Griesemerc2d55862009-02-19 16:49:10 -08002821if ch <- 3 { print("sent") } else { print("not sent") }
2822</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002823
Rob Pikedf3183f2009-02-26 16:37:23 -08002824<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002825In other words, if the program tests the value of a send operation,
2826the send is non-blocking and the value of the expression is the
2827success of the operation. If the program does not test the value,
2828the operation blocks until it succeeds.
Rob Pikedf3183f2009-02-26 16:37:23 -08002829</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002830<p>
2831The receive operation uses the prefix unary operator "&lt;-".
Rob Pikedf3183f2009-02-26 16:37:23 -08002832The value of the expression is the value received, whose type
2833is the element type of the channel.
2834</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002835
Robert Griesemerc2d55862009-02-19 16:49:10 -08002836<pre>
2837<-ch
2838</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002839
Rob Pikedf3183f2009-02-26 16:37:23 -08002840<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002841The expression blocks until a value is available, which then can
Rob Pikedf3183f2009-02-26 16:37:23 -08002842be assigned to a variable or used like any other expression.
2843If the receive expression does not save the value, the value is
2844discarded.
2845</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002846
Robert Griesemerc2d55862009-02-19 16:49:10 -08002847<pre>
2848v1 := <-ch
2849v2 = <-ch
2850f(<-ch)
Robert Griesemerc2d55862009-02-19 16:49:10 -08002851<-strobe // wait until clock pulse
2852</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002853
Rob Pikedf3183f2009-02-26 16:37:23 -08002854<p>
Rob Piked5537072009-08-22 00:04:04 -07002855If a receive expression is used in an assignment or initialization of the form
Rob Pikedf3183f2009-02-26 16:37:23 -08002856</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002857
Robert Griesemerc2d55862009-02-19 16:49:10 -08002858<pre>
Rob Piked5537072009-08-22 00:04:04 -07002859x, ok = <-ch
2860x, ok := <-ch
2861var x, ok = <-ch
Robert Griesemerc2d55862009-02-19 16:49:10 -08002862</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002863
Rob Pikedf3183f2009-02-26 16:37:23 -08002864<p>
2865the receive operation becomes non-blocking.
2866If the operation can proceeed, the boolean variable
2867<code>ok</code> will be set to <code>true</code>
2868and the value stored in <code>x</code>; otherwise
2869<code>ok</code> is set
2870to <code>false</code> and <code>x</code> is set to the
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002871zero value for its type (§<a href="#The_zero_value">The zero value</a>).
Rob Pikedf3183f2009-02-26 16:37:23 -08002872</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002873
Rob Pikedf3183f2009-02-26 16:37:23 -08002874<p>
2875<font color=red>TODO: Probably in a separate section, communication semantices
2876need to be presented regarding send, receive, select, and goroutines.</font>
2877</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002878
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002879<h3 id="Constant_expressions">Constant expressions</h3>
Robert Griesemerb90b2132008-09-19 15:49:55 -07002880
Rob Pikef27e9f02009-02-23 19:22:05 -08002881<p>
2882Constant expressions may contain only constants, <code>iota</code>,
2883numeric literals, string literals, and
2884some constant-valued built-in functions such as <code>unsafe.Sizeof</code>
2885and <code>len</code> applied to an array.
2886In practice, constant expressions are those that can be evaluated at compile time.
2887<p>
2888The type of a constant expression is determined by the type of its
Rob Pikedf3183f2009-02-26 16:37:23 -08002889elements. If it contains only numeric literals, its type is <i>ideal
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002890integer</i> or <i>ideal float</i><a href="#Ideal_number">Ideal number</a>). Whether a literal
Rob Pikece9417e2009-03-12 17:08:47 -07002891is an integer or float depends on the syntax of the literals (123 vs. 123.0).
Russ Cox5958dd62009-03-04 17:19:21 -08002892The nature of the arithmetic
Rob Pikef27e9f02009-02-23 19:22:05 -08002893operations within the expression depends, elementwise, on the values;
2894for example, 3/2 is an integer division yielding 1, while 3./2. is
2895a floating point division yielding 1.5. Thus
2896</p>
Robert Griesemerb90b2132008-09-19 15:49:55 -07002897
Robert Griesemerc2d55862009-02-19 16:49:10 -08002898<pre>
Rob Pikef27e9f02009-02-23 19:22:05 -08002899const x = 3./2. + 3/2;
Robert Griesemerc2d55862009-02-19 16:49:10 -08002900</pre>
Robert Griesemerb90b2132008-09-19 15:49:55 -07002901
Robert Griesemerc2d55862009-02-19 16:49:10 -08002902<p>
Rob Pikef27e9f02009-02-23 19:22:05 -08002903yields a floating point constant of ideal float value 2.5 (1.5 +
29041); its constituent expressions are evaluated using distinct rules
2905for division.
2906</p>
2907
2908<p>
2909Intermediate values and the constants themselves
2910may require precision significantly larger than any concrete type
2911in the language. The following are legal declarations:
2912</p>
2913
2914<pre>
Robert Griesemer4ed666e2009-08-27 16:45:42 -07002915const Huge = 1 &lt;&lt; 100;
2916const Four int8 = Huge &gt;&gt; 98;
Rob Pikef27e9f02009-02-23 19:22:05 -08002917</pre>
2918
2919<p>
2920A constant expression may appear in any context, such as assignment
2921to a variable of any numeric type, as long as the value of the
Rob Pikedf3183f2009-02-26 16:37:23 -08002922expression can be represented accurately in that context.
Rob Pikef27e9f02009-02-23 19:22:05 -08002923It is erroneous to assign a value with a non-zero fractional part
Rob Pikedf3183f2009-02-26 16:37:23 -08002924to an integer, or if the assignment would overflow or underflow,
2925or in general if the value cannot be represented by the type of
2926the variable.
2927For
2928instance, <code>3</code> can be assigned to any integer variable but also to any
2929floating point variable, while <code>-1e12</code> can be assigned to a
2930<code>float32</code>, <code>float64</code>, or even <code>int64</code>
2931but not <code>uint64</code> or <code>string</code>.
Rob Pikef27e9f02009-02-23 19:22:05 -08002932</p>
Robert Griesemerb90b2132008-09-19 15:49:55 -07002933
Rob Pike21d03492009-03-24 19:16:42 -07002934<p>
2935If a typed constant expression evaluates to a value that is not
2936representable by that type, the compiler reports an error.
2937</p>
2938
2939<pre>
2940uint8(-1) // error, out of range
2941uint8(100) * 100 // error, out of range
2942</pre>
2943
2944<p>
Russ Coxd83dc4f2009-05-29 16:04:16 -07002945The mask used by the unary bitwise complement operator matches
2946the rule for non-constants: the mask is the all 1s for unsigned constants
2947and -1 for signed and ideal constants.
Rob Pike21d03492009-03-24 19:16:42 -07002948</p>
2949
2950<pre>
2951^1 // ideal constant, equal to -2
2952uint8(^1) // error, same as uint8(-2), out of range
2953^uint8(1) // typed uint8 constant, same as 0xFF ^ uint8(1) = uint8(0xFE)
2954int8(^1) // same as int8(-2)
Russ Coxd83dc4f2009-05-29 16:04:16 -07002955^int8(1) // same as -1 ^ int8(1) = -2
Rob Pike21d03492009-03-24 19:16:42 -07002956</pre>
2957
2958<p>
2959TODO: perhaps ^ should be disallowed on non-uints instead of assuming twos complement.
2960Also it may be possible to make typed constants more like variables, at the cost of fewer
2961overflow etc. errors being caught.
2962</p>
2963
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002964<h3 id="Order_of_evaluation">Order of evaluation</h3>
Rob Pikec956e902009-04-14 20:10:49 -07002965
2966<p>
2967When evaluating the elements of an assignment or expression,
2968all function calls, method calls and
2969communication operations are evaluated in lexical left-to-right
2970order. Otherwise, the order of evaluation is unspecified.
2971</p>
2972
2973<p>
Robert Griesemer4f185492009-05-01 17:00:16 -07002974For example, in the assignment
Rob Pikec956e902009-04-14 20:10:49 -07002975</p>
2976<pre>
Robert Griesemer4f185492009-05-01 17:00:16 -07002977y[f()], ok = g(h(), i() + x[j()], <-c), k()
Rob Pikec956e902009-04-14 20:10:49 -07002978</pre>
2979<p>
Robert Griesemer4f185492009-05-01 17:00:16 -07002980the function calls and communication happen in the order
2981<code>f()</code>, <code>h()</code>, <code>i()</code>, <code>j()</code>,
2982<code><-c</code>, <code>g()</code>, and <code>k()</code>.
2983However, the order of those events compared to the evaluation
2984and indexing of <code>x</code> and the evaluation
2985of <code>y</code> is not specified.
Rob Pikec956e902009-04-14 20:10:49 -07002986</p>
2987
Rob Pikeff70f092009-02-20 13:36:14 -08002988<hr/>
Robert Griesemerb90b2132008-09-19 15:49:55 -07002989
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002990<h2 id="Statements">Statements</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002991
Rob Pike96750f12009-02-27 16:47:48 -08002992<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002993Statements control execution.
Rob Pike96750f12009-02-27 16:47:48 -08002994</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002995
Robert Griesemerf7ac31362009-07-10 16:06:40 -07002996<pre class="ebnf">
Robert Griesemerdea43942009-03-16 17:36:52 -07002997Statement =
Rob Pike11417162009-03-24 17:45:53 -07002998 Declaration | EmptyStmt | LabeledStmt |
2999 SimpleStmt | GoStmt | ReturnStmt | BreakStmt | ContinueStmt | GotoStmt |
3000 FallthroughStmt | Block | IfStmt | SwitchStmt | SelectStmt | ForStmt |
3001 DeferStmt .
Robert Griesemer7abfcd92008-10-07 17:14:30 -07003002
Robert Griesemere1b8cb82009-07-16 20:31:41 -07003003SimpleStmt = ExpressionStmt | IncDecStmt | Assignment | ShortVarDecl .
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003004
Rob Pike96750f12009-02-27 16:47:48 -08003005StatementList = Statement { Separator Statement } .
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003006Separator = [ ";" ] .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003007</pre>
Robert Griesemer7271e042008-10-09 20:05:24 -07003008
Robert Griesemerc2d55862009-02-19 16:49:10 -08003009<p>
Rob Pike96750f12009-02-27 16:47:48 -08003010Elements of a list of statements are separated by semicolons,
3011which may be omitted only if the previous statement:
Rob Pike4501d342009-02-19 17:31:36 -08003012</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08003013<ul>
Rob Pike96750f12009-02-27 16:47:48 -08003014 <li>ends with the closing parenthesis ")" of a list of declarations
Robert Griesemeraeaab592009-08-31 17:30:55 -07003015<a href="#Declarations_and_scope">Declarations and scope</a>); or</li>
Rob Pike736a1ae2009-04-02 23:03:41 -07003016 <li>ends with a closing brace "}" that is not part of an expression.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003017</ul>
Robert Griesemer7271e042008-10-09 20:05:24 -07003018
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003019
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003020<h3 id="Empty_statements">Empty statements</h3>
Robert Griesemeraed247f2008-10-08 17:05:30 -07003021
Rob Pike96750f12009-02-27 16:47:48 -08003022<p>
Robert Griesemeraed247f2008-10-08 17:05:30 -07003023The empty statement does nothing.
Rob Pike96750f12009-02-27 16:47:48 -08003024</p>
Robert Griesemeraed247f2008-10-08 17:05:30 -07003025
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003026<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003027EmptyStmt = .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003028</pre>
Robert Griesemeraed247f2008-10-08 17:05:30 -07003029
Rob Pike96750f12009-02-27 16:47:48 -08003030<p>
3031A statement list can always in effect be terminated with a semicolon by
3032adding an empty statement.
3033</p>
3034
Robert Griesemeraed247f2008-10-08 17:05:30 -07003035
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003036<h3 id="Labeled_statements">Labeled statements</h3>
Robert Griesemerdea43942009-03-16 17:36:52 -07003037
3038<p>
3039A labeled statement may be the target of a <code>goto</code>,
3040<code>break</code> or <code>continue</code> statement.
3041</p>
3042
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003043<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003044LabeledStmt = Label ":" Statement .
Robert Griesemerdea43942009-03-16 17:36:52 -07003045Label = identifier .
3046</pre>
3047
3048<pre>
3049Error: log.Fatal("error encountered")
3050</pre>
3051
3052
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003053<h3 id="Expression_statements">Expression statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003054
Rob Pike96750f12009-02-27 16:47:48 -08003055<p>
3056Function calls, method calls, and channel operations
3057can appear in statement context.
3058</p>
3059
3060
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003061<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003062ExpressionStmt = Expression .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003063</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003064
Robert Griesemerc2d55862009-02-19 16:49:10 -08003065<pre>
3066f(x+y)
Rob Pike96750f12009-02-27 16:47:48 -08003067<-ch
Robert Griesemerc2d55862009-02-19 16:49:10 -08003068</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003069
3070
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003071<h3 id="IncDec_statements">IncDec statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003072
Rob Pike96750f12009-02-27 16:47:48 -08003073<p>
Robert Griesemer52a54802008-09-30 13:02:50 -07003074The "++" and "--" statements increment or decrement their operands
Rob Pike96750f12009-02-27 16:47:48 -08003075by the ideal numeric value 1. As with an assignment, the operand
3076must be a variable, pointer indirection, field selector or index expression.
3077</p>
Robert Griesemer52a54802008-09-30 13:02:50 -07003078
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003079<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003080IncDecStmt = Expression ( "++" | "--" ) .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003081</pre>
Russ Cox5958dd62009-03-04 17:19:21 -08003082
Rob Pike96750f12009-02-27 16:47:48 -08003083<p>
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003084The following assignment statements (§<a href="#Assignments">Assignments</a>) are semantically
Robert Griesemer52a54802008-09-30 13:02:50 -07003085equivalent:
Rob Pike96750f12009-02-27 16:47:48 -08003086</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003087
Rob Pikeff70f092009-02-20 13:36:14 -08003088<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08003089IncDec statement Assignment
3090x++ x += 1
3091x-- x -= 1
3092</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003093
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003094<h3 id="Assignments">Assignments</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003095
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003096<pre class="ebnf">
Robert Griesemerc2d55862009-02-19 16:49:10 -08003097Assignment = ExpressionList assign_op ExpressionList .
Rob Pikeff70f092009-02-20 13:36:14 -08003098
Robert Griesemerc2d55862009-02-19 16:49:10 -08003099assign_op = [ add_op | mul_op ] "=" .
3100</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003101
Rob Pike96750f12009-02-27 16:47:48 -08003102<p>
3103Each left-hand side operand must be a variable, pointer indirection,
3104field selector, or index expression.
3105</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003106
Robert Griesemerc2d55862009-02-19 16:49:10 -08003107<pre>
3108x = 1
3109*p = f()
3110a[i] = 23
3111k = <-ch
Rob Pike307ec212009-03-12 15:53:56 -07003112i &amp;^= 1&lt;&lt;n
Robert Griesemerc2d55862009-02-19 16:49:10 -08003113</pre>
Rob Pike96750f12009-02-27 16:47:48 -08003114
3115<p>
3116An <i>assignment operation</i> <code>x</code> <i>op</i><code>=</code>
3117<code>y</code> where <i>op</i> is a binary arithmetic operation is equivalent
3118to <code>x</code> <code>=</code> <code>x</code> <i>op</i>
3119<code>y</code> but evalutates <code>x</code>
3120only once. The <i>op</i><code>=</code> construct is a single token.
3121</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003122
Robert Griesemerc2d55862009-02-19 16:49:10 -08003123<pre>
Robert Griesemer4ed666e2009-08-27 16:45:42 -07003124a[i] &lt;&lt;= 2
Robert Griesemerc2d55862009-02-19 16:49:10 -08003125</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003126
Rob Pike96750f12009-02-27 16:47:48 -08003127<p>
3128A tuple assignment assigns the individual elements of a multi-valued
3129operation to a list of variables. There are two forms. In the
3130first, the right hand operand is a single multi-valued expression
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003131such as a function evaluation or channel or map operation (§<a href="#Channel">Channel</a>
3132operations, §<a href="#Map_operations">Map operations</a>) or a type assertion (§<a href="#Type_assertions">Type assertions</a>).
Russ Cox5958dd62009-03-04 17:19:21 -08003133The number of operands on the left
Rob Pike96750f12009-02-27 16:47:48 -08003134hand side must match the number of values. For instance, If
3135<code>f</code> is a function returning two values,
3136</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003137
Robert Griesemerc2d55862009-02-19 16:49:10 -08003138<pre>
3139x, y = f()
3140</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003141
Rob Pike96750f12009-02-27 16:47:48 -08003142<p>
3143assigns the first value to <code>x</code> and the second to <code>y</code>.
3144</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003145
Rob Pike96750f12009-02-27 16:47:48 -08003146<p>
3147In the second form, the number of operands on the left must equal the number
Robert Griesemeref45e642009-08-21 11:25:00 -07003148of expressions on the right, each of which must be single-valued, and the
3149<i>n</i>th expression on the right is assigned to the <i>n</i>th
3150operand on the left.
Russ Cox5958dd62009-03-04 17:19:21 -08003151The expressions on the right are evaluated before assigning to
3152any of the operands on the left, but otherwise the evaluation
3153order is unspecified.
Rob Pike96750f12009-02-27 16:47:48 -08003154</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003155
Robert Griesemerc2d55862009-02-19 16:49:10 -08003156<pre>
Rob Pike96750f12009-02-27 16:47:48 -08003157a, b = b, a // exchange a and b
Robert Griesemerc2d55862009-02-19 16:49:10 -08003158</pre>
Rob Pike96750f12009-02-27 16:47:48 -08003159
3160<p>
Robert Griesemer4ed666e2009-08-27 16:45:42 -07003161In assignments, the type of each value must be
3162<a href="#Assignment_compatibility">assignment compatible</a> with the type of the
Rob Pike96750f12009-02-27 16:47:48 -08003163operand to which it is assigned.
3164</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003165
3166
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003167<h3 id="If_statements">If statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003168
Rob Pike96750f12009-02-27 16:47:48 -08003169<p>
3170"If" statements specify the conditional execution of two branches
3171according to the value of a boolean expression. If the expression
3172evaluates to true, the "if" branch is executed, otherwise, if
3173present, the "else" branch is executed. A missing condition
3174is equivalent to <code>true</code>.
3175</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003176
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003177<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003178IfStmt = "if" [ [ SimpleStmt ] ";" ] [ Expression ] Block [ "else" Statement ] .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003179</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003180
Robert Griesemerc2d55862009-02-19 16:49:10 -08003181<pre>
3182if x > 0 {
3183 return true;
3184}
3185</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003186
Russ Cox5958dd62009-03-04 17:19:21 -08003187<p>
Russ Cox16b95ba2009-08-20 10:22:52 -07003188The expression may be preceded by a simple statement, which
3189executes before the expression is evaluated.
Russ Cox5958dd62009-03-04 17:19:21 -08003190</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003191
Robert Griesemerc2d55862009-02-19 16:49:10 -08003192<pre>
3193if x := f(); x < y {
3194 return x;
3195} else if x > z {
3196 return z;
3197} else {
3198 return y;
3199}
3200</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003201
3202
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003203<h3 id="Switch_statements">Switch statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003204
Rob Pike96750f12009-02-27 16:47:48 -08003205<p>
3206"Switch" statements provide multi-way execution.
Rob Pike5a578492009-03-17 16:48:35 -07003207An expression or type specifier is compared to the "cases"
3208inside the "switch" to determine which branch
3209to execute.
Rob Pikeafee1c52009-03-20 17:41:25 -07003210</p>
Robert Griesemer091cba82009-03-19 08:39:40 -07003211
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003212<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003213SwitchStmt = ExprSwitchStmt | TypeSwitchStmt .
Robert Griesemer091cba82009-03-19 08:39:40 -07003214</pre>
3215
Rob Pikeafee1c52009-03-20 17:41:25 -07003216<p>
Rob Pike5a578492009-03-17 16:48:35 -07003217There are two forms: expression switches and type switches.
Rob Pike5a578492009-03-17 16:48:35 -07003218In an expression switch, the cases contain expressions that are compared
3219against the value of the switch expression.
3220In a type switch, the cases contain types that are compared against the
3221type of a specially annotated switch expression.
Rob Pike96750f12009-02-27 16:47:48 -08003222</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003223
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003224<h4 id="Expression_switches">Expression switches</h4>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003225
Rob Pike96750f12009-02-27 16:47:48 -08003226<p>
Rob Pike5a578492009-03-17 16:48:35 -07003227In an expression switch,
3228the switch expression is evaluated and
3229the case expressions, which need not be constants,
Robert Griesemer4f185492009-05-01 17:00:16 -07003230are evaluated left-to-right and top-to-bottom; the first one that equals the
Rob Pike5a578492009-03-17 16:48:35 -07003231switch expression
Rob Pike96750f12009-02-27 16:47:48 -08003232triggers execution of the statements of the associated case;
3233the other cases are skipped.
Rob Pike5a578492009-03-17 16:48:35 -07003234If no case matches and there is a "default" case,
3235its statements are executed.
Rob Pike96750f12009-02-27 16:47:48 -08003236There can be at most one default case and it may appear anywhere in the
3237"switch" statement.
Rob Pike70c1a102009-03-18 19:23:59 -07003238A missing expression is equivalent to
3239the expression <code>true</code>.
Rob Pike96750f12009-02-27 16:47:48 -08003240</p>
Rob Pike70c1a102009-03-18 19:23:59 -07003241
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003242<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003243ExprSwitchStmt = "switch" [ [ SimpleStmt ] ";" ] [ Expression ] "{" { ExprCaseClause } "}" .
Robert Griesemer091cba82009-03-19 08:39:40 -07003244ExprCaseClause = ExprSwitchCase ":" [ StatementList ] .
3245ExprSwitchCase = "case" ExpressionList | "default" .
Rob Pike70c1a102009-03-18 19:23:59 -07003246</pre>
3247
Rob Pike96750f12009-02-27 16:47:48 -08003248<p>
3249In a case or default clause,
3250the last statement only may be a "fallthrough" statement
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003251<a href="#Fallthrough_statement">Fallthrough statement</a>) to
Rob Pike96750f12009-02-27 16:47:48 -08003252indicate that control should flow from the end of this clause to
Robert Griesemeraed247f2008-10-08 17:05:30 -07003253the first statement of the next clause.
Rob Pike96750f12009-02-27 16:47:48 -08003254Otherwise control flows to the end of the "switch" statement.
3255</p>
Robert Griesemer0a162a12009-08-19 16:44:04 -07003256
Robert Griesemerc2d55862009-02-19 16:49:10 -08003257<p>
Russ Cox16b95ba2009-08-20 10:22:52 -07003258The expression may be preceded by a simple statement, which
3259executes before the expression is evaluated.
Rob Pike96750f12009-02-27 16:47:48 -08003260</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003261
Robert Griesemerc2d55862009-02-19 16:49:10 -08003262<pre>
3263switch tag {
Rob Pike65ec16b2009-05-29 15:46:03 -07003264default: s3()
3265case 0, 1, 2, 3: s1()
3266case 4, 5, 6, 7: s2()
Robert Griesemerc2d55862009-02-19 16:49:10 -08003267}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003268
Rob Pike96750f12009-02-27 16:47:48 -08003269switch x := f(); {
Rob Pike65ec16b2009-05-29 15:46:03 -07003270case x &lt; 0: return -x
3271default: return x
Robert Griesemerc2d55862009-02-19 16:49:10 -08003272}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003273
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003274switch { // missing expression means "true"
3275case x &lt; y: f1();
3276case x &lt; z: f2();
Russ Cox5958dd62009-03-04 17:19:21 -08003277case x == 4: f3();
Robert Griesemerc2d55862009-02-19 16:49:10 -08003278}
3279</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003280
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003281<h4 id="Type_switches">Type switches</h4>
Rob Pike70c1a102009-03-18 19:23:59 -07003282
Rob Pike5a578492009-03-17 16:48:35 -07003283<p>
Rob Pike70c1a102009-03-18 19:23:59 -07003284A type switch compares types rather than values. It is otherwise similar
Robert Griesemer1f95f0d2009-08-27 16:44:17 -07003285to an expression switch. It is marked by a special switch expression that
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003286has the form of a <a href="#Type_assertions">type assertion</a>
Rob Pike70c1a102009-03-18 19:23:59 -07003287using the reserved word <code>type</code> rather than an actual type.
3288Cases then match literal types against the dynamic type of the expression
Rob Pikef5387602009-03-30 16:08:41 -07003289in the type assertion.
Rob Pike5a578492009-03-17 16:48:35 -07003290</p>
3291
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003292<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003293TypeSwitchStmt = "switch" [ [ SimpleStmt ] ";" ] TypeSwitchGuard "{" { TypeCaseClause } "}" .
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003294TypeSwitchGuard = [ identifier ":=" ] Expression "." "(" "type" ")" .
Robert Griesemer091cba82009-03-19 08:39:40 -07003295TypeCaseClause = TypeSwitchCase ":" [ StatementList ] .
Robert Griesemer3b576a72009-06-17 14:31:33 -07003296TypeSwitchCase = "case" Type | "default" .
Rob Pike5a578492009-03-17 16:48:35 -07003297</pre>
3298
3299<p>
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003300The TypeSwitchGuard may include a
3301<a href="#Short_variable_declarations">short variable declaration</a>.
3302When that form is used, the variable is declared in each clause.
3303In clauses with a case listing exactly one type, the variable
3304has that type; otherwise, the variable has the type of the expression
3305in the TypeSwitchGuard.
Rob Pike94b67eb2009-03-24 17:40:47 -07003306</p>
3307
3308<p>
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003309The type in a case may be <code>nil</code>
3310<a href="#Predeclared_identifiers">Predeclared identifiers</a>);
3311that case is used when the expression in the TypeSwitchGuard
Robert Griesemer1f95f0d2009-08-27 16:44:17 -07003312is a <code>nil</code> interface value.
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003313</p>
3314
3315<p>
3316Given a function <code>f</code> that returns
3317a value of type <code>interface{}</code>,
Rob Pike70c1a102009-03-18 19:23:59 -07003318the following type switch:
Rob Pike5a578492009-03-17 16:48:35 -07003319</p>
3320
3321<pre>
3322switch i := f().(type) {
Rob Pike94b67eb2009-03-24 17:40:47 -07003323case nil:
3324 printString("f() returns nil");
Rob Pike5a578492009-03-17 16:48:35 -07003325case int:
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003326 printInt(i); // i is an int
Rob Pike5a578492009-03-17 16:48:35 -07003327case float:
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003328 printFloat(i); // i is a float
Rob Pike70c1a102009-03-18 19:23:59 -07003329case func(int) float:
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003330 printFunction(i); // i is a function
3331case bool, string:
3332 printString("type is bool or string"); // i is an interface{}
Rob Pike5a578492009-03-17 16:48:35 -07003333default:
3334 printString("don't know the type");
3335}
3336</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003337
Rob Pike70c1a102009-03-18 19:23:59 -07003338<p>
3339could be rewritten:
3340</p>
3341
3342<pre>
3343v := f();
Rob Pike94b67eb2009-03-24 17:40:47 -07003344if v == nil {
3345 printString("f() returns nil");
3346} else if i, is_int := v.(int); is_int {
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003347 printInt(i); // i is an int
Rob Pike70c1a102009-03-18 19:23:59 -07003348} else if i, is_float := v.(float); is_float {
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003349 printFloat(i); // i is a float
Rob Pike70c1a102009-03-18 19:23:59 -07003350} else if i, is_func := v.(func(int) float); is_func {
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003351 printFunction(i); // i is a function
Rob Pike70c1a102009-03-18 19:23:59 -07003352} else {
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003353 i1, is_bool := v.(bool);
3354 i2, is_string := v.(string);
3355 if is_bool || is_string {
3356 i := v;
3357 printString("type is bool or string"); // i is an interface{}
3358 } else {
3359 i := v;
3360 printString("don't know the type"); // i is an interface{}
3361 }
Rob Pike70c1a102009-03-18 19:23:59 -07003362}
3363</pre>
3364
Robert Griesemeraeaab592009-08-31 17:30:55 -07003365<p>
Russ Cox16b95ba2009-08-20 10:22:52 -07003366The type switch guard may be preceded by a simple statement, which
3367executes before the guard is evaluated.
Robert Griesemer1f95f0d2009-08-27 16:44:17 -07003368</p>
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003369
3370<p>
3371The "fallthrough" statement is not permitted in a type switch.
Russ Cox16b95ba2009-08-20 10:22:52 -07003372</p>
3373
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003374<h3 id="For_statements">For statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003375
Rob Pike96750f12009-02-27 16:47:48 -08003376<p>
3377A "for" statement specifies repeated execution of a block. The iteration is
3378controlled by a condition, a "for" clause, or a "range" clause.
3379</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003380
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003381<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003382ForStmt = "for" [ Condition | ForClause | RangeClause ] Block .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003383Condition = Expression .
3384</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003385
Rob Pike96750f12009-02-27 16:47:48 -08003386<p>
3387In its simplest form, a "for" statement specifies the repeated execution of
3388a block as long as a boolean condition evaluates to true.
3389The condition is evaluated before each iteration.
3390If the condition is absent, it is equivalent to <code>true</code>.
3391</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003392
Robert Griesemerc2d55862009-02-19 16:49:10 -08003393<pre>
3394for a &lt; b {
3395 a *= 2
3396}
3397</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003398
Rob Pike96750f12009-02-27 16:47:48 -08003399<p>
3400A "for" statement with a "for" clause is also controlled by its condition, but
3401additionally it may specify an <i>init</i>
3402and a <i>post</i> statement, such as an assignment,
Russ Cox16b95ba2009-08-20 10:22:52 -07003403an increment or decrement statement. The init statement may be a
Robert Griesemer4ed666e2009-08-27 16:45:42 -07003404<a href="#Short_variable_declarations">short variable declaration</a>, but the post statement must not.
Rob Pike96750f12009-02-27 16:47:48 -08003405</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003406
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003407<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003408ForClause = [ InitStmt ] ";" [ Condition ] ";" [ PostStmt ] .
3409InitStmt = SimpleStmt .
3410PostStmt = SimpleStmt .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003411</pre>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003412
Robert Griesemerc2d55862009-02-19 16:49:10 -08003413<pre>
3414for i := 0; i < 10; i++ {
3415 f(i)
3416}
3417</pre>
Russ Cox5958dd62009-03-04 17:19:21 -08003418
Robert Griesemerc2d55862009-02-19 16:49:10 -08003419<p>
Rob Pike96750f12009-02-27 16:47:48 -08003420If non-empty, the init statement is executed once before evaluating the
3421condition for the first iteration;
3422the post statement is executed after each execution of the block (and
3423only if the block was executed).
3424Any element of the "for" clause may be empty but the semicolons are
3425required unless there is only a condition.
3426If the condition is absent, it is equivalent to <code>true</code>.
3427</p>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003428
Robert Griesemerc2d55862009-02-19 16:49:10 -08003429<pre>
3430for ; cond ; { S() } is the same as for cond { S() }
3431for true { S() } is the same as for { S() }
3432</pre>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003433
Rob Pike96750f12009-02-27 16:47:48 -08003434<p>
3435A "for" statement with a "range" clause
Rob Pike7aee71b2009-04-15 20:28:25 -07003436iterates through all entries of an array, slice, string or map,
3437or values received on a channel.
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003438For each entry it first assigns the current index or key to an iteration
3439variable - or the current (index, element) or (key, value) pair to a pair
Rob Pike96750f12009-02-27 16:47:48 -08003440of iteration variables - and then executes the block.
3441</p>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003442
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003443<pre class="ebnf">
Rob Pikeb3408792009-04-15 20:51:17 -07003444RangeClause = ExpressionList ( "=" | ":=" ) "range" Expression .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003445</pre>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003446
Robert Griesemerc2d55862009-02-19 16:49:10 -08003447<p>
Rob Pike7aee71b2009-04-15 20:28:25 -07003448The type of the right-hand expression in the "range" clause must be an
3449array, slice, string or map, or a pointer to an array, slice, string or map;
Rob Pike94b67eb2009-03-24 17:40:47 -07003450or it may be a channel.
Rob Pike7aee71b2009-04-15 20:28:25 -07003451Except for channels,
Rob Pikeb3408792009-04-15 20:51:17 -07003452the identifier list must contain one or two expressions
3453(as in assignments, these must be a
3454variable, pointer indirection, field selector, or index expression)
3455denoting the
Rob Pike96750f12009-02-27 16:47:48 -08003456iteration variables. On each iteration,
Rob Pike7aee71b2009-04-15 20:28:25 -07003457the first variable is set to the string, array or slice index or
Rob Pike96750f12009-02-27 16:47:48 -08003458map key, and the second variable, if present, is set to the corresponding
Rob Pike7aee71b2009-04-15 20:28:25 -07003459string or array element or map value.
Rob Pike96750f12009-02-27 16:47:48 -08003460The types of the array or slice index (always <code>int</code>)
3461and element, or of the map key and value respectively,
Robert Griesemer4ed666e2009-08-27 16:45:42 -07003462must be <a href="#Assignment_compatibility">assignment compatible</a> to the iteration variables.
Rob Pike96750f12009-02-27 16:47:48 -08003463</p>
3464<p>
Rob Pike7aee71b2009-04-15 20:28:25 -07003465For strings, the "range" clause iterates over the Unicode code points
3466in the string. On successive iterations, the index variable will be the
Rob Pike55faa5f2009-04-15 21:49:50 -07003467index of successive UTF-8-encoded code points in the string, and
Rob Pike7aee71b2009-04-15 20:28:25 -07003468the second variable, of type <code>int</code>, will be the value of
3469the corresponding code point. If the iteration encounters an invalid
3470UTF-8 sequence, the second variable will be <code>0xFFFD</code>,
3471the Unicode replacement character, and the next iteration will advance
3472a single byte in the string.
3473</p>
3474<p>
Rob Pike94b67eb2009-03-24 17:40:47 -07003475For channels, the identifier list must contain one identifier.
Robert Griesemerce9fbdb2009-04-29 11:45:08 -07003476The iteration receives values sent on the channel until the channel is closed;
Rob Pike94b67eb2009-03-24 17:40:47 -07003477it does not process the zero value sent before the channel is closed.
3478</p>
3479<p>
Rob Pike96750f12009-02-27 16:47:48 -08003480The iteration variables may be declared by the "range" clause (":="), in which
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003481case their scope ends at the end of the "for" statement (§<a href="#Declarations_and">Declarations and</a>
Rob Pike96750f12009-02-27 16:47:48 -08003482scope rules). In this case their types are set to
Russ Cox5958dd62009-03-04 17:19:21 -08003483<code>int</code> and the array element type, or the map key and value types, respectively.
Rob Pike96750f12009-02-27 16:47:48 -08003484If the iteration variables are declared outside the "for" statement,
3485after execution their values will be those of the last iteration.
3486</p>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003487
Robert Griesemerc2d55862009-02-19 16:49:10 -08003488<pre>
3489var a [10]string;
Rob Pike426335f2009-03-02 17:52:52 -08003490m := map[string]int{"mon":0, "tue":1, "wed":2, "thu":3, "fri":4, "sat":5, "sun":6};
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003491
Robert Griesemerc2d55862009-02-19 16:49:10 -08003492for i, s := range a {
3493 // type of i is int
3494 // type of s is string
3495 // s == a[i]
3496 g(i, s)
3497}
3498
3499var key string;
3500var val interface {}; // value type of m is assignment-compatible to val
3501for key, value = range m {
3502 h(key, value)
3503}
3504// key == last map key encountered in iteration
3505// val == map[key]
3506</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003507
Rob Pike96750f12009-02-27 16:47:48 -08003508<p>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003509If map entries that have not yet been processed are deleted during iteration,
3510they will not be processed. If map entries are inserted during iteration, the
Russ Cox5958dd62009-03-04 17:19:21 -08003511behavior is implementation-dependent, but each entry will be processed at most once.
Rob Pike96750f12009-02-27 16:47:48 -08003512</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003513
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003514<h3 id="Go_statements">Go statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003515
Rob Pike96750f12009-02-27 16:47:48 -08003516<p>
Russ Cox5958dd62009-03-04 17:19:21 -08003517A "go" statement starts the execution of a function or method call
Rob Pike96750f12009-02-27 16:47:48 -08003518as an independent concurrent thread of control, or <i>goroutine</i>,
3519within the same address space.
3520</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003521
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003522<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003523GoStmt = "go" Expression .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003524</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003525
Rob Pike96750f12009-02-27 16:47:48 -08003526<p>
3527The expression must be a call, and
3528unlike with a regular call, program execution does not wait
Robert Griesemerb9f8b9c2008-09-26 13:38:38 -07003529for the invoked function to complete.
Rob Pike96750f12009-02-27 16:47:48 -08003530</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003531
Robert Griesemerc2d55862009-02-19 16:49:10 -08003532<pre>
3533go Server()
3534go func(ch chan <- bool) { for { sleep(10); ch <- true; }} (c)
3535</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003536
3537
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003538<h3 id="Select_statements">Select statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003539
Rob Pike96750f12009-02-27 16:47:48 -08003540<p>
3541A "select" statement chooses which of a set of possible communications
3542will proceed. It looks similar to a "switch" statement but with the
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003543cases all referring to communication operations.
Rob Pike96750f12009-02-27 16:47:48 -08003544</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003545
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003546<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003547SelectStmt = "select" "{" { CommClause } "}" .
Russ Cox5958dd62009-03-04 17:19:21 -08003548CommClause = CommCase ":" StatementList .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003549CommCase = "case" ( SendExpr | RecvExpr) | "default" .
3550SendExpr = Expression "&lt;-" Expression .
3551RecvExpr = [ Expression ( "=" | ":=" ) ] "&lt;-" Expression .
3552</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003553
Rob Pike96750f12009-02-27 16:47:48 -08003554<p>
Rob Pike96750f12009-02-27 16:47:48 -08003555For all the send and receive expressions in the "select"
3556statement, the channel expression is evaluated. Any expressions
Rob Pike569a1072008-10-03 11:18:45 -07003557that appear on the right hand side of send expressions are also
3558evaluated. If any of the resulting channels can proceed, one is
3559chosen and the corresponding communication and statements are
3560evaluated. Otherwise, if there is a default case, that executes;
Rob Pikecd368a22008-10-02 10:37:12 -07003561if not, the statement blocks until one of the communications can
Rob Pike569a1072008-10-03 11:18:45 -07003562complete. The channels and send expressions are not re-evaluated.
Rob Pike96750f12009-02-27 16:47:48 -08003563A channel pointer may be <code>nil</code>,
3564which is equivalent to that case not
3565being present in the select statement
3566except, if a send, its expression is still evaluated.
3567</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08003568<p>
Rob Pike569a1072008-10-03 11:18:45 -07003569Since all the channels and send expressions are evaluated, any side
3570effects in that evaluation will occur for all the communications
Rob Pike96750f12009-02-27 16:47:48 -08003571in the "select" statement.
3572</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08003573<p>
Rob Pike96750f12009-02-27 16:47:48 -08003574If multiple cases can proceed, a uniform fair choice is made to decide
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003575which single communication will execute.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003576<p>
Robert Griesemer4ed666e2009-08-27 16:45:42 -07003577The receive case may declare a new variable using a
3578<a href="#Short_variable_declarations">short variable declaration</a>.
Rob Pike96750f12009-02-27 16:47:48 -08003579</p>
Robert Griesemer2902a822008-09-17 13:57:11 -07003580
Robert Griesemerc2d55862009-02-19 16:49:10 -08003581<pre>
3582var c, c1, c2 chan int;
3583var i1, i2 int;
3584select {
3585case i1 = &lt;-c1:
3586 print("received ", i1, " from c1\n");
3587case c2 &lt;- i2:
3588 print("sent ", i2, " to c2\n");
3589default:
3590 print("no communication\n");
3591}
3592
3593for { // send random sequence of bits to c
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003594 select {
Robert Griesemerc2d55862009-02-19 16:49:10 -08003595 case c &lt;- 0: // note: no statement, no fallthrough, no folding of cases
3596 case c &lt;- 1:
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003597 }
Robert Griesemerc2d55862009-02-19 16:49:10 -08003598}
Robert Griesemerc2d55862009-02-19 16:49:10 -08003599</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003600
Robert Griesemerc2d55862009-02-19 16:49:10 -08003601<font color=red>
Robert Griesemer2902a822008-09-17 13:57:11 -07003602TODO: Make semantics more precise.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003603</font>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003604
3605
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003606<h3 id="Return_statements">Return statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003607
Rob Pike96750f12009-02-27 16:47:48 -08003608<p>
3609A "return" statement terminates execution of the containing function
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003610and optionally provides a result value or values to the caller.
Rob Pike96750f12009-02-27 16:47:48 -08003611</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003612
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003613<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003614ReturnStmt = "return" [ ExpressionList ] .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003615</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003616
Robert Griesemer4b908332009-08-07 17:05:41 -07003617<p>
3618In a function without a result type, a "return" statement must not
3619specify any result values.
3620</p>
Rob Pike96750f12009-02-27 16:47:48 -08003621<pre>
Robert Griesemer4b908332009-08-07 17:05:41 -07003622func no_result() {
Rob Pike96750f12009-02-27 16:47:48 -08003623 return
3624}
3625</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003626
Rob Pike96750f12009-02-27 16:47:48 -08003627<p>
Robert Griesemer4b908332009-08-07 17:05:41 -07003628There are three ways to return values from a function with a result
3629type:
Rob Pike96750f12009-02-27 16:47:48 -08003630</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003631
Robert Griesemer4b908332009-08-07 17:05:41 -07003632<ol>
3633 <li>The return value or values may be explicitly listed
3634 in the "return" statement. Each expression must be single-valued
Robert Griesemer4ed666e2009-08-27 16:45:42 -07003635 and <a href="#Assignment_compatibility">assignment compatible</a> to the corresponding element of
Robert Griesemer4b908332009-08-07 17:05:41 -07003636 the result type of the function.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003637<pre>
3638func simple_f() int {
Rob Pike96750f12009-02-27 16:47:48 -08003639 return 2
Robert Griesemerc2d55862009-02-19 16:49:10 -08003640}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003641
Robert Griesemerc2d55862009-02-19 16:49:10 -08003642func complex_f1() (re float, im float) {
Rob Pike96750f12009-02-27 16:47:48 -08003643 return -7.0, -4.0
Robert Griesemerc2d55862009-02-19 16:49:10 -08003644}
3645</pre>
Robert Griesemer4b908332009-08-07 17:05:41 -07003646 </li>
3647 <li>The expression list in the "return" statement may be a single
3648 call to a multi-valued function. The effect is as if each value
3649 returned from that function were assigned to a temporary
3650 variable with the type of the respective value, followed by a
3651 "return" statement listing these variables, at which point the
3652 rules of the previous case apply.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003653<pre>
3654func complex_f2() (re float, im float) {
Rob Pike96750f12009-02-27 16:47:48 -08003655 return complex_f1()
3656}
3657</pre>
Robert Griesemer4b908332009-08-07 17:05:41 -07003658 </li>
3659 <li>The expression list may be empty if the functions's result
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003660 type specifies names for its result parameters (§<a href="#Function_Types">Function Types</a>).
Robert Griesemer4b908332009-08-07 17:05:41 -07003661 The result parameters act as ordinary local variables that are
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003662 initialized to the zero values for their type (§<a href="#The_zero_value">The zero value</a>)
Robert Griesemer4b908332009-08-07 17:05:41 -07003663 and the function may assign values to them as necessary.
3664 The "return" statement returns the values of these variables.
Rob Pike96750f12009-02-27 16:47:48 -08003665<pre>
3666func complex_f3() (re float, im float) {
Robert Griesemerc2d55862009-02-19 16:49:10 -08003667 re = 7.0;
3668 im = 4.0;
3669 return;
3670}
3671</pre>
Robert Griesemer4b908332009-08-07 17:05:41 -07003672 </li>
3673</ol>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003674
Russ Cox5958dd62009-03-04 17:19:21 -08003675<p>
Robert Griesemer4b908332009-08-07 17:05:41 -07003676<font color=red>
3677TODO: Define when return is required.<br />
3678TODO: Language about result parameters needs to go into a section on
3679 function/method invocation<br />
3680</font>
Russ Cox5958dd62009-03-04 17:19:21 -08003681</p>
3682
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003683<h3 id="Break_statements">Break statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003684
Rob Pike96750f12009-02-27 16:47:48 -08003685<p>
3686A "break" statement terminates execution of the innermost
3687"for", "switch" or "select" statement.
3688</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003689
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003690<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003691BreakStmt = "break" [ Label ].
Robert Griesemerc2d55862009-02-19 16:49:10 -08003692</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003693
Rob Pike96750f12009-02-27 16:47:48 -08003694<p>
3695If there is a label, it must be that of an enclosing
3696"for", "switch" or "select" statement, and that is the one whose execution
3697terminates
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003698<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 -08003699</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003700
Robert Griesemerc2d55862009-02-19 16:49:10 -08003701<pre>
3702L: for i < n {
3703 switch i {
Rob Pike96750f12009-02-27 16:47:48 -08003704 case 5: break L
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003705 }
Robert Griesemerc2d55862009-02-19 16:49:10 -08003706}
3707</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003708
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003709<h3 id="Continue_statements">Continue statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003710
Rob Pike96750f12009-02-27 16:47:48 -08003711<p>
3712A "continue" statement begins the next iteration of the
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003713innermost "for" loop at the post statement (§<a href="#For_statements">For statements</a>).
Rob Pike96750f12009-02-27 16:47:48 -08003714</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003715
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003716<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003717ContinueStmt = "continue" [ Label ].
Robert Griesemerc2d55862009-02-19 16:49:10 -08003718</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003719
Rob Pike96750f12009-02-27 16:47:48 -08003720<p>
3721The optional label is analogous to that of a "break" statement.
3722</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003723
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003724<h3 id="Goto_statements">Goto statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003725
Rob Pike96750f12009-02-27 16:47:48 -08003726<p>
3727A "goto" statement transfers control to the statement with the corresponding label.
3728</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003729
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003730<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003731GotoStmt = "goto" Label .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003732</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003733
Robert Griesemerc2d55862009-02-19 16:49:10 -08003734<pre>
3735goto Error
3736</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003737
Rob Pike96750f12009-02-27 16:47:48 -08003738<p>
3739Executing the "goto" statement must not cause any variables to come into
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003740scope that were not already in scope at the point of the goto. For
3741instance, this example:
Rob Pike96750f12009-02-27 16:47:48 -08003742</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003743
Robert Griesemerc2d55862009-02-19 16:49:10 -08003744<pre>
3745goto L; // BAD
3746v := 3;
3747L:
3748</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003749
Rob Pike96750f12009-02-27 16:47:48 -08003750<p>
3751is erroneous because the jump to label <code>L</code> skips
3752the creation of <code>v</code>.
Russ Cox5958dd62009-03-04 17:19:21 -08003753(TODO: Eliminate in favor of used and not set errors?)
Rob Pike96750f12009-02-27 16:47:48 -08003754</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003755
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003756<h3 id="Fallthrough_statements">Fallthrough statements</h3>
Robert Griesemeraed247f2008-10-08 17:05:30 -07003757
Rob Pike96750f12009-02-27 16:47:48 -08003758<p>
3759A "fallthrough" statement transfers control to the first statement of the
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003760next case clause in a expression "switch" statement (§<a href="#Expression_switches">Expression switches</a>). It may
Robert Griesemer091cba82009-03-19 08:39:40 -07003761be used only as the final non-empty statement in a case or default clause in an
3762expression "switch" statement.
Rob Pike96750f12009-02-27 16:47:48 -08003763</p>
Robert Griesemeraed247f2008-10-08 17:05:30 -07003764
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003765<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003766FallthroughStmt = "fallthrough" .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003767</pre>
Robert Griesemeraed247f2008-10-08 17:05:30 -07003768
3769
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003770<h3 id="Defer_statements">Defer statements</h3>
Robert Griesemer4a903e02009-01-27 09:29:40 -08003771
Rob Pike96750f12009-02-27 16:47:48 -08003772<p>
3773A "defer" statement invokes a function whose execution is deferred to the moment
3774the surrounding function returns.
3775</p>
Robert Griesemer4a903e02009-01-27 09:29:40 -08003776
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003777<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003778DeferStmt = "defer" Expression .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003779</pre>
Robert Griesemer4a903e02009-01-27 09:29:40 -08003780
Rob Pike96750f12009-02-27 16:47:48 -08003781<p>
3782The expression must be a function or method call.
3783Each time the "defer" statement
Robert Griesemer7471eab2009-01-27 14:51:24 -08003784executes, the parameters to the function call are evaluated and saved anew but the
Robert Griesemer4a903e02009-01-27 09:29:40 -08003785function is not invoked. Immediately before the innermost function surrounding
Rob Pike96750f12009-02-27 16:47:48 -08003786the "defer" statement returns, but after its return value (if any) is evaluated,
Robert Griesemer4a903e02009-01-27 09:29:40 -08003787each deferred function is executed with its saved parameters. Deferred functions
3788are executed in LIFO order.
Rob Pike96750f12009-02-27 16:47:48 -08003789</p>
Robert Griesemer4a903e02009-01-27 09:29:40 -08003790
Robert Griesemerc2d55862009-02-19 16:49:10 -08003791<pre>
3792lock(l);
3793defer unlock(l); // unlocking happens before surrounding function returns
Robert Griesemer4a903e02009-01-27 09:29:40 -08003794
Robert Griesemerc2d55862009-02-19 16:49:10 -08003795// prints 3 2 1 0 before surrounding function returns
3796for i := 0; i &lt;= 3; i++ {
3797 defer fmt.Print(i);
3798}
3799</pre>
Robert Griesemer4a903e02009-01-27 09:29:40 -08003800
Rob Pikeff70f092009-02-20 13:36:14 -08003801<hr/>
Robert Griesemer4a903e02009-01-27 09:29:40 -08003802
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003803<h2 id="Predeclared_functions">Predeclared functions</h2>
Robert Griesemerc2d55862009-02-19 16:49:10 -08003804<ul>
3805 <li>cap
Rob Pike94b67eb2009-03-24 17:40:47 -07003806 <li>close
3807 <li>closed
Robert Griesemerc2d55862009-02-19 16:49:10 -08003808 <li>len
3809 <li>make
3810 <li>new
3811 <li>panic
3812 <li>panicln
3813 <li>print
3814 <li>println
Robert Griesemerc2d55862009-02-19 16:49:10 -08003815</ul>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003816
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003817<h3 id="Length_and_capacity">Length and capacity</h3>
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -07003818
Rob Pikeff70f092009-02-20 13:36:14 -08003819<pre class="grammar">
Rob Pikedf3183f2009-02-26 16:37:23 -08003820Call Argument type Result
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -07003821
Russ Coxfe537952009-08-20 10:47:40 -07003822len(s) string string length (in bytes)
Rob Pikedf3183f2009-02-26 16:37:23 -08003823 [n]T, *[n]T array length (== n)
Russ Coxfe537952009-08-20 10:47:40 -07003824 []T slice length
3825 map[K]T map length
Rob Pikedf3183f2009-02-26 16:37:23 -08003826 chan T number of elements in channel buffer
Robert Griesemer4dc25282008-09-09 10:37:19 -07003827
Russ Coxfe537952009-08-20 10:47:40 -07003828cap(s) [n]T, *[n]T array length (== n)
3829 []T slice capacity
Rob Pikedf3183f2009-02-26 16:37:23 -08003830 chan T channel buffer capacity
Robert Griesemerc2d55862009-02-19 16:49:10 -08003831</pre>
Robert Griesemer4dc25282008-09-09 10:37:19 -07003832
Robert Griesemerc2d55862009-02-19 16:49:10 -08003833<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08003834The type of the result is always <code>int</code> and the
3835implementation guarantees that
3836the result always fits into an <code>int</code>.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003837<p>
Robert Griesemer633957b2009-01-06 13:23:20 -08003838The capacity of a slice or map is the number of elements for which there is
Rob Pikedf3183f2009-02-26 16:37:23 -08003839space allocated in the underlying array (for a slice) or map. For a slice
3840<code>s</code>, at any time the following relationship holds:
Robert Griesemer633957b2009-01-06 13:23:20 -08003841
Robert Griesemerc2d55862009-02-19 16:49:10 -08003842<pre>
38430 <= len(s) <= cap(s)
3844</pre>
Robert Griesemer667ef6c2008-09-10 13:00:32 -07003845
Robert Griesemer4dc25282008-09-09 10:37:19 -07003846
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003847<h3 id="Conversions">Conversions</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003848
Rob Pike96750f12009-02-27 16:47:48 -08003849<p>
Rob Pike96750f12009-02-27 16:47:48 -08003850Conversions look like function calls of the form
3851</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003852
Rob Pikeff70f092009-02-20 13:36:14 -08003853<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08003854T(value)
3855</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003856
Rob Pike96750f12009-02-27 16:47:48 -08003857<p>
Russ Coxe8b43192009-03-03 08:10:25 -08003858where <code>T</code> is a type
3859and <code>value</code> is an expression
3860that can be converted to a value
Rob Pikedf3183f2009-02-26 16:37:23 -08003861of result type <code>T</code>.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003862<p>
Robert Griesemer133c68e2008-09-26 14:04:21 -07003863The following conversion rules apply:
Rob Pike96750f12009-02-27 16:47:48 -08003864</p>
3865<ul>
3866<li>
Robert Griesemere2cb60b2009-06-19 13:03:01 -070038671) The conversion succeeds if the value is assignment-compatible
3868to a variable of type T.
Russ Coxe8b43192009-03-03 08:10:25 -08003869</li>
3870<li>
Robert Griesemere2cb60b2009-06-19 13:03:01 -070038712) The conversion succeeds if the value would be assignment-compatible
3872to a variable of type T if the value type or T or any of their component
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003873types are unnamed (§<a href="#Type_identity_and_compatibility">Type identity and compatibility</a>).
Robert Griesemer533dfd62009-05-13 16:56:00 -07003874</li>
3875<li>
38763) Between integer types. If the value is a signed quantity, it is
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003877sign extended to implicit infinite precision; otherwise it is zero
3878extended. It is then truncated to fit in the result type size.
Rob Pike96750f12009-02-27 16:47:48 -08003879For example, <code>uint32(int8(0xFF))</code> is <code>0xFFFFFFFF</code>.
3880The conversion always yields a valid value; there is no signal for overflow.
3881</li>
3882<li>
Robert Griesemer533dfd62009-05-13 16:56:00 -070038834) Between integer and floating point types, or between floating point
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003884types. To avoid overdefining the properties of the conversion, for
Robert Griesemer434c6052008-11-07 13:34:37 -08003885now it is defined as a ``best effort'' conversion. The conversion
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003886always succeeds but the value may be a NaN or other problematic
Robert Griesemerc2d55862009-02-19 16:49:10 -08003887result. <font color=red>TODO: clarify?</font>
Rob Pike96750f12009-02-27 16:47:48 -08003888</li>
3889<li>
Robert Griesemer533dfd62009-05-13 16:56:00 -070038905) Strings permit three special conversions:
Rob Pike96750f12009-02-27 16:47:48 -08003891</li>
3892<li>
Robert Griesemer533dfd62009-05-13 16:56:00 -070038935a) Converting an integer value yields a string containing the UTF-8
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003894representation of the integer.
3895
Robert Griesemerc2d55862009-02-19 16:49:10 -08003896<pre>
3897string(0x65e5) // "\u65e5"
3898</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003899
Rob Pike96750f12009-02-27 16:47:48 -08003900</li>
3901<li>
Robert Griesemer533dfd62009-05-13 16:56:00 -070039025b) Converting a slice of integers yields a string that is the
Robert Griesemer1a304e12009-05-08 10:25:06 -07003903concatenation of the individual integers converted to strings.
3904If the slice value is <code>nil</code>, the result is the empty string.
3905<pre>
3906string([]int{0x65e5, 0x672c, 0x8a9e}) // "\u65e5\u672c\u8a9e"
3907</pre>
3908</li>
3909<li>
Robert Griesemer533dfd62009-05-13 16:56:00 -070039105c) Converting a slice of bytes yields a string whose successive
Robert Griesemer1a304e12009-05-08 10:25:06 -07003911bytes are those of the slice. If the slice value is <code>nil</code>,
3912the result is the empty string.
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003913
Robert Griesemerc2d55862009-02-19 16:49:10 -08003914<pre>
Robert Griesemer1a304e12009-05-08 10:25:06 -07003915string([]byte{'h', 'e', 'l', 'l', 'o'}) // "hello"
Robert Griesemerc2d55862009-02-19 16:49:10 -08003916</pre>
Rob Pike96750f12009-02-27 16:47:48 -08003917</li>
3918</ul>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003919
Rob Pikedf3183f2009-02-26 16:37:23 -08003920<p>
Rob Pike96750f12009-02-27 16:47:48 -08003921There is no linguistic mechanism to convert between pointers and integers.
3922The <code>unsafe</code> package
3923implements this functionality under
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003924restricted circumstances (§<a href="#Package_unsafe">Package <code>unsafe</code></a>).
Rob Pikedf3183f2009-02-26 16:37:23 -08003925</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003926
3927
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003928<h3 id="Allocation">Allocation</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003929
Rob Pike96750f12009-02-27 16:47:48 -08003930<p>
3931The built-in function <code>new</code> takes a type <code>T</code> and
3932returns a value of type <code>*T</code>.
Robert Griesemera3294712009-01-05 11:17:26 -08003933The memory is initialized as described in the section on initial values
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003934<a href="#The_zero_value">The zero value</a>).
Rob Pike96750f12009-02-27 16:47:48 -08003935</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003936
Robert Griesemerc2d55862009-02-19 16:49:10 -08003937<pre>
3938new(T)
3939</pre>
Robert Griesemer4dc25282008-09-09 10:37:19 -07003940
Rob Pike96750f12009-02-27 16:47:48 -08003941<p>
Robert Griesemer4dc25282008-09-09 10:37:19 -07003942For instance
Rob Pike96750f12009-02-27 16:47:48 -08003943</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003944
Robert Griesemerc2d55862009-02-19 16:49:10 -08003945<pre>
3946type S struct { a int; b float }
3947new(S)
3948</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003949
Rob Pike96750f12009-02-27 16:47:48 -08003950<p>
3951dynamically allocates memory for a variable of type <code>S</code>,
3952initializes it (<code>a=0</code>, <code>b=0.0</code>),
3953and returns a value of type <code>*S</code> containing the address
3954of the memory.
3955</p>
3956
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003957<h3 id="Making_slices_maps_and_channels">Making slices, maps and channels</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003958
Robert Griesemerc2d55862009-02-19 16:49:10 -08003959<p>
Rob Pike96750f12009-02-27 16:47:48 -08003960Slices, maps and channels are reference types that do not require the
3961extra indirection of an allocation with <code>new</code>.
3962The built-in function <code>make</code> takes a type <code>T</code>,
3963which must be a slice, map or channel type,
3964optionally followed by a type-specific list of expressions.
3965It returns a value of type <code>T</code> (not <code>*T</code>).
Robert Griesemer633957b2009-01-06 13:23:20 -08003966The memory is initialized as described in the section on initial values
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003967<a href="#The_zero_value">The zero value</a>).
Rob Pike96750f12009-02-27 16:47:48 -08003968</p>
Robert Griesemer633957b2009-01-06 13:23:20 -08003969
Robert Griesemerc2d55862009-02-19 16:49:10 -08003970<pre>
3971make(T [, optional list of expressions])
3972</pre>
Robert Griesemer633957b2009-01-06 13:23:20 -08003973
Rob Pike96750f12009-02-27 16:47:48 -08003974<p>
Robert Griesemer633957b2009-01-06 13:23:20 -08003975For instance
Rob Pike96750f12009-02-27 16:47:48 -08003976</p>
Robert Griesemer633957b2009-01-06 13:23:20 -08003977
Robert Griesemerc2d55862009-02-19 16:49:10 -08003978<pre>
3979make(map[string] int)
3980</pre>
Robert Griesemer633957b2009-01-06 13:23:20 -08003981
Rob Pike96750f12009-02-27 16:47:48 -08003982<p>
Robert Griesemer633957b2009-01-06 13:23:20 -08003983creates a new map value and initializes it to an empty map.
Rob Pike96750f12009-02-27 16:47:48 -08003984</p>
Robert Griesemer633957b2009-01-06 13:23:20 -08003985
Rob Pike96750f12009-02-27 16:47:48 -08003986<p>
3987The parameters affect sizes for allocating slices, maps, and
Robert Griesemer633957b2009-01-06 13:23:20 -08003988buffered channels:
Rob Pike96750f12009-02-27 16:47:48 -08003989</p>
Robert Griesemer633957b2009-01-06 13:23:20 -08003990
Robert Griesemerc2d55862009-02-19 16:49:10 -08003991<pre>
3992s := make([]int, 10, 100); # slice with len(s) == 10, cap(s) == 100
Russ Coxfe537952009-08-20 10:47:40 -07003993s := make([]int, 10); # slice with len(s) == cap(s) == 10
Robert Griesemerc2d55862009-02-19 16:49:10 -08003994c := make(chan int, 10); # channel with a buffer size of 10
3995m := make(map[string] int, 100); # map with initial space for 100 elements
3996</pre>
Robert Griesemer633957b2009-01-06 13:23:20 -08003997
Rob Pikeff70f092009-02-20 13:36:14 -08003998<hr/>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003999
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004000<h2 id="Packages">Packages</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004001
Rob Pike46596852009-03-02 16:17:29 -08004002<p>
4003Go programs are constructed by linking together <i>packages</i>.
4004A package is in turn constructed from one or more source files that
Rob Pike811dd252009-03-04 20:39:39 -08004005together provide access to a set of types, constants, functions,
Rob Pike46596852009-03-02 16:17:29 -08004006and variables. Those elements may be <i>imported</i> and used in
4007another package.
4008</p>
4009
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004010<h3 id="Source_file_organization">Source file organization</h3>
Rob Pike46596852009-03-02 16:17:29 -08004011
4012<p>
4013Each source file consists of a package clause defining the package
4014to which it belongs, followed by a possibly empty set of import
4015declarations that declare packages whose contents it wishes to use,
4016followed by a possibly empty set of declarations of functions,
Robert Griesemer0a162a12009-08-19 16:44:04 -07004017types, variables, and constants.
Rob Pike46596852009-03-02 16:17:29 -08004018</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004019
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004020<pre class="ebnf">
Robert Griesemer0a162a12009-08-19 16:44:04 -07004021SourceFile = PackageClause { ImportDecl [ ";" ] } { TopLevelDecl [ ";" ] } .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004022</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004023
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004024<h3 id="Package_clause">Package clause</h3>
Rob Pike46596852009-03-02 16:17:29 -08004025
Robert Griesemerc2d55862009-02-19 16:49:10 -08004026<p>
Rob Pike46596852009-03-02 16:17:29 -08004027A package clause begins each source file and defines the package
4028to which the file belongs.
4029</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004030
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004031<pre class="ebnf">
Rob Pike46596852009-03-02 16:17:29 -08004032PackageClause = "package" PackageName .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004033</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004034
Rob Pike46596852009-03-02 16:17:29 -08004035<pre>
4036package math
4037</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004038
Rob Pike46596852009-03-02 16:17:29 -08004039<p>
4040A set of files sharing the same PackageName form the implementation of a package.
4041An implementation may require that all source files for a package inhabit the same directory.
4042</p>
4043
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004044<h3 id="Import_declarations">Import declarations</h3>
Rob Pike46596852009-03-02 16:17:29 -08004045
4046<p>
Robert Griesemeraeaab592009-08-31 17:30:55 -07004047A source file gains access to <a href="#Exported_identifiers">exported identifiers</a>
4048from another package through an import declaration.
Rob Pike46596852009-03-02 16:17:29 -08004049In the general form, an import declaration provides an identifier
4050that code in the source file may use to access the imported package's
4051contents and a file name referring to the (compiled) implementation of
4052the package. The file name may be relative to a repository of
4053installed packages.
4054</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004055
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004056<pre class="ebnf">
Rob Pike46596852009-03-02 16:17:29 -08004057ImportDecl = "import" ( ImportSpec | "(" [ ImportSpecList ] ")" ) .
4058ImportSpecList = ImportSpec { ";" ImportSpec } [ ";" ] .
4059ImportSpec = [ "." | PackageName ] PackageFileName .
4060PackageFileName = StringLit .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004061</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004062
Robert Griesemerc2d55862009-02-19 16:49:10 -08004063<p>
Rob Pike46596852009-03-02 16:17:29 -08004064After an import, in the usual case an exported name <i>N</i> from the imported
4065package <i>P</i> may be accessed by the qualified identifier
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004066<i>P</i><code>.</code><i>N</i><a href="#Qualified_identifiers">Qualified identifiers</a>). The actual
Rob Pike46596852009-03-02 16:17:29 -08004067name <i>P</i> depends on the form of the import declaration. If
4068an explicit package name <code>p1</code> is provided, the qualified
4069identifer will have the form <code>p1.</code><i>N</i>. If no name
4070is provided in the import declaration, <i>P</i> will be the package
4071name declared within the source files of the imported package.
4072Finally, if the import declaration uses an explicit period
Russ Cox16b95ba2009-08-20 10:22:52 -07004073(<code>.</code>) for the package name, <i>N</i> will be declared
4074in the current file's file block and can be accessed without a qualifier.
Rob Pike46596852009-03-02 16:17:29 -08004075</p>
Russ Cox16b95ba2009-08-20 10:22:52 -07004076
Robert Griesemerc2d55862009-02-19 16:49:10 -08004077<p>
Rob Pike46596852009-03-02 16:17:29 -08004078In this table, assume we have compiled a package named
4079<code>math</code>, which exports function <code>Sin</code>, and
4080installed the compiled package in file
4081<code>"lib/math"</code>.
4082</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004083
Rob Pike46596852009-03-02 16:17:29 -08004084<pre class="grammar">
4085Import syntax Local name of Sin
4086
4087import M "lib/math" M.Sin
4088import "lib/math" math.Sin
4089import . "lib/math" Sin
4090</pre>
4091
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004092<h3 id="Multiple-file_packages">Multiple-file packages</h3>
Rob Pike46596852009-03-02 16:17:29 -08004093
4094<p>
Russ Coxfe537952009-08-20 10:47:40 -07004095If a package is constructed from multiple source files,
4096all names declared in the package block, not just uppercase ones,
4097are in scope in all the files in the package.
Rob Pike46596852009-03-02 16:17:29 -08004098</p>
4099
4100<p>
4101If source file <code>math1.go</code> contains
4102</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004103<pre>
Rob Pike46596852009-03-02 16:17:29 -08004104package math
4105
4106const twoPi = 6.283185307179586
4107
4108function Sin(x float) float { return ... }
Robert Griesemerc2d55862009-02-19 16:49:10 -08004109</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004110
Robert Griesemerc2d55862009-02-19 16:49:10 -08004111<p>
Russ Coxfe537952009-08-20 10:47:40 -07004112then a second file <code>math2.go</code> also in
4113<code>package math</code>
4114may refer directly to <code>Sin</code> and <code>twoPi</code>.
Rob Pike46596852009-03-02 16:17:29 -08004115</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004116
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004117<h3 id="An_example_package">An example package</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004118
Robert Griesemerc2d55862009-02-19 16:49:10 -08004119<p>
Rob Pike46596852009-03-02 16:17:29 -08004120Here is a complete Go package that implements a concurrent prime sieve.
4121</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004122
Robert Griesemerc2d55862009-02-19 16:49:10 -08004123<pre>
4124package main
4125
Rob Pike46596852009-03-02 16:17:29 -08004126import "fmt"
4127
Robert Griesemerc2d55862009-02-19 16:49:10 -08004128// Send the sequence 2, 3, 4, ... to channel 'ch'.
4129func generate(ch chan <- int) {
4130 for i := 2; ; i++ {
4131 ch <- i // Send 'i' to channel 'ch'.
4132 }
4133}
4134
4135// Copy the values from channel 'in' to channel 'out',
4136// removing those divisible by 'prime'.
Rob Pike94b67eb2009-03-24 17:40:47 -07004137func filter(src chan <- int, dst <-chan int, prime int) {
4138 for i := range src { // Loop over values received from 'src'.
Robert Griesemerc2d55862009-02-19 16:49:10 -08004139 if i % prime != 0 {
Rob Pike94b67eb2009-03-24 17:40:47 -07004140 dst <- i // Send 'i' to channel 'dst'.
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004141 }
4142 }
Robert Griesemerc2d55862009-02-19 16:49:10 -08004143}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004144
Robert Griesemerc2d55862009-02-19 16:49:10 -08004145// The prime sieve: Daisy-chain filter processes together.
4146func sieve() {
4147 ch := make(chan int); // Create a new channel.
4148 go generate(ch); // Start generate() as a subprocess.
4149 for {
4150 prime := <-ch;
Rob Pike46596852009-03-02 16:17:29 -08004151 fmt.Print(prime, "\n");
Robert Griesemerc2d55862009-02-19 16:49:10 -08004152 ch1 := make(chan int);
4153 go filter(ch, ch1, prime);
4154 ch = ch1
4155 }
4156}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004157
Robert Griesemerc2d55862009-02-19 16:49:10 -08004158func main() {
4159 sieve()
4160}
4161</pre>
Robert Griesemer67153582008-12-16 14:45:09 -08004162
Rob Pikeff70f092009-02-20 13:36:14 -08004163<hr/>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004164
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004165<h2 id="Program_initialization_and_execution">Program initialization and execution</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004166
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004167<h3 id="The_zero_value">The zero value</h3>
Rob Pike8f2330d2009-02-25 16:20:44 -08004168<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004169When memory is allocated to store a value, either through a declaration
Rob Pike8f2330d2009-02-25 16:20:44 -08004170or <code>new()</code>, and no explicit initialization is provided, the memory is
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004171given a default initialization. Each element of such a value is
Robert Griesemeref45e642009-08-21 11:25:00 -07004172set to the <i>zero value</i> for its type: <code>false</code> for booleans,
Rob Pike8f2330d2009-02-25 16:20:44 -08004173<code>0</code> for integers, <code>0.0</code> for floats, <code>""</code>
4174for strings, and <code>nil</code> for pointers and interfaces.
Rob Pikeff70f092009-02-20 13:36:14 -08004175This initialization is done recursively, so for instance each element of an
Rob Pike8f2330d2009-02-25 16:20:44 -08004176array of structs will have its fields zeroed if no value is specified.
4177</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004178<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004179These two simple declarations are equivalent:
Rob Pike8f2330d2009-02-25 16:20:44 -08004180</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004181
Robert Griesemerc2d55862009-02-19 16:49:10 -08004182<pre>
4183var i int;
4184var i int = 0;
4185</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004186
Rob Pike8f2330d2009-02-25 16:20:44 -08004187<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004188After
Rob Pike8f2330d2009-02-25 16:20:44 -08004189</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004190
Robert Griesemerc2d55862009-02-19 16:49:10 -08004191<pre>
4192type T struct { i int; f float; next *T };
4193t := new(T);
4194</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004195
Rob Pike8f2330d2009-02-25 16:20:44 -08004196<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004197the following holds:
Rob Pike8f2330d2009-02-25 16:20:44 -08004198</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004199
Robert Griesemerc2d55862009-02-19 16:49:10 -08004200<pre>
4201t.i == 0
4202t.f == 0.0
4203t.next == nil
4204</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004205
Rob Pike96750f12009-02-27 16:47:48 -08004206<p>
4207The same would also be true after
4208</p>
4209
4210<pre>
4211var t T
4212</pre>
4213
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004214<h3 id="Program_execution">Program execution</h3>
Rob Pike8f2330d2009-02-25 16:20:44 -08004215<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004216A package with no imports is initialized by assigning initial values to
Rob Pike96750f12009-02-27 16:47:48 -08004217all its package-level variables in declaration order and then calling any
4218package-level function with the name and signature of
4219</p>
4220<pre>
4221func init()
4222</pre>
4223<p>
4224defined in its source. Since a package may contain more
4225than one source file, there may be more than one
4226<code>init()</code> function in a package, but
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004227only one per source file.
Rob Pike8f2330d2009-02-25 16:20:44 -08004228</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004229<p>
Robert Griesemer566e3b22008-09-26 16:41:50 -07004230Initialization code may contain "go" statements, but the functions
Robert Griesemerd8a764c2009-02-06 17:01:10 -08004231they invoke do not begin execution until initialization of the entire
4232program is complete. Therefore, all initialization code is run in a single
Rob Pike96750f12009-02-27 16:47:48 -08004233goroutine.
Rob Pike8f2330d2009-02-25 16:20:44 -08004234</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004235<p>
Rob Pike96750f12009-02-27 16:47:48 -08004236An <code>init()</code> function cannot be referred to from anywhere
4237in a program. In particular, <code>init()</code> cannot be called explicitly,
4238nor can a pointer to <code>init</code> be assigned to a function variable.
Rob Pike8f2330d2009-02-25 16:20:44 -08004239</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004240<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004241If a package has imports, the imported packages are initialized
Robert Griesemer566e3b22008-09-26 16:41:50 -07004242before initializing the package itself. If multiple packages import
Rob Pike96750f12009-02-27 16:47:48 -08004243a package <code>P</code>, <code>P</code> will be initialized only once.
Rob Pike8f2330d2009-02-25 16:20:44 -08004244</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004245<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004246The importing of packages, by construction, guarantees that there can
4247be no cyclic dependencies in initialization.
Rob Pike8f2330d2009-02-25 16:20:44 -08004248</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004249<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004250A complete program, possibly created by linking multiple packages,
Rob Pike96750f12009-02-27 16:47:48 -08004251must have one package called <code>main</code>, with a function
Rob Pike8f2330d2009-02-25 16:20:44 -08004252</p>
Robert Griesemer4dc25282008-09-09 10:37:19 -07004253
Robert Griesemerc2d55862009-02-19 16:49:10 -08004254<pre>
Rob Pike96750f12009-02-27 16:47:48 -08004255func main() { ... }
Robert Griesemerc2d55862009-02-19 16:49:10 -08004256</pre>
Robert Griesemer4dc25282008-09-09 10:37:19 -07004257
Rob Pike8f2330d2009-02-25 16:20:44 -08004258<p>
Rob Pike96750f12009-02-27 16:47:48 -08004259defined.
4260The function <code>main.main()</code> takes no arguments and returns no value.
Rob Pike8f2330d2009-02-25 16:20:44 -08004261</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004262<p>
Rob Pike96750f12009-02-27 16:47:48 -08004263Program execution begins by initializing the <code>main</code> package and then
Rob Pike8f2330d2009-02-25 16:20:44 -08004264invoking <code>main.main()</code>.
4265</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004266<p>
Rob Pike46596852009-03-02 16:17:29 -08004267When <code>main.main()</code> returns, the program exits.
Rob Pike8f2330d2009-02-25 16:20:44 -08004268</p>
Rob Pike811dd252009-03-04 20:39:39 -08004269<p>
4270Implementation restriction: The compiler assumes package <code>main</code>
Robert Griesemer4023dce2009-08-14 17:41:52 -07004271is not imported by any other package.
Rob Pike811dd252009-03-04 20:39:39 -08004272</p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004273
Rob Pikeff70f092009-02-20 13:36:14 -08004274<hr/>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004275
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004276<h2 id="System_considerations">System considerations</h2>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004277
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004278<h3 id="Package_unsafe">Package <code>unsafe</code></h3>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004279
Robert Griesemerc2d55862009-02-19 16:49:10 -08004280<p>
Rob Pike96750f12009-02-27 16:47:48 -08004281The built-in package <code>unsafe</code>, known to the compiler,
4282provides facilities for low-level programming including operations
4283that violate the type system. A package using <code>unsafe</code>
4284must be vetted manually for type safety. The package provides the
4285following interface:
Rob Pikef27e9f02009-02-23 19:22:05 -08004286</p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004287
Rob Pikeff70f092009-02-20 13:36:14 -08004288<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08004289package unsafe
Robert Griesemer52c02c22009-02-11 13:46:30 -08004290
Robert Griesemer98b4f6a2009-05-12 21:37:46 -07004291type ArbitraryType int // shorthand for an arbitrary Go type; it is not a real type
4292type Pointer *ArbitraryType
Robert Griesemer52c02c22009-02-11 13:46:30 -08004293
Robert Griesemer98b4f6a2009-05-12 21:37:46 -07004294func Alignof(variable ArbitraryType) int
4295func Offsetof(selector ArbitraryType) int
4296func Reflect(i interface {}) (value uint64, typestring string, indir bool)
4297func Sizeof(variable ArbitraryType) int
4298func Unreflect(value uint64, typestring string, indir bool) interface {}
Robert Griesemerc2d55862009-02-19 16:49:10 -08004299</pre>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004300
Robert Griesemerc2d55862009-02-19 16:49:10 -08004301<p>
Rob Pikef27e9f02009-02-23 19:22:05 -08004302Any pointer or value of type <code>uintptr</code> can be converted into
4303a <code>Pointer</code> and vice versa.
4304</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004305<p>
Rob Pikef27e9f02009-02-23 19:22:05 -08004306The function <code>Sizeof</code> takes an expression denoting a
Robert Griesemer4023dce2009-08-14 17:41:52 -07004307variable of any type and returns the size of the variable in bytes.
Rob Pikef27e9f02009-02-23 19:22:05 -08004308</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004309<p>
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004310The function <code>Offsetof</code> takes a selector (§<a href="#Selectors">Selectors</a>) denoting a struct
Robert Griesemer52c02c22009-02-11 13:46:30 -08004311field of any type and returns the field offset in bytes relative to the
Rob Pikef27e9f02009-02-23 19:22:05 -08004312struct's address. For a struct <code>s</code> with field <code>f</code>:
4313</p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004314
Robert Griesemerc2d55862009-02-19 16:49:10 -08004315<pre>
Rob Pikef27e9f02009-02-23 19:22:05 -08004316uintptr(unsafe.Pointer(&amp;s)) + uintptr(unsafe.Offsetof(s.f)) == uintptr(unsafe.Pointer(&amp;s.f))
Robert Griesemerc2d55862009-02-19 16:49:10 -08004317</pre>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004318
Robert Griesemerc2d55862009-02-19 16:49:10 -08004319<p>
Rob Pikef27e9f02009-02-23 19:22:05 -08004320Computer architectures may require memory addresses to be <i>aligned</i>;
4321that is, for addresses of a variable to be a multiple of a factor,
4322the variable's type's <i>alignment</i>. The function <code>Alignof</code>
4323takes an expression denoting a variable of any type and returns the
4324alignment of the (type of the) variable in bytes. For a variable
4325<code>x</code>:
4326</p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004327
Robert Griesemerc2d55862009-02-19 16:49:10 -08004328<pre>
4329uintptr(unsafe.Pointer(&amp;x)) % uintptr(unsafe.Alignof(x)) == 0
4330</pre>
Robert Griesemer533dfd62009-05-13 16:56:00 -07004331
Rob Pikef27e9f02009-02-23 19:22:05 -08004332<p>
4333Calls to <code>Alignof</code>, <code>Offsetof</code>, and
4334<code>Sizeof</code> are constant expressions of type <code>int</code>.
4335</p>
Robert Griesemer98b4f6a2009-05-12 21:37:46 -07004336<p>
4337<font color=red>TODO describe Reflect, Unreflect</font>
4338</p>
Robert Griesemer6f8df7a2009-02-11 21:57:15 -08004339
Robert Griesemer52c02c22009-02-11 13:46:30 -08004340
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004341<h3 id="Size_and_alignment_guarantees">Size and alignment guarantees</h3>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004342
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004343For the numeric types (§<a href="#Numeric_types">Numeric types</a>), the following sizes are guaranteed:
Robert Griesemer52c02c22009-02-11 13:46:30 -08004344
Rob Pikeff70f092009-02-20 13:36:14 -08004345<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08004346type size in bytes
Robert Griesemer52c02c22009-02-11 13:46:30 -08004347
Robert Griesemerc2d55862009-02-19 16:49:10 -08004348byte, uint8, int8 1
4349uint16, int16 2
4350uint32, int32, float32 4
4351uint64, int64, float64 8
4352</pre>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004353
Robert Griesemerc2d55862009-02-19 16:49:10 -08004354<p>
Rob Pikef27e9f02009-02-23 19:22:05 -08004355The following minimal alignment properties are guaranteed:
Rob Pike4501d342009-02-19 17:31:36 -08004356</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004357<ol>
Rob Pikef27e9f02009-02-23 19:22:05 -08004358<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 -08004359
Russ Cox5958dd62009-03-04 17:19:21 -08004360<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 -08004361 of <code>unsafe.Sizeof(x)</code> and <code>unsafe.Maxalign</code>, but at least 1.
Robert Griesemer52c02c22009-02-11 13:46:30 -08004362
Rob Pikef27e9f02009-02-23 19:22:05 -08004363<li>For a variable <code>x</code> of struct type: <code>unsafe.Alignof(x)</code> is the largest of
4364 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 -08004365
Rob Pikef27e9f02009-02-23 19:22:05 -08004366<li>For a variable <code>x</code> of array type: <code>unsafe.Alignof(x)</code> is the same as
4367 <code>unsafe.Alignof(x[0])</code>, but at least 1.
Robert Griesemerc2d55862009-02-19 16:49:10 -08004368</ol>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004369
Rob Pikeff70f092009-02-20 13:36:14 -08004370<hr/>
4371
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004372<h2 id="Implementation_differences"><font color=red>Implementation differences - TODO</font></h2>
Rob Pikeff70f092009-02-20 13:36:14 -08004373<p>
4374<font color=red>
Robert Griesemer237c8ab2009-09-01 14:07:30 -07004375<ul>
4376 <li>Implementation does not honor the restriction on goto statements and targets (no intervening declarations).</li>
4377 <li>A type switch must have an assignment in the guard expression and does not support multiple types per case.</li>
4378</ul>
Rob Pikeff70f092009-02-20 13:36:14 -08004379</font>
4380</p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004381
Robert Griesemerc2d55862009-02-19 16:49:10 -08004382</div>
4383</body>
4384</html>