blob: 4c6d26e048cb3d93af8ff5fe399b30c40abef007 [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
10
Robert Griesemer57b34612008-10-10 12:45:44 -070011Todo's:
Robert Griesemer9f4a27c2009-01-16 15:44:08 -080012[ ] document illegality of package-external tuple assignments to structs
Robert Griesemer6f8df7a2009-02-11 21:57:15 -080013 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 -080014 a T struct { a b int }.
Robert Griesemer40d6bb52009-04-20 15:32:20 -070015[ ] should probably write something about evaluation order of statements even
16 though obvious
17[ ] string conversion: string([]int{}) vs string(int) conversion. Former is
18 "inverse" of string range iteration.
19[ ] do we need explicit channel conversion (to change channel direction)?
20
Robert Griesemer57b34612008-10-10 12:45:44 -070021
Robert Griesemer7471eab2009-01-27 14:51:24 -080022Wish list:
Robert Griesemer40d6bb52009-04-20 15:32:20 -070023[ ] enum symbols that are not mixable with ints or some other mechanism
24 (requirement that basic type aliases need conversion for compatibility)
Robert Griesemer7471eab2009-01-27 14:51:24 -080025[ ] Helper syntax for composite types: allow names/keys/indices for
Robert Griesemer40d6bb52009-04-20 15:32:20 -070026 structs/maps/arrays
27[ ] built-in assert() ("conditional panic") (gri)
Robert Griesemerc59d2f12008-09-09 10:48:14 -070028-->
29
Robert Griesemer40d6bb52009-04-20 15:32:20 -070030
Robert Griesemerc2d55862009-02-19 16:49:10 -080031<h2>Introduction</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -070032
Robert Griesemerc2d55862009-02-19 16:49:10 -080033<p>
Rob Pike4501d342009-02-19 17:31:36 -080034This is a reference manual for the Go programming language. For
35more information and other documents, see <a
36href="/">the Go home page</a>.
37</p>
Robert Griesemer67153582008-12-16 14:45:09 -080038
Robert Griesemerc2d55862009-02-19 16:49:10 -080039<p>
Rob Pike4501d342009-02-19 17:31:36 -080040Go is a general-purpose language designed with systems programming
41in mind. It is strongly typed and garbage-collected, and has explicit
42support for concurrent programming. Programs are constructed from
43<i>packages</i>, whose properties allow efficient management of
44dependencies. The existing implementations use a traditional
45compile/link model to generate executable binaries.
46</p>
47
Robert Griesemerc2d55862009-02-19 16:49:10 -080048<p>
Rob Pike4501d342009-02-19 17:31:36 -080049The grammar is compact and regular, allowing for easy analysis by
50automatic tools such as integrated development environments.
51</p>
Rob Pikeff70f092009-02-20 13:36:14 -080052<hr/>
Robert Griesemerc2d55862009-02-19 16:49:10 -080053<h2>Notation</h2>
Rob Pike4501d342009-02-19 17:31:36 -080054<p>
Robert Griesemer4d230302008-12-17 15:39:15 -080055The syntax is specified using Extended Backus-Naur Form (EBNF):
Rob Pike4501d342009-02-19 17:31:36 -080056</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -070057
Rob Pikeff70f092009-02-20 13:36:14 -080058<pre class="grammar">
Rob Pike4501d342009-02-19 17:31:36 -080059Production = production_name "=" Expression .
60Expression = Alternative { "|" Alternative } .
Robert Griesemerc2d55862009-02-19 16:49:10 -080061Alternative = Term { Term } .
Rob Pike4501d342009-02-19 17:31:36 -080062Term = production_name | token [ "..." token ] | Group | Option | Repetition .
63Group = "(" Expression ")" .
Rob Pikec956e902009-04-14 20:10:49 -070064Option = "[" Expression "]" .
Rob Pike4501d342009-02-19 17:31:36 -080065Repetition = "{" Expression "}" .
Robert Griesemerc2d55862009-02-19 16:49:10 -080066</pre>
Robert Griesemerbbfe3122008-10-09 17:12:09 -070067
Rob Pike4501d342009-02-19 17:31:36 -080068<p>
69Productions are expressions constructed from terms and the following
70operators, in increasing precedence:
71</p>
Rob Pikeff70f092009-02-20 13:36:14 -080072<pre class="grammar">
Rob Pike4501d342009-02-19 17:31:36 -080073| alternation
74() grouping
75[] option (0 or 1 times)
76{} repetition (0 to n times)
Robert Griesemerc2d55862009-02-19 16:49:10 -080077</pre>
Robert Griesemer4d230302008-12-17 15:39:15 -080078
Robert Griesemerc2d55862009-02-19 16:49:10 -080079<p>
Rob Pike4501d342009-02-19 17:31:36 -080080Lower-case production names are used to identify lexical tokens.
81Non-terminals are in CamelCase. Lexical symbols are enclosed in
Rob Pikef27e9f02009-02-23 19:22:05 -080082double quotes <code>""</code> (the double quote symbol is written as
83<code>'"'</code>).
Rob Pike4501d342009-02-19 17:31:36 -080084</p>
85
Robert Griesemerc2d55862009-02-19 16:49:10 -080086<p>
Rob Pikef27e9f02009-02-23 19:22:05 -080087The form <code>"a ... b"</code> represents the set of characters from
88<code>a</code> through <code>b</code> as alternatives.
Rob Pike4501d342009-02-19 17:31:36 -080089</p>
90
Rob Pikeff70f092009-02-20 13:36:14 -080091<hr/>
Robert Griesemer7abfcd92008-10-07 17:14:30 -070092
Robert Griesemerc2d55862009-02-19 16:49:10 -080093<h2>Source code representation</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -070094
Robert Griesemerc2d55862009-02-19 16:49:10 -080095<p>
Rob Pikeff70f092009-02-20 13:36:14 -080096Source code is Unicode text encoded in UTF-8. The text is not
97canonicalized, so a single accented code point is distinct from the
98same character constructed from combining an accent and a letter;
99those are treated as two code points. For simplicity, this document
100will use the term <i>character</i> to refer to a Unicode code point.
101</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -0800102<p>
Rob Pikeff70f092009-02-20 13:36:14 -0800103Each code point is distinct; for instance, upper and lower case letters
104are different characters.
105</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700106
Robert Griesemerc2d55862009-02-19 16:49:10 -0800107<h3>Characters</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700108
Robert Griesemerc2d55862009-02-19 16:49:10 -0800109<p>
Rob Pike4501d342009-02-19 17:31:36 -0800110The following terms are used to denote specific Unicode character classes:
111</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -0800112<ul>
Rob Pikeff70f092009-02-20 13:36:14 -0800113 <li>unicode_char an arbitrary Unicode code point</li>
114 <li>unicode_letter a Unicode code point classified as "Letter"</li>
115 <li>capital_letter a Unicode code point classified as "Letter, uppercase"</li>
116 <li>unicode_digit a Unicode code point classified as "Digit"</li>
Robert Griesemerc2d55862009-02-19 16:49:10 -0800117</ul>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700118
Robert Griesemer83c17602009-01-16 14:12:50 -0800119(The Unicode Standard, Section 4.5 General Category - Normative.)
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700120
Robert Griesemerc2d55862009-02-19 16:49:10 -0800121<h3>Letters and digits</h3>
Rob Pikeff70f092009-02-20 13:36:14 -0800122
123<p>
Rob Pikef27e9f02009-02-23 19:22:05 -0800124The underscore character <code>_</code> (U+005F) is considered a letter.
Rob Pikeff70f092009-02-20 13:36:14 -0800125</>
126<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -0800127letter = unicode_letter | "_" .
128decimal_digit = "0" ... "9" .
129octal_digit = "0" ... "7" .
130hex_digit = "0" ... "9" | "A" ... "F" | "a" ... "f" .
131</pre>
Rob Pikeff70f092009-02-20 13:36:14 -0800132<hr/>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700133
Rob Pikeff70f092009-02-20 13:36:14 -0800134<h2>Lexical elements</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700135
Rob Pikeff70f092009-02-20 13:36:14 -0800136<h3>Comments</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700137
Rob Pikeff70f092009-02-20 13:36:14 -0800138<p>
139There are two forms of comments. The first starts at the character
Rob Pikef27e9f02009-02-23 19:22:05 -0800140sequence <code>//</code> and continues through the next newline. The
141second starts at the character sequence <code>/*</code> and continues
142through the character sequence <code>*/</code>. Comments do not nest.
Rob Pikeff70f092009-02-20 13:36:14 -0800143</p>
144
145<h3>Tokens</h3>
146
147<p>
148Tokens form the vocabulary of the Go language.
149There are four classes: identifiers, keywords, operators
150and delimiters, and literals. <i>White space</i>, formed from
151blanks, tabs, and newlines, is ignored except as it separates tokens
152that would otherwise combine into a single token. Comments
153behave as white space. While breaking the input into tokens,
154the next token is the longest sequence of characters that form a
155valid token.
156</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700157
Robert Griesemerc2d55862009-02-19 16:49:10 -0800158<h3>Identifiers</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700159
Rob Pikeff70f092009-02-20 13:36:14 -0800160<p>
161Identifiers name program entities such as variables and types.
162An identifier is a sequence of one or more letters and digits.
163The first character in an identifier must be a letter.
164</p>
165<pre class="grammar">
166identifier = letter { letter | unicode_digit } .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800167</pre>
Robert Griesemerc2d55862009-02-19 16:49:10 -0800168<pre>
169a
170_x9
171ThisVariableIsExported
172αβ
173</pre>
Robert Griesemer83c17602009-01-16 14:12:50 -0800174Some identifiers are predeclared (§Predeclared identifiers).
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -0700175
Rob Pikeff70f092009-02-20 13:36:14 -0800176<h3>Keywords</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700177
Rob Pikeff70f092009-02-20 13:36:14 -0800178<p>
179The following keywords are reserved and may not be used as identifiers.
180</p>
181<pre class="grammar">
182break default func interface select
183case defer go map struct
184chan else goto package switch
185const fallthrough if range type
186continue for import return var
187</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700188
Rob Pikeff70f092009-02-20 13:36:14 -0800189<h3>Operators and Delimiters</h3>
190
191<p>
192The following character sequences represent operators, delimiters, and other special tokens:
193</p>
194<pre class="grammar">
195+ &amp; += &amp;= &amp;&amp; == != ( )
196- | -= |= || &lt; &lt;= [ ]
197* ^ *= ^= &lt;- &gt; &gt;= { }
198/ << /= <<= ++ = := , ;
199% >> %= >>= -- ! ... . :
Rob Pikecd04ec92009-03-11 21:59:05 -0700200 &amp;^ &amp;^=
Rob Pikeff70f092009-02-20 13:36:14 -0800201</pre>
202
203<h3>Integer literals</h3>
204
205<p>
206An integer literal is a sequence of one or more digits in the
207corresponding base, which may be 8, 10, or 16. An optional prefix
Rob Pikef27e9f02009-02-23 19:22:05 -0800208sets a non-decimal base: <code>0</code> for octal, <code>0x</code> or
209<code>0X</code> for hexadecimal. In hexadecimal literals, letters
210<code>a-f</code> and <code>A-F</code> represent values 10 through 15.
Rob Pikeff70f092009-02-20 13:36:14 -0800211</p>
212<pre class="grammar">
213int_lit = decimal_lit | octal_lit | hex_lit .
214decimal_lit = ( "1" ... "9" ) { decimal_digit } .
215octal_lit = "0" { octal_digit } .
216hex_lit = "0" ( "x" | "X" ) hex_digit { hex_digit } .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800217</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700218
Robert Griesemerc2d55862009-02-19 16:49:10 -0800219<pre>
22042
2210600
2220xBadFace
223170141183460469231731687303715884105727
224</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700225
Rob Pikeff70f092009-02-20 13:36:14 -0800226<h3>Floating-point literals</h3>
227<p>
228A floating-point literal is a decimal representation of a floating-point
229number. It has an integer part, a decimal point, a fractional part,
230and an exponent part. The integer and fractional part comprise
Rob Pikef27e9f02009-02-23 19:22:05 -0800231decimal digits; the exponent part is an <code>e</code> or <code>E</code>
Rob Pikeff70f092009-02-20 13:36:14 -0800232followed by an optionally signed decimal exponent. One of the
233integer part or the fractional part may be elided; one of the decimal
234point or the exponent may be elided.
235</p>
236<pre class="grammar">
237float_lit = decimals "." [ decimals ] [ exponent ] |
238 decimals exponent |
239 "." decimals [ exponent ] .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800240decimals = decimal_digit { decimal_digit } .
241exponent = ( "e" | "E" ) [ "+" | "-" ] decimals .
242</pre>
Robert Griesemerad711102008-09-11 17:48:20 -0700243
Robert Griesemerc2d55862009-02-19 16:49:10 -0800244<pre>
2450.
2462.71828
2471.e+0
2486.67428e-11
2491E6
250.25
251.12345E+5
252</pre>
Robert Griesemerad711102008-09-11 17:48:20 -0700253
Rob Pikeff70f092009-02-20 13:36:14 -0800254<h3>Ideal numbers</h3>
255
Robert Griesemerc2d55862009-02-19 16:49:10 -0800256<p>
Rob Pikeff70f092009-02-20 13:36:14 -0800257Integer literals represent values of arbitrary precision, or <i>ideal
258integers</i>. Similarly, floating-point literals represent values
259of arbitrary precision, or <i>ideal floats</i>. These <i>ideal
260numbers</i> have no size or type and cannot overflow. However,
261when (used in an expression) assigned to a variable or typed constant,
262the destination must be able to represent the assigned value.
263</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -0800264<p>
Robert Griesemerad711102008-09-11 17:48:20 -0700265Implementation restriction: A compiler may implement ideal numbers
Rob Pike8f2330d2009-02-25 16:20:44 -0800266by choosing an internal representation with at least twice the precision
267of any machine type.
Rob Pikeff70f092009-02-20 13:36:14 -0800268</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700269
Rob Pikeff70f092009-02-20 13:36:14 -0800270<h3>Character literals</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700271
Rob Pike4501d342009-02-19 17:31:36 -0800272<p>
Rob Pikeff70f092009-02-20 13:36:14 -0800273A character literal represents an integer value, typically a
274Unicode code point, as one or more characters enclosed in single
275quotes. Within the quotes, any character may appear except single
276quote and newline. A single quoted character represents itself,
277while multi-character sequences beginning with a backslash encode
278values in various formats.
Rob Pike4501d342009-02-19 17:31:36 -0800279</p>
Rob Pikeff70f092009-02-20 13:36:14 -0800280<p>
281The simplest form represents the single character within the quotes;
282since Go source text is Unicode characters encoded in UTF-8, multiple
283UTF-8-encoded bytes may represent a single integer value. For
Rob Pikef27e9f02009-02-23 19:22:05 -0800284instance, the literal <code>'a'</code> holds a single byte representing
285a literal <code>a</code>, Unicode U+0061, value <code>0x61</code>, while
286<code>'ä'</code> holds two bytes (<code>0xc3</code> <code>0xa4</code>) representing
287a literal <code>a</code>-dieresis, U+00E4, value <code>0xe4</code>.
Rob Pikeff70f092009-02-20 13:36:14 -0800288</p>
289<p>
290Several backslash escapes allow arbitrary values to be represented
291as ASCII text. There are four ways to represent the integer value
Rob Pikef27e9f02009-02-23 19:22:05 -0800292as a numeric constant: <code>\x</code> followed by exactly two hexadecimal
293digits; <code>\u</code> followed by exactly four hexadecimal digits;
294<code>\U</code> followed by exactly eight hexadecimal digits, and a
295plain backslash <code>\</code> followed by exactly three octal digits.
Rob Pikeff70f092009-02-20 13:36:14 -0800296In each case the value of the literal is the value represented by
297the digits in the corresponding base.
298</p>
299<p>
300Although these representations all result in an integer, they have
301different valid ranges. Octal escapes must represent a value between
3020 and 255 inclusive. (Hexadecimal escapes satisfy this condition
Rob Pikef27e9f02009-02-23 19:22:05 -0800303by construction). The `Unicode' escapes <code>\u</code> and <code>\U</code>
Rob Pikeff70f092009-02-20 13:36:14 -0800304represent Unicode code points so within them some values are illegal,
Rob Pikef27e9f02009-02-23 19:22:05 -0800305in particular those above <code>0x10FFFF</code> and surrogate halves.
Rob Pikeff70f092009-02-20 13:36:14 -0800306</p>
307<p>
308After a backslash, certain single-character escapes represent special values:
309</p>
310<pre class="grammar">
311\a U+0007 alert or bell
312\b U+0008 backspace
313\f U+000C form feed
314\n U+000A line feed or newline
315\r U+000D carriage return
316\t U+0009 horizontal tab
317\v U+000b vertical tab
318\\ U+005c backslash
319\' U+0027 single quote (valid escape only within character literals)
320\" U+0022 double quote (valid escape only within string literals)
Robert Griesemerc2d55862009-02-19 16:49:10 -0800321</pre>
Robert Griesemerc2d55862009-02-19 16:49:10 -0800322<p>
Rob Pikeff70f092009-02-20 13:36:14 -0800323All other sequences are illegal inside character literals.
Rob Pike4501d342009-02-19 17:31:36 -0800324</p>
Rob Pikeff70f092009-02-20 13:36:14 -0800325<pre class="grammar">
326char_lit = "'" ( unicode_value | byte_value ) "'" .
327unicode_value = unicode_char | little_u_value | big_u_value | escaped_char .
328byte_value = octal_byte_value | hex_byte_value .
329octal_byte_value = "\" octal_digit octal_digit octal_digit .
330hex_byte_value = "\" "x" hex_digit hex_digit .
331little_u_value = "\" "u" hex_digit hex_digit hex_digit hex_digit .
332big_u_value = "\" "U" hex_digit hex_digit hex_digit hex_digit
333 hex_digit hex_digit hex_digit hex_digit .
334escaped_char = "\" ( "a" | "b" | "f" | "n" | "r" | "t" | "v" | "\" | "'" | """ ) .
335</pre>
Robert Griesemerc2d55862009-02-19 16:49:10 -0800336<pre>
337'a'
338'ä'
339'本'
340'\t'
341'\000'
342'\007'
343'\377'
344'\x07'
345'\xff'
346'\u12e4'
347'\U00101234'
348</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700349
Rob Pikeff70f092009-02-20 13:36:14 -0800350<p>
351The value of a character literal is an ideal integer, just as with
352integer literals.
353</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700354
Rob Pikeff70f092009-02-20 13:36:14 -0800355<h3>String literals</h3>
356
357<p>
Rob Pikef27e9f02009-02-23 19:22:05 -0800358String literals represent constant values of type <code>string</code>.
Rob Pikeff70f092009-02-20 13:36:14 -0800359There are two forms: raw string literals and interpreted string
360literals.
361</p>
362<p>
363Raw string literals are character sequences between back quotes
Rob Pikef27e9f02009-02-23 19:22:05 -0800364<code>``</code>. Within the quotes, any character is legal except
Rob Pikeff70f092009-02-20 13:36:14 -0800365newline and back quote. The value of a raw string literal is the
366string composed of the uninterpreted bytes between the quotes;
367in particular, backslashes have no special meaning.
368</p>
369<p>
370Interpreted string literals are character sequences between double
Rob Pikef27e9f02009-02-23 19:22:05 -0800371quotes <code>&quot;&quot;</code>. The text between the quotes forms the
Rob Pikeff70f092009-02-20 13:36:14 -0800372value of the literal, with backslash escapes interpreted as they
Rob Pikef27e9f02009-02-23 19:22:05 -0800373are in character literals (except that <code>\'</code> is illegal and
374<code>\"</code> is legal). The three-digit octal (<code>\000</code>)
375and two-digit hexadecimal (<code>\x00</code>) escapes represent individual
Rob Pikeff70f092009-02-20 13:36:14 -0800376<i>bytes</i> of the resulting string; all other escapes represent
377the (possibly multi-byte) UTF-8 encoding of individual <i>characters</i>.
Rob Pikef27e9f02009-02-23 19:22:05 -0800378Thus inside a string literal <code>\377</code> and <code>\xFF</code> represent
379a single byte of value <code>0xFF</code>=255, while <code>ÿ</code>,
380<code>\u00FF</code>, <code>\U000000FF</code> and <code>\xc3\xbf</code> represent
381the two bytes <code>0xc3 0xbf</code> of the UTF-8 encoding of character
Rob Pikeff70f092009-02-20 13:36:14 -0800382U+00FF.
383</p>
384
385<pre class="grammar">
386string_lit = raw_string_lit | interpreted_string_lit .
387raw_string_lit = "`" { unicode_char } "`" .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800388interpreted_string_lit = """ { unicode_value | byte_value } """ .
389</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700390
Robert Griesemerc2d55862009-02-19 16:49:10 -0800391<pre>
392`abc`
393`\n`
394"hello, world\n"
395"\n"
396""
397"Hello, world!\n"
398"日本語"
399"\u65e5本\U00008a9e"
400"\xff\u00FF"
401</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700402
Rob Pikeff70f092009-02-20 13:36:14 -0800403<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700404These examples all represent the same string:
Rob Pikeff70f092009-02-20 13:36:14 -0800405</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700406
Robert Griesemerc2d55862009-02-19 16:49:10 -0800407<pre>
Rob Pikeff70f092009-02-20 13:36:14 -0800408"日本語" // UTF-8 input text
409`日本語` // UTF-8 input text as a raw literal
410"\u65e5\u672c\u8a9e" // The explicit Unicode code points
411"\U000065e5\U0000672c\U00008a9e" // The explicit Unicode code points
Robert Griesemerc2d55862009-02-19 16:49:10 -0800412"\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e" // The explicit UTF-8 bytes
413</pre>
Robert Griesemercd499272008-09-29 12:09:00 -0700414
Robert Griesemerc2d55862009-02-19 16:49:10 -0800415<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700416If the source code represents a character as two code points, such as
417a combining form involving an accent and a letter, the result will be
418an error if placed in a character literal (it is not a single code
419point), and will appear as two code points if placed in a string
420literal.
Rob Pikeff70f092009-02-20 13:36:14 -0800421</p>
422<hr/>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700423
Robert Griesemerc2d55862009-02-19 16:49:10 -0800424<h2>Types</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700425
Robert Griesemerc2d55862009-02-19 16:49:10 -0800426<p>
Rob Pike8f2330d2009-02-25 16:20:44 -0800427A type determines the set of values and operations specific to values of that type.
Rob Pike5af7de32009-02-24 15:17:59 -0800428A type may be specified by a (possibly qualified (§Qualified identifiers))
429type name (§Type declarations) or a <i>type literal</i>,
430which composes a new type in terms of previously declared types.
431</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700432
Rob Pikeff70f092009-02-20 13:36:14 -0800433<pre class="grammar">
Rob Pike8f2330d2009-02-25 16:20:44 -0800434Type = TypeName | TypeLit | "(" Type ")" .
435TypeName = QualifiedIdent.
436TypeLit = ArrayType | StructType | PointerType | FunctionType | InterfaceType |
437 SliceType | MapType | ChannelType .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800438</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -0800439
Robert Griesemerc2d55862009-02-19 16:49:10 -0800440<p>
Rob Pike5af7de32009-02-24 15:17:59 -0800441<i>Basic types</i> such as <code>int</code> are predeclared (§Predeclared identifiers).
442Other types may be constructed from these, recursively,
443including arrays, structs, pointers, functions, interfaces, slices, maps, and
Robert Griesemera3294712009-01-05 11:17:26 -0800444channels.
Rob Pike5af7de32009-02-24 15:17:59 -0800445</p>
Robert Griesemer7abfcd92008-10-07 17:14:30 -0700446
Rob Pike5af7de32009-02-24 15:17:59 -0800447<p>
Rob Pike5af7de32009-02-24 15:17:59 -0800448At any point in the source code, a type may be <i>complete</i> or
Rob Pikecdbf6192009-02-24 17:47:45 -0800449<i>incomplete</i>. An incomplete type is one whose size is not
450yet known, such as a struct whose fields are not yet fully
451defined or a forward declared type (§Forward declarations).
452Most types are always complete; for instance, a pointer
453type is always complete even if it points to an incomplete type
454because the size of the pointer itself is always known.
Russ Cox461dd912009-03-04 14:44:51 -0800455(TODO: Need to figure out how forward declarations of
456interface fit in here.)
Rob Pike5af7de32009-02-24 15:17:59 -0800457</p>
458<p>
459The <i>interface</i> of a type is the set of methods bound to it
460(§Method declarations); for pointer types, it is the interface
Robert Griesemera1065fa2008-09-29 20:37:46 -0700461of the pointer base type (§Pointer types). All types have an interface;
Robert Griesemerf88c6c12009-02-25 16:58:57 -0800462if they have no methods, it is the <i>empty interface</i>.
Rob Pike5af7de32009-02-24 15:17:59 -0800463</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -0800464<p>
Rob Pike5af7de32009-02-24 15:17:59 -0800465The <i>static type</i> (or just <i>type</i>) of a variable is the
466type defined by its declaration. Variables of interface type
467(§Interface types) also have a distinct <i>dynamic type</i>, which
468is the actual type of the value stored in the variable at run-time.
469The dynamic type may vary during execution but is always compatible
470with the static type of the interface variable. For non-interfaces
471types, the dynamic type is always the static type.
472</p>
Robert Griesemer1f3e8422008-09-29 18:41:30 -0700473
Robert Griesemerc2d55862009-02-19 16:49:10 -0800474<h3>Basic types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700475
Rob Pike5af7de32009-02-24 15:17:59 -0800476<p>
Russ Cox5958dd62009-03-04 17:19:21 -0800477Basic types include traditional numeric types, booleans, and strings. All are predeclared.
Rob Pike5af7de32009-02-24 15:17:59 -0800478</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700479
Russ Cox5958dd62009-03-04 17:19:21 -0800480<h3>Numeric types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700481
Rob Pike5af7de32009-02-24 15:17:59 -0800482<p>
483The architecture-independent numeric types are:
484</p>
Robert Griesemerebf14c62008-10-30 14:50:23 -0700485
Rob Pikeff70f092009-02-20 13:36:14 -0800486<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -0800487uint8 the set of all unsigned 8-bit integers (0 to 255)
488uint16 the set of all unsigned 16-bit integers (0 to 65535)
489uint32 the set of all unsigned 32-bit integers (0 to 4294967295)
490uint64 the set of all unsigned 64-bit integers (0 to 18446744073709551615)
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700491
Robert Griesemerc2d55862009-02-19 16:49:10 -0800492int8 the set of all signed 8-bit integers (-128 to 127)
493int16 the set of all signed 16-bit integers (-32768 to 32767)
494int32 the set of all signed 32-bit integers (-2147483648 to 2147483647)
495int64 the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700496
Robert Griesemerc2d55862009-02-19 16:49:10 -0800497float32 the set of all valid IEEE-754 32-bit floating point numbers
498float64 the set of all valid IEEE-754 64-bit floating point numbers
Rob Pike5af7de32009-02-24 15:17:59 -0800499
500byte familiar alias for uint8
Robert Griesemerc2d55862009-02-19 16:49:10 -0800501</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700502
Rob Pike5af7de32009-02-24 15:17:59 -0800503<p>
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -0800504Integer types are represented in the usual binary format; the value of
505an n-bit integer is n bits wide. A negative signed integer is represented
506as the two's complement of its absolute value.
Rob Pike5af7de32009-02-24 15:17:59 -0800507</p>
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -0800508
Rob Pike5af7de32009-02-24 15:17:59 -0800509<p>
510There is also a set of architecture-independent basic numeric types
511whose size depends on the architecture:
512</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700513
Rob Pikeff70f092009-02-20 13:36:14 -0800514<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -0800515uint at least 32 bits, at most the size of the largest uint type
516int at least 32 bits, at most the size of the largest int type
517float at least 32 bits, at most the size of the largest float type
518uintptr smallest uint type large enough to store the uninterpreted
519 bits of a pointer value
520</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700521
Robert Griesemerc2d55862009-02-19 16:49:10 -0800522<p>
Rob Pikeda389742009-03-02 19:13:40 -0800523To avoid portability issues all numeric types are distinct except
524<code>byte</code>, which is an alias for <code>uint8</code>.
525Conversions
Rob Pike5af7de32009-02-24 15:17:59 -0800526are required when different numeric types are mixed in an expression
527or assignment. For instance, <code>int32</code> and <code>int</code>
Russ Cox5958dd62009-03-04 17:19:21 -0800528are not the same type even though they may have the same size on a
Rob Pike5af7de32009-02-24 15:17:59 -0800529particular architecture.
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700530
531
Robert Griesemerc2d55862009-02-19 16:49:10 -0800532<h3>Booleans</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700533
Rob Pike5af7de32009-02-24 15:17:59 -0800534The type <code>bool</code> comprises the Boolean truth values
535represented by the predeclared constants <code>true</code>
536and <code>false</code>.
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700537
538
Robert Griesemerc2d55862009-02-19 16:49:10 -0800539<h3>Strings</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700540
Rob Pike4501d342009-02-19 17:31:36 -0800541<p>
Rob Pike5af7de32009-02-24 15:17:59 -0800542The <code>string</code> type represents the set of textual string values.
543Strings behave like arrays of bytes but are immutable: once created,
544it is impossible to change the contents of a string.
545
546<p>
547The elements of strings have type <code>byte</code> and may be
548accessed using the usual indexing operations (§Indexes). It is
549illegal to take the address of such an element, that is, even if
550<code>s[i]</code> is the <code>i</code><sup>th</sup> byte of a
551string, <code>&amp;s[i]</code> is invalid. The length of a string
552can be computed by the function <code>len(s1)</code>.
Rob Pike4501d342009-02-19 17:31:36 -0800553</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700554
Rob Pike5af7de32009-02-24 15:17:59 -0800555<p>
Rob Pikecdbf6192009-02-24 17:47:45 -0800556A sequence of string literals is concatenated into a single string.
Rob Pike5af7de32009-02-24 15:17:59 -0800557</p>
558<pre class="grammar">
Rob Pikecdbf6192009-02-24 17:47:45 -0800559StringLit = string_lit { string_lit } .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800560</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700561
Robert Griesemerc2d55862009-02-19 16:49:10 -0800562<pre>
Rob Pike8f2330d2009-02-25 16:20:44 -0800563"Alea iacta est."
564"Alea " /* The die */ `iacta est` /* is cast */ "."
Robert Griesemerc2d55862009-02-19 16:49:10 -0800565</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700566
Russ Cox461dd912009-03-04 14:44:51 -0800567<h3>Array types</h3>
568
569<p>
570An array is a numbered sequence of elements of a single
571type, called the element type, which must be complete
572(§Types). The number of elements is called the length and is never
573negative.
574</p>
575
576<pre class="grammar">
577ArrayType = "[" ArrayLength "]" ElementType .
578ArrayLength = Expression .
579ElementType = CompleteType .
580</pre>
581
582<p>
583The length is part of the array's type and must must be a constant
584expression (§Constant expressions) that evaluates to a non-negative
585integer value. The length of array <code>a</code> can be discovered
586using the built-in function <code>len(a)</code>, which is a
587compile-time constant. The elements can be indexed by integer
588indices 0 through the <code>len(a)-1</code> (§Indexes).
589</p>
590
591<pre>
592[32]byte
593[2*N] struct { x, y int32 }
594[1000]*float64
595</pre>
596
597<h3>Slice types</h3>
598
599<p>
600A slice is a reference to a contiguous segment of an array and
601contains a numbered sequence of elements from that array. A slice
602type denotes the set of all slices of arrays of its element type.
603A slice value may be <code>nil</code>.
604</p>
605
606<pre class="grammar">
607SliceType = "[" "]" ElementType .
608</pre>
609
610<p>
611Like arrays, slices are indexable and have a length. The length of a
612slice <code>s</code> can be discovered by the built-in function
613<code>len(s)</code>; unlike with arrays it may change during
614execution. The elements can be addressed by integer indices 0
615through <code>len(s)-1</code> (§Indexes). The slice index of a
616given element may be less than the index of the same element in the
617underlying array.
618</p>
619<p>
620A slice, once initialized, is always associated with an underlying
621array that holds its elements. A slice therfore shares storage
622with its array and with other slices of the same array; by contrast,
623distinct arrays always represent distinct storage.
624</p>
625<p>
626The array underlying a slice may extend past the end of the slice.
Russ Cox5958dd62009-03-04 17:19:21 -0800627The <i>capacity</i> is a measure of that extent: it is the sum of
Russ Cox461dd912009-03-04 14:44:51 -0800628the length of the slice and the length of the array beyond the slice;
629a slice of length up to that capacity can be created by `slicing' a new
630one from the original slice (§Slices).
631The capacity of a slice <code>a</code> can be discovered using the
632built-in function
633</p>
634
635<pre>
636cap(s)
637</pre>
638
639<p>
640and the relationship between <code>len()</code> and <code>cap()</code> is:
641</p>
642
643<pre>
6440 <= len(a) <= cap(a)
645</pre>
646
647<p>
648The value of an uninitialized slice is <code>nil</code>.
649The length and capacity of a <code>nil</code> slice
650are 0. A new, initialized slice value for a given element type <code>T</code> is
651made using the built-in function <code>make</code>, which takes a slice type
652and parameters specifying the length and optionally the capacity:
653</p>
654
655<pre>
656make([]T, length)
657make([]T, length, capacity)
658</pre>
Russ Cox5958dd62009-03-04 17:19:21 -0800659
Russ Cox461dd912009-03-04 14:44:51 -0800660<p>
661The <code>make()</code> call allocates a new, hidden array to which the returned
662slice value refers. That is, calling <code>make</code>
663</p>
664
665<pre>
666make([]T, length, capacity)
667</pre>
668
669<p>
670produces the same slice as allocating an array and slicing it, so these two examples
671result in the same slice:
672</p>
673
674<pre>
675make([]int, 50, 100)
676new([100]int)[0:50]
677</pre>
678
679
Robert Griesemerc2d55862009-02-19 16:49:10 -0800680<h3>Struct types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700681
Rob Pike5af7de32009-02-24 15:17:59 -0800682<p>
683A struct is a sequence of named
684elements, called fields, with various types. A struct type declares
Rob Pikeda389742009-03-02 19:13:40 -0800685an identifier and type for each field. Within a struct, field identifiers
Rob Pike5af7de32009-02-24 15:17:59 -0800686must be unique and field types must be complete (§Types).
687</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700688
Rob Pikeff70f092009-02-20 13:36:14 -0800689<pre class="grammar">
Russ Cox461dd912009-03-04 14:44:51 -0800690StructType = "struct" "{" [ FieldDeclList ] "}" .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800691FieldDeclList = FieldDecl { ";" FieldDecl } [ ";" ] .
692FieldDecl = (IdentifierList CompleteType | [ "*" ] TypeName) [ Tag ] .
693Tag = StringLit .
694</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700695
Robert Griesemerc2d55862009-02-19 16:49:10 -0800696<pre>
697// An empty struct.
698struct {}
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700699
Robert Griesemerc2d55862009-02-19 16:49:10 -0800700// A struct with 5 fields.
701struct {
702 x, y int;
703 u float;
704 A *[]int;
705 F func();
706}
707</pre>
Robert Griesemer1f3e8422008-09-29 18:41:30 -0700708
Rob Pike5af7de32009-02-24 15:17:59 -0800709<p>
710A field declared with a type but no field identifier is an <i>anonymous field</i>.
711Such a field type must be specified as
Rob Pikeda389742009-03-02 19:13:40 -0800712a type name <code>T</code> or as a pointer to a type name <code>*T</code>,
713and <code>T</code> itself, may not be
Robert Griesemer83c17602009-01-16 14:12:50 -0800714a pointer or interface type. The unqualified type name acts as the field identifier.
Rob Pike5af7de32009-02-24 15:17:59 -0800715</p>
Robert Griesemer1f3e8422008-09-29 18:41:30 -0700716
Robert Griesemerc2d55862009-02-19 16:49:10 -0800717<pre>
718// A struct with four anonymous fields of type T1, *T2, P.T3 and *P.T4
719struct {
720 T1; // the field name is T1
721 *T2; // the field name is T2
Rob Pikeda389742009-03-02 19:13:40 -0800722 P.T3; // the field name is T3
723 *P.T4; // the field name is T4
Russ Cox5958dd62009-03-04 17:19:21 -0800724 x, y int;
Robert Griesemerc2d55862009-02-19 16:49:10 -0800725}
726</pre>
Robert Griesemer1f3e8422008-09-29 18:41:30 -0700727
Rob Pike5af7de32009-02-24 15:17:59 -0800728<p>
Robert Griesemer7abfcd92008-10-07 17:14:30 -0700729The unqualified type name of an anonymous field must not conflict with the
730field identifier (or unqualified type name for an anonymous field) of any
Robert Griesemer071c91b2008-10-23 12:04:45 -0700731other field within the struct. The following declaration is illegal:
Rob Pike5af7de32009-02-24 15:17:59 -0800732</p>
Robert Griesemer071c91b2008-10-23 12:04:45 -0700733
Robert Griesemerc2d55862009-02-19 16:49:10 -0800734<pre>
735struct {
736 T; // conflicts with anonymous field *T and *P.T
737 *T; // conflicts with anonymous field T and *P.T
738 *P.T; // conflicts with anonymous field T and *T
739}
740</pre>
Robert Griesemer1f3e8422008-09-29 18:41:30 -0700741
Robert Griesemerc2d55862009-02-19 16:49:10 -0800742<p>
Rob Pike5af7de32009-02-24 15:17:59 -0800743Fields and methods (§Method declarations) of an anonymous field are
744promoted to be ordinary fields and methods of the struct (§Selectors).
745</p>
746<p>
747A field declaration may be followed by an optional string literal <i>tag</i>, which
748becomes an attribute for all the identifiers in the corresponding
749field declaration. The tags are made
750visible through a reflection library (TODO: reference?)
751but are otherwise ignored.
752</p>
Robert Griesemer2e90e542008-10-30 15:52:37 -0700753
Robert Griesemerc2d55862009-02-19 16:49:10 -0800754<pre>
755// A struct corresponding to the EventIdMessage protocol buffer.
Rob Pikecdbf6192009-02-24 17:47:45 -0800756// The tag strings define the protocol buffer field numbers.
Robert Griesemerc2d55862009-02-19 16:49:10 -0800757struct {
Rob Pike5af7de32009-02-24 15:17:59 -0800758 time_usec uint64 "field 1";
759 server_ip uint32 "field 2";
760 process_id uint32 "field 3";
Robert Griesemerc2d55862009-02-19 16:49:10 -0800761}
762</pre>
Robert Griesemer2e90e542008-10-30 15:52:37 -0700763
Robert Griesemerc2d55862009-02-19 16:49:10 -0800764<h3>Pointer types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700765
Rob Pike5af7de32009-02-24 15:17:59 -0800766<p>
Robert Griesemer4dc25282008-09-09 10:37:19 -0700767A pointer type denotes the set of all pointers to variables of a given
Rob Pikeda389742009-03-02 19:13:40 -0800768type, called the <i>base type</i> of the pointer.
Rob Pike8f2330d2009-02-25 16:20:44 -0800769A pointer value may be <code>nil</code>.
Rob Pike5af7de32009-02-24 15:17:59 -0800770</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700771
Rob Pikeff70f092009-02-20 13:36:14 -0800772<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -0800773PointerType = "*" BaseType .
774BaseType = Type .
775</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700776
Robert Griesemerc2d55862009-02-19 16:49:10 -0800777<pre>
778*int
Rob Pike8f2330d2009-02-25 16:20:44 -0800779*map[string] *chan int
Robert Griesemerc2d55862009-02-19 16:49:10 -0800780</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700781
Robert Griesemerc2d55862009-02-19 16:49:10 -0800782<h3>Function types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700783
Rob Pike8f2330d2009-02-25 16:20:44 -0800784<p>
Robert Griesemer7231ceb2008-09-08 15:01:04 -0700785A function type denotes the set of all functions with the same parameter
Rob Pike8f2330d2009-02-25 16:20:44 -0800786and result types.
787A function value may be <code>nil</code>.
788</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700789
Rob Pikeff70f092009-02-20 13:36:14 -0800790<pre class="grammar">
Rob Pike8f2330d2009-02-25 16:20:44 -0800791FunctionType = "func" Signature .
792Signature = Parameters [ Result ] .
793Result = Parameters | CompleteType .
794Parameters = "(" [ ParameterList ] ")" .
795ParameterList = ParameterDecl { "," ParameterDecl } .
796ParameterDecl = [ IdentifierList ] ( CompleteType | "..." ) .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800797</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700798
Robert Griesemerc2d55862009-02-19 16:49:10 -0800799<p>
Rob Pike8f2330d2009-02-25 16:20:44 -0800800Within a list of parameters or results, the names (IdentifierList)
801must either all be present or all be absent. If present, each name
802stands for one item (parameter or result) of the specified type; if absent, each
803type stands for one item of that type. Parameter and result
804lists are always parenthesized except that if there is exactly
805one unnamed result that is not a function type it may writen as an unparenthesized type.
806The types of parameters and results must be complete.
807(TODO: is completeness necessary?)
808</p>
809<p>
810For the last parameter only, instead of a type one may write
811<code>...</code> to indicate that the function may be invoked with
Rob Pikeda389742009-03-02 19:13:40 -0800812zero or more additional arguments of any
Rob Pike8f2330d2009-02-25 16:20:44 -0800813type. If parameters of such a function are named, the final identifier
814list must be a single name, that of the <code>...</code> parameter.
815</p>
Robert Griesemer2bfa9572008-10-24 13:13:12 -0700816
Robert Griesemerc2d55862009-02-19 16:49:10 -0800817<pre>
818func ()
819func (x int)
820func () int
821func (string, float, ...)
822func (a, b int, z float) bool
823func (a, b int, z float) (bool)
824func (a, b int, z float, opt ...) (success bool)
825func (int, int, float) (float, *[]int)
Robert Griesemerc2d55862009-02-19 16:49:10 -0800826func (n int) (func (p* T))
827</pre>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -0800828
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700829
Robert Griesemerc2d55862009-02-19 16:49:10 -0800830<h3>Interface types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700831
Rob Pike8f2330d2009-02-25 16:20:44 -0800832<p>
833An interface type specifies an unordered set of methods. A variable
834of interface type can store, dynamically, any value that implements
835at least that set of methods.
836An interface value may be <code>nil</code>.
837</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700838
Rob Pikeff70f092009-02-20 13:36:14 -0800839<pre class="grammar">
Russ Cox461dd912009-03-04 14:44:51 -0800840InterfaceType = "interface" "{" [ MethodSpecList ] "}" .
Rob Pike8f2330d2009-02-25 16:20:44 -0800841MethodSpecList = MethodSpec { ";" MethodSpec } [ ";" ] .
842MethodSpec = IdentifierList Signature | InterfaceTypeName .
843InterfaceTypeName = TypeName .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800844</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700845
Robert Griesemerc2d55862009-02-19 16:49:10 -0800846<pre>
Rob Pike8f2330d2009-02-25 16:20:44 -0800847// A simple File interface
Robert Griesemerc2d55862009-02-19 16:49:10 -0800848interface {
849 Read, Write (b Buffer) bool;
850 Close ();
851}
852</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700853
Rob Pike8f2330d2009-02-25 16:20:44 -0800854<p>
855Any type (including interface types) whose interface includes,
856possibly as a subset, the complete set of methods of an interface <code>I</code>
857is said to implement interface <code>I</code>.
858For instance, if two types <code>S1</code> and <code>S2</code>
859have the methods
860</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700861
Robert Griesemerc2d55862009-02-19 16:49:10 -0800862<pre>
863func (p T) Read(b Buffer) bool { return ... }
864func (p T) Write(b Buffer) bool { return ... }
865func (p T) Close() { ... }
866</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700867
Rob Pike8f2330d2009-02-25 16:20:44 -0800868<p>
869(where <code>T</code> stands for either <code>S1</code> or <code>S2</code>)
870then the <code>File</code> interface is implemented by both <code>S1</code> and
871<code>S2</code>, regardless of what other methods
872<code>S1</code> and <code>S2</code> may have or share.
873</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700874
Rob Pike8f2330d2009-02-25 16:20:44 -0800875<p>
876A type implements any interface comprising any subset of its methods
877and may therefore implement several distinct interfaces. For
878instance, all types implement the <i>empty interface</i>:
879</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700880
Robert Griesemerc2d55862009-02-19 16:49:10 -0800881<pre>
Rob Pike8f2330d2009-02-25 16:20:44 -0800882interface { }
Robert Griesemerc2d55862009-02-19 16:49:10 -0800883</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700884
Rob Pike8f2330d2009-02-25 16:20:44 -0800885<p>
886Similarly, consider this interface specification,
887which appears within a type declaration (§Type declarations)
888to define an interface called <code>Lock</code>:
889</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700890
Robert Griesemerc2d55862009-02-19 16:49:10 -0800891<pre>
892type Lock interface {
893 Lock, Unlock ();
894}
895</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700896
Rob Pike8f2330d2009-02-25 16:20:44 -0800897<p>
898If <code>S1</code> and <code>S2</code> also implement
899</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700900
Robert Griesemerc2d55862009-02-19 16:49:10 -0800901<pre>
902func (p T) Lock() { ... }
903func (p T) Unlock() { ... }
904</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700905
Robert Griesemerc2d55862009-02-19 16:49:10 -0800906<p>
Rob Pike8f2330d2009-02-25 16:20:44 -0800907they implement the <code>Lock</code> interface as well
908as the <code>File</code> interface.
909</p>
910<p>
911An interface may contain an interface type name <code>T</code>
912in place of a method specification.
913In this notation, <code>T</code> must denote a different, complete interface type
914and the effect is equivalent to enumerating the methods of <code>T</code> explicitly
915in the interface.
916</p>
Robert Griesemer38c232f2009-02-11 15:09:15 -0800917
Robert Griesemerc2d55862009-02-19 16:49:10 -0800918<pre>
919type ReadWrite interface {
920 Read, Write (b Buffer) bool;
921}
Robert Griesemer38c232f2009-02-11 15:09:15 -0800922
Robert Griesemerc2d55862009-02-19 16:49:10 -0800923type File interface {
924 ReadWrite; // same as enumerating the methods in ReadWrite
925 Lock; // same as enumerating the methods in Lock
926 Close();
927}
928</pre>
Robert Griesemer38c232f2009-02-11 15:09:15 -0800929
Robert Griesemerc2d55862009-02-19 16:49:10 -0800930<h3>Map types</h3>
Robert Griesemera3294712009-01-05 11:17:26 -0800931
Rob Pike8f2330d2009-02-25 16:20:44 -0800932<p>
933A map is an unordered group of elements of one type, called the
934value type, indexed by a set of unique <i>keys</i> of another type,
935called the key type. Both key and value types must be complete.
936(§Types).
937(TODO: is completeness necessary here?)
938A map value may be <code>nil</code>.
939
940</p>
Robert Griesemera3294712009-01-05 11:17:26 -0800941
Rob Pikeff70f092009-02-20 13:36:14 -0800942<pre class="grammar">
Rob Pike8f2330d2009-02-25 16:20:44 -0800943MapType = "map" "[" KeyType "]" ValueType .
944KeyType = CompleteType .
945ValueType = CompleteType .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800946</pre>
Robert Griesemera3294712009-01-05 11:17:26 -0800947
Robert Griesemerc2d55862009-02-19 16:49:10 -0800948<p>
Rob Pike8f2330d2009-02-25 16:20:44 -0800949The comparison operators <code>==</code> and <code>!=</code>
950(§Comparison operators) must be fully defined for operands of the
951key type; thus the key type must be a basic, pointer, interface,
952map, or channel type. If the key type is an interface type, these
953comparison operators must be defined for the dynamic key values;
954failure will cause a run-time error.
955
956</p>
Robert Griesemera3294712009-01-05 11:17:26 -0800957
Robert Griesemerc2d55862009-02-19 16:49:10 -0800958<pre>
959map [string] int
960map [*T] struct { x, y float }
961map [string] interface {}
962</pre>
Robert Griesemera3294712009-01-05 11:17:26 -0800963
Rob Pike5af7de32009-02-24 15:17:59 -0800964<p>
Rob Pike8f2330d2009-02-25 16:20:44 -0800965The number of elements is called the length and is never negative.
966The length of a map <code>m</code> can be discovered using the
967built-in function <code>len(m)</code> and may change during execution.
Rob Pikeda389742009-03-02 19:13:40 -0800968The value of an uninitialized map is <code>nil</code>.
Rob Pike5af7de32009-02-24 15:17:59 -0800969</p>
Rob Pike8f2330d2009-02-25 16:20:44 -0800970<p>
971Upon creation, a map is empty. Values may be added and removed
972during execution using special forms of assignment (§Assignments).
973A new, empty map value is made using the built-in
974function <code>make</code>, which takes the map type and an optional
Rob Pikeda389742009-03-02 19:13:40 -0800975capacity hint as arguments:
Rob Pike8f2330d2009-02-25 16:20:44 -0800976</p>
977
978<pre>
Rob Pikeda389742009-03-02 19:13:40 -0800979make(map[string] int)
980make(map[string] int, 100)
Rob Pike8f2330d2009-02-25 16:20:44 -0800981</pre>
Robert Griesemera3294712009-01-05 11:17:26 -0800982
Rob Pikeda389742009-03-02 19:13:40 -0800983<p>
984The initial capacity does not bound its size:
985maps grow to accommodate the number of items
986stored in them.
987</p>
988
Robert Griesemerc2d55862009-02-19 16:49:10 -0800989<h3>Channel types</h3>
Robert Griesemera3294712009-01-05 11:17:26 -0800990
Rob Pike8f2330d2009-02-25 16:20:44 -0800991<p>
Robert Griesemera3294712009-01-05 11:17:26 -0800992A channel provides a mechanism for two concurrently executing functions
Rob Pike8f2330d2009-02-25 16:20:44 -0800993to synchronize execution and communicate by passing a value of a
994specified element type. The element type must be complete (§Types).
995(TODO: is completeness necessary here?)
996A channel value may be <code>nil</code>.
997</p>
Robert Griesemera3294712009-01-05 11:17:26 -0800998
Rob Pikeff70f092009-02-20 13:36:14 -0800999<pre class="grammar">
Rob Pike8f2330d2009-02-25 16:20:44 -08001000ChannelType = Channel | SendChannel | RecvChannel .
1001Channel = "chan" ValueType .
1002SendChannel = "chan" "&lt;-" ValueType .
1003RecvChannel = "&lt;-" "chan" ValueType .
Robert Griesemerc2d55862009-02-19 16:49:10 -08001004</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001005
Rob Pike8f2330d2009-02-25 16:20:44 -08001006<p>
1007Upon creation, a channel can be used both to send and to receive values.
Robert Griesemera3294712009-01-05 11:17:26 -08001008By conversion or assignment, a channel may be constrained only to send or
Rob Pike8f2330d2009-02-25 16:20:44 -08001009to receive. This constraint is called a channel's <i>direction</i>; either
1010<i>send</i>, <i>receive</i>, or <i>bi-directional</i> (unconstrained).
1011</p>
Robert Griesemera3294712009-01-05 11:17:26 -08001012
Robert Griesemerc2d55862009-02-19 16:49:10 -08001013<pre>
Rob Pike46596852009-03-02 16:17:29 -08001014chan T // can be used to send and receive values of type T
Robert Griesemerc2d55862009-02-19 16:49:10 -08001015chan &lt;- float // can only be used to send floats
Rob Pike46596852009-03-02 16:17:29 -08001016&lt;-chan int // can only be used to receive ints
Robert Griesemerc2d55862009-02-19 16:49:10 -08001017</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001018
Rob Pike8f2330d2009-02-25 16:20:44 -08001019<p>
1020The value of an uninitialized channel is <code>nil</code>. A new, initialized channel
1021value is made using the built-in function <code>make</code>,
Robert Griesemer633957b2009-01-06 13:23:20 -08001022which takes the channel type and an optional capacity as arguments:
Rob Pike8f2330d2009-02-25 16:20:44 -08001023</p>
Robert Griesemera3294712009-01-05 11:17:26 -08001024
Rob Pike94b67eb2009-03-24 17:40:47 -07001025
Robert Griesemerc2d55862009-02-19 16:49:10 -08001026<pre>
Rob Pikeda389742009-03-02 19:13:40 -08001027make(chan int, 100)
Robert Griesemerc2d55862009-02-19 16:49:10 -08001028</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001029
Rob Pike8f2330d2009-02-25 16:20:44 -08001030<p>
1031The capacity, in number of elements, sets the size of the buffer in the channel. If the
Robert Griesemera3294712009-01-05 11:17:26 -08001032capacity is greater than zero, the channel is asynchronous and, provided the
Rob Pike8f2330d2009-02-25 16:20:44 -08001033buffer is not full, sends can succeed without blocking. If the capacity is zero
1034or absent, the communication succeeds only when both a sender and receiver are ready.
1035</p>
Robert Griesemera3294712009-01-05 11:17:26 -08001036
Rob Pike94b67eb2009-03-24 17:40:47 -07001037<p>
1038For a channel <code>c</code>, the predefined function <code>close(c)</code>
1039marks the channel as unable to accept more
1040values through a send operation. After any previously
1041sent values have been received, receives will return
1042the zero value for the channel's type. After at least one such zero value has been
1043received, <code>closed(c)</code> returns true.
1044</p>
1045
Rob Pike8f2330d2009-02-25 16:20:44 -08001046<h2>General properties of types and values</h2>
Robert Griesemer434c6052008-11-07 13:34:37 -08001047
Rob Pike4501d342009-02-19 17:31:36 -08001048<p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001049Types may be <i>different</i>, <i>structurally equal</i> (or just <i>equal</i>),
1050or <i>identical</i>.
1051Go is <i>type safe</i>: different types cannot be mixed
1052in binary operations and values cannot be assigned to variables of different
Rob Pikeda389742009-03-02 19:13:40 -08001053types. Values can be assigned to variables of equal type.
Rob Pike8f2330d2009-02-25 16:20:44 -08001054</p>
1055
1056<h3>Type equality and identity </h3>
1057
Robert Griesemerc2d55862009-02-19 16:49:10 -08001058<p>
Robert Griesemer434c6052008-11-07 13:34:37 -08001059Two type names denote equal types if the types in the corresponding declarations
Rob Pike8f2330d2009-02-25 16:20:44 -08001060are equal (§Declarations and Scope).
1061Two type literals specify equal types if they have the same
1062literal structure and corresponding components have equal types.
1063In detail:
Rob Pike4501d342009-02-19 17:31:36 -08001064</p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001065
Robert Griesemerc2d55862009-02-19 16:49:10 -08001066<ul>
Rob Pike8f2330d2009-02-25 16:20:44 -08001067 <li>Two pointer types are equal if they have equal base types.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001068
Rob Pike8f2330d2009-02-25 16:20:44 -08001069 <li>Two array types are equal if they have equal element types and
1070 the same array length.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001071
Rob Pike8f2330d2009-02-25 16:20:44 -08001072 <li>Two struct types are equal if they have the same sequence of fields,
1073 with the same names and equal types. Two anonymous fields are
1074 considered to have the same name.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001075
Robert Griesemerc2d55862009-02-19 16:49:10 -08001076 <li>Two function types are equal if they have the same number of parameters
Robert Griesemer434c6052008-11-07 13:34:37 -08001077 and result values and if corresponding parameter and result types are
Rob Pike8f2330d2009-02-25 16:20:44 -08001078 the same. All "..." parameters have equal type.
1079 Parameter and result names are not required to match.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001080
Rob Pike8f2330d2009-02-25 16:20:44 -08001081 <li>Two slice types are equal if they have equal element types.</li>
Robert Griesemera3294712009-01-05 11:17:26 -08001082
Robert Griesemerc2d55862009-02-19 16:49:10 -08001083 <li>Two channel types are equal if they have equal value types and
Rob Pike8f2330d2009-02-25 16:20:44 -08001084 the same direction.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001085
Rob Pike8f2330d2009-02-25 16:20:44 -08001086 <li>Two map types are equal if they have equal key and value types.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001087
Robert Griesemerc2d55862009-02-19 16:49:10 -08001088 <li>Two interface types are equal if they have the same set of methods
Rob Pike8f2330d2009-02-25 16:20:44 -08001089 with the same names and equal function types. The order
1090 of the methods is irrelevant.</li>
Robert Griesemerc2d55862009-02-19 16:49:10 -08001091</ul>
Robert Griesemer434c6052008-11-07 13:34:37 -08001092
Robert Griesemerc2d55862009-02-19 16:49:10 -08001093<p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001094Type identity is more stringent than type equality.
1095It requires for type names
1096that they originate in the same type declaration, while for equality it requires
1097only that they originate in equal type declarations.
1098Also, the names of parameters and results must match for function types.
1099In all other respects, the definition of type identity is the
1100same as for type equality listed above but with ``identical''
1101substitued for ``equal''.
Rob Pike4501d342009-02-19 17:31:36 -08001102</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08001103<p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001104By definition, identical types are also equal types.
1105Two types are different if they are not equal.
Rob Pike4501d342009-02-19 17:31:36 -08001106</p>
Robert Griesemer434c6052008-11-07 13:34:37 -08001107
Robert Griesemerc2d55862009-02-19 16:49:10 -08001108<p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001109Given the declarations
1110</p>
Robert Griesemer434c6052008-11-07 13:34:37 -08001111
Robert Griesemerc2d55862009-02-19 16:49:10 -08001112<pre>
1113type (
1114 T0 []string;
1115 T1 []string
1116 T2 struct { a, b int };
1117 T3 struct { a, c int };
1118 T4 func (int, float) *T0
1119 T5 func (x int, y float) *[]string
1120)
1121</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -08001122
Rob Pike8f2330d2009-02-25 16:20:44 -08001123<p>
Russ Cox461dd912009-03-04 14:44:51 -08001124these types are equal:
Rob Pike8f2330d2009-02-25 16:20:44 -08001125</p>
Robert Griesemer434c6052008-11-07 13:34:37 -08001126
Robert Griesemerc2d55862009-02-19 16:49:10 -08001127<pre>
1128T0 and T0
Rob Pike8f2330d2009-02-25 16:20:44 -08001129T0 and T1
Robert Griesemerc2d55862009-02-19 16:49:10 -08001130T0 and []string
Robert Griesemerc2d55862009-02-19 16:49:10 -08001131T4 and T5
Russ Cox461dd912009-03-04 14:44:51 -08001132T3 and struct { a int; c int }
Robert Griesemerc2d55862009-02-19 16:49:10 -08001133</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -08001134
Rob Pike8f2330d2009-02-25 16:20:44 -08001135<p>
Russ Cox461dd912009-03-04 14:44:51 -08001136<code>T2</code> and <code>T3</code> are not equal because
1137they have different field names.
1138</p>
1139
1140<p>
1141These types are identical:
Rob Pike8f2330d2009-02-25 16:20:44 -08001142</p>
Robert Griesemer434c6052008-11-07 13:34:37 -08001143
Robert Griesemerc2d55862009-02-19 16:49:10 -08001144<pre>
1145T0 and T0
1146[]int and []int
1147struct { a, b *T5 } and struct { a, b *T5 }
1148</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -08001149
Rob Pike8f2330d2009-02-25 16:20:44 -08001150<p>
1151<code>T0</code> and <code>T1</code> are equal but not
1152identical because they have distinct declarations.
1153</p>
Robert Griesemer434c6052008-11-07 13:34:37 -08001154
Rob Pike5af7de32009-02-24 15:17:59 -08001155<h3>Assignment compatibility</h3>
1156
Rob Pike5af7de32009-02-24 15:17:59 -08001157<p>
1158Values of any type may always be assigned to variables
1159of equal static type. Some types and values have conditions under which they may
1160be assigned to different types:
1161</p>
1162<ul>
1163<li>
1164The predeclared constant <code>nil</code> can be assigned to any
1165pointer, function, slice, map, channel, or interface variable.
1166<li>
Russ Coxbcdc2472009-04-16 23:06:48 -07001167A pointer to an array can be assigned to a slice variable with equal element type.
1168The slice variable then refers to the original array; the data is not copied.
Rob Pike5af7de32009-02-24 15:17:59 -08001169</li>
1170<li>
Rob Pikeda389742009-03-02 19:13:40 -08001171A value can be assigned to an interface variable if the static
Rob Pike5af7de32009-02-24 15:17:59 -08001172type of the value implements the interface.
1173</li>
1174<li>
1175A value of bidirectional channel type can be assigned to any channel
1176variable of equal channel value type.
1177</li>
1178</ul>
1179
1180<h3>Comparison compatibility</h3>
1181
1182<p>
1183Values of any type may be compared to other values of equal static
1184type. Values of numeric and string type may be compared using the
1185full range of comparison operators as described in §Comparison operators;
1186booleans may be compared only for equality or inequality.
1187</p>
1188
1189<p>
1190Values of composite type may be
1191compared for equality or inequality using the <code>==</code> and
1192<code>!=</code> operators, with the following provisos:
1193</p>
1194<ul>
1195<li>
1196Arrays and structs may not be compared to anything.
1197</li>
1198<li>
Rob Pikeda389742009-03-02 19:13:40 -08001199A slice value may only be compared explicitly against <code>nil</code>.
1200A slice value is equal to <code>nil</code> if it has been assigned the explicit
Rob Pike5af7de32009-02-24 15:17:59 -08001201value <code>nil</code> or if it is a variable (or array element,
1202field, etc.) that has not been modified since it was created
1203uninitialized.
1204</li>
1205<li>
1206Similarly, an interface value is equal to <code>nil</code> if it has
1207been assigned the explicit value <code>nil</code> or if it is a
1208variable (or array element, field, etc.) that has not been modified
1209since it was created uninitialized.
1210</li>
1211<li>
1212For types that can be compared to <code>nil</code>,
1213two values of the same type are equal if they both equal <code>nil</code>,
1214unequal if one equals <code>nil</code> and one does not.
1215</li>
1216<li>
1217Pointer values are equal if they point to the same location.
1218</li>
1219<li>
Rob Pikeda389742009-03-02 19:13:40 -08001220Function values are equal if they refer to the same function.
Rob Pike5af7de32009-02-24 15:17:59 -08001221</li>
1222<li>
Rob Pikeda389742009-03-02 19:13:40 -08001223Channel and map values are equal if they were created by the same call to <code>make</code>
Rob Pike5af7de32009-02-24 15:17:59 -08001224(§Making slices, maps, and channels).
1225</li>
1226<li>
Rob Pikeda389742009-03-02 19:13:40 -08001227Interface values may be compared if they have the same static type.
1228They will be equal only if they have the same dynamic type and the underlying values are equal.
Rob Pike5af7de32009-02-24 15:17:59 -08001229</li>
1230</ul>
Rob Pikeff70f092009-02-20 13:36:14 -08001231<hr/>
Robert Griesemer434c6052008-11-07 13:34:37 -08001232
Rob Pikea9ed30f2009-02-23 19:26:07 -08001233
1234<h2>Declarations and Scope</h2>
1235
1236<p>
1237A declaration binds an identifier to a language entity such as
1238a variable or function and specifies properties such as its type.
1239Every identifier in a program must be declared.
1240</p>
1241
1242<pre class="grammar">
1243Declaration = ConstDecl | TypeDecl | VarDecl | FunctionDecl | MethodDecl .
1244</pre>
Russ Cox5958dd62009-03-04 17:19:21 -08001245
Rob Pikea9ed30f2009-02-23 19:26:07 -08001246<p>
1247The <i>scope</i> of an identifier is the extent of source text within which the
1248identifier denotes the bound entity. No identifier may be declared twice in a
1249single scope, but inner blocks can declare a new entity with the same
1250identifier, in which case the scope created by the outer declaration excludes
1251that created by the inner.
1252</p>
1253<p>
1254There are levels of scoping in effect before each source file is compiled.
1255In order from outermost to innermost:
1256</p>
1257<ol>
1258 <li>The <i>universe</i> scope contains all predeclared identifiers.</li>
1259 <li>An implicit scope contains only the package name.</li>
1260 <li>The <i>package-level</i> scope surrounds all declarations at the
1261 top level of the file, that is, outside the body of any
1262 function or method. That scope is shared across all
1263 source files within the package (§Packages), allowing
1264 package-level identifiers to be shared between source
1265 files.</li>
1266</ol>
1267
1268<p>
1269The scope of an identifier depends on the entity declared:
1270</p>
1271
1272<ol>
1273 <li> The scope of predeclared identifiers is the universe scope.</li>
1274
Russ Cox5958dd62009-03-04 17:19:21 -08001275 <li> The scope of an identifier denoting a type, function or package
Rob Pikea9ed30f2009-02-23 19:26:07 -08001276 extends from the point of the identifier in the declaration
1277 to the end of the innermost surrounding block.</li>
1278
1279 <li> The scope of a constant or variable extends textually from
Russ Cox5958dd62009-03-04 17:19:21 -08001280 the end of its declaration to the end of the innermost
Rob Pikea9ed30f2009-02-23 19:26:07 -08001281 surrounding block. If the variable is declared in the
Russ Cox5958dd62009-03-04 17:19:21 -08001282 <i>init</i> statement of an <code>if</code>, <code>for</code>,
Rob Pikea9ed30f2009-02-23 19:26:07 -08001283 or <code>switch </code> statement, the
1284 innermost surrounding block is the block associated
1285 with that statement.</li>
1286
1287 <li> The scope of a parameter or result is the body of the
1288 corresponding function.</li>
1289
1290 <li> The scope of a field or method is selectors for the
1291 corresponding type containing the field or method (§Selectors).</li>
1292
Russ Cox5958dd62009-03-04 17:19:21 -08001293 <li> The scope of a label is a special scope emcompassing
Rob Pikea9ed30f2009-02-23 19:26:07 -08001294 the body of the innermost surrounding function, excluding
Rob Pike5af7de32009-02-24 15:17:59 -08001295 nested functions. Labels do not conflict with non-label identifiers.</li>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001296</ol>
1297
1298<h3>Predeclared identifiers</h3>
1299
1300<p>
1301The following identifiers are implicitly declared in the outermost scope:
1302</p>
1303<pre class="grammar">
1304Basic types:
Russ Cox5958dd62009-03-04 17:19:21 -08001305 bool byte float32 float64 int8 int16 int32 int64
1306 string uint8 uint16 uint32 uint64
Rob Pikea9ed30f2009-02-23 19:26:07 -08001307
Rob Pike5af7de32009-02-24 15:17:59 -08001308Architecture-specific convenience types:
Rob Pikea9ed30f2009-02-23 19:26:07 -08001309 float int uint uintptr
1310
1311Constants:
1312 true false iota nil
1313
1314Functions:
Russ Cox5958dd62009-03-04 17:19:21 -08001315 cap len make new panic panicln print println
Rob Pikea9ed30f2009-02-23 19:26:07 -08001316
1317Packages:
Russ Cox5958dd62009-03-04 17:19:21 -08001318 sys (TODO: does sys endure?)
Rob Pikea9ed30f2009-02-23 19:26:07 -08001319</pre>
1320
Rob Pikea9ed30f2009-02-23 19:26:07 -08001321<h3>Exported identifiers</h3>
1322
1323<p>
1324By default, identifiers are visible only within the package in which they are declared.
1325Some identifiers are <i>exported</i> and can be referenced using
1326<i>qualified identifiers</i> in other packages (§Qualified identifiers).
1327If an identifier satisfies these two conditions:
1328</p>
1329<ol>
1330<li>the first character of the identifier's name is a Unicode upper case letter;
1331<li>the identifier is declared at the package level or is a field or method of a type
1332declared at the top level;
1333</ol>
1334<p>
1335it will be exported automatically.
1336</p>
1337
1338<h3>Const declarations</h3>
1339
1340<p>
1341A constant declaration binds a list of identifiers (the names of
1342the constants) to the values of a list of constant expressions
1343(§Constant expressions). The number of identifiers must be equal
1344to the number of expressions, and the n<sup>th</sup> identifier on
1345the left is bound to value of the n<sup>th</sup> expression on the
1346right.
1347</p>
1348
1349<pre class="grammar">
1350ConstDecl = "const" ( ConstSpec | "(" [ ConstSpecList ] ")" ) .
1351ConstSpecList = ConstSpec { ";" ConstSpec } [ ";" ] .
Russ Coxf8ba0f42009-03-12 19:04:56 -07001352ConstSpec = IdentifierList [ [ CompleteType ] "=" ExpressionList ] .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001353
1354IdentifierList = identifier { "," identifier } .
1355ExpressionList = Expression { "," Expression } .
1356
1357CompleteType = Type .
1358</pre>
1359
1360<p>
1361If the type (CompleteType) is omitted, the constants take the
1362individual types of the corresponding expressions, which may be
Rob Pikeda389742009-03-02 19:13:40 -08001363<i>ideal integer</i> or <i>ideal float</i> (§Ideal number). If the type
Rob Pikea9ed30f2009-02-23 19:26:07 -08001364is present, all constants take the type specified, and the types
1365of all the expressions must be assignment-compatible
1366with that type.
1367</p>
1368
1369<pre>
1370const Pi float64 = 3.14159265358979323846
1371const E = 2.718281828
1372const (
1373 size int64 = 1024;
1374 eof = -1;
1375)
1376const a, b, c = 3, 4, "foo" // a = 3, b = 4, c = "foo"
1377const u, v float = 0, 3 // u = 0.0, v = 3.0
1378</pre>
1379
1380<p>
1381Within a parenthesized <code>const</code> declaration list the
1382expression list may be omitted from any but the first declaration.
1383Such an empty list is equivalent to the textual substitution of the
Russ Coxf8ba0f42009-03-12 19:04:56 -07001384first preceding non-empty expression list, and its type if any.
Russ Cox5958dd62009-03-04 17:19:21 -08001385Omitting the list of expressions is therefore equivalent to
1386repeating the previous list. The number of identifiers must be equal
1387to the number of expressions in the previous list.
1388Together with the <code>iota</code> constant generator
Rob Pikea9ed30f2009-02-23 19:26:07 -08001389(§Iota) this mechanism permits light-weight declaration of sequential values:
1390</p>
1391
1392<pre>
1393const (
1394 Sunday = iota;
1395 Monday;
1396 Tuesday;
1397 Wednesday;
1398 Thursday;
1399 Friday;
1400 Partyday;
1401 numberOfDays; // this constant is not exported
1402)
1403</pre>
1404
1405
1406<h3>Iota</h3>
1407
1408<p>
1409Within a constant declaration, the predeclared pseudo-constant
1410<code>iota</code> represents successive integers. It is reset to 0
1411whenever the reserved word <code>const</code> appears in the source
1412and increments with each semicolon. It can be used to construct a
1413set of related constants:
1414</p>
1415
1416<pre>
1417const ( // iota is reset to 0
1418 c0 = iota; // c0 == 0
1419 c1 = iota; // c1 == 1
1420 c2 = iota // c2 == 2
1421)
1422
1423const (
1424 a = 1 << iota; // a == 1 (iota has been reset)
1425 b = 1 << iota; // b == 2
1426 c = 1 << iota; // c == 4
1427)
1428
1429const (
1430 u = iota * 42; // u == 0 (ideal integer)
1431 v float = iota * 42; // v == 42.0 (float)
1432 w = iota * 42; // w == 84 (ideal integer)
1433)
1434
1435const x = iota; // x == 0 (iota has been reset)
1436const y = iota; // y == 0 (iota has been reset)
1437</pre>
1438
1439<p>
1440Within an ExpressionList, the value of each <code>iota</code> is the same because
1441it is only incremented at a semicolon:
1442</p>
1443
1444<pre>
1445const (
1446 bit0, mask0 = 1 << iota, 1 << iota - 1; // bit0 == 1, mask0 == 0
1447 bit1, mask1; // bit1 == 2, mask1 == 1
1448 bit2, mask2; // bit2 == 4, mask2 == 3
1449)
1450</pre>
1451
1452<p>
1453This last example exploits the implicit repetition of the
1454last non-empty expression list.
1455</p>
1456
1457
1458<h3>Type declarations</h3>
1459
1460<p>
1461A type declaration binds an identifier, the <i>type name</i>,
1462to a new type. <font color=red>TODO: what exactly is a "new type"?</font>
1463</p>
1464
1465<pre class="grammar">
1466TypeDecl = "type" ( TypeSpec | "(" [ TypeSpecList ] ")" ) .
1467TypeSpecList = TypeSpec { ";" TypeSpec } [ ";" ] .
Russ Cox461dd912009-03-04 14:44:51 -08001468TypeSpec = identifier ( Type | "struct" | "interface" ) .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001469</pre>
1470
1471<pre>
1472type IntArray [16] int
1473
1474type (
1475 Point struct { x, y float };
1476 Polar Point
1477)
1478
Russ Cox461dd912009-03-04 14:44:51 -08001479type Comparable interface
1480
Rob Pikea9ed30f2009-02-23 19:26:07 -08001481type TreeNode struct {
1482 left, right *TreeNode;
Russ Cox461dd912009-03-04 14:44:51 -08001483 value *Comparable;
Rob Pikea9ed30f2009-02-23 19:26:07 -08001484}
1485
1486type Comparable interface {
1487 cmp(Comparable) int
1488}
1489</pre>
1490
1491<h3>Variable declarations</h3>
1492
1493<p>
1494A variable declaration creates a variable, binds an identifier to it and
1495gives it a type and optionally an initial value.
Rob Pikecdbf6192009-02-24 17:47:45 -08001496The type must be complete (§Types).
Rob Pikea9ed30f2009-02-23 19:26:07 -08001497</p>
1498<pre class="grammar">
1499VarDecl = "var" ( VarSpec | "(" [ VarSpecList ] ")" ) .
1500VarSpecList = VarSpec { ";" VarSpec } [ ";" ] .
1501VarSpec = IdentifierList ( CompleteType [ "=" ExpressionList ] | "=" ExpressionList ) .
1502</pre>
1503
1504<pre>
1505var i int
1506var U, V, W float
1507var k = 0
1508var x, y float = -1.0, -2.0
1509var (
1510 i int;
1511 u, v, s = 2.0, 3.0, "bar"
1512)
1513</pre>
1514
1515<p>
1516If there are expressions, their number must be equal
1517to the number of identifiers, and the n<sup>th</sup> variable
1518is initialized to the value of the n<sup>th</sup> expression.
1519Otherwise, each variable is initialized to the <i>zero</i>
Rob Pike8f2330d2009-02-25 16:20:44 -08001520of the type (§The zero value).
Rob Pikea9ed30f2009-02-23 19:26:07 -08001521The expressions can be general expressions; they need not be constants.
1522</p>
1523<p>
1524Either the type or the expression list must be present. If the
1525type is present, it sets the type of each variable and the expressions
1526(if any) must be assignment-compatible to that type. If the type
1527is absent, the variables take the types of the corresponding
1528expressions.
1529</p>
1530<p>
1531If the type is absent and the corresponding expression is a constant
1532expression of ideal integer or ideal float type, the type of the
1533declared variable is <code>int</code> or <code>float</code>
1534respectively:
1535</p>
1536
1537<pre>
1538var i = 0 // i has type int
1539var f = 3.1415 // f has type float
1540</pre>
1541
1542<h3>Short variable declarations</h3>
1543
1544A <i>short variable declaration</i> uses the syntax
1545
1546<pre class="grammar">
1547SimpleVarDecl = IdentifierList ":=" ExpressionList .
1548</pre>
1549
1550and is shorthand for the declaration syntax
1551
1552<pre class="grammar">
1553"var" IdentifierList = ExpressionList .
1554</pre>
1555
1556<pre>
1557i, j := 0, 10;
1558f := func() int { return 7; }
Rob Pikef5387602009-03-30 16:08:41 -07001559ch := make(chan int);
Rob Pikea9ed30f2009-02-23 19:26:07 -08001560</pre>
1561
1562<p>
1563Unlike regular variable declarations, short variable declarations
1564can be used, by analogy with tuple assignment (§Assignments), to
1565receive the individual elements of a multi-valued expression such
Rob Pike5af7de32009-02-24 15:17:59 -08001566as a call to a multi-valued function. In this form, the ExpressionList
Rob Pikea9ed30f2009-02-23 19:26:07 -08001567must be a single such multi-valued expression, the number of
1568identifiers must equal the number of values, and the declared
1569variables will be assigned the corresponding values.
1570</p>
1571
1572<pre>
Russ Cox5958dd62009-03-04 17:19:21 -08001573r, w := os.Pipe(fd); // os.Pipe() returns two values
Rob Pikea9ed30f2009-02-23 19:26:07 -08001574</pre>
1575
1576<p>
Rob Pike2a1683a2009-04-19 20:04:15 -07001577A short variable declaration may redeclare variables provided they
1578were originally declared in the same block with the same type, and at
1579least one of the variables is new. As a consequence, redeclaration
1580can only appear in a multi-variable short declaration.
1581Redeclaration does not introduce a new
1582variable; it just assigns a new value to the original.
1583</p>
1584
1585<pre>
1586field1, offset := nextField(str, 0);
1587field2, offset := nextField(str, offset); // redeclares offset
1588</pre>
1589
1590<p>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001591Short variable declarations may appear only inside functions.
1592In some contexts such as the initializers for <code>if</code>,
1593<code>for</code>, or <code>switch</code> statements,
1594they can be used to declare local temporary variables (§Statements).
1595</p>
1596
1597<h3>Function declarations</h3>
1598
1599<p>
1600A function declaration binds an identifier to a function (§Function types).
1601</p>
1602
1603<pre class="grammar">
1604FunctionDecl = "func" identifier Signature [ Block ] .
1605</pre>
1606
1607<pre>
1608func min(x int, y int) int {
1609 if x &lt; y {
1610 return x;
1611 }
1612 return y;
1613}
1614</pre>
1615
1616<p>
1617A function must be declared or forward-declared before it can be invoked (§Forward declarations).
1618Implementation restriction: Functions can only be declared at the package level.
1619</p>
1620
1621<h3>Method declarations</h3>
1622
1623<p>
1624A method declaration binds an identifier to a method,
1625which is a function with a <i>receiver</i>.
1626</p>
1627<pre class="grammar">
1628MethodDecl = "func" Receiver identifier Signature [ Block ] .
1629Receiver = "(" [ identifier ] [ "*" ] TypeName ")" .
1630</pre>
1631
1632<p>
1633The receiver type must be a type name or a pointer to a type name,
1634and that name is called the <i>receiver base type</i> or just <i>base type</i>.
1635The base type must not be a pointer type and must be
1636declared in the same source file as the method.
1637The method is said to be <i>bound</i> to the base type
1638and is visible only within selectors for that type
1639(§Type declarations, §Selectors).
1640</p>
1641
1642<p>
1643All methods bound to a base type must have the same receiver type,
1644either all pointers to the base type or all the base type itself.
1645Given type <code>Point</code>, the declarations
1646</p>
1647
1648<pre>
1649func (p *Point) Length() float {
1650 return Math.sqrt(p.x * p.x + p.y * p.y);
1651}
1652
1653func (p *Point) Scale(factor float) {
1654 p.x = p.x * factor;
1655 p.y = p.y * factor;
1656}
1657</pre>
1658
1659<p>
1660bind the methods <code>Length</code> and <code>Scale</code>
1661to the base type <code>Point</code>.
1662</p>
1663
1664<p>
1665If the
1666receiver's value is not referenced inside the the body of the method,
1667its identifier may be omitted in the declaration. The same applies in
1668general to parameters of functions and methods.
1669</p>
1670
1671<p>
1672Methods can be declared
1673only after their base type is declared or forward-declared, and invoked
1674only after their own declaration or forward-declaration (§Forward declarations).
1675Implementation restriction: They can only be declared at package level.
1676</p>
1677
Rob Pikedf3183f2009-02-26 16:37:23 -08001678<p>
1679The type of a method is the type of a function with the receiver as first
1680argument. For instance, the method <code>Scale</code> has type
1681</p>
1682
1683<pre>
1684(p *Point, factor float)
1685</pre>
1686
1687<p>
1688However, a function declared this way is not a method.
1689</p>
1690
Rob Pikea9ed30f2009-02-23 19:26:07 -08001691<h3>Forward declarations</h3>
1692
1693<p>
Rob Pike5af7de32009-02-24 15:17:59 -08001694Mutually-recursive types require that one be
Rob Pikea9ed30f2009-02-23 19:26:07 -08001695<i>forward declared</i> so that it may be named in the other.
1696A forward declaration of a type omits the block containing the fields
1697or methods of the type.
1698</p>
1699
1700<pre>
1701type List struct // forward declaration of List
1702type Item struct {
1703 value int;
1704 next *List;
1705}
1706type List struct {
1707 head, tail *Item
1708}
1709</pre>
1710<p>
1711A forward-declared type is incomplete (§Types)
1712until it is fully declared. The full declaration must follow
Russ Cox5958dd62009-03-04 17:19:21 -08001713before the end of the block containing the forward declaration;
1714it cannot be contained in an inner block.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001715</p>
1716<p>
1717Functions and methods may similarly be forward-declared by omitting their body.
1718</p>
1719<pre>
1720func F(a int) int // forward declaration of F
1721func G(a, b int) int {
1722 return F(a) + F(b)
1723}
1724func F(a int) int {
1725 if a <= 0 { return 0 }
1726 return G(a-1, b+1)
1727}
1728</pre>
1729
1730<hr/>
1731
Robert Griesemerc2d55862009-02-19 16:49:10 -08001732<h2>Expressions</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001733
Rob Pikedf3183f2009-02-26 16:37:23 -08001734<p>
1735An expression specifies the computation of a value by applying
1736operators and functions to operands. An expression has a value and
Robert Griesemerad711102008-09-11 17:48:20 -07001737a type.
Rob Pikedf3183f2009-02-26 16:37:23 -08001738</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001739
Robert Griesemerc2d55862009-02-19 16:49:10 -08001740<h3>Operands</h3>
Robert Griesemerad711102008-09-11 17:48:20 -07001741
1742Operands denote the elementary values in an expression.
1743
Rob Pikeff70f092009-02-20 13:36:14 -08001744<pre class="grammar">
Rob Pikedf3183f2009-02-26 16:37:23 -08001745Operand = Literal | QualifiedIdent | "(" Expression ")" .
1746Literal = BasicLit | CompositeLit | FunctionLit .
1747BasicLit = int_lit | float_lit | char_lit | StringLit .
1748StringLit = string_lit { string_lit } .
Robert Griesemerc2d55862009-02-19 16:49:10 -08001749</pre>
Robert Griesemerad711102008-09-11 17:48:20 -07001750
1751
Robert Griesemerc2d55862009-02-19 16:49:10 -08001752<h3>Constants</h3>
Robert Griesemerb90b2132008-09-19 15:49:55 -07001753
Rob Pikedf3183f2009-02-26 16:37:23 -08001754<p>
Russ Cox5958dd62009-03-04 17:19:21 -08001755A <i>constant</i> is a literal of a basic type
1756(including the predeclared constants <code>true</code>, <code>false</code>
1757and <code>nil</code>
1758and values denoted by <code>iota</code>)
1759or a constant expression (§Constant expressions).
1760Constants have values that are known at compile time.
Rob Pikedf3183f2009-02-26 16:37:23 -08001761</p>
Robert Griesemerb90b2132008-09-19 15:49:55 -07001762
Robert Griesemerc2d55862009-02-19 16:49:10 -08001763<h3>Qualified identifiers</h3>
Robert Griesemerad711102008-09-11 17:48:20 -07001764
Robert Griesemerc2d55862009-02-19 16:49:10 -08001765<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08001766A qualified identifier is an identifier qualified by a package name prefix.
1767</p>
Robert Griesemer337af312008-11-17 18:11:36 -08001768
Rob Pikeff70f092009-02-20 13:36:14 -08001769<pre class="grammar">
Robert Griesemerb813ee02009-03-05 15:01:54 -08001770QualifiedIdent = [ ( LocalPackageName | PackageName ) "." ] identifier .
Rob Pikedf3183f2009-02-26 16:37:23 -08001771LocalPackageName = identifier .
Robert Griesemerc2d55862009-02-19 16:49:10 -08001772PackageName = identifier .
1773</pre>
Robert Griesemerad711102008-09-11 17:48:20 -07001774
Rob Pikedf3183f2009-02-26 16:37:23 -08001775<p>
1776A qualified identifier accesses an identifier in
1777a separate package. The identifier must be exported by that package, which
1778means that it must begin with a Unicode upper case letter (§Exported identifiers).
1779</p>
1780<p>
1781The LocalPackageName is that of the package in which the qualified identifier
1782appears and is only necessary to access names hidden by intervening declarations
1783of a package-level identifier.
1784</p>
1785
1786<pre>
1787Math.Sin
1788mypackage.hiddenName
1789mypackage.Math.Sin // if Math is declared in an intervening scope
1790</pre>
Robert Griesemerad711102008-09-11 17:48:20 -07001791
Russ Cox5958dd62009-03-04 17:19:21 -08001792TODO: 6g does not implement LocalPackageName. Is this new?
1793Is it needed?
1794
Robert Griesemerc2d55862009-02-19 16:49:10 -08001795<h3>Composite literals</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001796
Rob Pikedf3183f2009-02-26 16:37:23 -08001797<p>
1798Composite literals construct values for structs, arrays, slices, and maps
1799and create a new value each time they are evaluated.
1800They consist of the type of the value
Rob Pike426335f2009-03-02 17:52:52 -08001801followed by a brace-bound list of expressions,
Robert Griesemerc5c577c2009-03-27 13:43:28 -07001802or a list of key-value pairs for map literals.
Rob Pikedf3183f2009-02-26 16:37:23 -08001803</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001804
Rob Pikeff70f092009-02-20 13:36:14 -08001805<pre class="grammar">
Robert Griesemerc5c577c2009-03-27 13:43:28 -07001806CompositeLit = LiteralType "{" [ ( ExpressionList | KeyValueList ) [ "," ] ] "}" .
Rob Pikedf3183f2009-02-26 16:37:23 -08001807LiteralType = StructType | ArrayType | "[" "..." "]" ElementType |
1808 SliceType | MapType | TypeName .
Robert Griesemerc5c577c2009-03-27 13:43:28 -07001809KeyValueList = KeyValueExpr { "," KeyValueExpr } .
1810KeyValueExpr = Expression ":" Expression .
Robert Griesemerc2d55862009-02-19 16:49:10 -08001811</pre>
Robert Griesemer0976e342008-09-03 13:37:44 -07001812
Rob Pikedf3183f2009-02-26 16:37:23 -08001813<p>
1814The LiteralType must be a struct, array, slice, or map type.
Russ Cox7a5e97b2009-03-03 15:40:30 -08001815(The grammar enforces this constraint except when the type is given
1816as a TypeName.)
1817The types of the expressions must be assignment compatible to
1818the respective field, element, and key types of the LiteralType;
1819there is no additional conversion.
Rob Pikedf3183f2009-02-26 16:37:23 -08001820</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001821
Robert Griesemerc2d55862009-02-19 16:49:10 -08001822<pre>
1823type Rat struct { num, den int }
1824type Num struct { r Rat; f float; s string }
1825</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001826
Rob Pikedf3183f2009-02-26 16:37:23 -08001827<p>
1828one may write
1829</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001830
Robert Griesemerc2d55862009-02-19 16:49:10 -08001831<pre>
Rob Pike37ab8382009-03-18 22:58:36 -07001832pi := Num{Rat{22, 7}, 3.14159, "pi"}
1833</pre>
1834
1835<p>
Rob Pike2a5af742009-03-20 17:03:48 -07001836Taking the address of a composite literal (§Address operators)
1837generates a unique pointer to an instance of the literal's value.
Rob Pike37ab8382009-03-18 22:58:36 -07001838</p>
Rob Pike37ab8382009-03-18 22:58:36 -07001839<pre>
1840var pi_ptr *Rat = &amp;Rat{22, 7}
Robert Griesemerc2d55862009-02-19 16:49:10 -08001841</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001842
Rob Pikedf3183f2009-02-26 16:37:23 -08001843<p>
Robert Griesemera3294712009-01-05 11:17:26 -08001844The length of an array literal is the length specified in the LiteralType.
1845If fewer elements than the length are provided in the literal, the missing
Rob Pike8f2330d2009-02-25 16:20:44 -08001846elements are set to the zero value for the array element type.
Rob Pikedf3183f2009-02-26 16:37:23 -08001847It is an error to provide more elements than specified in the type. The
1848notation <code>...</code> specifies an array length equal
1849to the number of elements in the literal.
1850</p>
Robert Griesemerb90b2132008-09-19 15:49:55 -07001851
Robert Griesemerc2d55862009-02-19 16:49:10 -08001852<pre>
Rob Pike426335f2009-03-02 17:52:52 -08001853buffer := [10]string{}; // len(buffer) == 10
1854primes := [6]int{2, 3, 5, 7, 9, 11}; // len(primes) == 6
1855days := [...]string{"Sat", "Sun"}; // len(days) == 2
Robert Griesemerc2d55862009-02-19 16:49:10 -08001856</pre>
Robert Griesemer91bbd642009-01-07 09:31:35 -08001857
Rob Pikedf3183f2009-02-26 16:37:23 -08001858<p>
1859A slice literal describes the entire underlying array literal.
Robert Griesemer91bbd642009-01-07 09:31:35 -08001860Thus, the length and capacity of a slice literal is the number of elements
Rob Pikedf3183f2009-02-26 16:37:23 -08001861(of the array) provided in the literal. A slice literal has the form
1862</p>
Robert Griesemer91bbd642009-01-07 09:31:35 -08001863
Robert Griesemerc2d55862009-02-19 16:49:10 -08001864<pre>
Rob Pike426335f2009-03-02 17:52:52 -08001865[]T{x1, x2, ... xn}
Robert Griesemerc2d55862009-02-19 16:49:10 -08001866</pre>
Robert Griesemer91bbd642009-01-07 09:31:35 -08001867
Rob Pikedf3183f2009-02-26 16:37:23 -08001868<p>
1869and is a shortcut for a slice operation applied to an array literal:
1870</p>
Robert Griesemer91bbd642009-01-07 09:31:35 -08001871
Robert Griesemerc2d55862009-02-19 16:49:10 -08001872<pre>
Rob Pike426335f2009-03-02 17:52:52 -08001873[n]T{x1, x2, ... xn}[0 : n]
Robert Griesemerc2d55862009-02-19 16:49:10 -08001874</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001875
Rob Pikedf3183f2009-02-26 16:37:23 -08001876<p>
1877In map literals only, the list contains
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001878key-value pairs separated by a colon:
Rob Pikedf3183f2009-02-26 16:37:23 -08001879</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001880
Robert Griesemerc2d55862009-02-19 16:49:10 -08001881<pre>
Rob Pike426335f2009-03-02 17:52:52 -08001882m := map[string]int{"good": 0, "bad": 1, "indifferent": 7};
Robert Griesemerc2d55862009-02-19 16:49:10 -08001883</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001884
Russ Cox7a5e97b2009-03-03 15:40:30 -08001885<p>
1886A parsing ambiguity arises when a composite literal using the
1887TypeName form of the LiteralType appears in the condition of an
1888"if", "for", or "switch" statement, because the braces surrounding
1889the expressions in the literal are confused with those introducing
1890a block of statements. To resolve the ambiguity in this rare case,
1891the composite literal must appear within
1892parentheses.
1893</p>
1894
1895<pre>
1896if x == (T{a,b,c}[i]) { ... }
1897if (x == T{a,b,c}[i]) { ... }
1898</pre>
1899
Robert Griesemerc2d55862009-02-19 16:49:10 -08001900<h3>Function literals</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001901
Rob Pikedf3183f2009-02-26 16:37:23 -08001902<p>
1903A function literal represents an anonymous function.
1904It consists of a specification of the function type and a function body.
1905</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001906
Rob Pikeff70f092009-02-20 13:36:14 -08001907<pre class="grammar">
Robert Griesemer62fd90a2009-03-25 13:58:44 -07001908FunctionLit = FunctionType Block .
Russ Cox5958dd62009-03-04 17:19:21 -08001909Block = "{" StatementList "}" .
Robert Griesemerc2d55862009-02-19 16:49:10 -08001910</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001911
Robert Griesemerc2d55862009-02-19 16:49:10 -08001912<pre>
Rob Pikedf3183f2009-02-26 16:37:23 -08001913func (a, b int, z float) bool { return a*b &lt; int(z) }
Robert Griesemerc2d55862009-02-19 16:49:10 -08001914</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001915
Rob Pikedf3183f2009-02-26 16:37:23 -08001916<p>
1917A function literal can be assigned to a variable or invoked directly.
1918</p>
Robert Griesemer7231ceb2008-09-08 15:01:04 -07001919
Robert Griesemerc2d55862009-02-19 16:49:10 -08001920<pre>
Rob Pikedf3183f2009-02-26 16:37:23 -08001921f := func(x, y int) int { return x + y }
1922func(ch chan int) { ch &lt;- ACK } (reply_chan)
Robert Griesemerc2d55862009-02-19 16:49:10 -08001923</pre>
Robert Griesemer7231ceb2008-09-08 15:01:04 -07001924
Rob Pikedf3183f2009-02-26 16:37:23 -08001925<p>
1926Function literals are <i>closures</i>: they may refer to variables
Robert Griesemerd8a764c2009-02-06 17:01:10 -08001927defined in a surrounding function. Those variables are then shared between
1928the surrounding function and the function literal, and they survive as long
Rob Pikedf3183f2009-02-26 16:37:23 -08001929as they are accessible.
1930</p>
Robert Griesemer7231ceb2008-09-08 15:01:04 -07001931
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001932
Robert Griesemerc2d55862009-02-19 16:49:10 -08001933<h3>Primary expressions</h3>
Russ Cox5958dd62009-03-04 17:19:21 -08001934
Rob Pikeff70f092009-02-20 13:36:14 -08001935<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08001936PrimaryExpr =
1937 Operand |
1938 PrimaryExpr Selector |
1939 PrimaryExpr Index |
1940 PrimaryExpr Slice |
Russ Cox5958dd62009-03-04 17:19:21 -08001941 PrimaryExpr TypeAssertion |
Robert Griesemerc2d55862009-02-19 16:49:10 -08001942 PrimaryExpr Call .
Robert Griesemer57b34612008-10-10 12:45:44 -07001943
Russ Cox5958dd62009-03-04 17:19:21 -08001944Selector = "." identifier .
1945Index = "[" Expression "]" .
1946Slice = "[" Expression ":" Expression "]" .
1947TypeAssertion = "." "(" Type ")" .
1948Call = "(" [ ExpressionList ] ")" .
Robert Griesemerc2d55862009-02-19 16:49:10 -08001949</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001950
1951
Robert Griesemerc2d55862009-02-19 16:49:10 -08001952<pre>
1953x
19542
1955(s + ".txt")
1956f(3.1415, true)
Rob Pike426335f2009-03-02 17:52:52 -08001957Point{1, 2}
Robert Griesemerc2d55862009-02-19 16:49:10 -08001958m["foo"]
1959s[i : j + 1]
1960obj.color
1961Math.sin
1962f.p[i].x()
1963</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001964
1965
Robert Griesemerc2d55862009-02-19 16:49:10 -08001966<h3>Selectors</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001967
Rob Pikedf3183f2009-02-26 16:37:23 -08001968<p>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07001969A primary expression of the form
Rob Pikedf3183f2009-02-26 16:37:23 -08001970</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001971
Robert Griesemerc2d55862009-02-19 16:49:10 -08001972<pre>
1973x.f
1974</pre>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07001975
Robert Griesemerc2d55862009-02-19 16:49:10 -08001976<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08001977denotes the field or method <code>f</code> of the value denoted by <code>x</code>
1978(or of <code>*x</code> if
1979<code>x</code> is of pointer type). The identifier <code>f</code>
1980is called the (field or method)
1981<i>selector</i>.
1982The type of the expression is the type of <code>f</code>.
1983</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08001984<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08001985A selector <code>f</code> may denote a field or method <code>f</code> of
1986a type <code>T</code>, or it may refer
1987to a field or method <code>f</code> of a nested anonymous field of
1988<code>T</code>.
1989The number of anonymous fields traversed
1990to reach <code>f</code> is called its <i>depth</i> in <code>T</code>.
1991The depth of a field or method <code>f</code>
1992declared in <code>T</code> is zero.
1993The depth of a field or method <code>f</code> declared in
1994an anonymous field <code>A</code> in <code>T</code> is the
1995depth of <code>f</code> in <code>A</code> plus one.
1996</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08001997<p>
Robert Griesemer071c91b2008-10-23 12:04:45 -07001998The following rules apply to selectors:
Rob Pikedf3183f2009-02-26 16:37:23 -08001999</p>
2000<ol>
2001<li>
2002For a value <code>x</code> of type <code>T</code> or <code>*T</code>
2003where <code>T</code> is not an interface type,
2004<code>x.f</code> denotes the field or method at the shallowest depth
2005in <code>T</code> where there
2006is such an <code>f</code>.
2007If there is not exactly one <code>f</code> with shallowest depth, the selector
Robert Griesemer071c91b2008-10-23 12:04:45 -07002008expression is illegal.
Rob Pikedf3183f2009-02-26 16:37:23 -08002009</li>
2010<li>
2011For a variable <code>x</code> of type <code>I</code> or <code>*I</code>
2012where <code>I</code> is an interface type,
2013<code>x.f</code> denotes the actual method with name <code>f</code> of the value assigned
2014to <code>x</code> if there is such a method.
2015If no value or <code>nil</code> was assigned to <code>x</code>, <code>x.f</code> is illegal.
2016</li>
2017<li>
2018In all other cases, <code>x.f</code> is illegal.
2019</ol>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002020<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002021Selectors automatically dereference pointers as necessary.
2022If <code>x</code> is of pointer type, <code>x.y</code>
2023is shorthand for <code>(*x).y</code>; if <code>y</code>
2024is also of pointer type, <code>x.y.z</code> is shorthand
2025for <code>(*(*x).y).z</code>, and so on.
2026If <code>*x</code> is of pointer type, dereferencing
2027must be explicit;
2028only one level of automatic dereferencing is provided.
2029For an <code>x</code> of type <code>T</code> containing an
2030anonymous field declared as <code>*A</code>,
2031<code>x.f</code> is a shortcut for <code>(*x.A).f</code>.
2032</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002033<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002034For example, given the declarations:
2035</p>
Robert Griesemer071c91b2008-10-23 12:04:45 -07002036
Robert Griesemerc2d55862009-02-19 16:49:10 -08002037<pre>
2038type T0 struct {
2039 x int;
2040}
Robert Griesemer071c91b2008-10-23 12:04:45 -07002041
Robert Griesemerc2d55862009-02-19 16:49:10 -08002042func (recv *T0) M0()
Robert Griesemer071c91b2008-10-23 12:04:45 -07002043
Robert Griesemerc2d55862009-02-19 16:49:10 -08002044type T1 struct {
2045 y int;
2046}
Robert Griesemer071c91b2008-10-23 12:04:45 -07002047
Robert Griesemerc2d55862009-02-19 16:49:10 -08002048func (recv T1) M1()
Robert Griesemer071c91b2008-10-23 12:04:45 -07002049
Robert Griesemerc2d55862009-02-19 16:49:10 -08002050type T2 struct {
2051 z int;
2052 T1;
2053 *T0;
2054}
Robert Griesemer071c91b2008-10-23 12:04:45 -07002055
Robert Griesemerc2d55862009-02-19 16:49:10 -08002056func (recv *T2) M2()
Robert Griesemer071c91b2008-10-23 12:04:45 -07002057
Robert Griesemerc2d55862009-02-19 16:49:10 -08002058var p *T2; // with p != nil and p.T1 != nil
2059</pre>
Robert Griesemer071c91b2008-10-23 12:04:45 -07002060
Rob Pikedf3183f2009-02-26 16:37:23 -08002061<p>
2062one may write:
2063</p>
Robert Griesemer071c91b2008-10-23 12:04:45 -07002064
Robert Griesemerc2d55862009-02-19 16:49:10 -08002065<pre>
2066p.z // (*p).z
2067p.y // ((*p).T1).y
2068p.x // (*(*p).T0).x
Robert Griesemer071c91b2008-10-23 12:04:45 -07002069
Robert Griesemerc2d55862009-02-19 16:49:10 -08002070p.M2 // (*p).M2
2071p.M1 // ((*p).T1).M1
2072p.M0 // ((*p).T0).M0
2073</pre>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002074
2075
Robert Griesemerc2d55862009-02-19 16:49:10 -08002076<font color=red>
Robert Griesemer071c91b2008-10-23 12:04:45 -07002077TODO: Specify what happens to receivers.
Robert Griesemerc2d55862009-02-19 16:49:10 -08002078</font>
Robert Griesemer7abfcd92008-10-07 17:14:30 -07002079
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002080
Robert Griesemerc2d55862009-02-19 16:49:10 -08002081<h3>Indexes</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002082
Rob Pikedf3183f2009-02-26 16:37:23 -08002083<p>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002084A primary expression of the form
Rob Pikedf3183f2009-02-26 16:37:23 -08002085</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002086
Robert Griesemerc2d55862009-02-19 16:49:10 -08002087<pre>
2088a[x]
2089</pre>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002090
Rob Pike4501d342009-02-19 17:31:36 -08002091<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002092denotes the array or map element of <code>a</code> indexed by <code>x</code>.
2093The value <code>x</code> is called the
2094<i>array index</i> or <i>map key</i>, respectively. The following
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002095rules apply:
Rob Pike4501d342009-02-19 17:31:36 -08002096</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002097<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002098For <code>a</code> of type <code>A</code> or <code>*A</code>
2099where <code>A</code> is an array type (§Array types):
Rob Pike4501d342009-02-19 17:31:36 -08002100</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002101<ul>
Rob Pikedf3183f2009-02-26 16:37:23 -08002102 <li><code>x</code> must be an integer value and <code>0 &lt;= x &lt; len(a)</code>
2103 <li><code>a[x]</code> is the array element at index <code>x</code> and the type of
2104 <code>a[x]</code> is the element type of <code>A</code>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002105</ul>
2106<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002107For <code>a</code> of type <code>M</code> or <code>*M</code>
2108where <code>M</code> is a map type (§Map types):
Rob Pike4501d342009-02-19 17:31:36 -08002109</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002110<ul>
Russ Cox5958dd62009-03-04 17:19:21 -08002111 <li><code>x</code>'s type must be equal to the key type of <code>M</code>
Rob Pikedf3183f2009-02-26 16:37:23 -08002112 and the map must contain an entry with key <code>x</code> (but see special forms below)
2113 <li><code>a[x]</code> is the map value with key <code>x</code>
2114 and the type of <code>a[x]</code> is the value type of <code>M</code>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002115</ul>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002116
Robert Griesemerc2d55862009-02-19 16:49:10 -08002117<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002118Otherwise <code>a[x]</code> is illegal. If the index or key is out of range evaluating
2119an otherwise legal index expression, a run-time exception occurs.
2120</p>
Robert Griesemer7abfcd92008-10-07 17:14:30 -07002121
Rob Pikedf3183f2009-02-26 16:37:23 -08002122<p>
2123However, if an index expression on a map <code>a</code> of type <code>map[K] V</code>
2124is used in an assignment of one of the special forms
2125</p>
2126
2127<pre>
2128r, ok = a[x]
2129r, ok := a[x]
2130</pre>
2131
2132<p>
2133the result of the index expression is a pair of values with types
2134<code>(K, bool)</code>.
2135If the key is present in the map,
2136the expression returns the pair <code>(a[x], true)</code>;
2137otherwise it returns <code>(Z, false)</code> where <code>Z</code> is
2138the zero value for <code>V</code> (§The zero value).
2139No run-time exception occurs in this case.
2140The index expression in this construct thus acts like a function call
2141returning a value and a boolean indicating success. (§Assignments)
2142</p>
2143
2144<p>
2145Similarly, if an assignment to a map has the special form
2146</p>
2147
2148<pre>
2149a[x] = r, ok
2150</pre>
2151
2152<p>
2153and boolean <code>ok</code> has the value <code>false</code>,
2154the entry for key <code>x</code> is deleted from the map; if
2155<code>ok</code> is <code>true</code>, the construct acts like
2156a regular assignment to an element of the map.
2157</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002158
Robert Griesemerc2d55862009-02-19 16:49:10 -08002159<h3>Slices</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002160
Rob Pikedf3183f2009-02-26 16:37:23 -08002161<p>
Rob Pike8f2330d2009-02-25 16:20:44 -08002162Strings, arrays, and slices can be <i>sliced</i> to construct substrings or descriptors
Robert Griesemer633957b2009-01-06 13:23:20 -08002163of subarrays. The index expressions in the slice select which elements appear
2164in the result. The result has indexes starting at 0 and length equal to the
Rob Pikedf3183f2009-02-26 16:37:23 -08002165difference in the index values in the slice. After slicing the array <code>a</code>
2166</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002167
Robert Griesemerc2d55862009-02-19 16:49:10 -08002168<pre>
Rob Pike426335f2009-03-02 17:52:52 -08002169a := [4]int{1, 2, 3, 4};
Robert Griesemerc2d55862009-02-19 16:49:10 -08002170s := a[1:3];
2171</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002172
Rob Pikedf3183f2009-02-26 16:37:23 -08002173<p>
Russ Cox5958dd62009-03-04 17:19:21 -08002174the slice <code>s</code> has type <code>[]int</code>, length 2, capacity 3, and elements
Rob Pikedf3183f2009-02-26 16:37:23 -08002175</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002176
Robert Griesemerc2d55862009-02-19 16:49:10 -08002177<pre>
2178s[0] == 2
2179s[1] == 3
2180</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002181
Rob Pikedf3183f2009-02-26 16:37:23 -08002182<p>
Rob Pike8f2330d2009-02-25 16:20:44 -08002183The slice length must be non-negative.
Russ Cox5958dd62009-03-04 17:19:21 -08002184For arrays or strings, the indexes
Rob Pike811dd252009-03-04 20:39:39 -08002185<code>lo</code> and <code>hi</code> must satisfy
21860 &lt;= <code>lo</code> &lt;= <code>hi</code> &lt;= length;
Russ Cox5958dd62009-03-04 17:19:21 -08002187for slices, the upper bound is the capacity rather than the length.
Robert Griesemerc2d55862009-02-19 16:49:10 -08002188<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002189If the sliced operand is a string, the result of the slice operation is another, new
Robert Griesemer633957b2009-01-06 13:23:20 -08002190string (§String types). If the sliced operand is an array or slice, the result
2191of the slice operation is a slice (§Slice types).
Rob Pikedf3183f2009-02-26 16:37:23 -08002192</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002193
2194
Russ Cox5958dd62009-03-04 17:19:21 -08002195<h3>Type assertions</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002196
Rob Pikedf3183f2009-02-26 16:37:23 -08002197<p>
2198For an expression <code>x</code> and a type <code>T</code>, the primary expression
2199</p>
Robert Griesemer434c6052008-11-07 13:34:37 -08002200
Robert Griesemerc2d55862009-02-19 16:49:10 -08002201<pre>
2202x.(T)
2203</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -08002204
Robert Griesemerc2d55862009-02-19 16:49:10 -08002205<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002206asserts that the value stored in <code>x</code> is of type <code>T</code>.
Russ Cox5958dd62009-03-04 17:19:21 -08002207The notation <code>x.(T)</code> is called a <i>type assertion</i>.
2208The type of <code>x</code> must be an interface type.
Rob Pikedf3183f2009-02-26 16:37:23 -08002209</p>
2210<p>
Russ Cox5958dd62009-03-04 17:19:21 -08002211More precisely, if <code>T</code> is not an interface type, <code>x.(T)</code> asserts
Rob Pikedf3183f2009-02-26 16:37:23 -08002212that the dynamic type of <code>x</code> is identical to the type <code>T</code>
2213(§Type equality and identity).
Russ Cox5958dd62009-03-04 17:19:21 -08002214If <code>T</code> is an interface type, <code>x.(T)</code> asserts that the dynamic type
Rob Pikedf3183f2009-02-26 16:37:23 -08002215of <code>T</code> implements the interface <code>T</code> (§Interface types).
Rob Pikedf3183f2009-02-26 16:37:23 -08002216<font color=red>TODO: gri wants an error if x is already of type T.</font>
2217</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002218<p>
Russ Cox5958dd62009-03-04 17:19:21 -08002219If the type assertion holds, the value of the expression is the value
2220stored 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 -08002221exception occurs. In other words, even though the dynamic type of <code>x</code>
Russ Cox5958dd62009-03-04 17:19:21 -08002222is known only at run-time, the type of <code>x.(T)</code> is
Rob Pikedf3183f2009-02-26 16:37:23 -08002223known to be <code>T</code> in a correct program.
2224</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002225<p>
Russ Cox5958dd62009-03-04 17:19:21 -08002226If a type assertion is used in an assignment of one of the special forms,
Rob Pikedf3183f2009-02-26 16:37:23 -08002227</p>
Robert Griesemer434c6052008-11-07 13:34:37 -08002228
Robert Griesemerc2d55862009-02-19 16:49:10 -08002229<pre>
2230v, ok = x.(T)
2231v, ok := x.(T)
2232</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -08002233
Robert Griesemerc2d55862009-02-19 16:49:10 -08002234<p>
Russ Cox5958dd62009-03-04 17:19:21 -08002235the result of the assertion is a pair of values with types <code>(T, bool)</code>.
2236If the assertion holds, the expression returns the pair <code>(x.(T), true)</code>;
Rob Pikedf3183f2009-02-26 16:37:23 -08002237otherwise, the expression returns <code>(Z, false)</code> where <code>Z</code>
2238is the zero value for type <code>T</code> (§The zero value).
2239No run-time exception occurs in this case.
Russ Cox5958dd62009-03-04 17:19:21 -08002240The type assertion in this construct thus acts like a function call
Rob Pikedf3183f2009-02-26 16:37:23 -08002241returning a value and a boolean indicating success. (§Assignments)
2242</p>
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -07002243
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002244
Robert Griesemerc2d55862009-02-19 16:49:10 -08002245<h3>Calls</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002246
Robert Griesemerc2d55862009-02-19 16:49:10 -08002247<p>
Rob Pike96750f12009-02-27 16:47:48 -08002248Given an expression <code>f</code> of function type
2249<code>F</code>,
Rob Pikedf3183f2009-02-26 16:37:23 -08002250</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002251
Robert Griesemerc2d55862009-02-19 16:49:10 -08002252<pre>
Rob Pike96750f12009-02-27 16:47:48 -08002253f(a1, a2, ... an)
Robert Griesemerc2d55862009-02-19 16:49:10 -08002254</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002255
Robert Griesemerc2d55862009-02-19 16:49:10 -08002256<p>
Rob Pike96750f12009-02-27 16:47:48 -08002257calls <code>f</code> with arguments <code>a1, a2, ... an</code>.
2258The arguments must be single-valued expressions
2259assignment compatible with the parameters of
Rob Pikedf3183f2009-02-26 16:37:23 -08002260<code>F</code> and are evaluated before the function is called.
2261The type of the expression is the result type
2262of <code>F</code>.
2263A method invocation is similar but the method itself
2264is specified as a selector upon a value of the receiver type for
2265the method.
2266</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002267
Robert Griesemerc2d55862009-02-19 16:49:10 -08002268<pre>
Rob Pikedf3183f2009-02-26 16:37:23 -08002269Atan2(x, y) // function call
2270var pt *Point;
2271pt.Scale(3.5) // method call with receiver pt
Robert Griesemerc2d55862009-02-19 16:49:10 -08002272</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002273
Rob Pikedf3183f2009-02-26 16:37:23 -08002274<p>
2275If the receiver type of the method is declared as a pointer of type <code>*T</code>,
2276the actual receiver may be a value of type <code>T</code>;
2277in such cases method invocation implicitly takes the
2278receiver's address:
2279</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002280
Robert Griesemerc2d55862009-02-19 16:49:10 -08002281<pre>
Rob Pikedf3183f2009-02-26 16:37:23 -08002282var p Point;
2283p.Scale(3.5)
Robert Griesemerc2d55862009-02-19 16:49:10 -08002284</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002285
Robert Griesemerc2d55862009-02-19 16:49:10 -08002286<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002287There is no distinct method type and there are no method literals.
Rob Pikedf3183f2009-02-26 16:37:23 -08002288</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002289
Rob Pikedf3183f2009-02-26 16:37:23 -08002290<h3>Passing arguments to <code>...</code> parameters</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002291
Robert Griesemerc2d55862009-02-19 16:49:10 -08002292<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002293When a function <code>f</code> has a <code>...</code> parameter,
2294it is always the last formal parameter. Within calls to <code>f</code>,
2295the arguments before the <code>...</code> are treated normally.
2296After those, an arbitrary number (including zero) of trailing
2297arguments may appear in the call and are bound to the <code>...</code>
2298parameter.
2299</p>
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002300
Rob Pikedf3183f2009-02-26 16:37:23 -08002301<p>
2302Within <code>f</code>, the <code>...</code> parameter has static
2303type <code>interface{}</code> (the empty interface). For each call,
2304its dynamic type is a structure whose sequential fields are the
2305trailing arguments of the call. That is, the actual arguments
2306provided for a <code>...</code> parameter are wrapped into a struct
2307that is passed to the function instead of the actual arguments.
2308Using the reflection library (TODO: reference), <code>f</code> may
2309unpack the elements of the dynamic type to recover the actual
2310arguments.
2311</p>
2312
2313<p>
2314Given the function and call
2315</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002316<pre>
Rob Pikedf3183f2009-02-26 16:37:23 -08002317func Fprintf(f io.Write, format string, args ...)
2318Fprintf(os.Stdout, "%s %d", "hello", 23);
Robert Griesemerc2d55862009-02-19 16:49:10 -08002319</pre>
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002320
Rob Pikedf3183f2009-02-26 16:37:23 -08002321<p>
2322Within <code>Fprintf</code>, the dynamic type of <code>args</code> for this
2323call will be, schematically,
2324<code> struct { string; int }</code>.
2325</p>
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002326
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002327
Rob Pikedf3183f2009-02-26 16:37:23 -08002328<p>
2329As a special case, if a function passes its own <code>...</code> parameter as the argument
2330for a <code>...</code> in a call to another function with a <code>...</code> parameter,
2331the parameter is not wrapped again but passed directly. In short, a formal <code>...</code>
2332parameter is passed unchanged as an actual <code>...</code> parameter.
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002333
Robert Griesemerc2d55862009-02-19 16:49:10 -08002334<h3>Operators</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002335
Rob Pikedf3183f2009-02-26 16:37:23 -08002336<p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002337Operators combine operands into expressions.
Rob Pikedf3183f2009-02-26 16:37:23 -08002338</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002339
Rob Pikeff70f092009-02-20 13:36:14 -08002340<pre class="grammar">
Russ Cox5958dd62009-03-04 17:19:21 -08002341Expression = UnaryExpr | Expression binary_op UnaryExpr .
Rob Pikedf3183f2009-02-26 16:37:23 -08002342UnaryExpr = PrimaryExpr | unary_op UnaryExpr .
Robert Griesemer57b34612008-10-10 12:45:44 -07002343
Rob Pikedf3183f2009-02-26 16:37:23 -08002344binary_op = log_op | com_op | rel_op | add_op | mul_op .
2345log_op = "||" | "&amp;&amp;" .
2346com_op = "&lt;-" .
2347rel_op = "==" | "!=" | "&lt;" | "&lt;=" | ">" | ">=" .
2348add_op = "+" | "-" | "|" | "^" .
Rob Pikecd04ec92009-03-11 21:59:05 -07002349mul_op = "*" | "/" | "%" | "&lt;&lt;" | ">>" | "&amp;" | "&amp;^" .
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002350
Rob Pikedf3183f2009-02-26 16:37:23 -08002351unary_op = "+" | "-" | "!" | "^" | "*" | "&amp;" | "&lt;-" .
Robert Griesemerc2d55862009-02-19 16:49:10 -08002352</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002353
Robert Griesemerc2d55862009-02-19 16:49:10 -08002354<p>
Rob Pike4501d342009-02-19 17:31:36 -08002355The operand types in binary operations must be equal, with the following exceptions:
2356</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002357<ul>
Rob Pike9b5d8232009-03-12 18:47:49 -07002358 <li>Except in shift expressions, if one operand has numeric type and the other operand is
Robert Griesemera6b546f2008-10-20 11:46:40 -07002359 an ideal number, the ideal number is converted to match the type of
Rob Pikedf3183f2009-02-26 16:37:23 -08002360 the other operand (§Expressions).</li>
Robert Griesemerc8e18762008-09-12 12:26:22 -07002361
Robert Griesemerc2d55862009-02-19 16:49:10 -08002362 <li>If both operands are ideal numbers, the conversion is to ideal floats
Rob Pikedf3183f2009-02-26 16:37:23 -08002363 if one of the operands is an ideal float
2364 (relevant for <code>/</code> and <code>%</code>).</li>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002365
Rob Pikedf3183f2009-02-26 16:37:23 -08002366 <li>The right operand in a shift operation must be always be of unsigned integer type
2367 or an ideal number that can be safely converted into an unsigned integer type
2368 (§Arithmetic operators).</li>
2369
Russ Cox5958dd62009-03-04 17:19:21 -08002370 <li>The operands in channel sends differ in type: one is always a channel and the
Rob Pikedf3183f2009-02-26 16:37:23 -08002371 other is a variable or value of the channel's element type.</li>
Robert Griesemera6b546f2008-10-20 11:46:40 -07002372
Robert Griesemerc2d55862009-02-19 16:49:10 -08002373 <li>When comparing two operands of channel type, the channel value types
Rob Pikedf3183f2009-02-26 16:37:23 -08002374 must be equal but the channel direction is ignored.</li>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002375</ul>
Robert Griesemer18b05c12009-01-26 09:34:19 -08002376
Robert Griesemerc2d55862009-02-19 16:49:10 -08002377<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002378Unary operators have the highest precedence. They are evaluated from
2379right to left. As the <code>++</code> and <code>--</code> operators form
2380statements, not expressions, they fall
2381outside the unary operator hierarchy and apply
2382to the operand on the left.
2383As a consequence, statement <code>*p++</code> is the same as <code>(*p)++</code>.
2384<p>
2385There are six precedence levels for binary operators.
2386Multiplication operators bind strongest, followed by addition
Robert Griesemerad711102008-09-11 17:48:20 -07002387operators, comparison operators, communication operators,
Rob Pikedf3183f2009-02-26 16:37:23 -08002388<code>&amp;&amp;</code> (logical and), and finally <code>||</code> (logical or):
2389</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002390
Rob Pikeff70f092009-02-20 13:36:14 -08002391<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08002392Precedence Operator
Rob Pikecd04ec92009-03-11 21:59:05 -07002393 6 * / % &lt;&lt; >> &amp; &amp;^
Robert Griesemerc2d55862009-02-19 16:49:10 -08002394 5 + - | ^
2395 4 == != &lt; &lt;= > >=
2396 3 &lt;-
2397 2 &amp;&amp;
2398 1 ||
2399</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002400
Robert Griesemerc2d55862009-02-19 16:49:10 -08002401<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002402Binary operators of the same precedence associate from left to right.
2403For instance, <code>x / y / z</code> is the same as <code>(x / y) / z</code>.
2404</p>
2405<p>
2406Examples:
2407</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002408
Robert Griesemerc2d55862009-02-19 16:49:10 -08002409<pre>
2410+x
241123 + 3*x[i]
2412x &lt;= f()
2413^a >> b
2414f() || g()
2415x == y + 1 &amp;&amp; &lt;-chan_ptr > 0
2416</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002417
2418
Robert Griesemerc2d55862009-02-19 16:49:10 -08002419<h3>Arithmetic operators</h3>
2420<p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002421Arithmetic operators apply to numeric types and yield a result of the same
Rob Pikedf3183f2009-02-26 16:37:23 -08002422type as the first operand. The four standard arithmetic operators (<code>+</code>,
2423<code>-</code>, <code>*</code>, <code>/</code>) apply both to integer and
2424floating point types, while <code>+</code> applies also
2425to strings; all other arithmetic operators apply to integers only.
2426</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002427
Rob Pikeff70f092009-02-20 13:36:14 -08002428<pre class="grammar">
Rob Pike307ec212009-03-12 15:53:56 -07002429+ sum integers, floats, strings
2430- difference integers, floats
2431* product integers, floats
2432/ quotient integers, floats
2433% remainder integers
Robert Griesemerc2d55862009-02-19 16:49:10 -08002434
Rob Pike307ec212009-03-12 15:53:56 -07002435&amp; bitwise and integers
2436| bitwise or integers
2437^ bitwise xor integers
2438&amp;^ bit clear (and not) integers
Robert Griesemerc2d55862009-02-19 16:49:10 -08002439
Rob Pike307ec212009-03-12 15:53:56 -07002440<< left shift integer << unsigned integer
2441>> right shift integer >> unsigned integer
Robert Griesemerc2d55862009-02-19 16:49:10 -08002442</pre>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002443
Rob Pikedf3183f2009-02-26 16:37:23 -08002444<p>
2445Strings can be concatenated using the <code>+</code> operator
2446or the <code>+=</code> assignment operator:
2447</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002448
Robert Griesemerc2d55862009-02-19 16:49:10 -08002449<pre>
Rob Pikedf3183f2009-02-26 16:37:23 -08002450s := "hi" + string(c);
2451s += " and good bye";
Robert Griesemerc2d55862009-02-19 16:49:10 -08002452</pre>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002453
Robert Griesemerc2d55862009-02-19 16:49:10 -08002454<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002455String addition creates a new string by concatenating the operands.
2456</p>
2457<p>
2458For integer values, <code>/</code> and <code>%</code> satisfy the following relationship:
2459</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002460
Robert Griesemerc2d55862009-02-19 16:49:10 -08002461<pre>
2462(a / b) * b + a % b == a
2463</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002464
Rob Pikedf3183f2009-02-26 16:37:23 -08002465<p>
2466with <code>(a / b)</code> truncated towards zero.
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002467Examples:
Rob Pikedf3183f2009-02-26 16:37:23 -08002468</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002469
Robert Griesemerc2d55862009-02-19 16:49:10 -08002470<pre>
2471 x y x / y x % y
2472 5 3 1 2
2473-5 3 -1 -2
2474 5 -3 -1 2
2475-5 -3 1 -2
2476</pre>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002477
Rob Pikedf3183f2009-02-26 16:37:23 -08002478<p>
2479If the dividend is positive and the divisor is a constant power of 2,
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002480the division may be replaced by a left shift, and computing the remainder may
2481be replaced by a bitwise "and" operation:
Rob Pikedf3183f2009-02-26 16:37:23 -08002482</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002483
Robert Griesemerc2d55862009-02-19 16:49:10 -08002484<pre>
2485 x x / 4 x % 4 x >> 2 x &amp; 3
2486 11 2 3 2 3
2487-11 -2 -3 -3 1
2488</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002489
Rob Pikedf3183f2009-02-26 16:37:23 -08002490<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002491The shift operators shift the left operand by the shift count specified by the
2492right operand. They implement arithmetic shifts if the left operand is a signed
Rob Pikedf3183f2009-02-26 16:37:23 -08002493integer and logical shifts if it is an unsigned integer. The shift count must
2494be an unsigned integer. There is no upper limit on the shift count. Shifts behave
2495as if the left operand is shifted <code>n</code> times by 1 for a shift
2496count of <code>n</code>.
2497As a result, <code>x << 1</code> is the same as <code>x*2</code>
2498and <code>x >> 1</code> is the same as
2499<code>x/2</code> truncated towards negative infinity.
2500</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002501
Rob Pikedf3183f2009-02-26 16:37:23 -08002502<p>
2503For integer operands, the unary operators
2504<code>+</code>, <code>-</code>, and <code>^</code> are defined as
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002505follows:
Rob Pikedf3183f2009-02-26 16:37:23 -08002506</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002507
Rob Pikeff70f092009-02-20 13:36:14 -08002508<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08002509+x is 0 + x
2510-x negation is 0 - x
2511^x bitwise complement is m ^ x with m = "all bits set to 1"
2512</pre>
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002513
Russ Cox5958dd62009-03-04 17:19:21 -08002514<p>
2515For floating point numbers,
2516<code>+x</code> is the same as <code>x</code>,
2517while <code>-x</code> is the negation of <code>x</code>.
2518</p>
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002519
Robert Griesemerc2d55862009-02-19 16:49:10 -08002520<h3>Integer overflow</h3>
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002521
Rob Pikedf3183f2009-02-26 16:37:23 -08002522<p>
2523For unsigned integer values, the operations <code>+</code>,
2524<code>-</code>, <code>*</code>, and <code>&lt;&lt;</code> are
2525computed modulo 2<sup><i>n</i></sup>, where <i>n</i> is the bit width of
2526the unsigned integer's type
Russ Cox5958dd62009-03-04 17:19:21 -08002527(§Numeric types). Loosely speaking, these unsigned integer operations
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002528discard high bits upon overflow, and programs may rely on ``wrap around''.
Rob Pikedf3183f2009-02-26 16:37:23 -08002529</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002530<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002531For signed integers, the operations <code>+</code>,
2532<code>-</code>, <code>*</code>, and <code>&lt;&lt;</code> may legally
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002533overflow and the resulting value exists and is deterministically defined
2534by the signed integer representation, the operation, and its operands.
Rob Pikedf3183f2009-02-26 16:37:23 -08002535No exception is raised as a result of overflow. A
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002536compiler may not optimize code under the assumption that overflow does
Rob Pikedf3183f2009-02-26 16:37:23 -08002537not occur. For instance, it may not assume that <code>x &lt; x + 1</code> is always true.
2538</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002539
2540
Robert Griesemerc2d55862009-02-19 16:49:10 -08002541<h3>Comparison operators</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002542
Rob Pike5af7de32009-02-24 15:17:59 -08002543<p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002544Comparison operators yield a boolean result. All comparison operators apply
Rob Pike5af7de32009-02-24 15:17:59 -08002545to basic types except bools.
2546The operators <code>==</code> and <code>!=</code> apply, at least in some cases,
2547to all types except arrays and structs.
2548</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002549
Rob Pikeff70f092009-02-20 13:36:14 -08002550<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08002551== equal
2552!= not equal
2553< less
2554<= less or equal
2555> greater
2556>= greater or equal
2557</pre>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002558
Rob Pike5af7de32009-02-24 15:17:59 -08002559<p>
2560Numeric basic types are compared in the usual way.
2561</p>
2562<p>
Robert Griesemer52a54802008-09-30 13:02:50 -07002563Strings are compared byte-wise (lexically).
Rob Pike5af7de32009-02-24 15:17:59 -08002564</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002565<p>
Robert Griesemer18b05c12009-01-26 09:34:19 -08002566Booleans are equal if they are either both "true" or both "false".
Rob Pike5af7de32009-02-24 15:17:59 -08002567</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002568<p>
Rob Pike5af7de32009-02-24 15:17:59 -08002569The rules for comparison of composite types are described in the
2570section on §Comparison compatibility.
2571</p>
Robert Griesemera3294712009-01-05 11:17:26 -08002572
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002573
Robert Griesemerc2d55862009-02-19 16:49:10 -08002574<h3>Logical operators</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002575
Rob Pikedf3183f2009-02-26 16:37:23 -08002576<p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002577Logical operators apply to boolean operands and yield a boolean result.
2578The right operand is evaluated conditionally.
Rob Pikedf3183f2009-02-26 16:37:23 -08002579</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002580
Rob Pikeff70f092009-02-20 13:36:14 -08002581<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08002582&amp;&amp; conditional and p &amp;&amp; q is "if p then q else false"
2583|| conditional or p || q is "if p then true else q"
2584! not !p is "not p"
2585</pre>
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -07002586
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002587
Robert Griesemerc2d55862009-02-19 16:49:10 -08002588<h3>Address operators</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002589
Rob Pikedf3183f2009-02-26 16:37:23 -08002590<p>
Rob Pikeafee1c52009-03-20 17:41:25 -07002591The unary prefix address-of operator <code>&amp;</code> generates the address of its operand, which must be a variable,
2592pointer indirection, field selector, or array or slice indexing operation. It is illegal to take the address of a function
2593result variable.
2594Given an operand of pointer type, the unary prefix pointer indirection operator <code>*</code> retrieves the value pointed
2595to by the operand.
2596</p>
2597
2598<pre>
2599&amp;x
2600&amp;a[f(2)]
2601*p
2602*pf(x)
2603</pre>
2604
Robert Griesemerc2d55862009-02-19 16:49:10 -08002605<p>
2606<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 -08002607operators involved.
Robert Griesemerc2d55862009-02-19 16:49:10 -08002608</font>
Rob Pikeafee1c52009-03-20 17:41:25 -07002609</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002610<p>
Rob Pikeafee1c52009-03-20 17:41:25 -07002611Methods are a form of function and a method ``value'' has a function type.
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002612Consider the type T with method M:
Rob Pikeafee1c52009-03-20 17:41:25 -07002613</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002614
Robert Griesemerc2d55862009-02-19 16:49:10 -08002615<pre>
2616type T struct {
2617 a int;
2618}
2619func (tp *T) M(a int) int;
2620var t *T;
2621</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002622
Rob Pikeafee1c52009-03-20 17:41:25 -07002623<p>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002624To construct the value of method M, one writes
Rob Pikeafee1c52009-03-20 17:41:25 -07002625</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002626
Robert Griesemerc2d55862009-02-19 16:49:10 -08002627<pre>
2628t.M
2629</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002630
Rob Pikeafee1c52009-03-20 17:41:25 -07002631<p>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002632using the variable t (not the type T).
Robert Griesemerc2d55862009-02-19 16:49:10 -08002633<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 -08002634sense then t.M, since only the type T is needed to find the method M, i.e.,
2635its address). TBD.
Robert Griesemerc2d55862009-02-19 16:49:10 -08002636</font>
Rob Pikeafee1c52009-03-20 17:41:25 -07002637</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002638
Rob Pikeafee1c52009-03-20 17:41:25 -07002639<p>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002640The expression t.M is a function value with type
Rob Pikeafee1c52009-03-20 17:41:25 -07002641</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002642
Robert Griesemerc2d55862009-02-19 16:49:10 -08002643<pre>
2644func (t *T, a int) int
2645</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002646
Rob Pikeafee1c52009-03-20 17:41:25 -07002647<p>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002648and may be invoked only as a function, not as a method:
Rob Pikeafee1c52009-03-20 17:41:25 -07002649</p>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002650
Robert Griesemerc2d55862009-02-19 16:49:10 -08002651<pre>
2652var f func (t *T, a int) int;
2653f = t.M;
2654x := f(t, 7);
2655</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002656
Rob Pikeafee1c52009-03-20 17:41:25 -07002657<p>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002658Note that one does not write t.f(7); taking the value of a method demotes
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002659it to a function.
Rob Pikeafee1c52009-03-20 17:41:25 -07002660</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002661
Rob Pikeafee1c52009-03-20 17:41:25 -07002662<p>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002663In general, given type T with method M and variable t of type T,
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002664the method invocation
Rob Pikeafee1c52009-03-20 17:41:25 -07002665</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002666
Robert Griesemerc2d55862009-02-19 16:49:10 -08002667<pre>
2668t.M(args)
2669</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002670
Rob Pikeafee1c52009-03-20 17:41:25 -07002671<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002672is equivalent to the function call
Rob Pikeafee1c52009-03-20 17:41:25 -07002673</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002674
Robert Griesemerc2d55862009-02-19 16:49:10 -08002675<pre>
2676(t.M)(t, args)
2677</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002678
Rob Pikeafee1c52009-03-20 17:41:25 -07002679<p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002680<font color=red>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002681TODO: should probably describe the effect of (t.m) under §Expressions if t.m
2682denotes a method: Effect is as described above, converts into function.
Robert Griesemerc2d55862009-02-19 16:49:10 -08002683</font>
Rob Pikeafee1c52009-03-20 17:41:25 -07002684</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002685<p>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002686If T is an interface type, the expression t.M does not determine which
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002687underlying type's M is called until the point of the call itself. Thus given
Robert Griesemerd8a764c2009-02-06 17:01:10 -08002688T1 and T2, both implementing interface I with method M, the sequence
Rob Pikeafee1c52009-03-20 17:41:25 -07002689</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002690
Robert Griesemerc2d55862009-02-19 16:49:10 -08002691<pre>
2692var t1 *T1;
2693var t2 *T2;
2694var i I = t1;
2695m := i.M;
2696m(t2, 7);
2697</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002698
Rob Pikeafee1c52009-03-20 17:41:25 -07002699<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002700will invoke t2.M() even though m was constructed with an expression involving
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002701t1. Effectively, the value of m is a function literal
Rob Pikeafee1c52009-03-20 17:41:25 -07002702</p>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002703
Robert Griesemerc2d55862009-02-19 16:49:10 -08002704<pre>
2705func (recv I, a int) {
2706 recv.M(a);
2707}
2708</pre>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002709
Rob Pikeafee1c52009-03-20 17:41:25 -07002710<p>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002711that is automatically created.
Rob Pikeafee1c52009-03-20 17:41:25 -07002712</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002713<p>
2714<font color=red>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002715TODO: Document implementation restriction: It is illegal to take the address
Robert Griesemerc2d55862009-02-19 16:49:10 -08002716of a result parameter (e.g.: func f() (x int, p *int) { return 2, &amp;x }).
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002717(TBD: is it an implementation restriction or fact?)
Robert Griesemerc2d55862009-02-19 16:49:10 -08002718</font>
Rob Pikeafee1c52009-03-20 17:41:25 -07002719</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002720
Robert Griesemerc2d55862009-02-19 16:49:10 -08002721<h3>Communication operators</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002722
Robert Griesemerc2d55862009-02-19 16:49:10 -08002723<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002724The term <i>channel</i> means "variable of channel type" (§Channel types).
2725</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002726<p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002727The send operation uses the binary operator "&lt;-", which operates on
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002728a channel and a value (expression):
Rob Pikedf3183f2009-02-26 16:37:23 -08002729</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002730
Robert Griesemerc2d55862009-02-19 16:49:10 -08002731<pre>
2732ch <- 3
2733</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002734
Rob Pikedf3183f2009-02-26 16:37:23 -08002735<p>
2736The send operation sends the value on the channel. Both the channel
2737and the expression are evaluated before communication begins.
2738Communication blocks until the send can proceed, at which point the
2739value is transmitted on the channel. A send can proceed if the
2740channel is asynchronous and there is room in its buffer or the
2741channel is synchronous and a receiver is ready.
2742</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002743<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002744If the send operation appears in an expression context, the value
2745of the expression is a boolean and the operation is non-blocking.
2746The value of the boolean reports true if the communication succeeded,
Rob Pikedf3183f2009-02-26 16:37:23 -08002747false if it did not. (The channel and
2748the expression to be sent are evaluated regardless.)
2749These two examples are equivalent:
2750</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002751
Robert Griesemerc2d55862009-02-19 16:49:10 -08002752<pre>
2753ok := ch <- 3;
2754if ok { print("sent") } else { print("not sent") }
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002755
Robert Griesemerc2d55862009-02-19 16:49:10 -08002756if ch <- 3 { print("sent") } else { print("not sent") }
2757</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002758
Rob Pikedf3183f2009-02-26 16:37:23 -08002759<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002760In other words, if the program tests the value of a send operation,
2761the send is non-blocking and the value of the expression is the
2762success of the operation. If the program does not test the value,
2763the operation blocks until it succeeds.
Rob Pikedf3183f2009-02-26 16:37:23 -08002764</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002765<p>
2766The receive operation uses the prefix unary operator "&lt;-".
Rob Pikedf3183f2009-02-26 16:37:23 -08002767The value of the expression is the value received, whose type
2768is the element type of the channel.
2769</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002770
Robert Griesemerc2d55862009-02-19 16:49:10 -08002771<pre>
2772<-ch
2773</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002774
Rob Pikedf3183f2009-02-26 16:37:23 -08002775<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002776The expression blocks until a value is available, which then can
Rob Pikedf3183f2009-02-26 16:37:23 -08002777be assigned to a variable or used like any other expression.
2778If the receive expression does not save the value, the value is
2779discarded.
2780</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002781
Robert Griesemerc2d55862009-02-19 16:49:10 -08002782<pre>
2783v1 := <-ch
2784v2 = <-ch
2785f(<-ch)
Robert Griesemerc2d55862009-02-19 16:49:10 -08002786<-strobe // wait until clock pulse
2787</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002788
Rob Pikedf3183f2009-02-26 16:37:23 -08002789<p>
Robert Griesemer2902a822008-09-17 13:57:11 -07002790If a receive expression is used in a tuple assignment of the form
Rob Pikedf3183f2009-02-26 16:37:23 -08002791</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002792
Robert Griesemerc2d55862009-02-19 16:49:10 -08002793<pre>
2794x, ok = <-ch; // or: x, ok := <-ch
2795</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002796
Rob Pikedf3183f2009-02-26 16:37:23 -08002797<p>
2798the receive operation becomes non-blocking.
2799If the operation can proceeed, the boolean variable
2800<code>ok</code> will be set to <code>true</code>
2801and the value stored in <code>x</code>; otherwise
2802<code>ok</code> is set
2803to <code>false</code> and <code>x</code> is set to the
2804zero value for its type (§The zero value).
2805</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002806
Rob Pikedf3183f2009-02-26 16:37:23 -08002807<p>
2808<font color=red>TODO: Probably in a separate section, communication semantices
2809need to be presented regarding send, receive, select, and goroutines.</font>
2810</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002811
Robert Griesemerc2d55862009-02-19 16:49:10 -08002812<h3>Constant expressions</h3>
Robert Griesemerb90b2132008-09-19 15:49:55 -07002813
Rob Pikef27e9f02009-02-23 19:22:05 -08002814<p>
2815Constant expressions may contain only constants, <code>iota</code>,
2816numeric literals, string literals, and
2817some constant-valued built-in functions such as <code>unsafe.Sizeof</code>
2818and <code>len</code> applied to an array.
2819In practice, constant expressions are those that can be evaluated at compile time.
2820<p>
2821The type of a constant expression is determined by the type of its
Rob Pikedf3183f2009-02-26 16:37:23 -08002822elements. If it contains only numeric literals, its type is <i>ideal
Rob Pikece9417e2009-03-12 17:08:47 -07002823integer</i> or <i>ideal float</i> (§Ideal number). Whether a literal
2824is an integer or float depends on the syntax of the literals (123 vs. 123.0).
Russ Cox5958dd62009-03-04 17:19:21 -08002825The nature of the arithmetic
Rob Pikef27e9f02009-02-23 19:22:05 -08002826operations within the expression depends, elementwise, on the values;
2827for example, 3/2 is an integer division yielding 1, while 3./2. is
2828a floating point division yielding 1.5. Thus
2829</p>
Robert Griesemerb90b2132008-09-19 15:49:55 -07002830
Robert Griesemerc2d55862009-02-19 16:49:10 -08002831<pre>
Rob Pikef27e9f02009-02-23 19:22:05 -08002832const x = 3./2. + 3/2;
Robert Griesemerc2d55862009-02-19 16:49:10 -08002833</pre>
Robert Griesemerb90b2132008-09-19 15:49:55 -07002834
Robert Griesemerc2d55862009-02-19 16:49:10 -08002835<p>
Rob Pikef27e9f02009-02-23 19:22:05 -08002836yields a floating point constant of ideal float value 2.5 (1.5 +
28371); its constituent expressions are evaluated using distinct rules
2838for division.
2839</p>
2840
2841<p>
2842Intermediate values and the constants themselves
2843may require precision significantly larger than any concrete type
2844in the language. The following are legal declarations:
2845</p>
2846
2847<pre>
2848const Huge = 1 << 100;
2849const Four int8 = Huge >> 98;
2850</pre>
2851
2852<p>
2853A constant expression may appear in any context, such as assignment
2854to a variable of any numeric type, as long as the value of the
Rob Pikedf3183f2009-02-26 16:37:23 -08002855expression can be represented accurately in that context.
Rob Pikef27e9f02009-02-23 19:22:05 -08002856It is erroneous to assign a value with a non-zero fractional part
Rob Pikedf3183f2009-02-26 16:37:23 -08002857to an integer, or if the assignment would overflow or underflow,
2858or in general if the value cannot be represented by the type of
2859the variable.
2860For
2861instance, <code>3</code> can be assigned to any integer variable but also to any
2862floating point variable, while <code>-1e12</code> can be assigned to a
2863<code>float32</code>, <code>float64</code>, or even <code>int64</code>
2864but not <code>uint64</code> or <code>string</code>.
Rob Pikef27e9f02009-02-23 19:22:05 -08002865</p>
Robert Griesemerb90b2132008-09-19 15:49:55 -07002866
Rob Pike21d03492009-03-24 19:16:42 -07002867<p>
2868If a typed constant expression evaluates to a value that is not
2869representable by that type, the compiler reports an error.
2870</p>
2871
2872<pre>
2873uint8(-1) // error, out of range
2874uint8(100) * 100 // error, out of range
2875</pre>
2876
2877<p>
2878The size of the mask used by the unary bitwise complement
2879operator in a typed constant expression is equal to the size of the
2880expression's type. In an ideal constant expression, the bitwise
2881complement operator inverts all the bits, producing a negative value.
2882</p>
2883
2884<pre>
2885^1 // ideal constant, equal to -2
2886uint8(^1) // error, same as uint8(-2), out of range
2887^uint8(1) // typed uint8 constant, same as 0xFF ^ uint8(1) = uint8(0xFE)
2888int8(^1) // same as int8(-2)
2889^int8(1) // error, same as 0xFF ^ int8(1) = int8(0xFE), out of range
2890</pre>
2891
2892<p>
2893TODO: perhaps ^ should be disallowed on non-uints instead of assuming twos complement.
2894Also it may be possible to make typed constants more like variables, at the cost of fewer
2895overflow etc. errors being caught.
2896</p>
2897
Rob Pikec956e902009-04-14 20:10:49 -07002898<h3>Order of evaluation</h3>
2899
2900<p>
2901When evaluating the elements of an assignment or expression,
2902all function calls, method calls and
2903communication operations are evaluated in lexical left-to-right
2904order. Otherwise, the order of evaluation is unspecified.
2905</p>
2906
2907<p>
2908For example, while evaluating the arguments for this call
2909to function <code>f</code>,
2910</p>
2911<pre>
2912f(g(), h() + x[i()], <-c)
2913</pre>
2914<p>
2915the call to <code>g()</code> happens before the call to <code>h()</code>,
2916which happens before the call to <code>i()</code>, all of
2917of which happen before receiving the value from the channel
2918<code>c</code>.
2919However, the order of those events compared to the evaluation of
2920<code>f</code>, the evaluation of <code>x</code>, and the indexing
Russ Coxbcdc2472009-04-16 23:06:48 -07002921of <code>x</code> by the return value of
Rob Pikec956e902009-04-14 20:10:49 -07002922<code>i()</code> is not specified.
2923</p>
2924
Rob Pikeff70f092009-02-20 13:36:14 -08002925<hr/>
Robert Griesemerb90b2132008-09-19 15:49:55 -07002926
Robert Griesemerc2d55862009-02-19 16:49:10 -08002927<h2>Statements</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002928
Rob Pike96750f12009-02-27 16:47:48 -08002929<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002930Statements control execution.
Rob Pike96750f12009-02-27 16:47:48 -08002931</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002932
Rob Pikeff70f092009-02-20 13:36:14 -08002933<pre class="grammar">
Robert Griesemerdea43942009-03-16 17:36:52 -07002934Statement =
Rob Pike11417162009-03-24 17:45:53 -07002935 Declaration | EmptyStmt | LabeledStmt |
2936 SimpleStmt | GoStmt | ReturnStmt | BreakStmt | ContinueStmt | GotoStmt |
2937 FallthroughStmt | Block | IfStmt | SwitchStmt | SelectStmt | ForStmt |
2938 DeferStmt .
Robert Griesemer7abfcd92008-10-07 17:14:30 -07002939
Rob Pike11417162009-03-24 17:45:53 -07002940SimpleStmt = ExpressionStmt | IncDecStmt | Assignment | SimpleVarDecl .
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002941
Rob Pike96750f12009-02-27 16:47:48 -08002942StatementList = Statement { Separator Statement } .
2943Separator = [ ";" ]
Robert Griesemerc2d55862009-02-19 16:49:10 -08002944</pre>
Robert Griesemer7271e042008-10-09 20:05:24 -07002945
Robert Griesemerc2d55862009-02-19 16:49:10 -08002946<p>
Rob Pike96750f12009-02-27 16:47:48 -08002947Elements of a list of statements are separated by semicolons,
2948which may be omitted only if the previous statement:
Rob Pike4501d342009-02-19 17:31:36 -08002949</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002950<ul>
Rob Pike96750f12009-02-27 16:47:48 -08002951 <li>ends with the closing parenthesis ")" of a list of declarations
2952 (§Declarations and Scope); or</li>
Rob Pike736a1ae2009-04-02 23:03:41 -07002953 <li>ends with a closing brace "}" that is not part of an expression.
Robert Griesemerc2d55862009-02-19 16:49:10 -08002954</ul>
Robert Griesemer7271e042008-10-09 20:05:24 -07002955
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002956
Robert Griesemerc2d55862009-02-19 16:49:10 -08002957<h3>Empty statements</h3>
Robert Griesemeraed247f2008-10-08 17:05:30 -07002958
Rob Pike96750f12009-02-27 16:47:48 -08002959<p>
Robert Griesemeraed247f2008-10-08 17:05:30 -07002960The empty statement does nothing.
Rob Pike96750f12009-02-27 16:47:48 -08002961</p>
Robert Griesemeraed247f2008-10-08 17:05:30 -07002962
Rob Pikeff70f092009-02-20 13:36:14 -08002963<pre class="grammar">
Rob Pike11417162009-03-24 17:45:53 -07002964EmptyStmt = .
Robert Griesemerc2d55862009-02-19 16:49:10 -08002965</pre>
Robert Griesemeraed247f2008-10-08 17:05:30 -07002966
Rob Pike96750f12009-02-27 16:47:48 -08002967<p>
2968A statement list can always in effect be terminated with a semicolon by
2969adding an empty statement.
2970</p>
2971
Robert Griesemeraed247f2008-10-08 17:05:30 -07002972
Robert Griesemerdea43942009-03-16 17:36:52 -07002973<h3>Labeled statements</h3>
2974
2975<p>
2976A labeled statement may be the target of a <code>goto</code>,
2977<code>break</code> or <code>continue</code> statement.
2978</p>
2979
2980<pre class="grammar">
Rob Pike11417162009-03-24 17:45:53 -07002981LabeledStmt = Label ":" Statement .
Robert Griesemerdea43942009-03-16 17:36:52 -07002982Label = identifier .
2983</pre>
2984
2985<pre>
2986Error: log.Fatal("error encountered")
2987</pre>
2988
2989
Robert Griesemerc2d55862009-02-19 16:49:10 -08002990<h3>Expression statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002991
Rob Pike96750f12009-02-27 16:47:48 -08002992<p>
2993Function calls, method calls, and channel operations
2994can appear in statement context.
2995</p>
2996
2997
Rob Pikeff70f092009-02-20 13:36:14 -08002998<pre class="grammar">
Rob Pike11417162009-03-24 17:45:53 -07002999ExpressionStmt = Expression .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003000</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003001
Robert Griesemerc2d55862009-02-19 16:49:10 -08003002<pre>
3003f(x+y)
Rob Pike96750f12009-02-27 16:47:48 -08003004<-ch
Robert Griesemerc2d55862009-02-19 16:49:10 -08003005</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003006
3007
Robert Griesemerc2d55862009-02-19 16:49:10 -08003008<h3>IncDec statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003009
Rob Pike96750f12009-02-27 16:47:48 -08003010<p>
Robert Griesemer52a54802008-09-30 13:02:50 -07003011The "++" and "--" statements increment or decrement their operands
Rob Pike96750f12009-02-27 16:47:48 -08003012by the ideal numeric value 1. As with an assignment, the operand
3013must be a variable, pointer indirection, field selector or index expression.
3014</p>
Robert Griesemer52a54802008-09-30 13:02:50 -07003015
Rob Pikeff70f092009-02-20 13:36:14 -08003016<pre class="grammar">
Rob Pike11417162009-03-24 17:45:53 -07003017IncDecStmt = Expression ( "++" | "--" ) .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003018</pre>
Russ Cox5958dd62009-03-04 17:19:21 -08003019
Rob Pike96750f12009-02-27 16:47:48 -08003020<p>
Robert Griesemer52a54802008-09-30 13:02:50 -07003021The following assignment statements (§Assignments) are semantically
3022equivalent:
Rob Pike96750f12009-02-27 16:47:48 -08003023</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003024
Rob Pikeff70f092009-02-20 13:36:14 -08003025<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08003026IncDec statement Assignment
3027x++ x += 1
3028x-- x -= 1
3029</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003030
Robert Griesemerc2d55862009-02-19 16:49:10 -08003031<h3>Assignments</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003032
Rob Pikeff70f092009-02-20 13:36:14 -08003033<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08003034Assignment = ExpressionList assign_op ExpressionList .
Rob Pikeff70f092009-02-20 13:36:14 -08003035
Robert Griesemerc2d55862009-02-19 16:49:10 -08003036assign_op = [ add_op | mul_op ] "=" .
3037</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003038
Rob Pike96750f12009-02-27 16:47:48 -08003039<p>
3040Each left-hand side operand must be a variable, pointer indirection,
3041field selector, or index expression.
3042</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003043
Robert Griesemerc2d55862009-02-19 16:49:10 -08003044<pre>
3045x = 1
3046*p = f()
3047a[i] = 23
3048k = <-ch
Rob Pike307ec212009-03-12 15:53:56 -07003049i &amp;^= 1&lt;&lt;n
Robert Griesemerc2d55862009-02-19 16:49:10 -08003050</pre>
Rob Pike96750f12009-02-27 16:47:48 -08003051
3052<p>
3053An <i>assignment operation</i> <code>x</code> <i>op</i><code>=</code>
3054<code>y</code> where <i>op</i> is a binary arithmetic operation is equivalent
3055to <code>x</code> <code>=</code> <code>x</code> <i>op</i>
3056<code>y</code> but evalutates <code>x</code>
3057only once. The <i>op</i><code>=</code> construct is a single token.
3058</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003059
Robert Griesemerc2d55862009-02-19 16:49:10 -08003060<pre>
Rob Pike96750f12009-02-27 16:47:48 -08003061a[i] <<= 2
Robert Griesemerc2d55862009-02-19 16:49:10 -08003062</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003063
Rob Pike96750f12009-02-27 16:47:48 -08003064<p>
3065A tuple assignment assigns the individual elements of a multi-valued
3066operation to a list of variables. There are two forms. In the
3067first, the right hand operand is a single multi-valued expression
3068such as a function evaluation or channel or map operation (§Channel
Russ Cox5958dd62009-03-04 17:19:21 -08003069operations, §Map operations) or a type assertion (§Type assertions).
3070The number of operands on the left
Rob Pike96750f12009-02-27 16:47:48 -08003071hand side must match the number of values. For instance, If
3072<code>f</code> is a function returning two values,
3073</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003074
Robert Griesemerc2d55862009-02-19 16:49:10 -08003075<pre>
3076x, y = f()
3077</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003078
Rob Pike96750f12009-02-27 16:47:48 -08003079<p>
3080assigns the first value to <code>x</code> and the second to <code>y</code>.
3081</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003082
Rob Pike96750f12009-02-27 16:47:48 -08003083<p>
3084In the second form, the number of operands on the left must equal the number
Russ Cox5958dd62009-03-04 17:19:21 -08003085of expressions on the right, each of which must be single-valued.
3086The expressions on the right are evaluated before assigning to
3087any of the operands on the left, but otherwise the evaluation
3088order is unspecified.
Rob Pike96750f12009-02-27 16:47:48 -08003089</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003090
Robert Griesemerc2d55862009-02-19 16:49:10 -08003091<pre>
Rob Pike96750f12009-02-27 16:47:48 -08003092a, b = b, a // exchange a and b
Robert Griesemerc2d55862009-02-19 16:49:10 -08003093</pre>
Rob Pike96750f12009-02-27 16:47:48 -08003094
3095<p>
3096In assignments, the type of each value must be assignment compatible
3097(§Assignment compatibility) with the type of the
3098operand to which it is assigned.
3099</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003100
3101
Robert Griesemerc2d55862009-02-19 16:49:10 -08003102<h3>If statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003103
Rob Pike96750f12009-02-27 16:47:48 -08003104<p>
3105"If" statements specify the conditional execution of two branches
3106according to the value of a boolean expression. If the expression
3107evaluates to true, the "if" branch is executed, otherwise, if
3108present, the "else" branch is executed. A missing condition
3109is equivalent to <code>true</code>.
3110</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003111
Rob Pikeff70f092009-02-20 13:36:14 -08003112<pre class="grammar">
Rob Pike11417162009-03-24 17:45:53 -07003113IfStmt = "if" [ [ SimpleStmt ] ";" ] [ Expression ] Block [ "else" Statement ] .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003114</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003115
Robert Griesemerc2d55862009-02-19 16:49:10 -08003116<pre>
3117if x > 0 {
3118 return true;
3119}
3120</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003121
Russ Cox5958dd62009-03-04 17:19:21 -08003122<p>
3123An "if" statement may include a simple statement before the expression.
3124The scope of any variables declared by that statement
3125extends to the end of the "if" statement
Rob Pike96750f12009-02-27 16:47:48 -08003126and the variables are initialized once before the statement is entered.
Russ Cox5958dd62009-03-04 17:19:21 -08003127</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003128
Robert Griesemerc2d55862009-02-19 16:49:10 -08003129<pre>
3130if x := f(); x < y {
3131 return x;
3132} else if x > z {
3133 return z;
3134} else {
3135 return y;
3136}
3137</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003138
3139
Robert Griesemerc2d55862009-02-19 16:49:10 -08003140<h3>Switch statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003141
Rob Pike96750f12009-02-27 16:47:48 -08003142<p>
3143"Switch" statements provide multi-way execution.
Rob Pike5a578492009-03-17 16:48:35 -07003144An expression or type specifier is compared to the "cases"
3145inside the "switch" to determine which branch
3146to execute.
Rob Pikeafee1c52009-03-20 17:41:25 -07003147</p>
Robert Griesemer091cba82009-03-19 08:39:40 -07003148
3149<pre class="grammar">
Rob Pike11417162009-03-24 17:45:53 -07003150SwitchStmt = ExprSwitchStmt | TypeSwitchStmt .
Robert Griesemer091cba82009-03-19 08:39:40 -07003151</pre>
3152
Rob Pikeafee1c52009-03-20 17:41:25 -07003153<p>
Rob Pike5a578492009-03-17 16:48:35 -07003154There are two forms: expression switches and type switches.
Rob Pike5a578492009-03-17 16:48:35 -07003155In an expression switch, the cases contain expressions that are compared
3156against the value of the switch expression.
3157In a type switch, the cases contain types that are compared against the
3158type of a specially annotated switch expression.
Rob Pike96750f12009-02-27 16:47:48 -08003159</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003160
Rob Pike70c1a102009-03-18 19:23:59 -07003161<h4>Expression switches</h4>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003162
Rob Pike96750f12009-02-27 16:47:48 -08003163<p>
Rob Pike5a578492009-03-17 16:48:35 -07003164In an expression switch,
3165the switch expression is evaluated and
3166the case expressions, which need not be constants,
3167are evaluated top-to-bottom; the first one that equals the
3168switch expression
Rob Pike96750f12009-02-27 16:47:48 -08003169triggers execution of the statements of the associated case;
3170the other cases are skipped.
Rob Pike5a578492009-03-17 16:48:35 -07003171If no case matches and there is a "default" case,
3172its statements are executed.
Rob Pike96750f12009-02-27 16:47:48 -08003173There can be at most one default case and it may appear anywhere in the
3174"switch" statement.
Rob Pike70c1a102009-03-18 19:23:59 -07003175A missing expression is equivalent to
3176the expression <code>true</code>.
Rob Pike96750f12009-02-27 16:47:48 -08003177</p>
Rob Pike70c1a102009-03-18 19:23:59 -07003178
3179<pre class="grammar">
Rob Pike11417162009-03-24 17:45:53 -07003180ExprSwitchStmt = "switch" [ [ SimpleStmt ] ";" ] [ Expression ] "{" { ExprCaseClause } "}" .
Robert Griesemer091cba82009-03-19 08:39:40 -07003181ExprCaseClause = ExprSwitchCase ":" [ StatementList ] .
3182ExprSwitchCase = "case" ExpressionList | "default" .
Rob Pike70c1a102009-03-18 19:23:59 -07003183</pre>
3184
Rob Pike96750f12009-02-27 16:47:48 -08003185<p>
3186In a case or default clause,
3187the last statement only may be a "fallthrough" statement
Russ Cox5958dd62009-03-04 17:19:21 -08003188(§Fallthrough statement) to
Rob Pike96750f12009-02-27 16:47:48 -08003189indicate that control should flow from the end of this clause to
Robert Griesemeraed247f2008-10-08 17:05:30 -07003190the first statement of the next clause.
Rob Pike96750f12009-02-27 16:47:48 -08003191Otherwise control flows to the end of the "switch" statement.
3192</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08003193<p>
Rob Pike70c1a102009-03-18 19:23:59 -07003194Each case clause acts as a block for scoping purposes
Russ Cox5958dd62009-03-04 17:19:21 -08003195(§Declarations and scope rules).
Rob Pike96750f12009-02-27 16:47:48 -08003196</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08003197<p>
Russ Cox5958dd62009-03-04 17:19:21 -08003198A "switch" statement may include a simple statement before the
Rob Pike96750f12009-02-27 16:47:48 -08003199expression.
Russ Cox5958dd62009-03-04 17:19:21 -08003200The scope of any variables declared by that statement
3201extends to the end of the "switch" statement
3202and the variables are initialized once before the statement is entered.
Rob Pike96750f12009-02-27 16:47:48 -08003203</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003204
Robert Griesemerc2d55862009-02-19 16:49:10 -08003205<pre>
3206switch tag {
Russ Cox5958dd62009-03-04 17:19:21 -08003207default: s3()
3208case 0, 1, 2, 3: s1()
3209case 4, 5, 6, 7: s2()
Robert Griesemerc2d55862009-02-19 16:49:10 -08003210}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003211
Rob Pike96750f12009-02-27 16:47:48 -08003212switch x := f(); {
Russ Cox5958dd62009-03-04 17:19:21 -08003213case x &lt; 0: return -x
3214default: return x
Robert Griesemerc2d55862009-02-19 16:49:10 -08003215}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003216
Rob Pike96750f12009-02-27 16:47:48 -08003217switch { // missing expression means "true"
Russ Cox5958dd62009-03-04 17:19:21 -08003218case x < y: f1();
3219case x < z: f2();
3220case x == 4: f3();
Robert Griesemerc2d55862009-02-19 16:49:10 -08003221}
3222</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003223
Rob Pike70c1a102009-03-18 19:23:59 -07003224<h4>Type switches</h4>
3225
Rob Pike5a578492009-03-17 16:48:35 -07003226<p>
Rob Pike70c1a102009-03-18 19:23:59 -07003227A type switch compares types rather than values. It is otherwise similar
3228to an expression switch. It is introduced by special
3229notation in the form of a simple declaration whose right hand side
Rob Pikef5387602009-03-30 16:08:41 -07003230has the form of a type assertion (§Type assertions)
Rob Pike70c1a102009-03-18 19:23:59 -07003231using the reserved word <code>type</code> rather than an actual type.
3232Cases then match literal types against the dynamic type of the expression
Rob Pikef5387602009-03-30 16:08:41 -07003233in the type assertion.
Rob Pike5a578492009-03-17 16:48:35 -07003234</p>
3235
Rob Pike70c1a102009-03-18 19:23:59 -07003236<pre class="grammar">
Rob Pike11417162009-03-24 17:45:53 -07003237TypeSwitchStmt = "switch" [ [ SimpleStmt ] ";" ] TypeSwitchGuard "{" { TypeCaseClause } "}" .
Robert Griesemer091cba82009-03-19 08:39:40 -07003238TypeSwitchGuard = identifier ":=" Expression "." "(" "type" ")" .
3239TypeCaseClause = TypeSwitchCase ":" [ StatementList ] .
Rob Pike94b67eb2009-03-24 17:40:47 -07003240TypeSwitchCase = "case" ( type | "nil" ) | "default" .
Rob Pike5a578492009-03-17 16:48:35 -07003241</pre>
3242
3243<p>
Rob Pike94b67eb2009-03-24 17:40:47 -07003244If the interface value equals <code>nil</code>,
3245only an explict <code>nil</code> case or "default"
3246case will execute.
3247</p>
3248
3249<p>
Rob Pike5a578492009-03-17 16:48:35 -07003250Given a function <code>f</code>
3251that returns a value of interface type,
Rob Pike70c1a102009-03-18 19:23:59 -07003252the following type switch:
Rob Pike5a578492009-03-17 16:48:35 -07003253</p>
3254
3255<pre>
3256switch i := f().(type) {
Rob Pike94b67eb2009-03-24 17:40:47 -07003257case nil:
3258 printString("f() returns nil");
Rob Pike5a578492009-03-17 16:48:35 -07003259case int:
3260 printInt(i); // i is an int
3261case float:
3262 printFloat(i); // i is a float
Rob Pike70c1a102009-03-18 19:23:59 -07003263case func(int) float:
3264 printFunction(i); // i is a function
Rob Pike5a578492009-03-17 16:48:35 -07003265default:
3266 printString("don't know the type");
3267}
3268</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003269
Rob Pike70c1a102009-03-18 19:23:59 -07003270<p>
3271could be rewritten:
3272</p>
3273
3274<pre>
3275v := f();
Rob Pike94b67eb2009-03-24 17:40:47 -07003276if v == nil {
3277 printString("f() returns nil");
3278} else if i, is_int := v.(int); is_int {
Rob Pike70c1a102009-03-18 19:23:59 -07003279 printInt(i); // i is an int
3280} else if i, is_float := v.(float); is_float {
3281 printFloat(i); // i is a float
3282} else if i, is_func := v.(func(int) float); is_func {
3283 printFunction(i); // i is a function
3284} else {
3285 printString("don't know the type");
3286}
3287</pre>
3288
3289<p>
3290In a type switch, the guard is mandatory,
3291there can be only one type per "case", and
3292the "fallthrough" statement is not allowed.
3293</p>
3294
Robert Griesemerc2d55862009-02-19 16:49:10 -08003295<h3>For statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003296
Rob Pike96750f12009-02-27 16:47:48 -08003297<p>
3298A "for" statement specifies repeated execution of a block. The iteration is
3299controlled by a condition, a "for" clause, or a "range" clause.
3300</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003301
Rob Pikeff70f092009-02-20 13:36:14 -08003302<pre class="grammar">
Rob Pike11417162009-03-24 17:45:53 -07003303ForStmt = "for" [ Condition | ForClause | RangeClause ] Block .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003304Condition = Expression .
3305</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003306
Rob Pike96750f12009-02-27 16:47:48 -08003307<p>
3308In its simplest form, a "for" statement specifies the repeated execution of
3309a block as long as a boolean condition evaluates to true.
3310The condition is evaluated before each iteration.
3311If the condition is absent, it is equivalent to <code>true</code>.
3312</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003313
Robert Griesemerc2d55862009-02-19 16:49:10 -08003314<pre>
3315for a &lt; b {
3316 a *= 2
3317}
3318</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003319
Rob Pike96750f12009-02-27 16:47:48 -08003320<p>
3321A "for" statement with a "for" clause is also controlled by its condition, but
3322additionally it may specify an <i>init</i>
3323and a <i>post</i> statement, such as an assignment,
3324an increment or decrement statement. The init statement (but not the post
3325statement) may also be a short variable declaration; the scope of the variables
3326it declares ends at the end of the statement
Russ Cox5958dd62009-03-04 17:19:21 -08003327(§Declarations and scope rules).
Rob Pike96750f12009-02-27 16:47:48 -08003328</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003329
Rob Pikeff70f092009-02-20 13:36:14 -08003330<pre class="grammar">
Rob Pike11417162009-03-24 17:45:53 -07003331ForClause = [ InitStmt ] ";" [ Condition ] ";" [ PostStmt ] .
3332InitStmt = SimpleStmt .
3333PostStmt = SimpleStmt .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003334</pre>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003335
Robert Griesemerc2d55862009-02-19 16:49:10 -08003336<pre>
3337for i := 0; i < 10; i++ {
3338 f(i)
3339}
3340</pre>
Russ Cox5958dd62009-03-04 17:19:21 -08003341
Robert Griesemerc2d55862009-02-19 16:49:10 -08003342<p>
Rob Pike96750f12009-02-27 16:47:48 -08003343If non-empty, the init statement is executed once before evaluating the
3344condition for the first iteration;
3345the post statement is executed after each execution of the block (and
3346only if the block was executed).
3347Any element of the "for" clause may be empty but the semicolons are
3348required unless there is only a condition.
3349If the condition is absent, it is equivalent to <code>true</code>.
3350</p>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003351
Robert Griesemerc2d55862009-02-19 16:49:10 -08003352<pre>
3353for ; cond ; { S() } is the same as for cond { S() }
3354for true { S() } is the same as for { S() }
3355</pre>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003356
Rob Pike96750f12009-02-27 16:47:48 -08003357<p>
3358A "for" statement with a "range" clause
Rob Pike7aee71b2009-04-15 20:28:25 -07003359iterates through all entries of an array, slice, string or map,
3360or values received on a channel.
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003361For each entry it first assigns the current index or key to an iteration
3362variable - or the current (index, element) or (key, value) pair to a pair
Rob Pike96750f12009-02-27 16:47:48 -08003363of iteration variables - and then executes the block.
3364</p>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003365
Rob Pikeff70f092009-02-20 13:36:14 -08003366<pre class="grammar">
Rob Pikeb3408792009-04-15 20:51:17 -07003367RangeClause = ExpressionList ( "=" | ":=" ) "range" Expression .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003368</pre>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003369
Robert Griesemerc2d55862009-02-19 16:49:10 -08003370<p>
Rob Pike7aee71b2009-04-15 20:28:25 -07003371The type of the right-hand expression in the "range" clause must be an
3372array, slice, string or map, or a pointer to an array, slice, string or map;
Rob Pike94b67eb2009-03-24 17:40:47 -07003373or it may be a channel.
Rob Pike7aee71b2009-04-15 20:28:25 -07003374Except for channels,
Rob Pikeb3408792009-04-15 20:51:17 -07003375the identifier list must contain one or two expressions
3376(as in assignments, these must be a
3377variable, pointer indirection, field selector, or index expression)
3378denoting the
Rob Pike96750f12009-02-27 16:47:48 -08003379iteration variables. On each iteration,
Rob Pike7aee71b2009-04-15 20:28:25 -07003380the first variable is set to the string, array or slice index or
Rob Pike96750f12009-02-27 16:47:48 -08003381map key, and the second variable, if present, is set to the corresponding
Rob Pike7aee71b2009-04-15 20:28:25 -07003382string or array element or map value.
Rob Pike96750f12009-02-27 16:47:48 -08003383The types of the array or slice index (always <code>int</code>)
3384and element, or of the map key and value respectively,
3385must be assignment compatible to the iteration variables.
3386</p>
3387<p>
Rob Pike7aee71b2009-04-15 20:28:25 -07003388For strings, the "range" clause iterates over the Unicode code points
3389in the string. On successive iterations, the index variable will be the
Rob Pike55faa5f2009-04-15 21:49:50 -07003390index of successive UTF-8-encoded code points in the string, and
Rob Pike7aee71b2009-04-15 20:28:25 -07003391the second variable, of type <code>int</code>, will be the value of
3392the corresponding code point. If the iteration encounters an invalid
3393UTF-8 sequence, the second variable will be <code>0xFFFD</code>,
3394the Unicode replacement character, and the next iteration will advance
3395a single byte in the string.
3396</p>
3397<p>
Rob Pike94b67eb2009-03-24 17:40:47 -07003398For channels, the identifier list must contain one identifier.
3399The iteration recieves values sent on the channel until the channel is closed;
3400it does not process the zero value sent before the channel is closed.
3401</p>
3402<p>
Rob Pike96750f12009-02-27 16:47:48 -08003403The iteration variables may be declared by the "range" clause (":="), in which
Russ Cox5958dd62009-03-04 17:19:21 -08003404case their scope ends at the end of the "for" statement (§Declarations and
Rob Pike96750f12009-02-27 16:47:48 -08003405scope rules). In this case their types are set to
Russ Cox5958dd62009-03-04 17:19:21 -08003406<code>int</code> and the array element type, or the map key and value types, respectively.
Rob Pike96750f12009-02-27 16:47:48 -08003407If the iteration variables are declared outside the "for" statement,
3408after execution their values will be those of the last iteration.
3409</p>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003410
Robert Griesemerc2d55862009-02-19 16:49:10 -08003411<pre>
3412var a [10]string;
Rob Pike426335f2009-03-02 17:52:52 -08003413m := map[string]int{"mon":0, "tue":1, "wed":2, "thu":3, "fri":4, "sat":5, "sun":6};
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003414
Robert Griesemerc2d55862009-02-19 16:49:10 -08003415for i, s := range a {
3416 // type of i is int
3417 // type of s is string
3418 // s == a[i]
3419 g(i, s)
3420}
3421
3422var key string;
3423var val interface {}; // value type of m is assignment-compatible to val
3424for key, value = range m {
3425 h(key, value)
3426}
3427// key == last map key encountered in iteration
3428// val == map[key]
3429</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003430
Rob Pike96750f12009-02-27 16:47:48 -08003431<p>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003432If map entries that have not yet been processed are deleted during iteration,
3433they will not be processed. If map entries are inserted during iteration, the
Russ Cox5958dd62009-03-04 17:19:21 -08003434behavior is implementation-dependent, but each entry will be processed at most once.
Rob Pike96750f12009-02-27 16:47:48 -08003435</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003436
Robert Griesemerc2d55862009-02-19 16:49:10 -08003437<h3>Go statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003438
Rob Pike96750f12009-02-27 16:47:48 -08003439<p>
Russ Cox5958dd62009-03-04 17:19:21 -08003440A "go" statement starts the execution of a function or method call
Rob Pike96750f12009-02-27 16:47:48 -08003441as an independent concurrent thread of control, or <i>goroutine</i>,
3442within the same address space.
3443</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003444
Rob Pikeff70f092009-02-20 13:36:14 -08003445<pre class="grammar">
Rob Pike11417162009-03-24 17:45:53 -07003446GoStmt = "go" Expression .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003447</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003448
Rob Pike96750f12009-02-27 16:47:48 -08003449<p>
3450The expression must be a call, and
3451unlike with a regular call, program execution does not wait
Robert Griesemerb9f8b9c2008-09-26 13:38:38 -07003452for the invoked function to complete.
Rob Pike96750f12009-02-27 16:47:48 -08003453</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003454
Robert Griesemerc2d55862009-02-19 16:49:10 -08003455<pre>
3456go Server()
3457go func(ch chan <- bool) { for { sleep(10); ch <- true; }} (c)
3458</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003459
3460
Robert Griesemerc2d55862009-02-19 16:49:10 -08003461<h3>Select statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003462
Rob Pike96750f12009-02-27 16:47:48 -08003463<p>
3464A "select" statement chooses which of a set of possible communications
3465will proceed. It looks similar to a "switch" statement but with the
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003466cases all referring to communication operations.
Rob Pike96750f12009-02-27 16:47:48 -08003467</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003468
Rob Pikeff70f092009-02-20 13:36:14 -08003469<pre class="grammar">
Rob Pike11417162009-03-24 17:45:53 -07003470SelectStmt = "select" "{" { CommClause } "}" .
Russ Cox5958dd62009-03-04 17:19:21 -08003471CommClause = CommCase ":" StatementList .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003472CommCase = "case" ( SendExpr | RecvExpr) | "default" .
3473SendExpr = Expression "&lt;-" Expression .
3474RecvExpr = [ Expression ( "=" | ":=" ) ] "&lt;-" Expression .
3475</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003476
Rob Pike96750f12009-02-27 16:47:48 -08003477<p>
Robert Griesemer347cf672008-10-03 14:04:28 -07003478Each communication clause acts as a block for the purpose of scoping
3479(§Declarations and scope rules).
Rob Pike96750f12009-02-27 16:47:48 -08003480</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08003481<p>
Rob Pike96750f12009-02-27 16:47:48 -08003482For all the send and receive expressions in the "select"
3483statement, the channel expression is evaluated. Any expressions
Rob Pike569a1072008-10-03 11:18:45 -07003484that appear on the right hand side of send expressions are also
3485evaluated. If any of the resulting channels can proceed, one is
3486chosen and the corresponding communication and statements are
3487evaluated. Otherwise, if there is a default case, that executes;
Rob Pikecd368a22008-10-02 10:37:12 -07003488if not, the statement blocks until one of the communications can
Rob Pike569a1072008-10-03 11:18:45 -07003489complete. The channels and send expressions are not re-evaluated.
Rob Pike96750f12009-02-27 16:47:48 -08003490A channel pointer may be <code>nil</code>,
3491which is equivalent to that case not
3492being present in the select statement
3493except, if a send, its expression is still evaluated.
3494</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08003495<p>
Rob Pike569a1072008-10-03 11:18:45 -07003496Since all the channels and send expressions are evaluated, any side
3497effects in that evaluation will occur for all the communications
Rob Pike96750f12009-02-27 16:47:48 -08003498in the "select" statement.
3499</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08003500<p>
Rob Pike96750f12009-02-27 16:47:48 -08003501If multiple cases can proceed, a uniform fair choice is made to decide
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003502which single communication will execute.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003503<p>
Rob Pike96750f12009-02-27 16:47:48 -08003504The receive case may declare a new variable using a short variable declaration
3505(§Short variable declarations).
3506The scope of such variables continues to the end of the
3507respective case's statements.
3508</p>
Robert Griesemer2902a822008-09-17 13:57:11 -07003509
Robert Griesemerc2d55862009-02-19 16:49:10 -08003510<pre>
3511var c, c1, c2 chan int;
3512var i1, i2 int;
3513select {
3514case i1 = &lt;-c1:
3515 print("received ", i1, " from c1\n");
3516case c2 &lt;- i2:
3517 print("sent ", i2, " to c2\n");
3518default:
3519 print("no communication\n");
3520}
3521
3522for { // send random sequence of bits to c
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003523 select {
Robert Griesemerc2d55862009-02-19 16:49:10 -08003524 case c &lt;- 0: // note: no statement, no fallthrough, no folding of cases
3525 case c &lt;- 1:
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003526 }
Robert Griesemerc2d55862009-02-19 16:49:10 -08003527}
Robert Griesemerc2d55862009-02-19 16:49:10 -08003528</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003529
Robert Griesemerc2d55862009-02-19 16:49:10 -08003530<font color=red>
Robert Griesemer2902a822008-09-17 13:57:11 -07003531TODO: Make semantics more precise.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003532</font>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003533
3534
Robert Griesemerc2d55862009-02-19 16:49:10 -08003535<h3>Return statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003536
Rob Pike96750f12009-02-27 16:47:48 -08003537<p>
3538A "return" statement terminates execution of the containing function
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003539and optionally provides a result value or values to the caller.
Rob Pike96750f12009-02-27 16:47:48 -08003540</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003541
Rob Pikeff70f092009-02-20 13:36:14 -08003542<pre class="grammar">
Rob Pike11417162009-03-24 17:45:53 -07003543ReturnStmt = "return" [ ExpressionList ] .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003544</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003545
Rob Pike96750f12009-02-27 16:47:48 -08003546<pre>
3547func procedure() {
3548 return
3549}
3550</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003551
Rob Pike96750f12009-02-27 16:47:48 -08003552<p>
3553There are two ways to return values from a function with a result
3554type. The first is to explicitly list the return value or values
Russ Cox5958dd62009-03-04 17:19:21 -08003555in the "return" statement.
3556Normally, the expressions
Rob Pike96750f12009-02-27 16:47:48 -08003557must be single-valued and assignment-compatible to the elements of
3558the result type of the function.
3559</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003560
Robert Griesemerc2d55862009-02-19 16:49:10 -08003561<pre>
3562func simple_f() int {
Rob Pike96750f12009-02-27 16:47:48 -08003563 return 2
Robert Griesemerc2d55862009-02-19 16:49:10 -08003564}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003565
Robert Griesemerc2d55862009-02-19 16:49:10 -08003566func complex_f1() (re float, im float) {
Rob Pike96750f12009-02-27 16:47:48 -08003567 return -7.0, -4.0
Robert Griesemerc2d55862009-02-19 16:49:10 -08003568}
3569</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003570
Rob Pike96750f12009-02-27 16:47:48 -08003571<p>
3572However, if the expression list in the "return" statement is a single call
3573to a multi-valued function, the values returned from the called function
3574will be returned from this one. The result types of the current function
3575and the called function must match.
3576</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003577
Robert Griesemerc2d55862009-02-19 16:49:10 -08003578<pre>
3579func complex_f2() (re float, im float) {
Rob Pike96750f12009-02-27 16:47:48 -08003580 return complex_f1()
3581}
3582</pre>
3583
3584<p>
Russ Cox5958dd62009-03-04 17:19:21 -08003585The second way to return values is to use the elements of the
Rob Pike96750f12009-02-27 16:47:48 -08003586result list of the function as variables. When the function begins
3587execution, these variables are initialized to the zero values for
3588their type (§The zero value). The function can assign them as
3589necessary; if the "return" provides no values, those of the variables
3590will be returned to the caller.
3591</p>
3592
3593<pre>
3594func complex_f3() (re float, im float) {
Robert Griesemerc2d55862009-02-19 16:49:10 -08003595 re = 7.0;
3596 im = 4.0;
3597 return;
3598}
3599</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003600
Russ Cox5958dd62009-03-04 17:19:21 -08003601<p>
3602TODO: Define when return is required.
3603</p>
3604
Robert Griesemerc2d55862009-02-19 16:49:10 -08003605<h3>Break statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003606
Rob Pike96750f12009-02-27 16:47:48 -08003607<p>
3608A "break" statement terminates execution of the innermost
3609"for", "switch" or "select" statement.
3610</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003611
Rob Pikeff70f092009-02-20 13:36:14 -08003612<pre class="grammar">
Rob Pike11417162009-03-24 17:45:53 -07003613BreakStmt = "break" [ Label ].
Robert Griesemerc2d55862009-02-19 16:49:10 -08003614</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003615
Rob Pike96750f12009-02-27 16:47:48 -08003616<p>
3617If there is a label, it must be that of an enclosing
3618"for", "switch" or "select" statement, and that is the one whose execution
3619terminates
3620(§For statements, §Switch statements, §Select statements).
3621</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003622
Robert Griesemerc2d55862009-02-19 16:49:10 -08003623<pre>
3624L: for i < n {
3625 switch i {
Rob Pike96750f12009-02-27 16:47:48 -08003626 case 5: break L
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003627 }
Robert Griesemerc2d55862009-02-19 16:49:10 -08003628}
3629</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003630
Robert Griesemerc2d55862009-02-19 16:49:10 -08003631<h3>Continue statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003632
Rob Pike96750f12009-02-27 16:47:48 -08003633<p>
3634A "continue" statement begins the next iteration of the
3635innermost "for" loop at the post statement (§For statements).
3636</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003637
Rob Pikeff70f092009-02-20 13:36:14 -08003638<pre class="grammar">
Rob Pike11417162009-03-24 17:45:53 -07003639ContinueStmt = "continue" [ Label ].
Robert Griesemerc2d55862009-02-19 16:49:10 -08003640</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003641
Rob Pike96750f12009-02-27 16:47:48 -08003642<p>
3643The optional label is analogous to that of a "break" statement.
3644</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003645
Robert Griesemerc2d55862009-02-19 16:49:10 -08003646<h3>Goto statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003647
Rob Pike96750f12009-02-27 16:47:48 -08003648<p>
3649A "goto" statement transfers control to the statement with the corresponding label.
3650</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003651
Rob Pikeff70f092009-02-20 13:36:14 -08003652<pre class="grammar">
Rob Pike11417162009-03-24 17:45:53 -07003653GotoStmt = "goto" Label .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003654</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003655
Robert Griesemerc2d55862009-02-19 16:49:10 -08003656<pre>
3657goto Error
3658</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003659
Rob Pike96750f12009-02-27 16:47:48 -08003660<p>
3661Executing the "goto" statement must not cause any variables to come into
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003662scope that were not already in scope at the point of the goto. For
3663instance, this example:
Rob Pike96750f12009-02-27 16:47:48 -08003664</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003665
Robert Griesemerc2d55862009-02-19 16:49:10 -08003666<pre>
3667goto L; // BAD
3668v := 3;
3669L:
3670</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003671
Rob Pike96750f12009-02-27 16:47:48 -08003672<p>
3673is erroneous because the jump to label <code>L</code> skips
3674the creation of <code>v</code>.
Russ Cox5958dd62009-03-04 17:19:21 -08003675(TODO: Eliminate in favor of used and not set errors?)
Rob Pike96750f12009-02-27 16:47:48 -08003676</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003677
Robert Griesemerc2d55862009-02-19 16:49:10 -08003678<h3>Fallthrough statements</h3>
Robert Griesemeraed247f2008-10-08 17:05:30 -07003679
Rob Pike96750f12009-02-27 16:47:48 -08003680<p>
3681A "fallthrough" statement transfers control to the first statement of the
Robert Griesemer091cba82009-03-19 08:39:40 -07003682next case clause in a expression "switch" statement (§Expression switches). It may
3683be used only as the final non-empty statement in a case or default clause in an
3684expression "switch" statement.
Rob Pike96750f12009-02-27 16:47:48 -08003685</p>
Robert Griesemeraed247f2008-10-08 17:05:30 -07003686
Rob Pikeff70f092009-02-20 13:36:14 -08003687<pre class="grammar">
Rob Pike11417162009-03-24 17:45:53 -07003688FallthroughStmt = "fallthrough" .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003689</pre>
Robert Griesemeraed247f2008-10-08 17:05:30 -07003690
3691
Robert Griesemerc2d55862009-02-19 16:49:10 -08003692<h3>Defer statements</h3>
Robert Griesemer4a903e02009-01-27 09:29:40 -08003693
Rob Pike96750f12009-02-27 16:47:48 -08003694<p>
3695A "defer" statement invokes a function whose execution is deferred to the moment
3696the surrounding function returns.
3697</p>
Robert Griesemer4a903e02009-01-27 09:29:40 -08003698
Rob Pikeff70f092009-02-20 13:36:14 -08003699<pre class="grammar">
Rob Pike11417162009-03-24 17:45:53 -07003700DeferStmt = "defer" Expression .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003701</pre>
Robert Griesemer4a903e02009-01-27 09:29:40 -08003702
Rob Pike96750f12009-02-27 16:47:48 -08003703<p>
3704The expression must be a function or method call.
3705Each time the "defer" statement
Robert Griesemer7471eab2009-01-27 14:51:24 -08003706executes, the parameters to the function call are evaluated and saved anew but the
Robert Griesemer4a903e02009-01-27 09:29:40 -08003707function is not invoked. Immediately before the innermost function surrounding
Rob Pike96750f12009-02-27 16:47:48 -08003708the "defer" statement returns, but after its return value (if any) is evaluated,
Robert Griesemer4a903e02009-01-27 09:29:40 -08003709each deferred function is executed with its saved parameters. Deferred functions
3710are executed in LIFO order.
Rob Pike96750f12009-02-27 16:47:48 -08003711</p>
Robert Griesemer4a903e02009-01-27 09:29:40 -08003712
Robert Griesemerc2d55862009-02-19 16:49:10 -08003713<pre>
3714lock(l);
3715defer unlock(l); // unlocking happens before surrounding function returns
Robert Griesemer4a903e02009-01-27 09:29:40 -08003716
Robert Griesemerc2d55862009-02-19 16:49:10 -08003717// prints 3 2 1 0 before surrounding function returns
3718for i := 0; i &lt;= 3; i++ {
3719 defer fmt.Print(i);
3720}
3721</pre>
Robert Griesemer4a903e02009-01-27 09:29:40 -08003722
Rob Pikeff70f092009-02-20 13:36:14 -08003723<hr/>
Robert Griesemer4a903e02009-01-27 09:29:40 -08003724
Rob Pikef27e9f02009-02-23 19:22:05 -08003725<h2>Predeclared functions</h2>
Robert Griesemerc2d55862009-02-19 16:49:10 -08003726<ul>
3727 <li>cap
Rob Pike94b67eb2009-03-24 17:40:47 -07003728 <li>close
3729 <li>closed
Robert Griesemerc2d55862009-02-19 16:49:10 -08003730 <li>len
3731 <li>make
3732 <li>new
3733 <li>panic
3734 <li>panicln
3735 <li>print
3736 <li>println
Robert Griesemerc2d55862009-02-19 16:49:10 -08003737</ul>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003738
Robert Griesemerc2d55862009-02-19 16:49:10 -08003739<h3>Length and capacity</h3>
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -07003740
Rob Pikeff70f092009-02-20 13:36:14 -08003741<pre class="grammar">
Rob Pikedf3183f2009-02-26 16:37:23 -08003742Call Argument type Result
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -07003743
Robert Griesemerc2d55862009-02-19 16:49:10 -08003744len(s) string, *string string length (in bytes)
Rob Pikedf3183f2009-02-26 16:37:23 -08003745 [n]T, *[n]T array length (== n)
3746 []T, *[]T slice length
3747 map[K]T, *map[K]T map length
3748 chan T number of elements in channel buffer
Robert Griesemer4dc25282008-09-09 10:37:19 -07003749
Robert Griesemerc2d55862009-02-19 16:49:10 -08003750cap(s) []T, *[]T capacity of s
Rob Pikedf3183f2009-02-26 16:37:23 -08003751 map[K]T, *map[K]T capacity of s
3752 chan T channel buffer capacity
Robert Griesemerc2d55862009-02-19 16:49:10 -08003753</pre>
Robert Griesemer4dc25282008-09-09 10:37:19 -07003754
Robert Griesemerc2d55862009-02-19 16:49:10 -08003755<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08003756The type of the result is always <code>int</code> and the
3757implementation guarantees that
3758the result always fits into an <code>int</code>.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003759<p>
Robert Griesemer633957b2009-01-06 13:23:20 -08003760The capacity of a slice or map is the number of elements for which there is
Rob Pikedf3183f2009-02-26 16:37:23 -08003761space allocated in the underlying array (for a slice) or map. For a slice
3762<code>s</code>, at any time the following relationship holds:
Robert Griesemer633957b2009-01-06 13:23:20 -08003763
Robert Griesemerc2d55862009-02-19 16:49:10 -08003764<pre>
37650 <= len(s) <= cap(s)
3766</pre>
Robert Griesemer667ef6c2008-09-10 13:00:32 -07003767
Robert Griesemer4dc25282008-09-09 10:37:19 -07003768
Robert Griesemerc2d55862009-02-19 16:49:10 -08003769<h3>Conversions</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003770
Rob Pike96750f12009-02-27 16:47:48 -08003771<p>
Rob Pike96750f12009-02-27 16:47:48 -08003772Conversions look like function calls of the form
3773</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003774
Rob Pikeff70f092009-02-20 13:36:14 -08003775<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08003776T(value)
3777</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003778
Rob Pike96750f12009-02-27 16:47:48 -08003779<p>
Russ Coxe8b43192009-03-03 08:10:25 -08003780where <code>T</code> is a type
3781and <code>value</code> is an expression
3782that can be converted to a value
Rob Pikedf3183f2009-02-26 16:37:23 -08003783of result type <code>T</code>.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003784<p>
Robert Griesemer133c68e2008-09-26 14:04:21 -07003785The following conversion rules apply:
Rob Pike96750f12009-02-27 16:47:48 -08003786</p>
3787<ul>
3788<li>
Russ Coxe8b43192009-03-03 08:10:25 -080037891) Between equal types. The conversion always succeeds.
3790</li>
3791<li>
37922) Between integer types. If the value is a signed quantity, it is
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003793sign extended to implicit infinite precision; otherwise it is zero
3794extended. It is then truncated to fit in the result type size.
Rob Pike96750f12009-02-27 16:47:48 -08003795For example, <code>uint32(int8(0xFF))</code> is <code>0xFFFFFFFF</code>.
3796The conversion always yields a valid value; there is no signal for overflow.
3797</li>
3798<li>
Russ Coxe8b43192009-03-03 08:10:25 -080037993) Between integer and floating point types, or between floating point
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003800types. To avoid overdefining the properties of the conversion, for
Robert Griesemer434c6052008-11-07 13:34:37 -08003801now it is defined as a ``best effort'' conversion. The conversion
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003802always succeeds but the value may be a NaN or other problematic
Robert Griesemerc2d55862009-02-19 16:49:10 -08003803result. <font color=red>TODO: clarify?</font>
Rob Pike96750f12009-02-27 16:47:48 -08003804</li>
3805<li>
Russ Coxe8b43192009-03-03 08:10:25 -080038064) Strings permit two special conversions.
Rob Pike96750f12009-02-27 16:47:48 -08003807</li>
3808<li>
Russ Coxe8b43192009-03-03 08:10:25 -080038094a) Converting an integer value yields a string containing the UTF-8
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003810representation of the integer.
3811
Robert Griesemerc2d55862009-02-19 16:49:10 -08003812<pre>
3813string(0x65e5) // "\u65e5"
3814</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003815
Rob Pike96750f12009-02-27 16:47:48 -08003816</li>
3817<li>
Russ Coxbcdc2472009-04-16 23:06:48 -070038184b) Converting a slice of bytes yields a string whose successive
3819bytes are those of the slice.
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003820
Robert Griesemerc2d55862009-02-19 16:49:10 -08003821<pre>
Rob Pike426335f2009-03-02 17:52:52 -08003822string([]byte{'h', 'e', 'l', 'l', 'o'}) // "hello"
Robert Griesemerc2d55862009-02-19 16:49:10 -08003823</pre>
Rob Pike96750f12009-02-27 16:47:48 -08003824</li>
3825</ul>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003826
Rob Pikedf3183f2009-02-26 16:37:23 -08003827<p>
Rob Pike96750f12009-02-27 16:47:48 -08003828There is no linguistic mechanism to convert between pointers and integers.
3829The <code>unsafe</code> package
3830implements this functionality under
3831restricted circumstances (§Package <code>unsafe</code>).
Rob Pikedf3183f2009-02-26 16:37:23 -08003832</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003833
3834
Robert Griesemerc2d55862009-02-19 16:49:10 -08003835<h3>Allocation</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003836
Rob Pike96750f12009-02-27 16:47:48 -08003837<p>
3838The built-in function <code>new</code> takes a type <code>T</code> and
3839returns a value of type <code>*T</code>.
Robert Griesemera3294712009-01-05 11:17:26 -08003840The memory is initialized as described in the section on initial values
Rob Pike8f2330d2009-02-25 16:20:44 -08003841(§The zero value).
Rob Pike96750f12009-02-27 16:47:48 -08003842</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003843
Robert Griesemerc2d55862009-02-19 16:49:10 -08003844<pre>
3845new(T)
3846</pre>
Robert Griesemer4dc25282008-09-09 10:37:19 -07003847
Rob Pike96750f12009-02-27 16:47:48 -08003848<p>
Robert Griesemer4dc25282008-09-09 10:37:19 -07003849For instance
Rob Pike96750f12009-02-27 16:47:48 -08003850</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003851
Robert Griesemerc2d55862009-02-19 16:49:10 -08003852<pre>
3853type S struct { a int; b float }
3854new(S)
3855</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003856
Rob Pike96750f12009-02-27 16:47:48 -08003857<p>
3858dynamically allocates memory for a variable of type <code>S</code>,
3859initializes it (<code>a=0</code>, <code>b=0.0</code>),
3860and returns a value of type <code>*S</code> containing the address
3861of the memory.
3862</p>
3863
3864<h3>Making slices, maps and channels</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003865
Robert Griesemerc2d55862009-02-19 16:49:10 -08003866<p>
Rob Pike96750f12009-02-27 16:47:48 -08003867Slices, maps and channels are reference types that do not require the
3868extra indirection of an allocation with <code>new</code>.
3869The built-in function <code>make</code> takes a type <code>T</code>,
3870which must be a slice, map or channel type,
3871optionally followed by a type-specific list of expressions.
3872It returns a value of type <code>T</code> (not <code>*T</code>).
Robert Griesemer633957b2009-01-06 13:23:20 -08003873The memory is initialized as described in the section on initial values
Rob Pike8f2330d2009-02-25 16:20:44 -08003874(§The zero value).
Rob Pike96750f12009-02-27 16:47:48 -08003875</p>
Robert Griesemer633957b2009-01-06 13:23:20 -08003876
Robert Griesemerc2d55862009-02-19 16:49:10 -08003877<pre>
3878make(T [, optional list of expressions])
3879</pre>
Robert Griesemer633957b2009-01-06 13:23:20 -08003880
Rob Pike96750f12009-02-27 16:47:48 -08003881<p>
Robert Griesemer633957b2009-01-06 13:23:20 -08003882For instance
Rob Pike96750f12009-02-27 16:47:48 -08003883</p>
Robert Griesemer633957b2009-01-06 13:23:20 -08003884
Robert Griesemerc2d55862009-02-19 16:49:10 -08003885<pre>
3886make(map[string] int)
3887</pre>
Robert Griesemer633957b2009-01-06 13:23:20 -08003888
Rob Pike96750f12009-02-27 16:47:48 -08003889<p>
Robert Griesemer633957b2009-01-06 13:23:20 -08003890creates a new map value and initializes it to an empty map.
Rob Pike96750f12009-02-27 16:47:48 -08003891</p>
Robert Griesemer633957b2009-01-06 13:23:20 -08003892
Rob Pike96750f12009-02-27 16:47:48 -08003893<p>
3894The parameters affect sizes for allocating slices, maps, and
Robert Griesemer633957b2009-01-06 13:23:20 -08003895buffered channels:
Rob Pike96750f12009-02-27 16:47:48 -08003896</p>
Robert Griesemer633957b2009-01-06 13:23:20 -08003897
Robert Griesemerc2d55862009-02-19 16:49:10 -08003898<pre>
3899s := make([]int, 10, 100); # slice with len(s) == 10, cap(s) == 100
3900c := make(chan int, 10); # channel with a buffer size of 10
3901m := make(map[string] int, 100); # map with initial space for 100 elements
3902</pre>
Robert Griesemer633957b2009-01-06 13:23:20 -08003903
Rob Pikeff70f092009-02-20 13:36:14 -08003904<hr/>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003905
Robert Griesemerc2d55862009-02-19 16:49:10 -08003906<h2>Packages</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003907
Rob Pike46596852009-03-02 16:17:29 -08003908<p>
3909Go programs are constructed by linking together <i>packages</i>.
3910A package is in turn constructed from one or more source files that
Rob Pike811dd252009-03-04 20:39:39 -08003911together provide access to a set of types, constants, functions,
Rob Pike46596852009-03-02 16:17:29 -08003912and variables. Those elements may be <i>imported</i> and used in
3913another package.
3914</p>
3915
3916<h3>Source file organization</h3>
3917
3918<p>
3919Each source file consists of a package clause defining the package
3920to which it belongs, followed by a possibly empty set of import
3921declarations that declare packages whose contents it wishes to use,
3922followed by a possibly empty set of declarations of functions,
3923types, variables, and constants. The source text following the
Russ Cox5958dd62009-03-04 17:19:21 -08003924package clause acts as a block for scoping (§Declarations and scope
Rob Pike46596852009-03-02 16:17:29 -08003925rules).
3926</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003927
Rob Pikeff70f092009-02-20 13:36:14 -08003928<pre class="grammar">
Rob Pike46596852009-03-02 16:17:29 -08003929SourceFile = PackageClause { ImportDecl [ ";" ] } { Declaration [ ";" ] } .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003930</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003931
Rob Pike46596852009-03-02 16:17:29 -08003932<h3>Package clause</h3>
3933
Robert Griesemerc2d55862009-02-19 16:49:10 -08003934<p>
Rob Pike46596852009-03-02 16:17:29 -08003935A package clause begins each source file and defines the package
3936to which the file belongs.
3937</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003938
Rob Pikeff70f092009-02-20 13:36:14 -08003939<pre class="grammar">
Rob Pike46596852009-03-02 16:17:29 -08003940PackageClause = "package" PackageName .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003941</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003942
Rob Pike46596852009-03-02 16:17:29 -08003943<pre>
3944package math
3945</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003946
Rob Pike46596852009-03-02 16:17:29 -08003947<p>
3948A set of files sharing the same PackageName form the implementation of a package.
3949An implementation may require that all source files for a package inhabit the same directory.
3950</p>
3951
3952<h3>Import</h3>
3953
3954<p>
3955A source file gains access to exported identifiers (§Exported
3956identifiers) from another package through an import declaration.
3957In the general form, an import declaration provides an identifier
3958that code in the source file may use to access the imported package's
3959contents and a file name referring to the (compiled) implementation of
3960the package. The file name may be relative to a repository of
3961installed packages.
3962</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003963
Rob Pikeff70f092009-02-20 13:36:14 -08003964<pre class="grammar">
Rob Pike46596852009-03-02 16:17:29 -08003965ImportDecl = "import" ( ImportSpec | "(" [ ImportSpecList ] ")" ) .
3966ImportSpecList = ImportSpec { ";" ImportSpec } [ ";" ] .
3967ImportSpec = [ "." | PackageName ] PackageFileName .
3968PackageFileName = StringLit .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003969</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003970
Robert Griesemerc2d55862009-02-19 16:49:10 -08003971<p>
Rob Pike46596852009-03-02 16:17:29 -08003972After an import, in the usual case an exported name <i>N</i> from the imported
3973package <i>P</i> may be accessed by the qualified identifier
3974<i>P</i><code>.</code><i>N</i> (§Qualified identifiers). The actual
3975name <i>P</i> depends on the form of the import declaration. If
3976an explicit package name <code>p1</code> is provided, the qualified
3977identifer will have the form <code>p1.</code><i>N</i>. If no name
3978is provided in the import declaration, <i>P</i> will be the package
3979name declared within the source files of the imported package.
3980Finally, if the import declaration uses an explicit period
3981(<code>.</code>) for the package name, <i>N</i> will appear
3982in the package-level scope of the current file and the qualified name is
3983unnecessary and erroneous. In this form, it is an error if the import introduces
3984a name conflict.
3985</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08003986<p>
Rob Pike46596852009-03-02 16:17:29 -08003987In this table, assume we have compiled a package named
3988<code>math</code>, which exports function <code>Sin</code>, and
3989installed the compiled package in file
3990<code>"lib/math"</code>.
3991</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003992
Rob Pike46596852009-03-02 16:17:29 -08003993<pre class="grammar">
3994Import syntax Local name of Sin
3995
3996import M "lib/math" M.Sin
3997import "lib/math" math.Sin
3998import . "lib/math" Sin
3999</pre>
4000
4001<h3>Multi-file packages</h3>
4002
4003<p>
4004If a package is constructed from multiple source files, all names
4005at package-level scope, not just exported names, are visible to all the
4006files in the package. An import declaration is still necessary to
4007declare intention to use the names,
4008but the imported names do not need a qualified identifer to be
4009accessed.
4010</p>
4011
4012<p>
4013The compilation of a multi-file package may require
4014that the files be compiled and installed in an order that satisfies
4015the resolution of names imported within the package.
4016</p>
4017
4018<p>
4019If source file <code>math1.go</code> contains
4020</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004021<pre>
Rob Pike46596852009-03-02 16:17:29 -08004022package math
4023
4024const twoPi = 6.283185307179586
4025
4026function Sin(x float) float { return ... }
Robert Griesemerc2d55862009-02-19 16:49:10 -08004027</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004028
Robert Griesemerc2d55862009-02-19 16:49:10 -08004029<p>
Rob Pike46596852009-03-02 16:17:29 -08004030and file <code>"math2.go"</code> begins
4031</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004032<pre>
Rob Pike46596852009-03-02 16:17:29 -08004033package math
4034
4035import "lib/math"
Robert Griesemerc2d55862009-02-19 16:49:10 -08004036</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004037
Robert Griesemerc2d55862009-02-19 16:49:10 -08004038<p>
Rob Pike46596852009-03-02 16:17:29 -08004039then, provided <code>"math1.go"</code> is compiled first and
4040installed in <code>"lib/math"</code>, <code>math2.go</code>
4041may refer directly to <code>Sin</code> and <code>twoPi</code>
4042without a qualified identifier.
4043</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004044
Rob Pike46596852009-03-02 16:17:29 -08004045<h3>An example package</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004046
Robert Griesemerc2d55862009-02-19 16:49:10 -08004047<p>
Rob Pike46596852009-03-02 16:17:29 -08004048Here is a complete Go package that implements a concurrent prime sieve.
4049</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004050
Robert Griesemerc2d55862009-02-19 16:49:10 -08004051<pre>
4052package main
4053
Rob Pike46596852009-03-02 16:17:29 -08004054import "fmt"
4055
Robert Griesemerc2d55862009-02-19 16:49:10 -08004056// Send the sequence 2, 3, 4, ... to channel 'ch'.
4057func generate(ch chan <- int) {
4058 for i := 2; ; i++ {
4059 ch <- i // Send 'i' to channel 'ch'.
4060 }
4061}
4062
4063// Copy the values from channel 'in' to channel 'out',
4064// removing those divisible by 'prime'.
Rob Pike94b67eb2009-03-24 17:40:47 -07004065func filter(src chan <- int, dst <-chan int, prime int) {
4066 for i := range src { // Loop over values received from 'src'.
Robert Griesemerc2d55862009-02-19 16:49:10 -08004067 if i % prime != 0 {
Rob Pike94b67eb2009-03-24 17:40:47 -07004068 dst <- i // Send 'i' to channel 'dst'.
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004069 }
4070 }
Robert Griesemerc2d55862009-02-19 16:49:10 -08004071}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004072
Robert Griesemerc2d55862009-02-19 16:49:10 -08004073// The prime sieve: Daisy-chain filter processes together.
4074func sieve() {
4075 ch := make(chan int); // Create a new channel.
4076 go generate(ch); // Start generate() as a subprocess.
4077 for {
4078 prime := <-ch;
Rob Pike46596852009-03-02 16:17:29 -08004079 fmt.Print(prime, "\n");
Robert Griesemerc2d55862009-02-19 16:49:10 -08004080 ch1 := make(chan int);
4081 go filter(ch, ch1, prime);
4082 ch = ch1
4083 }
4084}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004085
Robert Griesemerc2d55862009-02-19 16:49:10 -08004086func main() {
4087 sieve()
4088}
4089</pre>
Robert Griesemer67153582008-12-16 14:45:09 -08004090
Rob Pikeff70f092009-02-20 13:36:14 -08004091<hr/>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004092
4093<h2>Program initialization and execution</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004094
Rob Pike8f2330d2009-02-25 16:20:44 -08004095<h3>The zero value</h3>
4096<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004097When memory is allocated to store a value, either through a declaration
Rob Pike8f2330d2009-02-25 16:20:44 -08004098or <code>new()</code>, and no explicit initialization is provided, the memory is
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004099given a default initialization. Each element of such a value is
Rob Pike8f2330d2009-02-25 16:20:44 -08004100set to the zero value for its type: <code>false</code> for booleans,
4101<code>0</code> for integers, <code>0.0</code> for floats, <code>""</code>
4102for strings, and <code>nil</code> for pointers and interfaces.
Rob Pikeff70f092009-02-20 13:36:14 -08004103This initialization is done recursively, so for instance each element of an
Rob Pike8f2330d2009-02-25 16:20:44 -08004104array of structs will have its fields zeroed if no value is specified.
4105</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004106<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004107These two simple declarations are equivalent:
Rob Pike8f2330d2009-02-25 16:20:44 -08004108</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004109
Robert Griesemerc2d55862009-02-19 16:49:10 -08004110<pre>
4111var i int;
4112var i int = 0;
4113</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004114
Rob Pike8f2330d2009-02-25 16:20:44 -08004115<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004116After
Rob Pike8f2330d2009-02-25 16:20:44 -08004117</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004118
Robert Griesemerc2d55862009-02-19 16:49:10 -08004119<pre>
4120type T struct { i int; f float; next *T };
4121t := new(T);
4122</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004123
Rob Pike8f2330d2009-02-25 16:20:44 -08004124<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004125the following holds:
Rob Pike8f2330d2009-02-25 16:20:44 -08004126</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004127
Robert Griesemerc2d55862009-02-19 16:49:10 -08004128<pre>
4129t.i == 0
4130t.f == 0.0
4131t.next == nil
4132</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004133
Rob Pike96750f12009-02-27 16:47:48 -08004134<p>
4135The same would also be true after
4136</p>
4137
4138<pre>
4139var t T
4140</pre>
4141
Rob Pike8f2330d2009-02-25 16:20:44 -08004142<h3>Program execution</h3>
4143<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004144A package with no imports is initialized by assigning initial values to
Rob Pike96750f12009-02-27 16:47:48 -08004145all its package-level variables in declaration order and then calling any
4146package-level function with the name and signature of
4147</p>
4148<pre>
4149func init()
4150</pre>
4151<p>
4152defined in its source. Since a package may contain more
4153than one source file, there may be more than one
4154<code>init()</code> function in a package, but
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004155only one per source file.
Rob Pike8f2330d2009-02-25 16:20:44 -08004156</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004157<p>
Robert Griesemer566e3b22008-09-26 16:41:50 -07004158Initialization code may contain "go" statements, but the functions
Robert Griesemerd8a764c2009-02-06 17:01:10 -08004159they invoke do not begin execution until initialization of the entire
4160program is complete. Therefore, all initialization code is run in a single
Rob Pike96750f12009-02-27 16:47:48 -08004161goroutine.
Rob Pike8f2330d2009-02-25 16:20:44 -08004162</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004163<p>
Rob Pike96750f12009-02-27 16:47:48 -08004164An <code>init()</code> function cannot be referred to from anywhere
4165in a program. In particular, <code>init()</code> cannot be called explicitly,
4166nor can a pointer to <code>init</code> be assigned to a function variable.
Rob Pike8f2330d2009-02-25 16:20:44 -08004167</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004168<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004169If a package has imports, the imported packages are initialized
Robert Griesemer566e3b22008-09-26 16:41:50 -07004170before initializing the package itself. If multiple packages import
Rob Pike96750f12009-02-27 16:47:48 -08004171a package <code>P</code>, <code>P</code> will be initialized only once.
Rob Pike8f2330d2009-02-25 16:20:44 -08004172</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004173<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004174The importing of packages, by construction, guarantees that there can
4175be no cyclic dependencies in initialization.
Rob Pike8f2330d2009-02-25 16:20:44 -08004176</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004177<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004178A complete program, possibly created by linking multiple packages,
Rob Pike96750f12009-02-27 16:47:48 -08004179must have one package called <code>main</code>, with a function
Rob Pike8f2330d2009-02-25 16:20:44 -08004180</p>
Robert Griesemer4dc25282008-09-09 10:37:19 -07004181
Robert Griesemerc2d55862009-02-19 16:49:10 -08004182<pre>
Rob Pike96750f12009-02-27 16:47:48 -08004183func main() { ... }
Robert Griesemerc2d55862009-02-19 16:49:10 -08004184</pre>
Robert Griesemer4dc25282008-09-09 10:37:19 -07004185
Rob Pike8f2330d2009-02-25 16:20:44 -08004186<p>
Rob Pike96750f12009-02-27 16:47:48 -08004187defined.
4188The function <code>main.main()</code> takes no arguments and returns no value.
Rob Pike8f2330d2009-02-25 16:20:44 -08004189</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004190<p>
Rob Pike96750f12009-02-27 16:47:48 -08004191Program execution begins by initializing the <code>main</code> package and then
Rob Pike8f2330d2009-02-25 16:20:44 -08004192invoking <code>main.main()</code>.
4193</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004194<p>
Rob Pike46596852009-03-02 16:17:29 -08004195When <code>main.main()</code> returns, the program exits.
Rob Pike8f2330d2009-02-25 16:20:44 -08004196</p>
Rob Pike811dd252009-03-04 20:39:39 -08004197<p>
4198Implementation restriction: The compiler assumes package <code>main</code>
4199is created by a single source file and that it is not imported by any other package.
4200</p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004201
Rob Pikeff70f092009-02-20 13:36:14 -08004202<hr/>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004203
Rob Pikef27e9f02009-02-23 19:22:05 -08004204<h2>System considerations</h2>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004205
Rob Pikef27e9f02009-02-23 19:22:05 -08004206<h3>Package <code>unsafe</code></h3>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004207
Robert Griesemerc2d55862009-02-19 16:49:10 -08004208<p>
Rob Pike96750f12009-02-27 16:47:48 -08004209The built-in package <code>unsafe</code>, known to the compiler,
4210provides facilities for low-level programming including operations
4211that violate the type system. A package using <code>unsafe</code>
4212must be vetted manually for type safety. The package provides the
4213following interface:
Rob Pikef27e9f02009-02-23 19:22:05 -08004214</p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004215
Rob Pikeff70f092009-02-20 13:36:14 -08004216<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08004217package unsafe
Robert Griesemer52c02c22009-02-11 13:46:30 -08004218
Robert Griesemerc2d55862009-02-19 16:49:10 -08004219const Maxalign int
Robert Griesemer52c02c22009-02-11 13:46:30 -08004220
Rob Pikef27e9f02009-02-23 19:22:05 -08004221type Pointer *any // "any" is shorthand for any Go type; it is not a real type.
Robert Griesemer52c02c22009-02-11 13:46:30 -08004222
Robert Griesemerc2d55862009-02-19 16:49:10 -08004223func Alignof(variable any) int
4224func Offsetof(selector any) int
4225func Sizeof(variable any) int
4226</pre>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004227
Robert Griesemerc2d55862009-02-19 16:49:10 -08004228<p>
Rob Pikef27e9f02009-02-23 19:22:05 -08004229Any pointer or value of type <code>uintptr</code> can be converted into
4230a <code>Pointer</code> and vice versa.
4231</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004232<p>
Rob Pikef27e9f02009-02-23 19:22:05 -08004233The function <code>Sizeof</code> takes an expression denoting a
Rob Pikecdbf6192009-02-24 17:47:45 -08004234variable of any (complete) type and returns the size of the variable in bytes.
Rob Pikef27e9f02009-02-23 19:22:05 -08004235</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004236<p>
Rob Pikef27e9f02009-02-23 19:22:05 -08004237The function <code>Offsetof</code> takes a selector (§Selectors) denoting a struct
Robert Griesemer52c02c22009-02-11 13:46:30 -08004238field of any type and returns the field offset in bytes relative to the
Rob Pikef27e9f02009-02-23 19:22:05 -08004239struct's address. For a struct <code>s</code> with field <code>f</code>:
4240</p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004241
Robert Griesemerc2d55862009-02-19 16:49:10 -08004242<pre>
Rob Pikef27e9f02009-02-23 19:22:05 -08004243uintptr(unsafe.Pointer(&amp;s)) + uintptr(unsafe.Offsetof(s.f)) == uintptr(unsafe.Pointer(&amp;s.f))
Robert Griesemerc2d55862009-02-19 16:49:10 -08004244</pre>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004245
Robert Griesemerc2d55862009-02-19 16:49:10 -08004246<p>
Rob Pikef27e9f02009-02-23 19:22:05 -08004247Computer architectures may require memory addresses to be <i>aligned</i>;
4248that is, for addresses of a variable to be a multiple of a factor,
4249the variable's type's <i>alignment</i>. The function <code>Alignof</code>
4250takes an expression denoting a variable of any type and returns the
4251alignment of the (type of the) variable in bytes. For a variable
4252<code>x</code>:
4253</p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004254
Robert Griesemerc2d55862009-02-19 16:49:10 -08004255<pre>
4256uintptr(unsafe.Pointer(&amp;x)) % uintptr(unsafe.Alignof(x)) == 0
4257</pre>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004258
Robert Griesemerc2d55862009-02-19 16:49:10 -08004259<p>
Rob Pikef27e9f02009-02-23 19:22:05 -08004260The maximum alignment is given by the constant <code>Maxalign</code>.
4261It usually corresponds to the value of <code>Sizeof(x)</code> for
Russ Cox5958dd62009-03-04 17:19:21 -08004262a variable <code>x</code> of the largest numeric type (8 for a
Rob Pikef27e9f02009-02-23 19:22:05 -08004263<code>float64</code>), but may
4264be smaller on systems with weaker alignment restrictions.
4265</p>
4266<p>
4267Calls to <code>Alignof</code>, <code>Offsetof</code>, and
4268<code>Sizeof</code> are constant expressions of type <code>int</code>.
4269</p>
Robert Griesemer6f8df7a2009-02-11 21:57:15 -08004270
Robert Griesemer52c02c22009-02-11 13:46:30 -08004271
Robert Griesemerc2d55862009-02-19 16:49:10 -08004272<h3>Size and alignment guarantees</h3>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004273
Russ Cox5958dd62009-03-04 17:19:21 -08004274For the numeric types (§Numeric types), the following sizes are guaranteed:
Robert Griesemer52c02c22009-02-11 13:46:30 -08004275
Rob Pikeff70f092009-02-20 13:36:14 -08004276<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08004277type size in bytes
Robert Griesemer52c02c22009-02-11 13:46:30 -08004278
Robert Griesemerc2d55862009-02-19 16:49:10 -08004279byte, uint8, int8 1
4280uint16, int16 2
4281uint32, int32, float32 4
4282uint64, int64, float64 8
4283</pre>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004284
Robert Griesemerc2d55862009-02-19 16:49:10 -08004285<p>
Rob Pikef27e9f02009-02-23 19:22:05 -08004286The following minimal alignment properties are guaranteed:
Rob Pike4501d342009-02-19 17:31:36 -08004287</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004288<ol>
Rob Pikef27e9f02009-02-23 19:22:05 -08004289<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 -08004290
Russ Cox5958dd62009-03-04 17:19:21 -08004291<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 -08004292 of <code>unsafe.Sizeof(x)</code> and <code>unsafe.Maxalign</code>, but at least 1.
Robert Griesemer52c02c22009-02-11 13:46:30 -08004293
Rob Pikef27e9f02009-02-23 19:22:05 -08004294<li>For a variable <code>x</code> of struct type: <code>unsafe.Alignof(x)</code> is the largest of
4295 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 -08004296
Rob Pikef27e9f02009-02-23 19:22:05 -08004297<li>For a variable <code>x</code> of array type: <code>unsafe.Alignof(x)</code> is the same as
4298 <code>unsafe.Alignof(x[0])</code>, but at least 1.
Robert Griesemerc2d55862009-02-19 16:49:10 -08004299</ol>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004300
Rob Pikeff70f092009-02-20 13:36:14 -08004301<hr/>
4302
4303<h2><font color=red>Differences between this doc and implementation - TODO</font></h2>
4304<p>
4305<font color=red>
Rob Pikedf3183f2009-02-26 16:37:23 -08004306Implementation accepts only ASCII digits for digits; doc says Unicode.
4307<br/>
Rob Pikedf3183f2009-02-26 16:37:23 -08004308Implementation does not honor the restriction on goto statements and targets (no intervening declarations).
4309<br/>
4310cap() does not work on maps or chans.
4311<br/>
4312len() does not work on chans.
Rob Pikeff70f092009-02-20 13:36:14 -08004313</font>
4314</p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004315
Robert Griesemerc2d55862009-02-19 16:49:10 -08004316</div>
4317</body>
4318</html>