blob: 28a96549a9eaa464e02d665acf53124ab0eb0d6e [file] [log] [blame]
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002
Robert Griesemerc59d2f12008-09-09 10:48:14 -07003<!--
Robert Griesemer40d6bb52009-04-20 15:32:20 -07004Open issues:
Robert Griesemer7471eab2009-01-27 14:51:24 -08005[ ] Semantics of type declaration:
6 - creating a new type (status quo), or only a new type name?
Robert Griesemer40d6bb52009-04-20 15:32:20 -07007 - declaration "type T S" strips methods of S. why/why not?
8 - no mechanism to declare a local type name: type T P.T
Robert Griesemer57b34612008-10-10 12:45:44 -07009
Robert Griesemer57b34612008-10-10 12:45:44 -070010Todo's:
Robert Griesemer9f4a27c2009-01-16 15:44:08 -080011[ ] document illegality of package-external tuple assignments to structs
Robert Griesemer6f8df7a2009-02-11 21:57:15 -080012 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 -080013 a T struct { a b int }.
Robert Griesemer40d6bb52009-04-20 15:32:20 -070014[ ] should probably write something about evaluation order of statements even
15 though obvious
Robert Griesemer1a304e12009-05-08 10:25:06 -070016[ ] document new assignment rules (for named types on either side of an
17 assignment, the types must be identical)
18[ ] document T.m mechanism to obtain a function from a method
Robert Griesemerc59d2f12008-09-09 10:48:14 -070019-->
20
Robert Griesemer40d6bb52009-04-20 15:32:20 -070021
Robert Griesemerc2d55862009-02-19 16:49:10 -080022<h2>Introduction</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -070023
Robert Griesemerc2d55862009-02-19 16:49:10 -080024<p>
Rob Pike4501d342009-02-19 17:31:36 -080025This is a reference manual for the Go programming language. For
26more information and other documents, see <a
27href="/">the Go home page</a>.
28</p>
Robert Griesemer67153582008-12-16 14:45:09 -080029
Robert Griesemerc2d55862009-02-19 16:49:10 -080030<p>
Rob Pike4501d342009-02-19 17:31:36 -080031Go is a general-purpose language designed with systems programming
32in mind. It is strongly typed and garbage-collected, and has explicit
33support for concurrent programming. Programs are constructed from
34<i>packages</i>, whose properties allow efficient management of
35dependencies. The existing implementations use a traditional
36compile/link model to generate executable binaries.
37</p>
38
Robert Griesemerc2d55862009-02-19 16:49:10 -080039<p>
Rob Pike4501d342009-02-19 17:31:36 -080040The grammar is compact and regular, allowing for easy analysis by
41automatic tools such as integrated development environments.
42</p>
Rob Pikeff70f092009-02-20 13:36:14 -080043<hr/>
Robert Griesemerc2d55862009-02-19 16:49:10 -080044<h2>Notation</h2>
Rob Pike4501d342009-02-19 17:31:36 -080045<p>
Robert Griesemer4d230302008-12-17 15:39:15 -080046The syntax is specified using Extended Backus-Naur Form (EBNF):
Rob Pike4501d342009-02-19 17:31:36 -080047</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -070048
Rob Pikeff70f092009-02-20 13:36:14 -080049<pre class="grammar">
Robert Griesemer88a0c402009-04-23 14:42:21 -070050Production = production_name "=" Expression "." .
Rob Pike4501d342009-02-19 17:31:36 -080051Expression = Alternative { "|" Alternative } .
Robert Griesemerc2d55862009-02-19 16:49:10 -080052Alternative = Term { Term } .
Rob Pike4501d342009-02-19 17:31:36 -080053Term = production_name | token [ "..." token ] | Group | Option | Repetition .
54Group = "(" Expression ")" .
Rob Pikec956e902009-04-14 20:10:49 -070055Option = "[" Expression "]" .
Rob Pike4501d342009-02-19 17:31:36 -080056Repetition = "{" Expression "}" .
Robert Griesemerc2d55862009-02-19 16:49:10 -080057</pre>
Robert Griesemerbbfe3122008-10-09 17:12:09 -070058
Rob Pike4501d342009-02-19 17:31:36 -080059<p>
60Productions are expressions constructed from terms and the following
61operators, in increasing precedence:
62</p>
Rob Pikeff70f092009-02-20 13:36:14 -080063<pre class="grammar">
Rob Pike4501d342009-02-19 17:31:36 -080064| alternation
65() grouping
66[] option (0 or 1 times)
67{} repetition (0 to n times)
Robert Griesemerc2d55862009-02-19 16:49:10 -080068</pre>
Robert Griesemer4d230302008-12-17 15:39:15 -080069
Robert Griesemerc2d55862009-02-19 16:49:10 -080070<p>
Rob Pike4501d342009-02-19 17:31:36 -080071Lower-case production names are used to identify lexical tokens.
72Non-terminals are in CamelCase. Lexical symbols are enclosed in
Rob Pikef27e9f02009-02-23 19:22:05 -080073double quotes <code>""</code> (the double quote symbol is written as
74<code>'"'</code>).
Rob Pike4501d342009-02-19 17:31:36 -080075</p>
76
Robert Griesemerc2d55862009-02-19 16:49:10 -080077<p>
Rob Pikef27e9f02009-02-23 19:22:05 -080078The form <code>"a ... b"</code> represents the set of characters from
79<code>a</code> through <code>b</code> as alternatives.
Rob Pike4501d342009-02-19 17:31:36 -080080</p>
81
Rob Pikeff70f092009-02-20 13:36:14 -080082<hr/>
Robert Griesemer7abfcd92008-10-07 17:14:30 -070083
Robert Griesemerc2d55862009-02-19 16:49:10 -080084<h2>Source code representation</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -070085
Robert Griesemerc2d55862009-02-19 16:49:10 -080086<p>
Rob Pikeff70f092009-02-20 13:36:14 -080087Source code is Unicode text encoded in UTF-8. The text is not
88canonicalized, so a single accented code point is distinct from the
89same character constructed from combining an accent and a letter;
90those are treated as two code points. For simplicity, this document
91will use the term <i>character</i> to refer to a Unicode code point.
92</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -080093<p>
Rob Pikeff70f092009-02-20 13:36:14 -080094Each code point is distinct; for instance, upper and lower case letters
95are different characters.
96</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -070097
Robert Griesemerc2d55862009-02-19 16:49:10 -080098<h3>Characters</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -070099
Robert Griesemerc2d55862009-02-19 16:49:10 -0800100<p>
Rob Pike4501d342009-02-19 17:31:36 -0800101The following terms are used to denote specific Unicode character classes:
102</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -0800103<ul>
Rob Pikeff70f092009-02-20 13:36:14 -0800104 <li>unicode_char an arbitrary Unicode code point</li>
105 <li>unicode_letter a Unicode code point classified as "Letter"</li>
106 <li>capital_letter a Unicode code point classified as "Letter, uppercase"</li>
107 <li>unicode_digit a Unicode code point classified as "Digit"</li>
Robert Griesemerc2d55862009-02-19 16:49:10 -0800108</ul>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700109
Robert Griesemer83c17602009-01-16 14:12:50 -0800110(The Unicode Standard, Section 4.5 General Category - Normative.)
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700111
Robert Griesemerc2d55862009-02-19 16:49:10 -0800112<h3>Letters and digits</h3>
Rob Pikeff70f092009-02-20 13:36:14 -0800113
114<p>
Rob Pikef27e9f02009-02-23 19:22:05 -0800115The underscore character <code>_</code> (U+005F) is considered a letter.
Rob Pikeff70f092009-02-20 13:36:14 -0800116</>
117<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -0800118letter = unicode_letter | "_" .
119decimal_digit = "0" ... "9" .
120octal_digit = "0" ... "7" .
121hex_digit = "0" ... "9" | "A" ... "F" | "a" ... "f" .
122</pre>
Rob Pikeff70f092009-02-20 13:36:14 -0800123<hr/>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700124
Rob Pikeff70f092009-02-20 13:36:14 -0800125<h2>Lexical elements</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700126
Rob Pikeff70f092009-02-20 13:36:14 -0800127<h3>Comments</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700128
Rob Pikeff70f092009-02-20 13:36:14 -0800129<p>
130There are two forms of comments. The first starts at the character
Rob Pikef27e9f02009-02-23 19:22:05 -0800131sequence <code>//</code> and continues through the next newline. The
132second starts at the character sequence <code>/*</code> and continues
133through the character sequence <code>*/</code>. Comments do not nest.
Rob Pikeff70f092009-02-20 13:36:14 -0800134</p>
135
136<h3>Tokens</h3>
137
138<p>
139Tokens form the vocabulary of the Go language.
140There are four classes: identifiers, keywords, operators
141and delimiters, and literals. <i>White space</i>, formed from
142blanks, tabs, and newlines, is ignored except as it separates tokens
143that would otherwise combine into a single token. Comments
144behave as white space. While breaking the input into tokens,
145the next token is the longest sequence of characters that form a
146valid token.
147</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700148
Robert Griesemerc2d55862009-02-19 16:49:10 -0800149<h3>Identifiers</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700150
Rob Pikeff70f092009-02-20 13:36:14 -0800151<p>
152Identifiers name program entities such as variables and types.
153An identifier is a sequence of one or more letters and digits.
154The first character in an identifier must be a letter.
155</p>
156<pre class="grammar">
157identifier = letter { letter | unicode_digit } .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800158</pre>
Robert Griesemerc2d55862009-02-19 16:49:10 -0800159<pre>
160a
161_x9
162ThisVariableIsExported
163αβ
164</pre>
Robert Griesemer83c17602009-01-16 14:12:50 -0800165Some identifiers are predeclared (§Predeclared identifiers).
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -0700166
Rob Pikeff70f092009-02-20 13:36:14 -0800167<h3>Keywords</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700168
Rob Pikeff70f092009-02-20 13:36:14 -0800169<p>
170The following keywords are reserved and may not be used as identifiers.
171</p>
172<pre class="grammar">
173break default func interface select
174case defer go map struct
175chan else goto package switch
176const fallthrough if range type
177continue for import return var
178</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700179
Rob Pikeff70f092009-02-20 13:36:14 -0800180<h3>Operators and Delimiters</h3>
181
182<p>
183The following character sequences represent operators, delimiters, and other special tokens:
184</p>
185<pre class="grammar">
186+ &amp; += &amp;= &amp;&amp; == != ( )
187- | -= |= || &lt; &lt;= [ ]
188* ^ *= ^= &lt;- &gt; &gt;= { }
189/ << /= <<= ++ = := , ;
190% >> %= >>= -- ! ... . :
Rob Pikecd04ec92009-03-11 21:59:05 -0700191 &amp;^ &amp;^=
Rob Pikeff70f092009-02-20 13:36:14 -0800192</pre>
193
194<h3>Integer literals</h3>
195
196<p>
197An integer literal is a sequence of one or more digits in the
198corresponding base, which may be 8, 10, or 16. An optional prefix
Rob Pikef27e9f02009-02-23 19:22:05 -0800199sets a non-decimal base: <code>0</code> for octal, <code>0x</code> or
200<code>0X</code> for hexadecimal. In hexadecimal literals, letters
201<code>a-f</code> and <code>A-F</code> represent values 10 through 15.
Rob Pikeff70f092009-02-20 13:36:14 -0800202</p>
203<pre class="grammar">
204int_lit = decimal_lit | octal_lit | hex_lit .
205decimal_lit = ( "1" ... "9" ) { decimal_digit } .
206octal_lit = "0" { octal_digit } .
207hex_lit = "0" ( "x" | "X" ) hex_digit { hex_digit } .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800208</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700209
Robert Griesemerc2d55862009-02-19 16:49:10 -0800210<pre>
21142
2120600
2130xBadFace
214170141183460469231731687303715884105727
215</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700216
Rob Pikeff70f092009-02-20 13:36:14 -0800217<h3>Floating-point literals</h3>
218<p>
219A floating-point literal is a decimal representation of a floating-point
220number. It has an integer part, a decimal point, a fractional part,
221and an exponent part. The integer and fractional part comprise
Rob Pikef27e9f02009-02-23 19:22:05 -0800222decimal digits; the exponent part is an <code>e</code> or <code>E</code>
Rob Pikeff70f092009-02-20 13:36:14 -0800223followed by an optionally signed decimal exponent. One of the
224integer part or the fractional part may be elided; one of the decimal
225point or the exponent may be elided.
226</p>
227<pre class="grammar">
228float_lit = decimals "." [ decimals ] [ exponent ] |
229 decimals exponent |
230 "." decimals [ exponent ] .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800231decimals = decimal_digit { decimal_digit } .
232exponent = ( "e" | "E" ) [ "+" | "-" ] decimals .
233</pre>
Robert Griesemerad711102008-09-11 17:48:20 -0700234
Robert Griesemerc2d55862009-02-19 16:49:10 -0800235<pre>
2360.
2372.71828
2381.e+0
2396.67428e-11
2401E6
241.25
242.12345E+5
243</pre>
Robert Griesemerad711102008-09-11 17:48:20 -0700244
Rob Pikeff70f092009-02-20 13:36:14 -0800245<h3>Ideal numbers</h3>
246
Robert Griesemerc2d55862009-02-19 16:49:10 -0800247<p>
Rob Pikeff70f092009-02-20 13:36:14 -0800248Integer literals represent values of arbitrary precision, or <i>ideal
249integers</i>. Similarly, floating-point literals represent values
250of arbitrary precision, or <i>ideal floats</i>. These <i>ideal
251numbers</i> have no size or type and cannot overflow. However,
252when (used in an expression) assigned to a variable or typed constant,
253the destination must be able to represent the assigned value.
254</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -0800255<p>
Robert Griesemerad711102008-09-11 17:48:20 -0700256Implementation restriction: A compiler may implement ideal numbers
Rob Pike8f2330d2009-02-25 16:20:44 -0800257by choosing an internal representation with at least twice the precision
258of any machine type.
Rob Pikeff70f092009-02-20 13:36:14 -0800259</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700260
Rob Pikeff70f092009-02-20 13:36:14 -0800261<h3>Character literals</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700262
Rob Pike4501d342009-02-19 17:31:36 -0800263<p>
Rob Pikeff70f092009-02-20 13:36:14 -0800264A character literal represents an integer value, typically a
265Unicode code point, as one or more characters enclosed in single
266quotes. Within the quotes, any character may appear except single
267quote and newline. A single quoted character represents itself,
268while multi-character sequences beginning with a backslash encode
269values in various formats.
Rob Pike4501d342009-02-19 17:31:36 -0800270</p>
Rob Pikeff70f092009-02-20 13:36:14 -0800271<p>
272The simplest form represents the single character within the quotes;
273since Go source text is Unicode characters encoded in UTF-8, multiple
274UTF-8-encoded bytes may represent a single integer value. For
Rob Pikef27e9f02009-02-23 19:22:05 -0800275instance, the literal <code>'a'</code> holds a single byte representing
276a literal <code>a</code>, Unicode U+0061, value <code>0x61</code>, while
277<code>'ä'</code> holds two bytes (<code>0xc3</code> <code>0xa4</code>) representing
278a literal <code>a</code>-dieresis, U+00E4, value <code>0xe4</code>.
Rob Pikeff70f092009-02-20 13:36:14 -0800279</p>
280<p>
281Several backslash escapes allow arbitrary values to be represented
282as ASCII text. There are four ways to represent the integer value
Rob Pikef27e9f02009-02-23 19:22:05 -0800283as a numeric constant: <code>\x</code> followed by exactly two hexadecimal
284digits; <code>\u</code> followed by exactly four hexadecimal digits;
285<code>\U</code> followed by exactly eight hexadecimal digits, and a
286plain backslash <code>\</code> followed by exactly three octal digits.
Rob Pikeff70f092009-02-20 13:36:14 -0800287In each case the value of the literal is the value represented by
288the digits in the corresponding base.
289</p>
290<p>
291Although these representations all result in an integer, they have
292different valid ranges. Octal escapes must represent a value between
2930 and 255 inclusive. (Hexadecimal escapes satisfy this condition
Rob Pikef27e9f02009-02-23 19:22:05 -0800294by construction). The `Unicode' escapes <code>\u</code> and <code>\U</code>
Rob Pikeff70f092009-02-20 13:36:14 -0800295represent Unicode code points so within them some values are illegal,
Rob Pikef27e9f02009-02-23 19:22:05 -0800296in particular those above <code>0x10FFFF</code> and surrogate halves.
Rob Pikeff70f092009-02-20 13:36:14 -0800297</p>
298<p>
299After a backslash, certain single-character escapes represent special values:
300</p>
301<pre class="grammar">
302\a U+0007 alert or bell
303\b U+0008 backspace
304\f U+000C form feed
305\n U+000A line feed or newline
306\r U+000D carriage return
307\t U+0009 horizontal tab
308\v U+000b vertical tab
309\\ U+005c backslash
310\' U+0027 single quote (valid escape only within character literals)
311\" U+0022 double quote (valid escape only within string literals)
Robert Griesemerc2d55862009-02-19 16:49:10 -0800312</pre>
Robert Griesemerc2d55862009-02-19 16:49:10 -0800313<p>
Rob Pikeff70f092009-02-20 13:36:14 -0800314All other sequences are illegal inside character literals.
Rob Pike4501d342009-02-19 17:31:36 -0800315</p>
Rob Pikeff70f092009-02-20 13:36:14 -0800316<pre class="grammar">
317char_lit = "'" ( unicode_value | byte_value ) "'" .
318unicode_value = unicode_char | little_u_value | big_u_value | escaped_char .
319byte_value = octal_byte_value | hex_byte_value .
320octal_byte_value = "\" octal_digit octal_digit octal_digit .
321hex_byte_value = "\" "x" hex_digit hex_digit .
322little_u_value = "\" "u" hex_digit hex_digit hex_digit hex_digit .
323big_u_value = "\" "U" hex_digit hex_digit hex_digit hex_digit
324 hex_digit hex_digit hex_digit hex_digit .
325escaped_char = "\" ( "a" | "b" | "f" | "n" | "r" | "t" | "v" | "\" | "'" | """ ) .
326</pre>
Robert Griesemerc2d55862009-02-19 16:49:10 -0800327<pre>
328'a'
329'ä'
330'本'
331'\t'
332'\000'
333'\007'
334'\377'
335'\x07'
336'\xff'
337'\u12e4'
338'\U00101234'
339</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700340
Rob Pikeff70f092009-02-20 13:36:14 -0800341<p>
342The value of a character literal is an ideal integer, just as with
343integer literals.
344</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700345
Rob Pikeff70f092009-02-20 13:36:14 -0800346<h3>String literals</h3>
347
348<p>
Rob Pikef27e9f02009-02-23 19:22:05 -0800349String literals represent constant values of type <code>string</code>.
Rob Pikeff70f092009-02-20 13:36:14 -0800350There are two forms: raw string literals and interpreted string
351literals.
352</p>
353<p>
354Raw string literals are character sequences between back quotes
Rob Pikef27e9f02009-02-23 19:22:05 -0800355<code>``</code>. Within the quotes, any character is legal except
Rob Pikeff70f092009-02-20 13:36:14 -0800356newline and back quote. The value of a raw string literal is the
357string composed of the uninterpreted bytes between the quotes;
358in particular, backslashes have no special meaning.
359</p>
360<p>
361Interpreted string literals are character sequences between double
Rob Pikef27e9f02009-02-23 19:22:05 -0800362quotes <code>&quot;&quot;</code>. The text between the quotes forms the
Rob Pikeff70f092009-02-20 13:36:14 -0800363value of the literal, with backslash escapes interpreted as they
Rob Pikef27e9f02009-02-23 19:22:05 -0800364are in character literals (except that <code>\'</code> is illegal and
365<code>\"</code> is legal). The three-digit octal (<code>\000</code>)
366and two-digit hexadecimal (<code>\x00</code>) escapes represent individual
Rob Pikeff70f092009-02-20 13:36:14 -0800367<i>bytes</i> of the resulting string; all other escapes represent
368the (possibly multi-byte) UTF-8 encoding of individual <i>characters</i>.
Rob Pikef27e9f02009-02-23 19:22:05 -0800369Thus inside a string literal <code>\377</code> and <code>\xFF</code> represent
370a single byte of value <code>0xFF</code>=255, while <code>ÿ</code>,
371<code>\u00FF</code>, <code>\U000000FF</code> and <code>\xc3\xbf</code> represent
372the two bytes <code>0xc3 0xbf</code> of the UTF-8 encoding of character
Rob Pikeff70f092009-02-20 13:36:14 -0800373U+00FF.
374</p>
375
376<pre class="grammar">
377string_lit = raw_string_lit | interpreted_string_lit .
378raw_string_lit = "`" { unicode_char } "`" .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800379interpreted_string_lit = """ { unicode_value | byte_value } """ .
380</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700381
Robert Griesemerc2d55862009-02-19 16:49:10 -0800382<pre>
383`abc`
384`\n`
385"hello, world\n"
386"\n"
387""
388"Hello, world!\n"
389"日本語"
390"\u65e5本\U00008a9e"
391"\xff\u00FF"
392</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700393
Rob Pikeff70f092009-02-20 13:36:14 -0800394<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700395These examples all represent the same string:
Rob Pikeff70f092009-02-20 13:36:14 -0800396</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700397
Robert Griesemerc2d55862009-02-19 16:49:10 -0800398<pre>
Rob Pikeff70f092009-02-20 13:36:14 -0800399"日本語" // UTF-8 input text
400`日本語` // UTF-8 input text as a raw literal
401"\u65e5\u672c\u8a9e" // The explicit Unicode code points
402"\U000065e5\U0000672c\U00008a9e" // The explicit Unicode code points
Robert Griesemerc2d55862009-02-19 16:49:10 -0800403"\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e" // The explicit UTF-8 bytes
404</pre>
Robert Griesemercd499272008-09-29 12:09:00 -0700405
Robert Griesemerc2d55862009-02-19 16:49:10 -0800406<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700407If the source code represents a character as two code points, such as
408a combining form involving an accent and a letter, the result will be
409an error if placed in a character literal (it is not a single code
410point), and will appear as two code points if placed in a string
411literal.
Rob Pikeff70f092009-02-20 13:36:14 -0800412</p>
413<hr/>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700414
Robert Griesemerc2d55862009-02-19 16:49:10 -0800415<h2>Types</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700416
Robert Griesemerc2d55862009-02-19 16:49:10 -0800417<p>
Rob Pike8f2330d2009-02-25 16:20:44 -0800418A type determines the set of values and operations specific to values of that type.
Rob Pike5af7de32009-02-24 15:17:59 -0800419A type may be specified by a (possibly qualified (§Qualified identifiers))
420type name (§Type declarations) or a <i>type literal</i>,
421which composes a new type in terms of previously declared types.
422</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700423
Rob Pikeff70f092009-02-20 13:36:14 -0800424<pre class="grammar">
Rob Pike8f2330d2009-02-25 16:20:44 -0800425Type = TypeName | TypeLit | "(" Type ")" .
426TypeName = QualifiedIdent.
427TypeLit = ArrayType | StructType | PointerType | FunctionType | InterfaceType |
428 SliceType | MapType | ChannelType .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800429</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -0800430
Robert Griesemerc2d55862009-02-19 16:49:10 -0800431<p>
Rob Pike5af7de32009-02-24 15:17:59 -0800432<i>Basic types</i> such as <code>int</code> are predeclared (§Predeclared identifiers).
433Other types may be constructed from these, recursively,
434including arrays, structs, pointers, functions, interfaces, slices, maps, and
Robert Griesemera3294712009-01-05 11:17:26 -0800435channels.
Rob Pike5af7de32009-02-24 15:17:59 -0800436</p>
Robert Griesemer7abfcd92008-10-07 17:14:30 -0700437
Rob Pike5af7de32009-02-24 15:17:59 -0800438<p>
Rob Pike5af7de32009-02-24 15:17:59 -0800439At any point in the source code, a type may be <i>complete</i> or
Rob Pikecdbf6192009-02-24 17:47:45 -0800440<i>incomplete</i>. An incomplete type is one whose size is not
441yet known, such as a struct whose fields are not yet fully
442defined or a forward declared type (§Forward declarations).
443Most types are always complete; for instance, a pointer
444type is always complete even if it points to an incomplete type
445because the size of the pointer itself is always known.
Russ Cox461dd912009-03-04 14:44:51 -0800446(TODO: Need to figure out how forward declarations of
447interface fit in here.)
Rob Pike5af7de32009-02-24 15:17:59 -0800448</p>
449<p>
450The <i>interface</i> of a type is the set of methods bound to it
451(§Method declarations); for pointer types, it is the interface
Robert Griesemera1065fa2008-09-29 20:37:46 -0700452of the pointer base type (§Pointer types). All types have an interface;
Robert Griesemerf88c6c12009-02-25 16:58:57 -0800453if they have no methods, it is the <i>empty interface</i>.
Rob Pike5af7de32009-02-24 15:17:59 -0800454</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -0800455<p>
Rob Pike5af7de32009-02-24 15:17:59 -0800456The <i>static type</i> (or just <i>type</i>) of a variable is the
457type defined by its declaration. Variables of interface type
458(§Interface types) also have a distinct <i>dynamic type</i>, which
459is the actual type of the value stored in the variable at run-time.
460The dynamic type may vary during execution but is always compatible
461with the static type of the interface variable. For non-interfaces
462types, the dynamic type is always the static type.
463</p>
Robert Griesemer1f3e8422008-09-29 18:41:30 -0700464
Robert Griesemerc2d55862009-02-19 16:49:10 -0800465<h3>Basic types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700466
Rob Pike5af7de32009-02-24 15:17:59 -0800467<p>
Russ Cox5958dd62009-03-04 17:19:21 -0800468Basic types include traditional numeric types, booleans, and strings. All are predeclared.
Rob Pike5af7de32009-02-24 15:17:59 -0800469</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700470
Russ Cox5958dd62009-03-04 17:19:21 -0800471<h3>Numeric types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700472
Rob Pike5af7de32009-02-24 15:17:59 -0800473<p>
474The architecture-independent numeric types are:
475</p>
Robert Griesemerebf14c62008-10-30 14:50:23 -0700476
Rob Pikeff70f092009-02-20 13:36:14 -0800477<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -0800478uint8 the set of all unsigned 8-bit integers (0 to 255)
479uint16 the set of all unsigned 16-bit integers (0 to 65535)
480uint32 the set of all unsigned 32-bit integers (0 to 4294967295)
481uint64 the set of all unsigned 64-bit integers (0 to 18446744073709551615)
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700482
Robert Griesemerc2d55862009-02-19 16:49:10 -0800483int8 the set of all signed 8-bit integers (-128 to 127)
484int16 the set of all signed 16-bit integers (-32768 to 32767)
485int32 the set of all signed 32-bit integers (-2147483648 to 2147483647)
486int64 the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700487
Robert Griesemerc2d55862009-02-19 16:49:10 -0800488float32 the set of all valid IEEE-754 32-bit floating point numbers
489float64 the set of all valid IEEE-754 64-bit floating point numbers
Rob Pike5af7de32009-02-24 15:17:59 -0800490
491byte familiar alias for uint8
Robert Griesemerc2d55862009-02-19 16:49:10 -0800492</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700493
Rob Pike5af7de32009-02-24 15:17:59 -0800494<p>
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -0800495Integer types are represented in the usual binary format; the value of
496an n-bit integer is n bits wide. A negative signed integer is represented
497as the two's complement of its absolute value.
Rob Pike5af7de32009-02-24 15:17:59 -0800498</p>
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -0800499
Rob Pike5af7de32009-02-24 15:17:59 -0800500<p>
501There is also a set of architecture-independent basic numeric types
502whose size depends on the architecture:
503</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700504
Rob Pikeff70f092009-02-20 13:36:14 -0800505<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -0800506uint at least 32 bits, at most the size of the largest uint type
507int at least 32 bits, at most the size of the largest int type
508float at least 32 bits, at most the size of the largest float type
509uintptr smallest uint type large enough to store the uninterpreted
510 bits of a pointer value
511</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700512
Robert Griesemerc2d55862009-02-19 16:49:10 -0800513<p>
Rob Pikeda389742009-03-02 19:13:40 -0800514To avoid portability issues all numeric types are distinct except
515<code>byte</code>, which is an alias for <code>uint8</code>.
516Conversions
Rob Pike5af7de32009-02-24 15:17:59 -0800517are required when different numeric types are mixed in an expression
518or assignment. For instance, <code>int32</code> and <code>int</code>
Russ Cox5958dd62009-03-04 17:19:21 -0800519are not the same type even though they may have the same size on a
Rob Pike5af7de32009-02-24 15:17:59 -0800520particular architecture.
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700521
522
Robert Griesemerc2d55862009-02-19 16:49:10 -0800523<h3>Booleans</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700524
Rob Pike5af7de32009-02-24 15:17:59 -0800525The type <code>bool</code> comprises the Boolean truth values
526represented by the predeclared constants <code>true</code>
527and <code>false</code>.
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700528
529
Robert Griesemerc2d55862009-02-19 16:49:10 -0800530<h3>Strings</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700531
Rob Pike4501d342009-02-19 17:31:36 -0800532<p>
Rob Pike5af7de32009-02-24 15:17:59 -0800533The <code>string</code> type represents the set of textual string values.
534Strings behave like arrays of bytes but are immutable: once created,
535it is impossible to change the contents of a string.
536
537<p>
538The elements of strings have type <code>byte</code> and may be
539accessed using the usual indexing operations (§Indexes). It is
540illegal to take the address of such an element, that is, even if
541<code>s[i]</code> is the <code>i</code><sup>th</sup> byte of a
542string, <code>&amp;s[i]</code> is invalid. The length of a string
543can be computed by the function <code>len(s1)</code>.
Rob Pike4501d342009-02-19 17:31:36 -0800544</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700545
Rob Pike5af7de32009-02-24 15:17:59 -0800546<p>
Rob Pikecdbf6192009-02-24 17:47:45 -0800547A sequence of string literals is concatenated into a single string.
Rob Pike5af7de32009-02-24 15:17:59 -0800548</p>
549<pre class="grammar">
Rob Pikecdbf6192009-02-24 17:47:45 -0800550StringLit = string_lit { string_lit } .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800551</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700552
Robert Griesemerc2d55862009-02-19 16:49:10 -0800553<pre>
Rob Pike8f2330d2009-02-25 16:20:44 -0800554"Alea iacta est."
555"Alea " /* The die */ `iacta est` /* is cast */ "."
Robert Griesemerc2d55862009-02-19 16:49:10 -0800556</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700557
Russ Cox461dd912009-03-04 14:44:51 -0800558<h3>Array types</h3>
559
560<p>
561An array is a numbered sequence of elements of a single
562type, called the element type, which must be complete
563(§Types). The number of elements is called the length and is never
564negative.
565</p>
566
567<pre class="grammar">
568ArrayType = "[" ArrayLength "]" ElementType .
569ArrayLength = Expression .
570ElementType = CompleteType .
571</pre>
572
573<p>
574The length is part of the array's type and must must be a constant
575expression (§Constant expressions) that evaluates to a non-negative
576integer value. The length of array <code>a</code> can be discovered
577using the built-in function <code>len(a)</code>, which is a
578compile-time constant. The elements can be indexed by integer
579indices 0 through the <code>len(a)-1</code> (§Indexes).
580</p>
581
582<pre>
583[32]byte
584[2*N] struct { x, y int32 }
585[1000]*float64
586</pre>
587
588<h3>Slice types</h3>
589
590<p>
591A slice is a reference to a contiguous segment of an array and
592contains a numbered sequence of elements from that array. A slice
593type denotes the set of all slices of arrays of its element type.
594A slice value may be <code>nil</code>.
595</p>
596
597<pre class="grammar">
598SliceType = "[" "]" ElementType .
599</pre>
600
601<p>
602Like arrays, slices are indexable and have a length. The length of a
603slice <code>s</code> can be discovered by the built-in function
604<code>len(s)</code>; unlike with arrays it may change during
605execution. The elements can be addressed by integer indices 0
606through <code>len(s)-1</code> (§Indexes). The slice index of a
607given element may be less than the index of the same element in the
608underlying array.
609</p>
610<p>
611A slice, once initialized, is always associated with an underlying
612array that holds its elements. A slice therfore shares storage
613with its array and with other slices of the same array; by contrast,
614distinct arrays always represent distinct storage.
615</p>
616<p>
617The array underlying a slice may extend past the end of the slice.
Russ Cox5958dd62009-03-04 17:19:21 -0800618The <i>capacity</i> is a measure of that extent: it is the sum of
Russ Cox461dd912009-03-04 14:44:51 -0800619the length of the slice and the length of the array beyond the slice;
620a slice of length up to that capacity can be created by `slicing' a new
621one from the original slice (§Slices).
622The capacity of a slice <code>a</code> can be discovered using the
623built-in function
624</p>
625
626<pre>
627cap(s)
628</pre>
629
630<p>
631and the relationship between <code>len()</code> and <code>cap()</code> is:
632</p>
633
634<pre>
6350 <= len(a) <= cap(a)
636</pre>
637
638<p>
639The value of an uninitialized slice is <code>nil</code>.
640The length and capacity of a <code>nil</code> slice
641are 0. A new, initialized slice value for a given element type <code>T</code> is
642made using the built-in function <code>make</code>, which takes a slice type
643and parameters specifying the length and optionally the capacity:
644</p>
645
646<pre>
647make([]T, length)
648make([]T, length, capacity)
649</pre>
Russ Cox5958dd62009-03-04 17:19:21 -0800650
Russ Cox461dd912009-03-04 14:44:51 -0800651<p>
652The <code>make()</code> call allocates a new, hidden array to which the returned
653slice value refers. That is, calling <code>make</code>
654</p>
655
656<pre>
657make([]T, length, capacity)
658</pre>
659
660<p>
661produces the same slice as allocating an array and slicing it, so these two examples
662result in the same slice:
663</p>
664
665<pre>
666make([]int, 50, 100)
667new([100]int)[0:50]
668</pre>
669
670
Robert Griesemerc2d55862009-02-19 16:49:10 -0800671<h3>Struct types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700672
Rob Pike5af7de32009-02-24 15:17:59 -0800673<p>
674A struct is a sequence of named
675elements, called fields, with various types. A struct type declares
Rob Pikeda389742009-03-02 19:13:40 -0800676an identifier and type for each field. Within a struct, field identifiers
Rob Pike5af7de32009-02-24 15:17:59 -0800677must be unique and field types must be complete (§Types).
678</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700679
Rob Pikeff70f092009-02-20 13:36:14 -0800680<pre class="grammar">
Russ Cox461dd912009-03-04 14:44:51 -0800681StructType = "struct" "{" [ FieldDeclList ] "}" .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800682FieldDeclList = FieldDecl { ";" FieldDecl } [ ";" ] .
683FieldDecl = (IdentifierList CompleteType | [ "*" ] TypeName) [ Tag ] .
684Tag = StringLit .
685</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700686
Robert Griesemerc2d55862009-02-19 16:49:10 -0800687<pre>
688// An empty struct.
689struct {}
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700690
Robert Griesemerc2d55862009-02-19 16:49:10 -0800691// A struct with 5 fields.
692struct {
693 x, y int;
694 u float;
695 A *[]int;
696 F func();
697}
698</pre>
Robert Griesemer1f3e8422008-09-29 18:41:30 -0700699
Rob Pike5af7de32009-02-24 15:17:59 -0800700<p>
701A field declared with a type but no field identifier is an <i>anonymous field</i>.
702Such a field type must be specified as
Rob Pikeda389742009-03-02 19:13:40 -0800703a type name <code>T</code> or as a pointer to a type name <code>*T</code>,
704and <code>T</code> itself, may not be
Robert Griesemer83c17602009-01-16 14:12:50 -0800705a pointer or interface type. The unqualified type name acts as the field identifier.
Rob Pike5af7de32009-02-24 15:17:59 -0800706</p>
Robert Griesemer1f3e8422008-09-29 18:41:30 -0700707
Robert Griesemerc2d55862009-02-19 16:49:10 -0800708<pre>
709// A struct with four anonymous fields of type T1, *T2, P.T3 and *P.T4
710struct {
711 T1; // the field name is T1
712 *T2; // the field name is T2
Rob Pikeda389742009-03-02 19:13:40 -0800713 P.T3; // the field name is T3
714 *P.T4; // the field name is T4
Russ Cox5958dd62009-03-04 17:19:21 -0800715 x, y int;
Robert Griesemerc2d55862009-02-19 16:49:10 -0800716}
717</pre>
Robert Griesemer1f3e8422008-09-29 18:41:30 -0700718
Rob Pike5af7de32009-02-24 15:17:59 -0800719<p>
Robert Griesemer7abfcd92008-10-07 17:14:30 -0700720The unqualified type name of an anonymous field must not conflict with the
721field identifier (or unqualified type name for an anonymous field) of any
Robert Griesemer071c91b2008-10-23 12:04:45 -0700722other field within the struct. The following declaration is illegal:
Rob Pike5af7de32009-02-24 15:17:59 -0800723</p>
Robert Griesemer071c91b2008-10-23 12:04:45 -0700724
Robert Griesemerc2d55862009-02-19 16:49:10 -0800725<pre>
726struct {
727 T; // conflicts with anonymous field *T and *P.T
728 *T; // conflicts with anonymous field T and *P.T
729 *P.T; // conflicts with anonymous field T and *T
730}
731</pre>
Robert Griesemer1f3e8422008-09-29 18:41:30 -0700732
Robert Griesemerc2d55862009-02-19 16:49:10 -0800733<p>
Rob Pike5af7de32009-02-24 15:17:59 -0800734Fields and methods (§Method declarations) of an anonymous field are
735promoted to be ordinary fields and methods of the struct (§Selectors).
736</p>
737<p>
738A field declaration may be followed by an optional string literal <i>tag</i>, which
739becomes an attribute for all the identifiers in the corresponding
740field declaration. The tags are made
741visible through a reflection library (TODO: reference?)
742but are otherwise ignored.
743</p>
Robert Griesemer2e90e542008-10-30 15:52:37 -0700744
Robert Griesemerc2d55862009-02-19 16:49:10 -0800745<pre>
746// A struct corresponding to the EventIdMessage protocol buffer.
Rob Pikecdbf6192009-02-24 17:47:45 -0800747// The tag strings define the protocol buffer field numbers.
Robert Griesemerc2d55862009-02-19 16:49:10 -0800748struct {
Rob Pike5af7de32009-02-24 15:17:59 -0800749 time_usec uint64 "field 1";
750 server_ip uint32 "field 2";
751 process_id uint32 "field 3";
Robert Griesemerc2d55862009-02-19 16:49:10 -0800752}
753</pre>
Robert Griesemer2e90e542008-10-30 15:52:37 -0700754
Robert Griesemerc2d55862009-02-19 16:49:10 -0800755<h3>Pointer types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700756
Rob Pike5af7de32009-02-24 15:17:59 -0800757<p>
Robert Griesemer4dc25282008-09-09 10:37:19 -0700758A pointer type denotes the set of all pointers to variables of a given
Rob Pikeda389742009-03-02 19:13:40 -0800759type, called the <i>base type</i> of the pointer.
Rob Pike8f2330d2009-02-25 16:20:44 -0800760A pointer value may be <code>nil</code>.
Rob Pike5af7de32009-02-24 15:17:59 -0800761</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700762
Rob Pikeff70f092009-02-20 13:36:14 -0800763<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -0800764PointerType = "*" BaseType .
765BaseType = Type .
766</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700767
Robert Griesemerc2d55862009-02-19 16:49:10 -0800768<pre>
769*int
Rob Pike8f2330d2009-02-25 16:20:44 -0800770*map[string] *chan int
Robert Griesemerc2d55862009-02-19 16:49:10 -0800771</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700772
Robert Griesemerc2d55862009-02-19 16:49:10 -0800773<h3>Function types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700774
Rob Pike8f2330d2009-02-25 16:20:44 -0800775<p>
Robert Griesemer7231ceb2008-09-08 15:01:04 -0700776A function type denotes the set of all functions with the same parameter
Rob Pike8f2330d2009-02-25 16:20:44 -0800777and result types.
778A function value may be <code>nil</code>.
779</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700780
Rob Pikeff70f092009-02-20 13:36:14 -0800781<pre class="grammar">
Rob Pike8f2330d2009-02-25 16:20:44 -0800782FunctionType = "func" Signature .
783Signature = Parameters [ Result ] .
784Result = Parameters | CompleteType .
785Parameters = "(" [ ParameterList ] ")" .
786ParameterList = ParameterDecl { "," ParameterDecl } .
787ParameterDecl = [ IdentifierList ] ( CompleteType | "..." ) .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800788</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700789
Robert Griesemerc2d55862009-02-19 16:49:10 -0800790<p>
Rob Pike8f2330d2009-02-25 16:20:44 -0800791Within a list of parameters or results, the names (IdentifierList)
792must either all be present or all be absent. If present, each name
793stands for one item (parameter or result) of the specified type; if absent, each
794type stands for one item of that type. Parameter and result
795lists are always parenthesized except that if there is exactly
796one unnamed result that is not a function type it may writen as an unparenthesized type.
797The types of parameters and results must be complete.
798(TODO: is completeness necessary?)
799</p>
800<p>
801For the last parameter only, instead of a type one may write
802<code>...</code> to indicate that the function may be invoked with
Rob Pikeda389742009-03-02 19:13:40 -0800803zero or more additional arguments of any
Rob Pike8f2330d2009-02-25 16:20:44 -0800804type. If parameters of such a function are named, the final identifier
805list must be a single name, that of the <code>...</code> parameter.
806</p>
Robert Griesemer2bfa9572008-10-24 13:13:12 -0700807
Robert Griesemerc2d55862009-02-19 16:49:10 -0800808<pre>
809func ()
810func (x int)
811func () int
812func (string, float, ...)
813func (a, b int, z float) bool
814func (a, b int, z float) (bool)
815func (a, b int, z float, opt ...) (success bool)
816func (int, int, float) (float, *[]int)
Robert Griesemerc2d55862009-02-19 16:49:10 -0800817func (n int) (func (p* T))
818</pre>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -0800819
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700820
Robert Griesemerc2d55862009-02-19 16:49:10 -0800821<h3>Interface types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700822
Rob Pike8f2330d2009-02-25 16:20:44 -0800823<p>
824An interface type specifies an unordered set of methods. A variable
825of interface type can store, dynamically, any value that implements
826at least that set of methods.
827An interface value may be <code>nil</code>.
828</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700829
Rob Pikeff70f092009-02-20 13:36:14 -0800830<pre class="grammar">
Russ Cox461dd912009-03-04 14:44:51 -0800831InterfaceType = "interface" "{" [ MethodSpecList ] "}" .
Rob Pike8f2330d2009-02-25 16:20:44 -0800832MethodSpecList = MethodSpec { ";" MethodSpec } [ ";" ] .
833MethodSpec = IdentifierList Signature | InterfaceTypeName .
834InterfaceTypeName = TypeName .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800835</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700836
Robert Griesemerc2d55862009-02-19 16:49:10 -0800837<pre>
Rob Pike8f2330d2009-02-25 16:20:44 -0800838// A simple File interface
Robert Griesemerc2d55862009-02-19 16:49:10 -0800839interface {
840 Read, Write (b Buffer) bool;
841 Close ();
842}
843</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700844
Rob Pike8f2330d2009-02-25 16:20:44 -0800845<p>
846Any type (including interface types) whose interface includes,
847possibly as a subset, the complete set of methods of an interface <code>I</code>
848is said to implement interface <code>I</code>.
849For instance, if two types <code>S1</code> and <code>S2</code>
850have the methods
851</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700852
Robert Griesemerc2d55862009-02-19 16:49:10 -0800853<pre>
854func (p T) Read(b Buffer) bool { return ... }
855func (p T) Write(b Buffer) bool { return ... }
856func (p T) Close() { ... }
857</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700858
Rob Pike8f2330d2009-02-25 16:20:44 -0800859<p>
860(where <code>T</code> stands for either <code>S1</code> or <code>S2</code>)
861then the <code>File</code> interface is implemented by both <code>S1</code> and
862<code>S2</code>, regardless of what other methods
863<code>S1</code> and <code>S2</code> may have or share.
864</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700865
Rob Pike8f2330d2009-02-25 16:20:44 -0800866<p>
867A type implements any interface comprising any subset of its methods
868and may therefore implement several distinct interfaces. For
869instance, all types implement the <i>empty interface</i>:
870</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700871
Robert Griesemerc2d55862009-02-19 16:49:10 -0800872<pre>
Rob Pike8f2330d2009-02-25 16:20:44 -0800873interface { }
Robert Griesemerc2d55862009-02-19 16:49:10 -0800874</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700875
Rob Pike8f2330d2009-02-25 16:20:44 -0800876<p>
877Similarly, consider this interface specification,
878which appears within a type declaration (§Type declarations)
879to define an interface called <code>Lock</code>:
880</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700881
Robert Griesemerc2d55862009-02-19 16:49:10 -0800882<pre>
883type Lock interface {
884 Lock, Unlock ();
885}
886</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700887
Rob Pike8f2330d2009-02-25 16:20:44 -0800888<p>
889If <code>S1</code> and <code>S2</code> also implement
890</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700891
Robert Griesemerc2d55862009-02-19 16:49:10 -0800892<pre>
893func (p T) Lock() { ... }
894func (p T) Unlock() { ... }
895</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700896
Robert Griesemerc2d55862009-02-19 16:49:10 -0800897<p>
Rob Pike8f2330d2009-02-25 16:20:44 -0800898they implement the <code>Lock</code> interface as well
899as the <code>File</code> interface.
900</p>
901<p>
902An interface may contain an interface type name <code>T</code>
903in place of a method specification.
904In this notation, <code>T</code> must denote a different, complete interface type
905and the effect is equivalent to enumerating the methods of <code>T</code> explicitly
906in the interface.
907</p>
Robert Griesemer38c232f2009-02-11 15:09:15 -0800908
Robert Griesemerc2d55862009-02-19 16:49:10 -0800909<pre>
910type ReadWrite interface {
911 Read, Write (b Buffer) bool;
912}
Robert Griesemer38c232f2009-02-11 15:09:15 -0800913
Robert Griesemerc2d55862009-02-19 16:49:10 -0800914type File interface {
915 ReadWrite; // same as enumerating the methods in ReadWrite
916 Lock; // same as enumerating the methods in Lock
917 Close();
918}
919</pre>
Robert Griesemer38c232f2009-02-11 15:09:15 -0800920
Robert Griesemerc2d55862009-02-19 16:49:10 -0800921<h3>Map types</h3>
Robert Griesemera3294712009-01-05 11:17:26 -0800922
Rob Pike8f2330d2009-02-25 16:20:44 -0800923<p>
924A map is an unordered group of elements of one type, called the
925value type, indexed by a set of unique <i>keys</i> of another type,
926called the key type. Both key and value types must be complete.
927(§Types).
928(TODO: is completeness necessary here?)
929A map value may be <code>nil</code>.
930
931</p>
Robert Griesemera3294712009-01-05 11:17:26 -0800932
Rob Pikeff70f092009-02-20 13:36:14 -0800933<pre class="grammar">
Rob Pike8f2330d2009-02-25 16:20:44 -0800934MapType = "map" "[" KeyType "]" ValueType .
935KeyType = CompleteType .
936ValueType = CompleteType .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800937</pre>
Robert Griesemera3294712009-01-05 11:17:26 -0800938
Robert Griesemerc2d55862009-02-19 16:49:10 -0800939<p>
Rob Pike8f2330d2009-02-25 16:20:44 -0800940The comparison operators <code>==</code> and <code>!=</code>
941(§Comparison operators) must be fully defined for operands of the
942key type; thus the key type must be a basic, pointer, interface,
943map, or channel type. If the key type is an interface type, these
944comparison operators must be defined for the dynamic key values;
945failure will cause a run-time error.
946
947</p>
Robert Griesemera3294712009-01-05 11:17:26 -0800948
Robert Griesemerc2d55862009-02-19 16:49:10 -0800949<pre>
950map [string] int
951map [*T] struct { x, y float }
952map [string] interface {}
953</pre>
Robert Griesemera3294712009-01-05 11:17:26 -0800954
Rob Pike5af7de32009-02-24 15:17:59 -0800955<p>
Rob Pike8f2330d2009-02-25 16:20:44 -0800956The number of elements is called the length and is never negative.
957The length of a map <code>m</code> can be discovered using the
958built-in function <code>len(m)</code> and may change during execution.
Rob Pikeda389742009-03-02 19:13:40 -0800959The value of an uninitialized map is <code>nil</code>.
Rob Pike5af7de32009-02-24 15:17:59 -0800960</p>
Rob Pike8f2330d2009-02-25 16:20:44 -0800961<p>
962Upon creation, a map is empty. Values may be added and removed
963during execution using special forms of assignment (§Assignments).
964A new, empty map value is made using the built-in
965function <code>make</code>, which takes the map type and an optional
Rob Pikeda389742009-03-02 19:13:40 -0800966capacity hint as arguments:
Rob Pike8f2330d2009-02-25 16:20:44 -0800967</p>
968
969<pre>
Rob Pikeda389742009-03-02 19:13:40 -0800970make(map[string] int)
971make(map[string] int, 100)
Rob Pike8f2330d2009-02-25 16:20:44 -0800972</pre>
Robert Griesemera3294712009-01-05 11:17:26 -0800973
Rob Pikeda389742009-03-02 19:13:40 -0800974<p>
975The initial capacity does not bound its size:
976maps grow to accommodate the number of items
977stored in them.
978</p>
979
Robert Griesemerc2d55862009-02-19 16:49:10 -0800980<h3>Channel types</h3>
Robert Griesemera3294712009-01-05 11:17:26 -0800981
Rob Pike8f2330d2009-02-25 16:20:44 -0800982<p>
Robert Griesemera3294712009-01-05 11:17:26 -0800983A channel provides a mechanism for two concurrently executing functions
Rob Pike8f2330d2009-02-25 16:20:44 -0800984to synchronize execution and communicate by passing a value of a
985specified element type. The element type must be complete (§Types).
986(TODO: is completeness necessary here?)
987A channel value may be <code>nil</code>.
988</p>
Robert Griesemera3294712009-01-05 11:17:26 -0800989
Rob Pikeff70f092009-02-20 13:36:14 -0800990<pre class="grammar">
Rob Pike8f2330d2009-02-25 16:20:44 -0800991ChannelType = Channel | SendChannel | RecvChannel .
992Channel = "chan" ValueType .
993SendChannel = "chan" "&lt;-" ValueType .
994RecvChannel = "&lt;-" "chan" ValueType .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800995</pre>
Robert Griesemera3294712009-01-05 11:17:26 -0800996
Rob Pike8f2330d2009-02-25 16:20:44 -0800997<p>
998Upon creation, a channel can be used both to send and to receive values.
Robert Griesemera3294712009-01-05 11:17:26 -0800999By conversion or assignment, a channel may be constrained only to send or
Rob Pike8f2330d2009-02-25 16:20:44 -08001000to receive. This constraint is called a channel's <i>direction</i>; either
1001<i>send</i>, <i>receive</i>, or <i>bi-directional</i> (unconstrained).
1002</p>
Robert Griesemera3294712009-01-05 11:17:26 -08001003
Robert Griesemerc2d55862009-02-19 16:49:10 -08001004<pre>
Rob Pike46596852009-03-02 16:17:29 -08001005chan T // can be used to send and receive values of type T
Robert Griesemerc2d55862009-02-19 16:49:10 -08001006chan &lt;- float // can only be used to send floats
Rob Pike46596852009-03-02 16:17:29 -08001007&lt;-chan int // can only be used to receive ints
Robert Griesemerc2d55862009-02-19 16:49:10 -08001008</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001009
Rob Pike8f2330d2009-02-25 16:20:44 -08001010<p>
1011The value of an uninitialized channel is <code>nil</code>. A new, initialized channel
1012value is made using the built-in function <code>make</code>,
Robert Griesemer633957b2009-01-06 13:23:20 -08001013which takes the channel type and an optional capacity as arguments:
Rob Pike8f2330d2009-02-25 16:20:44 -08001014</p>
Robert Griesemera3294712009-01-05 11:17:26 -08001015
Rob Pike94b67eb2009-03-24 17:40:47 -07001016
Robert Griesemerc2d55862009-02-19 16:49:10 -08001017<pre>
Rob Pikeda389742009-03-02 19:13:40 -08001018make(chan int, 100)
Robert Griesemerc2d55862009-02-19 16:49:10 -08001019</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001020
Rob Pike8f2330d2009-02-25 16:20:44 -08001021<p>
1022The capacity, in number of elements, sets the size of the buffer in the channel. If the
Robert Griesemera3294712009-01-05 11:17:26 -08001023capacity is greater than zero, the channel is asynchronous and, provided the
Rob Pike8f2330d2009-02-25 16:20:44 -08001024buffer is not full, sends can succeed without blocking. If the capacity is zero
1025or absent, the communication succeeds only when both a sender and receiver are ready.
1026</p>
Robert Griesemera3294712009-01-05 11:17:26 -08001027
Rob Pike94b67eb2009-03-24 17:40:47 -07001028<p>
1029For a channel <code>c</code>, the predefined function <code>close(c)</code>
1030marks the channel as unable to accept more
1031values through a send operation. After any previously
1032sent values have been received, receives will return
1033the zero value for the channel's type. After at least one such zero value has been
1034received, <code>closed(c)</code> returns true.
1035</p>
1036
Rob Pike8f2330d2009-02-25 16:20:44 -08001037<h2>General properties of types and values</h2>
Robert Griesemer434c6052008-11-07 13:34:37 -08001038
Rob Pike4501d342009-02-19 17:31:36 -08001039<p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001040Types may be <i>different</i>, <i>structurally equal</i> (or just <i>equal</i>),
1041or <i>identical</i>.
1042Go is <i>type safe</i>: different types cannot be mixed
1043in binary operations and values cannot be assigned to variables of different
Rob Pikeda389742009-03-02 19:13:40 -08001044types. Values can be assigned to variables of equal type.
Rob Pike8f2330d2009-02-25 16:20:44 -08001045</p>
1046
1047<h3>Type equality and identity </h3>
1048
Robert Griesemerc2d55862009-02-19 16:49:10 -08001049<p>
Robert Griesemer434c6052008-11-07 13:34:37 -08001050Two type names denote equal types if the types in the corresponding declarations
Rob Pike8f2330d2009-02-25 16:20:44 -08001051are equal (§Declarations and Scope).
1052Two type literals specify equal types if they have the same
1053literal structure and corresponding components have equal types.
1054In detail:
Rob Pike4501d342009-02-19 17:31:36 -08001055</p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001056
Robert Griesemerc2d55862009-02-19 16:49:10 -08001057<ul>
Rob Pike8f2330d2009-02-25 16:20:44 -08001058 <li>Two pointer types are equal if they have equal base types.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001059
Rob Pike8f2330d2009-02-25 16:20:44 -08001060 <li>Two array types are equal if they have equal element types and
1061 the same array length.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001062
Rob Pike8f2330d2009-02-25 16:20:44 -08001063 <li>Two struct types are equal if they have the same sequence of fields,
1064 with the same names and equal types. Two anonymous fields are
1065 considered to have the same name.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001066
Robert Griesemerc2d55862009-02-19 16:49:10 -08001067 <li>Two function types are equal if they have the same number of parameters
Robert Griesemer434c6052008-11-07 13:34:37 -08001068 and result values and if corresponding parameter and result types are
Rob Pike8f2330d2009-02-25 16:20:44 -08001069 the same. All "..." parameters have equal type.
1070 Parameter and result names are not required to match.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001071
Rob Pike8f2330d2009-02-25 16:20:44 -08001072 <li>Two slice types are equal if they have equal element types.</li>
Robert Griesemera3294712009-01-05 11:17:26 -08001073
Robert Griesemerc2d55862009-02-19 16:49:10 -08001074 <li>Two channel types are equal if they have equal value types and
Rob Pike8f2330d2009-02-25 16:20:44 -08001075 the same direction.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001076
Rob Pike8f2330d2009-02-25 16:20:44 -08001077 <li>Two map types are equal if they have equal key and value types.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001078
Robert Griesemerc2d55862009-02-19 16:49:10 -08001079 <li>Two interface types are equal if they have the same set of methods
Rob Pike8f2330d2009-02-25 16:20:44 -08001080 with the same names and equal function types. The order
1081 of the methods is irrelevant.</li>
Robert Griesemerc2d55862009-02-19 16:49:10 -08001082</ul>
Robert Griesemer434c6052008-11-07 13:34:37 -08001083
Robert Griesemerc2d55862009-02-19 16:49:10 -08001084<p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001085Type identity is more stringent than type equality.
1086It requires for type names
1087that they originate in the same type declaration, while for equality it requires
1088only that they originate in equal type declarations.
1089Also, the names of parameters and results must match for function types.
1090In all other respects, the definition of type identity is the
1091same as for type equality listed above but with ``identical''
1092substitued for ``equal''.
Rob Pike4501d342009-02-19 17:31:36 -08001093</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08001094<p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001095By definition, identical types are also equal types.
1096Two types are different if they are not equal.
Rob Pike4501d342009-02-19 17:31:36 -08001097</p>
Robert Griesemer434c6052008-11-07 13:34:37 -08001098
Robert Griesemerc2d55862009-02-19 16:49:10 -08001099<p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001100Given the declarations
1101</p>
Robert Griesemer434c6052008-11-07 13:34:37 -08001102
Robert Griesemerc2d55862009-02-19 16:49:10 -08001103<pre>
1104type (
1105 T0 []string;
1106 T1 []string
1107 T2 struct { a, b int };
1108 T3 struct { a, c int };
1109 T4 func (int, float) *T0
1110 T5 func (x int, y float) *[]string
1111)
1112</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -08001113
Rob Pike8f2330d2009-02-25 16:20:44 -08001114<p>
Russ Cox461dd912009-03-04 14:44:51 -08001115these types are equal:
Rob Pike8f2330d2009-02-25 16:20:44 -08001116</p>
Robert Griesemer434c6052008-11-07 13:34:37 -08001117
Robert Griesemerc2d55862009-02-19 16:49:10 -08001118<pre>
1119T0 and T0
Rob Pike8f2330d2009-02-25 16:20:44 -08001120T0 and T1
Robert Griesemerc2d55862009-02-19 16:49:10 -08001121T0 and []string
Robert Griesemerc2d55862009-02-19 16:49:10 -08001122T4 and T5
Russ Cox461dd912009-03-04 14:44:51 -08001123T3 and struct { a int; c int }
Robert Griesemerc2d55862009-02-19 16:49:10 -08001124</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -08001125
Rob Pike8f2330d2009-02-25 16:20:44 -08001126<p>
Russ Cox461dd912009-03-04 14:44:51 -08001127<code>T2</code> and <code>T3</code> are not equal because
1128they have different field names.
1129</p>
1130
1131<p>
1132These types are identical:
Rob Pike8f2330d2009-02-25 16:20:44 -08001133</p>
Robert Griesemer434c6052008-11-07 13:34:37 -08001134
Robert Griesemerc2d55862009-02-19 16:49:10 -08001135<pre>
1136T0 and T0
1137[]int and []int
1138struct { a, b *T5 } and struct { a, b *T5 }
1139</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -08001140
Rob Pike8f2330d2009-02-25 16:20:44 -08001141<p>
1142<code>T0</code> and <code>T1</code> are equal but not
1143identical because they have distinct declarations.
1144</p>
Robert Griesemer434c6052008-11-07 13:34:37 -08001145
Rob Pike5af7de32009-02-24 15:17:59 -08001146<h3>Assignment compatibility</h3>
1147
Rob Pike5af7de32009-02-24 15:17:59 -08001148<p>
1149Values of any type may always be assigned to variables
1150of equal static type. Some types and values have conditions under which they may
1151be assigned to different types:
1152</p>
1153<ul>
1154<li>
1155The predeclared constant <code>nil</code> can be assigned to any
1156pointer, function, slice, map, channel, or interface variable.
1157<li>
Russ Coxbcdc2472009-04-16 23:06:48 -07001158A pointer to an array can be assigned to a slice variable with equal element type.
1159The slice variable then refers to the original array; the data is not copied.
Rob Pike5af7de32009-02-24 15:17:59 -08001160</li>
1161<li>
Rob Pikeda389742009-03-02 19:13:40 -08001162A value can be assigned to an interface variable if the static
Rob Pike5af7de32009-02-24 15:17:59 -08001163type of the value implements the interface.
1164</li>
1165<li>
1166A value of bidirectional channel type can be assigned to any channel
1167variable of equal channel value type.
1168</li>
1169</ul>
1170
1171<h3>Comparison compatibility</h3>
1172
1173<p>
1174Values of any type may be compared to other values of equal static
1175type. Values of numeric and string type may be compared using the
1176full range of comparison operators as described in §Comparison operators;
1177booleans may be compared only for equality or inequality.
1178</p>
1179
1180<p>
1181Values of composite type may be
1182compared for equality or inequality using the <code>==</code> and
1183<code>!=</code> operators, with the following provisos:
1184</p>
1185<ul>
1186<li>
1187Arrays and structs may not be compared to anything.
1188</li>
1189<li>
Rob Pikeda389742009-03-02 19:13:40 -08001190A slice value may only be compared explicitly against <code>nil</code>.
1191A slice value is equal to <code>nil</code> if it has been assigned the explicit
Rob Pike5af7de32009-02-24 15:17:59 -08001192value <code>nil</code> or if it is a variable (or array element,
1193field, etc.) that has not been modified since it was created
1194uninitialized.
1195</li>
1196<li>
1197Similarly, an interface value is equal to <code>nil</code> if it has
1198been assigned the explicit value <code>nil</code> or if it is a
1199variable (or array element, field, etc.) that has not been modified
1200since it was created uninitialized.
1201</li>
1202<li>
1203For types that can be compared to <code>nil</code>,
1204two values of the same type are equal if they both equal <code>nil</code>,
1205unequal if one equals <code>nil</code> and one does not.
1206</li>
1207<li>
1208Pointer values are equal if they point to the same location.
1209</li>
1210<li>
Rob Pikeda389742009-03-02 19:13:40 -08001211Function values are equal if they refer to the same function.
Rob Pike5af7de32009-02-24 15:17:59 -08001212</li>
1213<li>
Rob Pikeda389742009-03-02 19:13:40 -08001214Channel and map values are equal if they were created by the same call to <code>make</code>
Rob Pike5af7de32009-02-24 15:17:59 -08001215(§Making slices, maps, and channels).
1216</li>
1217<li>
Rob Pikeda389742009-03-02 19:13:40 -08001218Interface values may be compared if they have the same static type.
1219They will be equal only if they have the same dynamic type and the underlying values are equal.
Rob Pike5af7de32009-02-24 15:17:59 -08001220</li>
1221</ul>
Rob Pikeff70f092009-02-20 13:36:14 -08001222<hr/>
Robert Griesemer434c6052008-11-07 13:34:37 -08001223
Rob Pikea9ed30f2009-02-23 19:26:07 -08001224
1225<h2>Declarations and Scope</h2>
1226
1227<p>
1228A declaration binds an identifier to a language entity such as
1229a variable or function and specifies properties such as its type.
1230Every identifier in a program must be declared.
1231</p>
1232
1233<pre class="grammar">
1234Declaration = ConstDecl | TypeDecl | VarDecl | FunctionDecl | MethodDecl .
1235</pre>
Russ Cox5958dd62009-03-04 17:19:21 -08001236
Rob Pikea9ed30f2009-02-23 19:26:07 -08001237<p>
1238The <i>scope</i> of an identifier is the extent of source text within which the
1239identifier denotes the bound entity. No identifier may be declared twice in a
1240single scope, but inner blocks can declare a new entity with the same
1241identifier, in which case the scope created by the outer declaration excludes
1242that created by the inner.
1243</p>
1244<p>
1245There are levels of scoping in effect before each source file is compiled.
1246In order from outermost to innermost:
1247</p>
1248<ol>
1249 <li>The <i>universe</i> scope contains all predeclared identifiers.</li>
1250 <li>An implicit scope contains only the package name.</li>
1251 <li>The <i>package-level</i> scope surrounds all declarations at the
1252 top level of the file, that is, outside the body of any
1253 function or method. That scope is shared across all
1254 source files within the package (§Packages), allowing
1255 package-level identifiers to be shared between source
1256 files.</li>
1257</ol>
1258
1259<p>
1260The scope of an identifier depends on the entity declared:
1261</p>
1262
1263<ol>
1264 <li> The scope of predeclared identifiers is the universe scope.</li>
1265
Russ Cox5958dd62009-03-04 17:19:21 -08001266 <li> The scope of an identifier denoting a type, function or package
Rob Pikea9ed30f2009-02-23 19:26:07 -08001267 extends from the point of the identifier in the declaration
1268 to the end of the innermost surrounding block.</li>
1269
1270 <li> The scope of a constant or variable extends textually from
Russ Cox5958dd62009-03-04 17:19:21 -08001271 the end of its declaration to the end of the innermost
Rob Pikea9ed30f2009-02-23 19:26:07 -08001272 surrounding block. If the variable is declared in the
Russ Cox5958dd62009-03-04 17:19:21 -08001273 <i>init</i> statement of an <code>if</code>, <code>for</code>,
Rob Pikea9ed30f2009-02-23 19:26:07 -08001274 or <code>switch </code> statement, the
1275 innermost surrounding block is the block associated
1276 with that statement.</li>
1277
1278 <li> The scope of a parameter or result is the body of the
1279 corresponding function.</li>
1280
1281 <li> The scope of a field or method is selectors for the
1282 corresponding type containing the field or method (§Selectors).</li>
1283
Russ Cox5958dd62009-03-04 17:19:21 -08001284 <li> The scope of a label is a special scope emcompassing
Rob Pikea9ed30f2009-02-23 19:26:07 -08001285 the body of the innermost surrounding function, excluding
Rob Pike5af7de32009-02-24 15:17:59 -08001286 nested functions. Labels do not conflict with non-label identifiers.</li>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001287</ol>
1288
1289<h3>Predeclared identifiers</h3>
1290
1291<p>
1292The following identifiers are implicitly declared in the outermost scope:
1293</p>
1294<pre class="grammar">
1295Basic types:
Russ Cox5958dd62009-03-04 17:19:21 -08001296 bool byte float32 float64 int8 int16 int32 int64
1297 string uint8 uint16 uint32 uint64
Rob Pikea9ed30f2009-02-23 19:26:07 -08001298
Rob Pike5af7de32009-02-24 15:17:59 -08001299Architecture-specific convenience types:
Rob Pikea9ed30f2009-02-23 19:26:07 -08001300 float int uint uintptr
1301
1302Constants:
1303 true false iota nil
1304
1305Functions:
Russ Cox5958dd62009-03-04 17:19:21 -08001306 cap len make new panic panicln print println
Rob Pikea9ed30f2009-02-23 19:26:07 -08001307
1308Packages:
Russ Cox5958dd62009-03-04 17:19:21 -08001309 sys (TODO: does sys endure?)
Rob Pikea9ed30f2009-02-23 19:26:07 -08001310</pre>
1311
Rob Pikea9ed30f2009-02-23 19:26:07 -08001312<h3>Exported identifiers</h3>
1313
1314<p>
1315By default, identifiers are visible only within the package in which they are declared.
1316Some identifiers are <i>exported</i> and can be referenced using
1317<i>qualified identifiers</i> in other packages (§Qualified identifiers).
1318If an identifier satisfies these two conditions:
1319</p>
1320<ol>
1321<li>the first character of the identifier's name is a Unicode upper case letter;
1322<li>the identifier is declared at the package level or is a field or method of a type
1323declared at the top level;
1324</ol>
1325<p>
1326it will be exported automatically.
1327</p>
1328
1329<h3>Const declarations</h3>
1330
1331<p>
1332A constant declaration binds a list of identifiers (the names of
1333the constants) to the values of a list of constant expressions
1334(§Constant expressions). The number of identifiers must be equal
1335to the number of expressions, and the n<sup>th</sup> identifier on
1336the left is bound to value of the n<sup>th</sup> expression on the
1337right.
1338</p>
1339
1340<pre class="grammar">
1341ConstDecl = "const" ( ConstSpec | "(" [ ConstSpecList ] ")" ) .
1342ConstSpecList = ConstSpec { ";" ConstSpec } [ ";" ] .
Russ Coxf8ba0f42009-03-12 19:04:56 -07001343ConstSpec = IdentifierList [ [ CompleteType ] "=" ExpressionList ] .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001344
1345IdentifierList = identifier { "," identifier } .
1346ExpressionList = Expression { "," Expression } .
1347
1348CompleteType = Type .
1349</pre>
1350
1351<p>
1352If the type (CompleteType) is omitted, the constants take the
1353individual types of the corresponding expressions, which may be
Rob Pikeda389742009-03-02 19:13:40 -08001354<i>ideal integer</i> or <i>ideal float</i> (§Ideal number). If the type
Rob Pikea9ed30f2009-02-23 19:26:07 -08001355is present, all constants take the type specified, and the types
1356of all the expressions must be assignment-compatible
1357with that type.
1358</p>
1359
1360<pre>
1361const Pi float64 = 3.14159265358979323846
1362const E = 2.718281828
1363const (
1364 size int64 = 1024;
1365 eof = -1;
1366)
1367const a, b, c = 3, 4, "foo" // a = 3, b = 4, c = "foo"
1368const u, v float = 0, 3 // u = 0.0, v = 3.0
1369</pre>
1370
1371<p>
1372Within a parenthesized <code>const</code> declaration list the
1373expression list may be omitted from any but the first declaration.
1374Such an empty list is equivalent to the textual substitution of the
Russ Coxf8ba0f42009-03-12 19:04:56 -07001375first preceding non-empty expression list, and its type if any.
Russ Cox5958dd62009-03-04 17:19:21 -08001376Omitting the list of expressions is therefore equivalent to
1377repeating the previous list. The number of identifiers must be equal
1378to the number of expressions in the previous list.
1379Together with the <code>iota</code> constant generator
Rob Pikea9ed30f2009-02-23 19:26:07 -08001380(§Iota) this mechanism permits light-weight declaration of sequential values:
1381</p>
1382
1383<pre>
1384const (
1385 Sunday = iota;
1386 Monday;
1387 Tuesday;
1388 Wednesday;
1389 Thursday;
1390 Friday;
1391 Partyday;
1392 numberOfDays; // this constant is not exported
1393)
1394</pre>
1395
1396
1397<h3>Iota</h3>
1398
1399<p>
1400Within a constant declaration, the predeclared pseudo-constant
1401<code>iota</code> represents successive integers. It is reset to 0
1402whenever the reserved word <code>const</code> appears in the source
1403and increments with each semicolon. It can be used to construct a
1404set of related constants:
1405</p>
1406
1407<pre>
1408const ( // iota is reset to 0
1409 c0 = iota; // c0 == 0
1410 c1 = iota; // c1 == 1
1411 c2 = iota // c2 == 2
1412)
1413
1414const (
1415 a = 1 << iota; // a == 1 (iota has been reset)
1416 b = 1 << iota; // b == 2
1417 c = 1 << iota; // c == 4
1418)
1419
1420const (
1421 u = iota * 42; // u == 0 (ideal integer)
1422 v float = iota * 42; // v == 42.0 (float)
1423 w = iota * 42; // w == 84 (ideal integer)
1424)
1425
1426const x = iota; // x == 0 (iota has been reset)
1427const y = iota; // y == 0 (iota has been reset)
1428</pre>
1429
1430<p>
1431Within an ExpressionList, the value of each <code>iota</code> is the same because
1432it is only incremented at a semicolon:
1433</p>
1434
1435<pre>
1436const (
1437 bit0, mask0 = 1 << iota, 1 << iota - 1; // bit0 == 1, mask0 == 0
1438 bit1, mask1; // bit1 == 2, mask1 == 1
1439 bit2, mask2; // bit2 == 4, mask2 == 3
1440)
1441</pre>
1442
1443<p>
1444This last example exploits the implicit repetition of the
1445last non-empty expression list.
1446</p>
1447
1448
1449<h3>Type declarations</h3>
1450
1451<p>
1452A type declaration binds an identifier, the <i>type name</i>,
1453to a new type. <font color=red>TODO: what exactly is a "new type"?</font>
1454</p>
1455
1456<pre class="grammar">
1457TypeDecl = "type" ( TypeSpec | "(" [ TypeSpecList ] ")" ) .
1458TypeSpecList = TypeSpec { ";" TypeSpec } [ ";" ] .
Russ Cox461dd912009-03-04 14:44:51 -08001459TypeSpec = identifier ( Type | "struct" | "interface" ) .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001460</pre>
1461
1462<pre>
1463type IntArray [16] int
1464
1465type (
1466 Point struct { x, y float };
1467 Polar Point
1468)
1469
Russ Cox461dd912009-03-04 14:44:51 -08001470type Comparable interface
1471
Rob Pikea9ed30f2009-02-23 19:26:07 -08001472type TreeNode struct {
1473 left, right *TreeNode;
Russ Cox461dd912009-03-04 14:44:51 -08001474 value *Comparable;
Rob Pikea9ed30f2009-02-23 19:26:07 -08001475}
1476
1477type Comparable interface {
1478 cmp(Comparable) int
1479}
1480</pre>
1481
1482<h3>Variable declarations</h3>
1483
1484<p>
1485A variable declaration creates a variable, binds an identifier to it and
1486gives it a type and optionally an initial value.
Rob Pikecdbf6192009-02-24 17:47:45 -08001487The type must be complete (§Types).
Rob Pikea9ed30f2009-02-23 19:26:07 -08001488</p>
1489<pre class="grammar">
1490VarDecl = "var" ( VarSpec | "(" [ VarSpecList ] ")" ) .
1491VarSpecList = VarSpec { ";" VarSpec } [ ";" ] .
1492VarSpec = IdentifierList ( CompleteType [ "=" ExpressionList ] | "=" ExpressionList ) .
1493</pre>
1494
1495<pre>
1496var i int
1497var U, V, W float
1498var k = 0
1499var x, y float = -1.0, -2.0
1500var (
1501 i int;
1502 u, v, s = 2.0, 3.0, "bar"
1503)
1504</pre>
1505
1506<p>
1507If there are expressions, their number must be equal
1508to the number of identifiers, and the n<sup>th</sup> variable
1509is initialized to the value of the n<sup>th</sup> expression.
1510Otherwise, each variable is initialized to the <i>zero</i>
Rob Pike8f2330d2009-02-25 16:20:44 -08001511of the type (§The zero value).
Rob Pikea9ed30f2009-02-23 19:26:07 -08001512The expressions can be general expressions; they need not be constants.
1513</p>
1514<p>
1515Either the type or the expression list must be present. If the
1516type is present, it sets the type of each variable and the expressions
1517(if any) must be assignment-compatible to that type. If the type
1518is absent, the variables take the types of the corresponding
1519expressions.
1520</p>
1521<p>
1522If the type is absent and the corresponding expression is a constant
1523expression of ideal integer or ideal float type, the type of the
1524declared variable is <code>int</code> or <code>float</code>
1525respectively:
1526</p>
1527
1528<pre>
1529var i = 0 // i has type int
1530var f = 3.1415 // f has type float
1531</pre>
1532
1533<h3>Short variable declarations</h3>
1534
1535A <i>short variable declaration</i> uses the syntax
1536
1537<pre class="grammar">
1538SimpleVarDecl = IdentifierList ":=" ExpressionList .
1539</pre>
1540
1541and is shorthand for the declaration syntax
1542
1543<pre class="grammar">
1544"var" IdentifierList = ExpressionList .
1545</pre>
1546
1547<pre>
1548i, j := 0, 10;
1549f := func() int { return 7; }
Rob Pikef5387602009-03-30 16:08:41 -07001550ch := make(chan int);
Rob Pikea9ed30f2009-02-23 19:26:07 -08001551</pre>
1552
1553<p>
1554Unlike regular variable declarations, short variable declarations
1555can be used, by analogy with tuple assignment (§Assignments), to
1556receive the individual elements of a multi-valued expression such
Rob Pike5af7de32009-02-24 15:17:59 -08001557as a call to a multi-valued function. In this form, the ExpressionList
Rob Pikea9ed30f2009-02-23 19:26:07 -08001558must be a single such multi-valued expression, the number of
1559identifiers must equal the number of values, and the declared
1560variables will be assigned the corresponding values.
1561</p>
1562
1563<pre>
Russ Cox5958dd62009-03-04 17:19:21 -08001564r, w := os.Pipe(fd); // os.Pipe() returns two values
Rob Pikea9ed30f2009-02-23 19:26:07 -08001565</pre>
1566
1567<p>
Rob Pike2a1683a2009-04-19 20:04:15 -07001568A short variable declaration may redeclare variables provided they
1569were originally declared in the same block with the same type, and at
1570least one of the variables is new. As a consequence, redeclaration
1571can only appear in a multi-variable short declaration.
1572Redeclaration does not introduce a new
1573variable; it just assigns a new value to the original.
1574</p>
1575
1576<pre>
1577field1, offset := nextField(str, 0);
1578field2, offset := nextField(str, offset); // redeclares offset
1579</pre>
1580
1581<p>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001582Short variable declarations may appear only inside functions.
1583In some contexts such as the initializers for <code>if</code>,
1584<code>for</code>, or <code>switch</code> statements,
1585they can be used to declare local temporary variables (§Statements).
1586</p>
1587
1588<h3>Function declarations</h3>
1589
1590<p>
1591A function declaration binds an identifier to a function (§Function types).
1592</p>
1593
1594<pre class="grammar">
1595FunctionDecl = "func" identifier Signature [ Block ] .
1596</pre>
1597
1598<pre>
1599func min(x int, y int) int {
1600 if x &lt; y {
1601 return x;
1602 }
1603 return y;
1604}
1605</pre>
1606
1607<p>
1608A function must be declared or forward-declared before it can be invoked (§Forward declarations).
1609Implementation restriction: Functions can only be declared at the package level.
1610</p>
1611
1612<h3>Method declarations</h3>
1613
1614<p>
1615A method declaration binds an identifier to a method,
1616which is a function with a <i>receiver</i>.
1617</p>
1618<pre class="grammar">
1619MethodDecl = "func" Receiver identifier Signature [ Block ] .
1620Receiver = "(" [ identifier ] [ "*" ] TypeName ")" .
1621</pre>
1622
1623<p>
1624The receiver type must be a type name or a pointer to a type name,
1625and that name is called the <i>receiver base type</i> or just <i>base type</i>.
1626The base type must not be a pointer type and must be
1627declared in the same source file as the method.
1628The method is said to be <i>bound</i> to the base type
1629and is visible only within selectors for that type
1630(§Type declarations, §Selectors).
1631</p>
1632
1633<p>
1634All methods bound to a base type must have the same receiver type,
1635either all pointers to the base type or all the base type itself.
1636Given type <code>Point</code>, the declarations
1637</p>
1638
1639<pre>
1640func (p *Point) Length() float {
1641 return Math.sqrt(p.x * p.x + p.y * p.y);
1642}
1643
1644func (p *Point) Scale(factor float) {
1645 p.x = p.x * factor;
1646 p.y = p.y * factor;
1647}
1648</pre>
1649
1650<p>
1651bind the methods <code>Length</code> and <code>Scale</code>
1652to the base type <code>Point</code>.
1653</p>
1654
1655<p>
1656If the
1657receiver's value is not referenced inside the the body of the method,
1658its identifier may be omitted in the declaration. The same applies in
1659general to parameters of functions and methods.
1660</p>
1661
1662<p>
1663Methods can be declared
1664only after their base type is declared or forward-declared, and invoked
1665only after their own declaration or forward-declaration (§Forward declarations).
1666Implementation restriction: They can only be declared at package level.
1667</p>
1668
Rob Pikedf3183f2009-02-26 16:37:23 -08001669<p>
1670The type of a method is the type of a function with the receiver as first
1671argument. For instance, the method <code>Scale</code> has type
1672</p>
1673
1674<pre>
1675(p *Point, factor float)
1676</pre>
1677
1678<p>
1679However, a function declared this way is not a method.
1680</p>
1681
Rob Pikea9ed30f2009-02-23 19:26:07 -08001682<h3>Forward declarations</h3>
1683
1684<p>
Rob Pike5af7de32009-02-24 15:17:59 -08001685Mutually-recursive types require that one be
Rob Pikea9ed30f2009-02-23 19:26:07 -08001686<i>forward declared</i> so that it may be named in the other.
1687A forward declaration of a type omits the block containing the fields
1688or methods of the type.
1689</p>
1690
1691<pre>
1692type List struct // forward declaration of List
1693type Item struct {
1694 value int;
1695 next *List;
1696}
1697type List struct {
1698 head, tail *Item
1699}
1700</pre>
1701<p>
1702A forward-declared type is incomplete (§Types)
1703until it is fully declared. The full declaration must follow
Russ Cox5958dd62009-03-04 17:19:21 -08001704before the end of the block containing the forward declaration;
1705it cannot be contained in an inner block.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001706</p>
1707<p>
1708Functions and methods may similarly be forward-declared by omitting their body.
1709</p>
1710<pre>
1711func F(a int) int // forward declaration of F
1712func G(a, b int) int {
1713 return F(a) + F(b)
1714}
1715func F(a int) int {
1716 if a <= 0 { return 0 }
1717 return G(a-1, b+1)
1718}
1719</pre>
1720
1721<hr/>
1722
Robert Griesemerc2d55862009-02-19 16:49:10 -08001723<h2>Expressions</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001724
Rob Pikedf3183f2009-02-26 16:37:23 -08001725<p>
1726An expression specifies the computation of a value by applying
1727operators and functions to operands. An expression has a value and
Robert Griesemerad711102008-09-11 17:48:20 -07001728a type.
Rob Pikedf3183f2009-02-26 16:37:23 -08001729</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001730
Robert Griesemerc2d55862009-02-19 16:49:10 -08001731<h3>Operands</h3>
Robert Griesemerad711102008-09-11 17:48:20 -07001732
1733Operands denote the elementary values in an expression.
1734
Rob Pikeff70f092009-02-20 13:36:14 -08001735<pre class="grammar">
Rob Pikedf3183f2009-02-26 16:37:23 -08001736Operand = Literal | QualifiedIdent | "(" Expression ")" .
1737Literal = BasicLit | CompositeLit | FunctionLit .
1738BasicLit = int_lit | float_lit | char_lit | StringLit .
1739StringLit = string_lit { string_lit } .
Robert Griesemerc2d55862009-02-19 16:49:10 -08001740</pre>
Robert Griesemerad711102008-09-11 17:48:20 -07001741
1742
Robert Griesemerc2d55862009-02-19 16:49:10 -08001743<h3>Constants</h3>
Robert Griesemerb90b2132008-09-19 15:49:55 -07001744
Rob Pikedf3183f2009-02-26 16:37:23 -08001745<p>
Russ Cox5958dd62009-03-04 17:19:21 -08001746A <i>constant</i> is a literal of a basic type
1747(including the predeclared constants <code>true</code>, <code>false</code>
1748and <code>nil</code>
1749and values denoted by <code>iota</code>)
1750or a constant expression (§Constant expressions).
1751Constants have values that are known at compile time.
Rob Pikedf3183f2009-02-26 16:37:23 -08001752</p>
Robert Griesemerb90b2132008-09-19 15:49:55 -07001753
Robert Griesemerc2d55862009-02-19 16:49:10 -08001754<h3>Qualified identifiers</h3>
Robert Griesemerad711102008-09-11 17:48:20 -07001755
Robert Griesemerc2d55862009-02-19 16:49:10 -08001756<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08001757A qualified identifier is an identifier qualified by a package name prefix.
1758</p>
Robert Griesemer337af312008-11-17 18:11:36 -08001759
Rob Pikeff70f092009-02-20 13:36:14 -08001760<pre class="grammar">
Robert Griesemerb813ee02009-03-05 15:01:54 -08001761QualifiedIdent = [ ( LocalPackageName | PackageName ) "." ] identifier .
Rob Pikedf3183f2009-02-26 16:37:23 -08001762LocalPackageName = identifier .
Robert Griesemerc2d55862009-02-19 16:49:10 -08001763PackageName = identifier .
1764</pre>
Robert Griesemerad711102008-09-11 17:48:20 -07001765
Rob Pikedf3183f2009-02-26 16:37:23 -08001766<p>
1767A qualified identifier accesses an identifier in
1768a separate package. The identifier must be exported by that package, which
1769means that it must begin with a Unicode upper case letter (§Exported identifiers).
1770</p>
1771<p>
1772The LocalPackageName is that of the package in which the qualified identifier
1773appears and is only necessary to access names hidden by intervening declarations
1774of a package-level identifier.
1775</p>
1776
1777<pre>
1778Math.Sin
1779mypackage.hiddenName
1780mypackage.Math.Sin // if Math is declared in an intervening scope
1781</pre>
Robert Griesemerad711102008-09-11 17:48:20 -07001782
Russ Cox5958dd62009-03-04 17:19:21 -08001783TODO: 6g does not implement LocalPackageName. Is this new?
1784Is it needed?
1785
Robert Griesemerc2d55862009-02-19 16:49:10 -08001786<h3>Composite literals</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001787
Rob Pikedf3183f2009-02-26 16:37:23 -08001788<p>
1789Composite literals construct values for structs, arrays, slices, and maps
1790and create a new value each time they are evaluated.
1791They consist of the type of the value
Rob Pike426335f2009-03-02 17:52:52 -08001792followed by a brace-bound list of expressions,
Robert Griesemerc5c577c2009-03-27 13:43:28 -07001793or a list of key-value pairs for map literals.
Rob Pikedf3183f2009-02-26 16:37:23 -08001794</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001795
Rob Pikeff70f092009-02-20 13:36:14 -08001796<pre class="grammar">
Robert Griesemerc5c577c2009-03-27 13:43:28 -07001797CompositeLit = LiteralType "{" [ ( ExpressionList | KeyValueList ) [ "," ] ] "}" .
Rob Pikedf3183f2009-02-26 16:37:23 -08001798LiteralType = StructType | ArrayType | "[" "..." "]" ElementType |
1799 SliceType | MapType | TypeName .
Robert Griesemerc5c577c2009-03-27 13:43:28 -07001800KeyValueList = KeyValueExpr { "," KeyValueExpr } .
1801KeyValueExpr = Expression ":" Expression .
Robert Griesemerc2d55862009-02-19 16:49:10 -08001802</pre>
Robert Griesemer0976e342008-09-03 13:37:44 -07001803
Rob Pikedf3183f2009-02-26 16:37:23 -08001804<p>
1805The LiteralType must be a struct, array, slice, or map type.
Russ Cox7a5e97b2009-03-03 15:40:30 -08001806(The grammar enforces this constraint except when the type is given
1807as a TypeName.)
1808The types of the expressions must be assignment compatible to
1809the respective field, element, and key types of the LiteralType;
1810there is no additional conversion.
Rob Pikedf3183f2009-02-26 16:37:23 -08001811</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001812
Robert Griesemerc2d55862009-02-19 16:49:10 -08001813<pre>
1814type Rat struct { num, den int }
1815type Num struct { r Rat; f float; s string }
1816</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001817
Rob Pikedf3183f2009-02-26 16:37:23 -08001818<p>
1819one may write
1820</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001821
Robert Griesemerc2d55862009-02-19 16:49:10 -08001822<pre>
Rob Pike37ab8382009-03-18 22:58:36 -07001823pi := Num{Rat{22, 7}, 3.14159, "pi"}
1824</pre>
1825
1826<p>
Rob Pike2a5af742009-03-20 17:03:48 -07001827Taking the address of a composite literal (§Address operators)
1828generates a unique pointer to an instance of the literal's value.
Rob Pike37ab8382009-03-18 22:58:36 -07001829</p>
Rob Pike37ab8382009-03-18 22:58:36 -07001830<pre>
1831var pi_ptr *Rat = &amp;Rat{22, 7}
Robert Griesemerc2d55862009-02-19 16:49:10 -08001832</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001833
Rob Pikedf3183f2009-02-26 16:37:23 -08001834<p>
Robert Griesemera3294712009-01-05 11:17:26 -08001835The length of an array literal is the length specified in the LiteralType.
1836If fewer elements than the length are provided in the literal, the missing
Rob Pike8f2330d2009-02-25 16:20:44 -08001837elements are set to the zero value for the array element type.
Rob Pikedf3183f2009-02-26 16:37:23 -08001838It is an error to provide more elements than specified in the type. The
1839notation <code>...</code> specifies an array length equal
1840to the number of elements in the literal.
1841</p>
Robert Griesemerb90b2132008-09-19 15:49:55 -07001842
Robert Griesemerc2d55862009-02-19 16:49:10 -08001843<pre>
Rob Pike426335f2009-03-02 17:52:52 -08001844buffer := [10]string{}; // len(buffer) == 10
1845primes := [6]int{2, 3, 5, 7, 9, 11}; // len(primes) == 6
1846days := [...]string{"Sat", "Sun"}; // len(days) == 2
Robert Griesemerc2d55862009-02-19 16:49:10 -08001847</pre>
Robert Griesemer91bbd642009-01-07 09:31:35 -08001848
Rob Pikedf3183f2009-02-26 16:37:23 -08001849<p>
1850A slice literal describes the entire underlying array literal.
Robert Griesemer91bbd642009-01-07 09:31:35 -08001851Thus, the length and capacity of a slice literal is the number of elements
Rob Pikedf3183f2009-02-26 16:37:23 -08001852(of the array) provided in the literal. A slice literal has the form
1853</p>
Robert Griesemer91bbd642009-01-07 09:31:35 -08001854
Robert Griesemerc2d55862009-02-19 16:49:10 -08001855<pre>
Rob Pike426335f2009-03-02 17:52:52 -08001856[]T{x1, x2, ... xn}
Robert Griesemerc2d55862009-02-19 16:49:10 -08001857</pre>
Robert Griesemer91bbd642009-01-07 09:31:35 -08001858
Rob Pikedf3183f2009-02-26 16:37:23 -08001859<p>
1860and is a shortcut for a slice operation applied to an array literal:
1861</p>
Robert Griesemer91bbd642009-01-07 09:31:35 -08001862
Robert Griesemerc2d55862009-02-19 16:49:10 -08001863<pre>
Rob Pike426335f2009-03-02 17:52:52 -08001864[n]T{x1, x2, ... xn}[0 : n]
Robert Griesemerc2d55862009-02-19 16:49:10 -08001865</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001866
Rob Pikedf3183f2009-02-26 16:37:23 -08001867<p>
1868In map literals only, the list contains
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001869key-value pairs separated by a colon:
Rob Pikedf3183f2009-02-26 16:37:23 -08001870</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001871
Robert Griesemerc2d55862009-02-19 16:49:10 -08001872<pre>
Rob Pike426335f2009-03-02 17:52:52 -08001873m := map[string]int{"good": 0, "bad": 1, "indifferent": 7};
Robert Griesemerc2d55862009-02-19 16:49:10 -08001874</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001875
Russ Cox7a5e97b2009-03-03 15:40:30 -08001876<p>
1877A parsing ambiguity arises when a composite literal using the
1878TypeName form of the LiteralType appears in the condition of an
1879"if", "for", or "switch" statement, because the braces surrounding
1880the expressions in the literal are confused with those introducing
1881a block of statements. To resolve the ambiguity in this rare case,
1882the composite literal must appear within
1883parentheses.
1884</p>
1885
1886<pre>
1887if x == (T{a,b,c}[i]) { ... }
1888if (x == T{a,b,c}[i]) { ... }
1889</pre>
1890
Robert Griesemerc2d55862009-02-19 16:49:10 -08001891<h3>Function literals</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001892
Rob Pikedf3183f2009-02-26 16:37:23 -08001893<p>
1894A function literal represents an anonymous function.
1895It consists of a specification of the function type and a function body.
1896</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001897
Rob Pikeff70f092009-02-20 13:36:14 -08001898<pre class="grammar">
Robert Griesemer62fd90a2009-03-25 13:58:44 -07001899FunctionLit = FunctionType Block .
Russ Cox5958dd62009-03-04 17:19:21 -08001900Block = "{" StatementList "}" .
Robert Griesemerc2d55862009-02-19 16:49:10 -08001901</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001902
Robert Griesemerc2d55862009-02-19 16:49:10 -08001903<pre>
Rob Pikedf3183f2009-02-26 16:37:23 -08001904func (a, b int, z float) bool { return a*b &lt; int(z) }
Robert Griesemerc2d55862009-02-19 16:49:10 -08001905</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001906
Rob Pikedf3183f2009-02-26 16:37:23 -08001907<p>
1908A function literal can be assigned to a variable or invoked directly.
1909</p>
Robert Griesemer7231ceb2008-09-08 15:01:04 -07001910
Robert Griesemerc2d55862009-02-19 16:49:10 -08001911<pre>
Rob Pikedf3183f2009-02-26 16:37:23 -08001912f := func(x, y int) int { return x + y }
1913func(ch chan int) { ch &lt;- ACK } (reply_chan)
Robert Griesemerc2d55862009-02-19 16:49:10 -08001914</pre>
Robert Griesemer7231ceb2008-09-08 15:01:04 -07001915
Rob Pikedf3183f2009-02-26 16:37:23 -08001916<p>
1917Function literals are <i>closures</i>: they may refer to variables
Robert Griesemerd8a764c2009-02-06 17:01:10 -08001918defined in a surrounding function. Those variables are then shared between
1919the surrounding function and the function literal, and they survive as long
Rob Pikedf3183f2009-02-26 16:37:23 -08001920as they are accessible.
1921</p>
Robert Griesemer7231ceb2008-09-08 15:01:04 -07001922
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001923
Robert Griesemerc2d55862009-02-19 16:49:10 -08001924<h3>Primary expressions</h3>
Russ Cox5958dd62009-03-04 17:19:21 -08001925
Rob Pikeff70f092009-02-20 13:36:14 -08001926<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08001927PrimaryExpr =
1928 Operand |
1929 PrimaryExpr Selector |
1930 PrimaryExpr Index |
1931 PrimaryExpr Slice |
Russ Cox5958dd62009-03-04 17:19:21 -08001932 PrimaryExpr TypeAssertion |
Robert Griesemerc2d55862009-02-19 16:49:10 -08001933 PrimaryExpr Call .
Robert Griesemer57b34612008-10-10 12:45:44 -07001934
Russ Cox5958dd62009-03-04 17:19:21 -08001935Selector = "." identifier .
1936Index = "[" Expression "]" .
1937Slice = "[" Expression ":" Expression "]" .
1938TypeAssertion = "." "(" Type ")" .
1939Call = "(" [ ExpressionList ] ")" .
Robert Griesemerc2d55862009-02-19 16:49:10 -08001940</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001941
1942
Robert Griesemerc2d55862009-02-19 16:49:10 -08001943<pre>
1944x
19452
1946(s + ".txt")
1947f(3.1415, true)
Rob Pike426335f2009-03-02 17:52:52 -08001948Point{1, 2}
Robert Griesemerc2d55862009-02-19 16:49:10 -08001949m["foo"]
1950s[i : j + 1]
1951obj.color
1952Math.sin
1953f.p[i].x()
1954</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001955
1956
Robert Griesemerc2d55862009-02-19 16:49:10 -08001957<h3>Selectors</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001958
Rob Pikedf3183f2009-02-26 16:37:23 -08001959<p>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07001960A primary expression of the form
Rob Pikedf3183f2009-02-26 16:37:23 -08001961</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001962
Robert Griesemerc2d55862009-02-19 16:49:10 -08001963<pre>
1964x.f
1965</pre>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07001966
Robert Griesemerc2d55862009-02-19 16:49:10 -08001967<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08001968denotes the field or method <code>f</code> of the value denoted by <code>x</code>
1969(or of <code>*x</code> if
1970<code>x</code> is of pointer type). The identifier <code>f</code>
1971is called the (field or method)
1972<i>selector</i>.
1973The type of the expression is the type of <code>f</code>.
1974</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08001975<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08001976A selector <code>f</code> may denote a field or method <code>f</code> of
1977a type <code>T</code>, or it may refer
1978to a field or method <code>f</code> of a nested anonymous field of
1979<code>T</code>.
1980The number of anonymous fields traversed
1981to reach <code>f</code> is called its <i>depth</i> in <code>T</code>.
1982The depth of a field or method <code>f</code>
1983declared in <code>T</code> is zero.
1984The depth of a field or method <code>f</code> declared in
1985an anonymous field <code>A</code> in <code>T</code> is the
1986depth of <code>f</code> in <code>A</code> plus one.
1987</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08001988<p>
Robert Griesemer071c91b2008-10-23 12:04:45 -07001989The following rules apply to selectors:
Rob Pikedf3183f2009-02-26 16:37:23 -08001990</p>
1991<ol>
1992<li>
1993For a value <code>x</code> of type <code>T</code> or <code>*T</code>
1994where <code>T</code> is not an interface type,
1995<code>x.f</code> denotes the field or method at the shallowest depth
1996in <code>T</code> where there
1997is such an <code>f</code>.
1998If there is not exactly one <code>f</code> with shallowest depth, the selector
Robert Griesemer071c91b2008-10-23 12:04:45 -07001999expression is illegal.
Rob Pikedf3183f2009-02-26 16:37:23 -08002000</li>
2001<li>
2002For a variable <code>x</code> of type <code>I</code> or <code>*I</code>
2003where <code>I</code> is an interface type,
2004<code>x.f</code> denotes the actual method with name <code>f</code> of the value assigned
2005to <code>x</code> if there is such a method.
2006If no value or <code>nil</code> was assigned to <code>x</code>, <code>x.f</code> is illegal.
2007</li>
2008<li>
2009In all other cases, <code>x.f</code> is illegal.
2010</ol>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002011<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002012Selectors automatically dereference pointers as necessary.
2013If <code>x</code> is of pointer type, <code>x.y</code>
2014is shorthand for <code>(*x).y</code>; if <code>y</code>
2015is also of pointer type, <code>x.y.z</code> is shorthand
2016for <code>(*(*x).y).z</code>, and so on.
2017If <code>*x</code> is of pointer type, dereferencing
2018must be explicit;
2019only one level of automatic dereferencing is provided.
2020For an <code>x</code> of type <code>T</code> containing an
2021anonymous field declared as <code>*A</code>,
2022<code>x.f</code> is a shortcut for <code>(*x.A).f</code>.
2023</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002024<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002025For example, given the declarations:
2026</p>
Robert Griesemer071c91b2008-10-23 12:04:45 -07002027
Robert Griesemerc2d55862009-02-19 16:49:10 -08002028<pre>
2029type T0 struct {
2030 x int;
2031}
Robert Griesemer071c91b2008-10-23 12:04:45 -07002032
Robert Griesemerc2d55862009-02-19 16:49:10 -08002033func (recv *T0) M0()
Robert Griesemer071c91b2008-10-23 12:04:45 -07002034
Robert Griesemerc2d55862009-02-19 16:49:10 -08002035type T1 struct {
2036 y int;
2037}
Robert Griesemer071c91b2008-10-23 12:04:45 -07002038
Robert Griesemerc2d55862009-02-19 16:49:10 -08002039func (recv T1) M1()
Robert Griesemer071c91b2008-10-23 12:04:45 -07002040
Robert Griesemerc2d55862009-02-19 16:49:10 -08002041type T2 struct {
2042 z int;
2043 T1;
2044 *T0;
2045}
Robert Griesemer071c91b2008-10-23 12:04:45 -07002046
Robert Griesemerc2d55862009-02-19 16:49:10 -08002047func (recv *T2) M2()
Robert Griesemer071c91b2008-10-23 12:04:45 -07002048
Robert Griesemerc2d55862009-02-19 16:49:10 -08002049var p *T2; // with p != nil and p.T1 != nil
2050</pre>
Robert Griesemer071c91b2008-10-23 12:04:45 -07002051
Rob Pikedf3183f2009-02-26 16:37:23 -08002052<p>
2053one may write:
2054</p>
Robert Griesemer071c91b2008-10-23 12:04:45 -07002055
Robert Griesemerc2d55862009-02-19 16:49:10 -08002056<pre>
2057p.z // (*p).z
2058p.y // ((*p).T1).y
2059p.x // (*(*p).T0).x
Robert Griesemer071c91b2008-10-23 12:04:45 -07002060
Robert Griesemerc2d55862009-02-19 16:49:10 -08002061p.M2 // (*p).M2
2062p.M1 // ((*p).T1).M1
2063p.M0 // ((*p).T0).M0
2064</pre>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002065
2066
Robert Griesemerc2d55862009-02-19 16:49:10 -08002067<font color=red>
Robert Griesemer071c91b2008-10-23 12:04:45 -07002068TODO: Specify what happens to receivers.
Robert Griesemerc2d55862009-02-19 16:49:10 -08002069</font>
Robert Griesemer7abfcd92008-10-07 17:14:30 -07002070
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002071
Robert Griesemerc2d55862009-02-19 16:49:10 -08002072<h3>Indexes</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002073
Rob Pikedf3183f2009-02-26 16:37:23 -08002074<p>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002075A primary expression of the form
Rob Pikedf3183f2009-02-26 16:37:23 -08002076</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002077
Robert Griesemerc2d55862009-02-19 16:49:10 -08002078<pre>
2079a[x]
2080</pre>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002081
Rob Pike4501d342009-02-19 17:31:36 -08002082<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002083denotes the array or map element of <code>a</code> indexed by <code>x</code>.
2084The value <code>x</code> is called the
2085<i>array index</i> or <i>map key</i>, respectively. The following
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002086rules apply:
Rob Pike4501d342009-02-19 17:31:36 -08002087</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002088<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002089For <code>a</code> of type <code>A</code> or <code>*A</code>
2090where <code>A</code> is an array type (§Array types):
Rob Pike4501d342009-02-19 17:31:36 -08002091</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002092<ul>
Rob Pikedf3183f2009-02-26 16:37:23 -08002093 <li><code>x</code> must be an integer value and <code>0 &lt;= x &lt; len(a)</code>
2094 <li><code>a[x]</code> is the array element at index <code>x</code> and the type of
2095 <code>a[x]</code> is the element type of <code>A</code>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002096</ul>
2097<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002098For <code>a</code> of type <code>M</code> or <code>*M</code>
2099where <code>M</code> is a map type (§Map types):
Rob Pike4501d342009-02-19 17:31:36 -08002100</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002101<ul>
Russ Cox5958dd62009-03-04 17:19:21 -08002102 <li><code>x</code>'s type must be equal to the key type of <code>M</code>
Rob Pikedf3183f2009-02-26 16:37:23 -08002103 and the map must contain an entry with key <code>x</code> (but see special forms below)
2104 <li><code>a[x]</code> is the map value with key <code>x</code>
2105 and the type of <code>a[x]</code> is the value type of <code>M</code>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002106</ul>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002107
Robert Griesemerc2d55862009-02-19 16:49:10 -08002108<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002109Otherwise <code>a[x]</code> is illegal. If the index or key is out of range evaluating
2110an otherwise legal index expression, a run-time exception occurs.
2111</p>
Robert Griesemer7abfcd92008-10-07 17:14:30 -07002112
Rob Pikedf3183f2009-02-26 16:37:23 -08002113<p>
2114However, if an index expression on a map <code>a</code> of type <code>map[K] V</code>
2115is used in an assignment of one of the special forms
2116</p>
2117
2118<pre>
2119r, ok = a[x]
2120r, ok := a[x]
2121</pre>
2122
2123<p>
2124the result of the index expression is a pair of values with types
2125<code>(K, bool)</code>.
2126If the key is present in the map,
2127the expression returns the pair <code>(a[x], true)</code>;
2128otherwise it returns <code>(Z, false)</code> where <code>Z</code> is
2129the zero value for <code>V</code> (§The zero value).
2130No run-time exception occurs in this case.
2131The index expression in this construct thus acts like a function call
2132returning a value and a boolean indicating success. (§Assignments)
2133</p>
2134
2135<p>
2136Similarly, if an assignment to a map has the special form
2137</p>
2138
2139<pre>
2140a[x] = r, ok
2141</pre>
2142
2143<p>
2144and boolean <code>ok</code> has the value <code>false</code>,
2145the entry for key <code>x</code> is deleted from the map; if
2146<code>ok</code> is <code>true</code>, the construct acts like
2147a regular assignment to an element of the map.
2148</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002149
Robert Griesemerc2d55862009-02-19 16:49:10 -08002150<h3>Slices</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002151
Rob Pikedf3183f2009-02-26 16:37:23 -08002152<p>
Rob Pike8f2330d2009-02-25 16:20:44 -08002153Strings, arrays, and slices can be <i>sliced</i> to construct substrings or descriptors
Robert Griesemer633957b2009-01-06 13:23:20 -08002154of subarrays. The index expressions in the slice select which elements appear
2155in the result. The result has indexes starting at 0 and length equal to the
Rob Pikedf3183f2009-02-26 16:37:23 -08002156difference in the index values in the slice. After slicing the array <code>a</code>
2157</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002158
Robert Griesemerc2d55862009-02-19 16:49:10 -08002159<pre>
Rob Pike426335f2009-03-02 17:52:52 -08002160a := [4]int{1, 2, 3, 4};
Robert Griesemerc2d55862009-02-19 16:49:10 -08002161s := a[1:3];
2162</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002163
Rob Pikedf3183f2009-02-26 16:37:23 -08002164<p>
Russ Cox5958dd62009-03-04 17:19:21 -08002165the slice <code>s</code> has type <code>[]int</code>, length 2, capacity 3, and elements
Rob Pikedf3183f2009-02-26 16:37:23 -08002166</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002167
Robert Griesemerc2d55862009-02-19 16:49:10 -08002168<pre>
2169s[0] == 2
2170s[1] == 3
2171</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002172
Rob Pikedf3183f2009-02-26 16:37:23 -08002173<p>
Rob Pike8f2330d2009-02-25 16:20:44 -08002174The slice length must be non-negative.
Russ Cox5958dd62009-03-04 17:19:21 -08002175For arrays or strings, the indexes
Rob Pike811dd252009-03-04 20:39:39 -08002176<code>lo</code> and <code>hi</code> must satisfy
21770 &lt;= <code>lo</code> &lt;= <code>hi</code> &lt;= length;
Russ Cox5958dd62009-03-04 17:19:21 -08002178for slices, the upper bound is the capacity rather than the length.
Robert Griesemerc2d55862009-02-19 16:49:10 -08002179<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002180If the sliced operand is a string, the result of the slice operation is another, new
Robert Griesemer633957b2009-01-06 13:23:20 -08002181string (§String types). If the sliced operand is an array or slice, the result
2182of the slice operation is a slice (§Slice types).
Rob Pikedf3183f2009-02-26 16:37:23 -08002183</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002184
2185
Russ Cox5958dd62009-03-04 17:19:21 -08002186<h3>Type assertions</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002187
Rob Pikedf3183f2009-02-26 16:37:23 -08002188<p>
2189For an expression <code>x</code> and a type <code>T</code>, the primary expression
2190</p>
Robert Griesemer434c6052008-11-07 13:34:37 -08002191
Robert Griesemerc2d55862009-02-19 16:49:10 -08002192<pre>
2193x.(T)
2194</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -08002195
Robert Griesemerc2d55862009-02-19 16:49:10 -08002196<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002197asserts that the value stored in <code>x</code> is of type <code>T</code>.
Russ Cox5958dd62009-03-04 17:19:21 -08002198The notation <code>x.(T)</code> is called a <i>type assertion</i>.
2199The type of <code>x</code> must be an interface type.
Rob Pikedf3183f2009-02-26 16:37:23 -08002200</p>
2201<p>
Russ Cox5958dd62009-03-04 17:19:21 -08002202More precisely, if <code>T</code> is not an interface type, <code>x.(T)</code> asserts
Rob Pikedf3183f2009-02-26 16:37:23 -08002203that the dynamic type of <code>x</code> is identical to the type <code>T</code>
2204(§Type equality and identity).
Russ Cox5958dd62009-03-04 17:19:21 -08002205If <code>T</code> is an interface type, <code>x.(T)</code> asserts that the dynamic type
Rob Pikedf3183f2009-02-26 16:37:23 -08002206of <code>T</code> implements the interface <code>T</code> (§Interface types).
Rob Pikedf3183f2009-02-26 16:37:23 -08002207<font color=red>TODO: gri wants an error if x is already of type T.</font>
2208</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002209<p>
Russ Cox5958dd62009-03-04 17:19:21 -08002210If the type assertion holds, the value of the expression is the value
2211stored 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 -08002212exception occurs. In other words, even though the dynamic type of <code>x</code>
Russ Cox5958dd62009-03-04 17:19:21 -08002213is known only at run-time, the type of <code>x.(T)</code> is
Rob Pikedf3183f2009-02-26 16:37:23 -08002214known to be <code>T</code> in a correct program.
2215</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002216<p>
Russ Cox5958dd62009-03-04 17:19:21 -08002217If a type assertion is used in an assignment of one of the special forms,
Rob Pikedf3183f2009-02-26 16:37:23 -08002218</p>
Robert Griesemer434c6052008-11-07 13:34:37 -08002219
Robert Griesemerc2d55862009-02-19 16:49:10 -08002220<pre>
2221v, ok = x.(T)
2222v, ok := x.(T)
2223</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -08002224
Robert Griesemerc2d55862009-02-19 16:49:10 -08002225<p>
Russ Cox5958dd62009-03-04 17:19:21 -08002226the result of the assertion is a pair of values with types <code>(T, bool)</code>.
2227If the assertion holds, the expression returns the pair <code>(x.(T), true)</code>;
Rob Pikedf3183f2009-02-26 16:37:23 -08002228otherwise, the expression returns <code>(Z, false)</code> where <code>Z</code>
2229is the zero value for type <code>T</code> (§The zero value).
2230No run-time exception occurs in this case.
Russ Cox5958dd62009-03-04 17:19:21 -08002231The type assertion in this construct thus acts like a function call
Rob Pikedf3183f2009-02-26 16:37:23 -08002232returning a value and a boolean indicating success. (§Assignments)
2233</p>
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -07002234
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002235
Robert Griesemerc2d55862009-02-19 16:49:10 -08002236<h3>Calls</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002237
Robert Griesemerc2d55862009-02-19 16:49:10 -08002238<p>
Rob Pike96750f12009-02-27 16:47:48 -08002239Given an expression <code>f</code> of function type
2240<code>F</code>,
Rob Pikedf3183f2009-02-26 16:37:23 -08002241</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002242
Robert Griesemerc2d55862009-02-19 16:49:10 -08002243<pre>
Rob Pike96750f12009-02-27 16:47:48 -08002244f(a1, a2, ... an)
Robert Griesemerc2d55862009-02-19 16:49:10 -08002245</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002246
Robert Griesemerc2d55862009-02-19 16:49:10 -08002247<p>
Rob Pike96750f12009-02-27 16:47:48 -08002248calls <code>f</code> with arguments <code>a1, a2, ... an</code>.
2249The arguments must be single-valued expressions
2250assignment compatible with the parameters of
Rob Pikedf3183f2009-02-26 16:37:23 -08002251<code>F</code> and are evaluated before the function is called.
2252The type of the expression is the result type
2253of <code>F</code>.
2254A method invocation is similar but the method itself
2255is specified as a selector upon a value of the receiver type for
2256the method.
2257</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002258
Robert Griesemerc2d55862009-02-19 16:49:10 -08002259<pre>
Rob Pikedf3183f2009-02-26 16:37:23 -08002260Atan2(x, y) // function call
2261var pt *Point;
2262pt.Scale(3.5) // method call with receiver pt
Robert Griesemerc2d55862009-02-19 16:49:10 -08002263</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002264
Rob Pikedf3183f2009-02-26 16:37:23 -08002265<p>
2266If the receiver type of the method is declared as a pointer of type <code>*T</code>,
2267the actual receiver may be a value of type <code>T</code>;
2268in such cases method invocation implicitly takes the
2269receiver's address:
2270</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002271
Robert Griesemerc2d55862009-02-19 16:49:10 -08002272<pre>
Rob Pikedf3183f2009-02-26 16:37:23 -08002273var p Point;
2274p.Scale(3.5)
Robert Griesemerc2d55862009-02-19 16:49:10 -08002275</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002276
Robert Griesemerc2d55862009-02-19 16:49:10 -08002277<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002278There is no distinct method type and there are no method literals.
Rob Pikedf3183f2009-02-26 16:37:23 -08002279</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002280
Rob Pikedf3183f2009-02-26 16:37:23 -08002281<h3>Passing arguments to <code>...</code> parameters</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002282
Robert Griesemerc2d55862009-02-19 16:49:10 -08002283<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002284When a function <code>f</code> has a <code>...</code> parameter,
2285it is always the last formal parameter. Within calls to <code>f</code>,
2286the arguments before the <code>...</code> are treated normally.
2287After those, an arbitrary number (including zero) of trailing
2288arguments may appear in the call and are bound to the <code>...</code>
2289parameter.
2290</p>
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002291
Rob Pikedf3183f2009-02-26 16:37:23 -08002292<p>
2293Within <code>f</code>, the <code>...</code> parameter has static
2294type <code>interface{}</code> (the empty interface). For each call,
2295its dynamic type is a structure whose sequential fields are the
2296trailing arguments of the call. That is, the actual arguments
2297provided for a <code>...</code> parameter are wrapped into a struct
2298that is passed to the function instead of the actual arguments.
2299Using the reflection library (TODO: reference), <code>f</code> may
2300unpack the elements of the dynamic type to recover the actual
2301arguments.
2302</p>
2303
2304<p>
2305Given the function and call
2306</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002307<pre>
Rob Pikedf3183f2009-02-26 16:37:23 -08002308func Fprintf(f io.Write, format string, args ...)
2309Fprintf(os.Stdout, "%s %d", "hello", 23);
Robert Griesemerc2d55862009-02-19 16:49:10 -08002310</pre>
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002311
Rob Pikedf3183f2009-02-26 16:37:23 -08002312<p>
2313Within <code>Fprintf</code>, the dynamic type of <code>args</code> for this
2314call will be, schematically,
2315<code> struct { string; int }</code>.
2316</p>
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002317
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002318
Rob Pikedf3183f2009-02-26 16:37:23 -08002319<p>
2320As a special case, if a function passes its own <code>...</code> parameter as the argument
2321for a <code>...</code> in a call to another function with a <code>...</code> parameter,
2322the parameter is not wrapped again but passed directly. In short, a formal <code>...</code>
2323parameter is passed unchanged as an actual <code>...</code> parameter.
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002324
Robert Griesemerc2d55862009-02-19 16:49:10 -08002325<h3>Operators</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002326
Rob Pikedf3183f2009-02-26 16:37:23 -08002327<p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002328Operators combine operands into expressions.
Rob Pikedf3183f2009-02-26 16:37:23 -08002329</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002330
Rob Pikeff70f092009-02-20 13:36:14 -08002331<pre class="grammar">
Russ Cox5958dd62009-03-04 17:19:21 -08002332Expression = UnaryExpr | Expression binary_op UnaryExpr .
Rob Pikedf3183f2009-02-26 16:37:23 -08002333UnaryExpr = PrimaryExpr | unary_op UnaryExpr .
Robert Griesemer57b34612008-10-10 12:45:44 -07002334
Rob Pikedf3183f2009-02-26 16:37:23 -08002335binary_op = log_op | com_op | rel_op | add_op | mul_op .
2336log_op = "||" | "&amp;&amp;" .
2337com_op = "&lt;-" .
2338rel_op = "==" | "!=" | "&lt;" | "&lt;=" | ">" | ">=" .
2339add_op = "+" | "-" | "|" | "^" .
Rob Pikecd04ec92009-03-11 21:59:05 -07002340mul_op = "*" | "/" | "%" | "&lt;&lt;" | ">>" | "&amp;" | "&amp;^" .
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002341
Rob Pikedf3183f2009-02-26 16:37:23 -08002342unary_op = "+" | "-" | "!" | "^" | "*" | "&amp;" | "&lt;-" .
Robert Griesemerc2d55862009-02-19 16:49:10 -08002343</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002344
Robert Griesemerc2d55862009-02-19 16:49:10 -08002345<p>
Rob Pike4501d342009-02-19 17:31:36 -08002346The operand types in binary operations must be equal, with the following exceptions:
2347</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002348<ul>
Rob Pike9b5d8232009-03-12 18:47:49 -07002349 <li>Except in shift expressions, if one operand has numeric type and the other operand is
Robert Griesemera6b546f2008-10-20 11:46:40 -07002350 an ideal number, the ideal number is converted to match the type of
Rob Pikedf3183f2009-02-26 16:37:23 -08002351 the other operand (§Expressions).</li>
Robert Griesemerc8e18762008-09-12 12:26:22 -07002352
Robert Griesemerc2d55862009-02-19 16:49:10 -08002353 <li>If both operands are ideal numbers, the conversion is to ideal floats
Rob Pikedf3183f2009-02-26 16:37:23 -08002354 if one of the operands is an ideal float
2355 (relevant for <code>/</code> and <code>%</code>).</li>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002356
Rob Pikedf3183f2009-02-26 16:37:23 -08002357 <li>The right operand in a shift operation must be always be of unsigned integer type
2358 or an ideal number that can be safely converted into an unsigned integer type
2359 (§Arithmetic operators).</li>
2360
Russ Cox5958dd62009-03-04 17:19:21 -08002361 <li>The operands in channel sends differ in type: one is always a channel and the
Rob Pikedf3183f2009-02-26 16:37:23 -08002362 other is a variable or value of the channel's element type.</li>
Robert Griesemera6b546f2008-10-20 11:46:40 -07002363
Robert Griesemerc2d55862009-02-19 16:49:10 -08002364 <li>When comparing two operands of channel type, the channel value types
Rob Pikedf3183f2009-02-26 16:37:23 -08002365 must be equal but the channel direction is ignored.</li>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002366</ul>
Robert Griesemer18b05c12009-01-26 09:34:19 -08002367
Robert Griesemerc2d55862009-02-19 16:49:10 -08002368<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002369Unary operators have the highest precedence. They are evaluated from
2370right to left. As the <code>++</code> and <code>--</code> operators form
2371statements, not expressions, they fall
2372outside the unary operator hierarchy and apply
2373to the operand on the left.
2374As a consequence, statement <code>*p++</code> is the same as <code>(*p)++</code>.
2375<p>
2376There are six precedence levels for binary operators.
2377Multiplication operators bind strongest, followed by addition
Robert Griesemerad711102008-09-11 17:48:20 -07002378operators, comparison operators, communication operators,
Rob Pikedf3183f2009-02-26 16:37:23 -08002379<code>&amp;&amp;</code> (logical and), and finally <code>||</code> (logical or):
2380</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002381
Rob Pikeff70f092009-02-20 13:36:14 -08002382<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08002383Precedence Operator
Rob Pikecd04ec92009-03-11 21:59:05 -07002384 6 * / % &lt;&lt; >> &amp; &amp;^
Robert Griesemerc2d55862009-02-19 16:49:10 -08002385 5 + - | ^
2386 4 == != &lt; &lt;= > >=
2387 3 &lt;-
2388 2 &amp;&amp;
2389 1 ||
2390</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002391
Robert Griesemerc2d55862009-02-19 16:49:10 -08002392<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002393Binary operators of the same precedence associate from left to right.
2394For instance, <code>x / y / z</code> is the same as <code>(x / y) / z</code>.
2395</p>
2396<p>
2397Examples:
2398</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002399
Robert Griesemerc2d55862009-02-19 16:49:10 -08002400<pre>
2401+x
240223 + 3*x[i]
2403x &lt;= f()
2404^a >> b
2405f() || g()
2406x == y + 1 &amp;&amp; &lt;-chan_ptr > 0
2407</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002408
2409
Robert Griesemerc2d55862009-02-19 16:49:10 -08002410<h3>Arithmetic operators</h3>
2411<p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002412Arithmetic operators apply to numeric types and yield a result of the same
Rob Pikedf3183f2009-02-26 16:37:23 -08002413type as the first operand. The four standard arithmetic operators (<code>+</code>,
2414<code>-</code>, <code>*</code>, <code>/</code>) apply both to integer and
2415floating point types, while <code>+</code> applies also
2416to strings; all other arithmetic operators apply to integers only.
2417</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002418
Rob Pikeff70f092009-02-20 13:36:14 -08002419<pre class="grammar">
Rob Pike307ec212009-03-12 15:53:56 -07002420+ sum integers, floats, strings
2421- difference integers, floats
2422* product integers, floats
2423/ quotient integers, floats
2424% remainder integers
Robert Griesemerc2d55862009-02-19 16:49:10 -08002425
Rob Pike307ec212009-03-12 15:53:56 -07002426&amp; bitwise and integers
2427| bitwise or integers
2428^ bitwise xor integers
2429&amp;^ bit clear (and not) integers
Robert Griesemerc2d55862009-02-19 16:49:10 -08002430
Rob Pike307ec212009-03-12 15:53:56 -07002431<< left shift integer << unsigned integer
2432>> right shift integer >> unsigned integer
Robert Griesemerc2d55862009-02-19 16:49:10 -08002433</pre>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002434
Rob Pikedf3183f2009-02-26 16:37:23 -08002435<p>
2436Strings can be concatenated using the <code>+</code> operator
2437or the <code>+=</code> assignment operator:
2438</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002439
Robert Griesemerc2d55862009-02-19 16:49:10 -08002440<pre>
Rob Pikedf3183f2009-02-26 16:37:23 -08002441s := "hi" + string(c);
2442s += " and good bye";
Robert Griesemerc2d55862009-02-19 16:49:10 -08002443</pre>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002444
Robert Griesemerc2d55862009-02-19 16:49:10 -08002445<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002446String addition creates a new string by concatenating the operands.
2447</p>
2448<p>
2449For integer values, <code>/</code> and <code>%</code> satisfy the following relationship:
2450</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002451
Robert Griesemerc2d55862009-02-19 16:49:10 -08002452<pre>
2453(a / b) * b + a % b == a
2454</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002455
Rob Pikedf3183f2009-02-26 16:37:23 -08002456<p>
2457with <code>(a / b)</code> truncated towards zero.
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002458Examples:
Rob Pikedf3183f2009-02-26 16:37:23 -08002459</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002460
Robert Griesemerc2d55862009-02-19 16:49:10 -08002461<pre>
2462 x y x / y x % y
2463 5 3 1 2
2464-5 3 -1 -2
2465 5 -3 -1 2
2466-5 -3 1 -2
2467</pre>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002468
Rob Pikedf3183f2009-02-26 16:37:23 -08002469<p>
2470If the dividend is positive and the divisor is a constant power of 2,
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002471the division may be replaced by a left shift, and computing the remainder may
2472be replaced by a bitwise "and" operation:
Rob Pikedf3183f2009-02-26 16:37:23 -08002473</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002474
Robert Griesemerc2d55862009-02-19 16:49:10 -08002475<pre>
2476 x x / 4 x % 4 x >> 2 x &amp; 3
2477 11 2 3 2 3
2478-11 -2 -3 -3 1
2479</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002480
Rob Pikedf3183f2009-02-26 16:37:23 -08002481<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002482The shift operators shift the left operand by the shift count specified by the
2483right operand. They implement arithmetic shifts if the left operand is a signed
Rob Pikedf3183f2009-02-26 16:37:23 -08002484integer and logical shifts if it is an unsigned integer. The shift count must
2485be an unsigned integer. There is no upper limit on the shift count. Shifts behave
2486as if the left operand is shifted <code>n</code> times by 1 for a shift
2487count of <code>n</code>.
2488As a result, <code>x << 1</code> is the same as <code>x*2</code>
2489and <code>x >> 1</code> is the same as
2490<code>x/2</code> truncated towards negative infinity.
2491</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002492
Rob Pikedf3183f2009-02-26 16:37:23 -08002493<p>
2494For integer operands, the unary operators
2495<code>+</code>, <code>-</code>, and <code>^</code> are defined as
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002496follows:
Rob Pikedf3183f2009-02-26 16:37:23 -08002497</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002498
Rob Pikeff70f092009-02-20 13:36:14 -08002499<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08002500+x is 0 + x
2501-x negation is 0 - x
2502^x bitwise complement is m ^ x with m = "all bits set to 1"
2503</pre>
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002504
Russ Cox5958dd62009-03-04 17:19:21 -08002505<p>
2506For floating point numbers,
2507<code>+x</code> is the same as <code>x</code>,
2508while <code>-x</code> is the negation of <code>x</code>.
2509</p>
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002510
Robert Griesemerc2d55862009-02-19 16:49:10 -08002511<h3>Integer overflow</h3>
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002512
Rob Pikedf3183f2009-02-26 16:37:23 -08002513<p>
2514For unsigned integer values, the operations <code>+</code>,
2515<code>-</code>, <code>*</code>, and <code>&lt;&lt;</code> are
2516computed modulo 2<sup><i>n</i></sup>, where <i>n</i> is the bit width of
2517the unsigned integer's type
Russ Cox5958dd62009-03-04 17:19:21 -08002518(§Numeric types). Loosely speaking, these unsigned integer operations
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002519discard high bits upon overflow, and programs may rely on ``wrap around''.
Rob Pikedf3183f2009-02-26 16:37:23 -08002520</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002521<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002522For signed integers, the operations <code>+</code>,
2523<code>-</code>, <code>*</code>, and <code>&lt;&lt;</code> may legally
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002524overflow and the resulting value exists and is deterministically defined
2525by the signed integer representation, the operation, and its operands.
Rob Pikedf3183f2009-02-26 16:37:23 -08002526No exception is raised as a result of overflow. A
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002527compiler may not optimize code under the assumption that overflow does
Rob Pikedf3183f2009-02-26 16:37:23 -08002528not occur. For instance, it may not assume that <code>x &lt; x + 1</code> is always true.
2529</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002530
2531
Robert Griesemerc2d55862009-02-19 16:49:10 -08002532<h3>Comparison operators</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002533
Rob Pike5af7de32009-02-24 15:17:59 -08002534<p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002535Comparison operators yield a boolean result. All comparison operators apply
Rob Pike5af7de32009-02-24 15:17:59 -08002536to basic types except bools.
2537The operators <code>==</code> and <code>!=</code> apply, at least in some cases,
2538to all types except arrays and structs.
2539</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002540
Rob Pikeff70f092009-02-20 13:36:14 -08002541<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08002542== equal
2543!= not equal
2544< less
2545<= less or equal
2546> greater
2547>= greater or equal
2548</pre>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002549
Rob Pike5af7de32009-02-24 15:17:59 -08002550<p>
2551Numeric basic types are compared in the usual way.
2552</p>
2553<p>
Robert Griesemer52a54802008-09-30 13:02:50 -07002554Strings are compared byte-wise (lexically).
Rob Pike5af7de32009-02-24 15:17:59 -08002555</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002556<p>
Robert Griesemer18b05c12009-01-26 09:34:19 -08002557Booleans are equal if they are either both "true" or both "false".
Rob Pike5af7de32009-02-24 15:17:59 -08002558</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002559<p>
Rob Pike5af7de32009-02-24 15:17:59 -08002560The rules for comparison of composite types are described in the
2561section on §Comparison compatibility.
2562</p>
Robert Griesemera3294712009-01-05 11:17:26 -08002563
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002564
Robert Griesemerc2d55862009-02-19 16:49:10 -08002565<h3>Logical operators</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002566
Rob Pikedf3183f2009-02-26 16:37:23 -08002567<p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002568Logical operators apply to boolean operands and yield a boolean result.
2569The right operand is evaluated conditionally.
Rob Pikedf3183f2009-02-26 16:37:23 -08002570</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002571
Rob Pikeff70f092009-02-20 13:36:14 -08002572<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08002573&amp;&amp; conditional and p &amp;&amp; q is "if p then q else false"
2574|| conditional or p || q is "if p then true else q"
2575! not !p is "not p"
2576</pre>
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -07002577
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002578
Robert Griesemerc2d55862009-02-19 16:49:10 -08002579<h3>Address operators</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002580
Rob Pikedf3183f2009-02-26 16:37:23 -08002581<p>
Rob Pikeafee1c52009-03-20 17:41:25 -07002582The unary prefix address-of operator <code>&amp;</code> generates the address of its operand, which must be a variable,
2583pointer indirection, field selector, or array or slice indexing operation. It is illegal to take the address of a function
2584result variable.
2585Given an operand of pointer type, the unary prefix pointer indirection operator <code>*</code> retrieves the value pointed
2586to by the operand.
2587</p>
2588
2589<pre>
2590&amp;x
2591&amp;a[f(2)]
2592*p
2593*pf(x)
2594</pre>
2595
Robert Griesemerc2d55862009-02-19 16:49:10 -08002596<p>
2597<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 -08002598operators involved.
Robert Griesemerc2d55862009-02-19 16:49:10 -08002599</font>
Rob Pikeafee1c52009-03-20 17:41:25 -07002600</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002601<p>
Rob Pikeafee1c52009-03-20 17:41:25 -07002602Methods are a form of function and a method ``value'' has a function type.
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002603Consider the type T with method M:
Rob Pikeafee1c52009-03-20 17:41:25 -07002604</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002605
Robert Griesemerc2d55862009-02-19 16:49:10 -08002606<pre>
2607type T struct {
2608 a int;
2609}
2610func (tp *T) M(a int) int;
2611var t *T;
2612</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002613
Rob Pikeafee1c52009-03-20 17:41:25 -07002614<p>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002615To construct the value of method M, one writes
Rob Pikeafee1c52009-03-20 17:41:25 -07002616</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002617
Robert Griesemerc2d55862009-02-19 16:49:10 -08002618<pre>
2619t.M
2620</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002621
Rob Pikeafee1c52009-03-20 17:41:25 -07002622<p>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002623using the variable t (not the type T).
Robert Griesemerc2d55862009-02-19 16:49:10 -08002624<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 -08002625sense then t.M, since only the type T is needed to find the method M, i.e.,
2626its address). TBD.
Robert Griesemerc2d55862009-02-19 16:49:10 -08002627</font>
Rob Pikeafee1c52009-03-20 17:41:25 -07002628</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002629
Rob Pikeafee1c52009-03-20 17:41:25 -07002630<p>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002631The expression t.M is a function value with type
Rob Pikeafee1c52009-03-20 17:41:25 -07002632</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002633
Robert Griesemerc2d55862009-02-19 16:49:10 -08002634<pre>
2635func (t *T, a int) int
2636</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002637
Rob Pikeafee1c52009-03-20 17:41:25 -07002638<p>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002639and may be invoked only as a function, not as a method:
Rob Pikeafee1c52009-03-20 17:41:25 -07002640</p>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002641
Robert Griesemerc2d55862009-02-19 16:49:10 -08002642<pre>
2643var f func (t *T, a int) int;
2644f = t.M;
2645x := f(t, 7);
2646</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002647
Rob Pikeafee1c52009-03-20 17:41:25 -07002648<p>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002649Note that one does not write t.f(7); taking the value of a method demotes
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002650it to a function.
Rob Pikeafee1c52009-03-20 17:41:25 -07002651</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002652
Rob Pikeafee1c52009-03-20 17:41:25 -07002653<p>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002654In general, given type T with method M and variable t of type T,
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002655the method invocation
Rob Pikeafee1c52009-03-20 17:41:25 -07002656</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002657
Robert Griesemerc2d55862009-02-19 16:49:10 -08002658<pre>
2659t.M(args)
2660</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002661
Rob Pikeafee1c52009-03-20 17:41:25 -07002662<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002663is equivalent to the function call
Rob Pikeafee1c52009-03-20 17:41:25 -07002664</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002665
Robert Griesemerc2d55862009-02-19 16:49:10 -08002666<pre>
2667(t.M)(t, args)
2668</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002669
Rob Pikeafee1c52009-03-20 17:41:25 -07002670<p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002671<font color=red>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002672TODO: should probably describe the effect of (t.m) under §Expressions if t.m
2673denotes a method: Effect is as described above, converts into function.
Robert Griesemerc2d55862009-02-19 16:49:10 -08002674</font>
Rob Pikeafee1c52009-03-20 17:41:25 -07002675</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002676<p>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002677If T is an interface type, the expression t.M does not determine which
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002678underlying type's M is called until the point of the call itself. Thus given
Robert Griesemerd8a764c2009-02-06 17:01:10 -08002679T1 and T2, both implementing interface I with method M, the sequence
Rob Pikeafee1c52009-03-20 17:41:25 -07002680</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002681
Robert Griesemerc2d55862009-02-19 16:49:10 -08002682<pre>
2683var t1 *T1;
2684var t2 *T2;
2685var i I = t1;
2686m := i.M;
2687m(t2, 7);
2688</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002689
Rob Pikeafee1c52009-03-20 17:41:25 -07002690<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002691will invoke t2.M() even though m was constructed with an expression involving
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002692t1. Effectively, the value of m is a function literal
Rob Pikeafee1c52009-03-20 17:41:25 -07002693</p>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002694
Robert Griesemerc2d55862009-02-19 16:49:10 -08002695<pre>
2696func (recv I, a int) {
2697 recv.M(a);
2698}
2699</pre>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002700
Rob Pikeafee1c52009-03-20 17:41:25 -07002701<p>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002702that is automatically created.
Rob Pikeafee1c52009-03-20 17:41:25 -07002703</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002704<p>
2705<font color=red>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002706TODO: Document implementation restriction: It is illegal to take the address
Robert Griesemerc2d55862009-02-19 16:49:10 -08002707of a result parameter (e.g.: func f() (x int, p *int) { return 2, &amp;x }).
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002708(TBD: is it an implementation restriction or fact?)
Robert Griesemerc2d55862009-02-19 16:49:10 -08002709</font>
Rob Pikeafee1c52009-03-20 17:41:25 -07002710</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002711
Robert Griesemerc2d55862009-02-19 16:49:10 -08002712<h3>Communication operators</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002713
Robert Griesemerc2d55862009-02-19 16:49:10 -08002714<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002715The term <i>channel</i> means "variable of channel type" (§Channel types).
2716</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002717<p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002718The send operation uses the binary operator "&lt;-", which operates on
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002719a channel and a value (expression):
Rob Pikedf3183f2009-02-26 16:37:23 -08002720</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002721
Robert Griesemerc2d55862009-02-19 16:49:10 -08002722<pre>
2723ch <- 3
2724</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002725
Rob Pikedf3183f2009-02-26 16:37:23 -08002726<p>
2727The send operation sends the value on the channel. Both the channel
2728and the expression are evaluated before communication begins.
2729Communication blocks until the send can proceed, at which point the
2730value is transmitted on the channel. A send can proceed if the
2731channel is asynchronous and there is room in its buffer or the
2732channel is synchronous and a receiver is ready.
2733</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002734<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002735If the send operation appears in an expression context, the value
2736of the expression is a boolean and the operation is non-blocking.
2737The value of the boolean reports true if the communication succeeded,
Rob Pikedf3183f2009-02-26 16:37:23 -08002738false if it did not. (The channel and
2739the expression to be sent are evaluated regardless.)
2740These two examples are equivalent:
2741</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002742
Robert Griesemerc2d55862009-02-19 16:49:10 -08002743<pre>
2744ok := ch <- 3;
2745if ok { print("sent") } else { print("not sent") }
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002746
Robert Griesemerc2d55862009-02-19 16:49:10 -08002747if ch <- 3 { print("sent") } else { print("not sent") }
2748</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002749
Rob Pikedf3183f2009-02-26 16:37:23 -08002750<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002751In other words, if the program tests the value of a send operation,
2752the send is non-blocking and the value of the expression is the
2753success of the operation. If the program does not test the value,
2754the operation blocks until it succeeds.
Rob Pikedf3183f2009-02-26 16:37:23 -08002755</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002756<p>
2757The receive operation uses the prefix unary operator "&lt;-".
Rob Pikedf3183f2009-02-26 16:37:23 -08002758The value of the expression is the value received, whose type
2759is the element type of the channel.
2760</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002761
Robert Griesemerc2d55862009-02-19 16:49:10 -08002762<pre>
2763<-ch
2764</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002765
Rob Pikedf3183f2009-02-26 16:37:23 -08002766<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002767The expression blocks until a value is available, which then can
Rob Pikedf3183f2009-02-26 16:37:23 -08002768be assigned to a variable or used like any other expression.
2769If the receive expression does not save the value, the value is
2770discarded.
2771</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002772
Robert Griesemerc2d55862009-02-19 16:49:10 -08002773<pre>
2774v1 := <-ch
2775v2 = <-ch
2776f(<-ch)
Robert Griesemerc2d55862009-02-19 16:49:10 -08002777<-strobe // wait until clock pulse
2778</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002779
Rob Pikedf3183f2009-02-26 16:37:23 -08002780<p>
Robert Griesemer2902a822008-09-17 13:57:11 -07002781If a receive expression is used in a tuple assignment of the form
Rob Pikedf3183f2009-02-26 16:37:23 -08002782</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002783
Robert Griesemerc2d55862009-02-19 16:49:10 -08002784<pre>
2785x, ok = <-ch; // or: x, ok := <-ch
2786</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002787
Rob Pikedf3183f2009-02-26 16:37:23 -08002788<p>
2789the receive operation becomes non-blocking.
2790If the operation can proceeed, the boolean variable
2791<code>ok</code> will be set to <code>true</code>
2792and the value stored in <code>x</code>; otherwise
2793<code>ok</code> is set
2794to <code>false</code> and <code>x</code> is set to the
2795zero value for its type (§The zero value).
2796</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002797
Rob Pikedf3183f2009-02-26 16:37:23 -08002798<p>
2799<font color=red>TODO: Probably in a separate section, communication semantices
2800need to be presented regarding send, receive, select, and goroutines.</font>
2801</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002802
Robert Griesemerc2d55862009-02-19 16:49:10 -08002803<h3>Constant expressions</h3>
Robert Griesemerb90b2132008-09-19 15:49:55 -07002804
Rob Pikef27e9f02009-02-23 19:22:05 -08002805<p>
2806Constant expressions may contain only constants, <code>iota</code>,
2807numeric literals, string literals, and
2808some constant-valued built-in functions such as <code>unsafe.Sizeof</code>
2809and <code>len</code> applied to an array.
2810In practice, constant expressions are those that can be evaluated at compile time.
2811<p>
2812The type of a constant expression is determined by the type of its
Rob Pikedf3183f2009-02-26 16:37:23 -08002813elements. If it contains only numeric literals, its type is <i>ideal
Rob Pikece9417e2009-03-12 17:08:47 -07002814integer</i> or <i>ideal float</i> (§Ideal number). Whether a literal
2815is an integer or float depends on the syntax of the literals (123 vs. 123.0).
Russ Cox5958dd62009-03-04 17:19:21 -08002816The nature of the arithmetic
Rob Pikef27e9f02009-02-23 19:22:05 -08002817operations within the expression depends, elementwise, on the values;
2818for example, 3/2 is an integer division yielding 1, while 3./2. is
2819a floating point division yielding 1.5. Thus
2820</p>
Robert Griesemerb90b2132008-09-19 15:49:55 -07002821
Robert Griesemerc2d55862009-02-19 16:49:10 -08002822<pre>
Rob Pikef27e9f02009-02-23 19:22:05 -08002823const x = 3./2. + 3/2;
Robert Griesemerc2d55862009-02-19 16:49:10 -08002824</pre>
Robert Griesemerb90b2132008-09-19 15:49:55 -07002825
Robert Griesemerc2d55862009-02-19 16:49:10 -08002826<p>
Rob Pikef27e9f02009-02-23 19:22:05 -08002827yields a floating point constant of ideal float value 2.5 (1.5 +
28281); its constituent expressions are evaluated using distinct rules
2829for division.
2830</p>
2831
2832<p>
2833Intermediate values and the constants themselves
2834may require precision significantly larger than any concrete type
2835in the language. The following are legal declarations:
2836</p>
2837
2838<pre>
2839const Huge = 1 << 100;
2840const Four int8 = Huge >> 98;
2841</pre>
2842
2843<p>
2844A constant expression may appear in any context, such as assignment
2845to a variable of any numeric type, as long as the value of the
Rob Pikedf3183f2009-02-26 16:37:23 -08002846expression can be represented accurately in that context.
Rob Pikef27e9f02009-02-23 19:22:05 -08002847It is erroneous to assign a value with a non-zero fractional part
Rob Pikedf3183f2009-02-26 16:37:23 -08002848to an integer, or if the assignment would overflow or underflow,
2849or in general if the value cannot be represented by the type of
2850the variable.
2851For
2852instance, <code>3</code> can be assigned to any integer variable but also to any
2853floating point variable, while <code>-1e12</code> can be assigned to a
2854<code>float32</code>, <code>float64</code>, or even <code>int64</code>
2855but not <code>uint64</code> or <code>string</code>.
Rob Pikef27e9f02009-02-23 19:22:05 -08002856</p>
Robert Griesemerb90b2132008-09-19 15:49:55 -07002857
Rob Pike21d03492009-03-24 19:16:42 -07002858<p>
2859If a typed constant expression evaluates to a value that is not
2860representable by that type, the compiler reports an error.
2861</p>
2862
2863<pre>
2864uint8(-1) // error, out of range
2865uint8(100) * 100 // error, out of range
2866</pre>
2867
2868<p>
2869The size of the mask used by the unary bitwise complement
2870operator in a typed constant expression is equal to the size of the
2871expression's type. In an ideal constant expression, the bitwise
2872complement operator inverts all the bits, producing a negative value.
2873</p>
2874
2875<pre>
2876^1 // ideal constant, equal to -2
2877uint8(^1) // error, same as uint8(-2), out of range
2878^uint8(1) // typed uint8 constant, same as 0xFF ^ uint8(1) = uint8(0xFE)
2879int8(^1) // same as int8(-2)
2880^int8(1) // error, same as 0xFF ^ int8(1) = int8(0xFE), out of range
2881</pre>
2882
2883<p>
2884TODO: perhaps ^ should be disallowed on non-uints instead of assuming twos complement.
2885Also it may be possible to make typed constants more like variables, at the cost of fewer
2886overflow etc. errors being caught.
2887</p>
2888
Rob Pikec956e902009-04-14 20:10:49 -07002889<h3>Order of evaluation</h3>
2890
2891<p>
2892When evaluating the elements of an assignment or expression,
2893all function calls, method calls and
2894communication operations are evaluated in lexical left-to-right
2895order. Otherwise, the order of evaluation is unspecified.
2896</p>
2897
2898<p>
Robert Griesemer4f185492009-05-01 17:00:16 -07002899For example, in the assignment
Rob Pikec956e902009-04-14 20:10:49 -07002900</p>
2901<pre>
Robert Griesemer4f185492009-05-01 17:00:16 -07002902y[f()], ok = g(h(), i() + x[j()], <-c), k()
Rob Pikec956e902009-04-14 20:10:49 -07002903</pre>
2904<p>
Robert Griesemer4f185492009-05-01 17:00:16 -07002905the function calls and communication happen in the order
2906<code>f()</code>, <code>h()</code>, <code>i()</code>, <code>j()</code>,
2907<code><-c</code>, <code>g()</code>, and <code>k()</code>.
2908However, the order of those events compared to the evaluation
2909and indexing of <code>x</code> and the evaluation
2910of <code>y</code> is not specified.
Rob Pikec956e902009-04-14 20:10:49 -07002911</p>
2912
Rob Pikeff70f092009-02-20 13:36:14 -08002913<hr/>
Robert Griesemerb90b2132008-09-19 15:49:55 -07002914
Robert Griesemerc2d55862009-02-19 16:49:10 -08002915<h2>Statements</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002916
Rob Pike96750f12009-02-27 16:47:48 -08002917<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002918Statements control execution.
Rob Pike96750f12009-02-27 16:47:48 -08002919</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002920
Rob Pikeff70f092009-02-20 13:36:14 -08002921<pre class="grammar">
Robert Griesemerdea43942009-03-16 17:36:52 -07002922Statement =
Rob Pike11417162009-03-24 17:45:53 -07002923 Declaration | EmptyStmt | LabeledStmt |
2924 SimpleStmt | GoStmt | ReturnStmt | BreakStmt | ContinueStmt | GotoStmt |
2925 FallthroughStmt | Block | IfStmt | SwitchStmt | SelectStmt | ForStmt |
2926 DeferStmt .
Robert Griesemer7abfcd92008-10-07 17:14:30 -07002927
Rob Pike11417162009-03-24 17:45:53 -07002928SimpleStmt = ExpressionStmt | IncDecStmt | Assignment | SimpleVarDecl .
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002929
Rob Pike96750f12009-02-27 16:47:48 -08002930StatementList = Statement { Separator Statement } .
2931Separator = [ ";" ]
Robert Griesemerc2d55862009-02-19 16:49:10 -08002932</pre>
Robert Griesemer7271e042008-10-09 20:05:24 -07002933
Robert Griesemerc2d55862009-02-19 16:49:10 -08002934<p>
Rob Pike96750f12009-02-27 16:47:48 -08002935Elements of a list of statements are separated by semicolons,
2936which may be omitted only if the previous statement:
Rob Pike4501d342009-02-19 17:31:36 -08002937</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002938<ul>
Rob Pike96750f12009-02-27 16:47:48 -08002939 <li>ends with the closing parenthesis ")" of a list of declarations
2940 (§Declarations and Scope); or</li>
Rob Pike736a1ae2009-04-02 23:03:41 -07002941 <li>ends with a closing brace "}" that is not part of an expression.
Robert Griesemerc2d55862009-02-19 16:49:10 -08002942</ul>
Robert Griesemer7271e042008-10-09 20:05:24 -07002943
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002944
Robert Griesemerc2d55862009-02-19 16:49:10 -08002945<h3>Empty statements</h3>
Robert Griesemeraed247f2008-10-08 17:05:30 -07002946
Rob Pike96750f12009-02-27 16:47:48 -08002947<p>
Robert Griesemeraed247f2008-10-08 17:05:30 -07002948The empty statement does nothing.
Rob Pike96750f12009-02-27 16:47:48 -08002949</p>
Robert Griesemeraed247f2008-10-08 17:05:30 -07002950
Rob Pikeff70f092009-02-20 13:36:14 -08002951<pre class="grammar">
Rob Pike11417162009-03-24 17:45:53 -07002952EmptyStmt = .
Robert Griesemerc2d55862009-02-19 16:49:10 -08002953</pre>
Robert Griesemeraed247f2008-10-08 17:05:30 -07002954
Rob Pike96750f12009-02-27 16:47:48 -08002955<p>
2956A statement list can always in effect be terminated with a semicolon by
2957adding an empty statement.
2958</p>
2959
Robert Griesemeraed247f2008-10-08 17:05:30 -07002960
Robert Griesemerdea43942009-03-16 17:36:52 -07002961<h3>Labeled statements</h3>
2962
2963<p>
2964A labeled statement may be the target of a <code>goto</code>,
2965<code>break</code> or <code>continue</code> statement.
2966</p>
2967
2968<pre class="grammar">
Rob Pike11417162009-03-24 17:45:53 -07002969LabeledStmt = Label ":" Statement .
Robert Griesemerdea43942009-03-16 17:36:52 -07002970Label = identifier .
2971</pre>
2972
2973<pre>
2974Error: log.Fatal("error encountered")
2975</pre>
2976
2977
Robert Griesemerc2d55862009-02-19 16:49:10 -08002978<h3>Expression statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002979
Rob Pike96750f12009-02-27 16:47:48 -08002980<p>
2981Function calls, method calls, and channel operations
2982can appear in statement context.
2983</p>
2984
2985
Rob Pikeff70f092009-02-20 13:36:14 -08002986<pre class="grammar">
Rob Pike11417162009-03-24 17:45:53 -07002987ExpressionStmt = Expression .
Robert Griesemerc2d55862009-02-19 16:49:10 -08002988</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002989
Robert Griesemerc2d55862009-02-19 16:49:10 -08002990<pre>
2991f(x+y)
Rob Pike96750f12009-02-27 16:47:48 -08002992<-ch
Robert Griesemerc2d55862009-02-19 16:49:10 -08002993</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002994
2995
Robert Griesemerc2d55862009-02-19 16:49:10 -08002996<h3>IncDec statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002997
Rob Pike96750f12009-02-27 16:47:48 -08002998<p>
Robert Griesemer52a54802008-09-30 13:02:50 -07002999The "++" and "--" statements increment or decrement their operands
Rob Pike96750f12009-02-27 16:47:48 -08003000by the ideal numeric value 1. As with an assignment, the operand
3001must be a variable, pointer indirection, field selector or index expression.
3002</p>
Robert Griesemer52a54802008-09-30 13:02:50 -07003003
Rob Pikeff70f092009-02-20 13:36:14 -08003004<pre class="grammar">
Rob Pike11417162009-03-24 17:45:53 -07003005IncDecStmt = Expression ( "++" | "--" ) .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003006</pre>
Russ Cox5958dd62009-03-04 17:19:21 -08003007
Rob Pike96750f12009-02-27 16:47:48 -08003008<p>
Robert Griesemer52a54802008-09-30 13:02:50 -07003009The following assignment statements (§Assignments) are semantically
3010equivalent:
Rob Pike96750f12009-02-27 16:47:48 -08003011</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003012
Rob Pikeff70f092009-02-20 13:36:14 -08003013<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08003014IncDec statement Assignment
3015x++ x += 1
3016x-- x -= 1
3017</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003018
Robert Griesemerc2d55862009-02-19 16:49:10 -08003019<h3>Assignments</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003020
Rob Pikeff70f092009-02-20 13:36:14 -08003021<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08003022Assignment = ExpressionList assign_op ExpressionList .
Rob Pikeff70f092009-02-20 13:36:14 -08003023
Robert Griesemerc2d55862009-02-19 16:49:10 -08003024assign_op = [ add_op | mul_op ] "=" .
3025</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003026
Rob Pike96750f12009-02-27 16:47:48 -08003027<p>
3028Each left-hand side operand must be a variable, pointer indirection,
3029field selector, or index expression.
3030</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003031
Robert Griesemerc2d55862009-02-19 16:49:10 -08003032<pre>
3033x = 1
3034*p = f()
3035a[i] = 23
3036k = <-ch
Rob Pike307ec212009-03-12 15:53:56 -07003037i &amp;^= 1&lt;&lt;n
Robert Griesemerc2d55862009-02-19 16:49:10 -08003038</pre>
Rob Pike96750f12009-02-27 16:47:48 -08003039
3040<p>
3041An <i>assignment operation</i> <code>x</code> <i>op</i><code>=</code>
3042<code>y</code> where <i>op</i> is a binary arithmetic operation is equivalent
3043to <code>x</code> <code>=</code> <code>x</code> <i>op</i>
3044<code>y</code> but evalutates <code>x</code>
3045only once. The <i>op</i><code>=</code> construct is a single token.
3046</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003047
Robert Griesemerc2d55862009-02-19 16:49:10 -08003048<pre>
Rob Pike96750f12009-02-27 16:47:48 -08003049a[i] <<= 2
Robert Griesemerc2d55862009-02-19 16:49:10 -08003050</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003051
Rob Pike96750f12009-02-27 16:47:48 -08003052<p>
3053A tuple assignment assigns the individual elements of a multi-valued
3054operation to a list of variables. There are two forms. In the
3055first, the right hand operand is a single multi-valued expression
3056such as a function evaluation or channel or map operation (§Channel
Russ Cox5958dd62009-03-04 17:19:21 -08003057operations, §Map operations) or a type assertion (§Type assertions).
3058The number of operands on the left
Rob Pike96750f12009-02-27 16:47:48 -08003059hand side must match the number of values. For instance, If
3060<code>f</code> is a function returning two values,
3061</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003062
Robert Griesemerc2d55862009-02-19 16:49:10 -08003063<pre>
3064x, y = f()
3065</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003066
Rob Pike96750f12009-02-27 16:47:48 -08003067<p>
3068assigns the first value to <code>x</code> and the second to <code>y</code>.
3069</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003070
Rob Pike96750f12009-02-27 16:47:48 -08003071<p>
3072In the second form, the number of operands on the left must equal the number
Russ Cox5958dd62009-03-04 17:19:21 -08003073of expressions on the right, each of which must be single-valued.
3074The expressions on the right are evaluated before assigning to
3075any of the operands on the left, but otherwise the evaluation
3076order is unspecified.
Rob Pike96750f12009-02-27 16:47:48 -08003077</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003078
Robert Griesemerc2d55862009-02-19 16:49:10 -08003079<pre>
Rob Pike96750f12009-02-27 16:47:48 -08003080a, b = b, a // exchange a and b
Robert Griesemerc2d55862009-02-19 16:49:10 -08003081</pre>
Rob Pike96750f12009-02-27 16:47:48 -08003082
3083<p>
3084In assignments, the type of each value must be assignment compatible
3085(§Assignment compatibility) with the type of the
3086operand to which it is assigned.
3087</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003088
3089
Robert Griesemerc2d55862009-02-19 16:49:10 -08003090<h3>If statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003091
Rob Pike96750f12009-02-27 16:47:48 -08003092<p>
3093"If" statements specify the conditional execution of two branches
3094according to the value of a boolean expression. If the expression
3095evaluates to true, the "if" branch is executed, otherwise, if
3096present, the "else" branch is executed. A missing condition
3097is equivalent to <code>true</code>.
3098</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003099
Rob Pikeff70f092009-02-20 13:36:14 -08003100<pre class="grammar">
Rob Pike11417162009-03-24 17:45:53 -07003101IfStmt = "if" [ [ SimpleStmt ] ";" ] [ Expression ] Block [ "else" Statement ] .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003102</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003103
Robert Griesemerc2d55862009-02-19 16:49:10 -08003104<pre>
3105if x > 0 {
3106 return true;
3107}
3108</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003109
Russ Cox5958dd62009-03-04 17:19:21 -08003110<p>
3111An "if" statement may include a simple statement before the expression.
3112The scope of any variables declared by that statement
3113extends to the end of the "if" statement
Rob Pike96750f12009-02-27 16:47:48 -08003114and the variables are initialized once before the statement is entered.
Russ Cox5958dd62009-03-04 17:19:21 -08003115</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003116
Robert Griesemerc2d55862009-02-19 16:49:10 -08003117<pre>
3118if x := f(); x < y {
3119 return x;
3120} else if x > z {
3121 return z;
3122} else {
3123 return y;
3124}
3125</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003126
3127
Robert Griesemerc2d55862009-02-19 16:49:10 -08003128<h3>Switch statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003129
Rob Pike96750f12009-02-27 16:47:48 -08003130<p>
3131"Switch" statements provide multi-way execution.
Rob Pike5a578492009-03-17 16:48:35 -07003132An expression or type specifier is compared to the "cases"
3133inside the "switch" to determine which branch
3134to execute.
Rob Pikeafee1c52009-03-20 17:41:25 -07003135</p>
Robert Griesemer091cba82009-03-19 08:39:40 -07003136
3137<pre class="grammar">
Rob Pike11417162009-03-24 17:45:53 -07003138SwitchStmt = ExprSwitchStmt | TypeSwitchStmt .
Robert Griesemer091cba82009-03-19 08:39:40 -07003139</pre>
3140
Rob Pikeafee1c52009-03-20 17:41:25 -07003141<p>
Rob Pike5a578492009-03-17 16:48:35 -07003142There are two forms: expression switches and type switches.
Rob Pike5a578492009-03-17 16:48:35 -07003143In an expression switch, the cases contain expressions that are compared
3144against the value of the switch expression.
3145In a type switch, the cases contain types that are compared against the
3146type of a specially annotated switch expression.
Rob Pike96750f12009-02-27 16:47:48 -08003147</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003148
Rob Pike70c1a102009-03-18 19:23:59 -07003149<h4>Expression switches</h4>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003150
Rob Pike96750f12009-02-27 16:47:48 -08003151<p>
Rob Pike5a578492009-03-17 16:48:35 -07003152In an expression switch,
3153the switch expression is evaluated and
3154the case expressions, which need not be constants,
Robert Griesemer4f185492009-05-01 17:00:16 -07003155are evaluated left-to-right and top-to-bottom; the first one that equals the
Rob Pike5a578492009-03-17 16:48:35 -07003156switch expression
Rob Pike96750f12009-02-27 16:47:48 -08003157triggers execution of the statements of the associated case;
3158the other cases are skipped.
Rob Pike5a578492009-03-17 16:48:35 -07003159If no case matches and there is a "default" case,
3160its statements are executed.
Rob Pike96750f12009-02-27 16:47:48 -08003161There can be at most one default case and it may appear anywhere in the
3162"switch" statement.
Rob Pike70c1a102009-03-18 19:23:59 -07003163A missing expression is equivalent to
3164the expression <code>true</code>.
Rob Pike96750f12009-02-27 16:47:48 -08003165</p>
Rob Pike70c1a102009-03-18 19:23:59 -07003166
3167<pre class="grammar">
Rob Pike11417162009-03-24 17:45:53 -07003168ExprSwitchStmt = "switch" [ [ SimpleStmt ] ";" ] [ Expression ] "{" { ExprCaseClause } "}" .
Robert Griesemer091cba82009-03-19 08:39:40 -07003169ExprCaseClause = ExprSwitchCase ":" [ StatementList ] .
3170ExprSwitchCase = "case" ExpressionList | "default" .
Rob Pike70c1a102009-03-18 19:23:59 -07003171</pre>
3172
Rob Pike96750f12009-02-27 16:47:48 -08003173<p>
3174In a case or default clause,
3175the last statement only may be a "fallthrough" statement
Russ Cox5958dd62009-03-04 17:19:21 -08003176(§Fallthrough statement) to
Rob Pike96750f12009-02-27 16:47:48 -08003177indicate that control should flow from the end of this clause to
Robert Griesemeraed247f2008-10-08 17:05:30 -07003178the first statement of the next clause.
Rob Pike96750f12009-02-27 16:47:48 -08003179Otherwise control flows to the end of the "switch" statement.
3180</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08003181<p>
Rob Pike70c1a102009-03-18 19:23:59 -07003182Each case clause acts as a block for scoping purposes
Russ Cox5958dd62009-03-04 17:19:21 -08003183(§Declarations and scope rules).
Rob Pike96750f12009-02-27 16:47:48 -08003184</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08003185<p>
Russ Cox5958dd62009-03-04 17:19:21 -08003186A "switch" statement may include a simple statement before the
Rob Pike96750f12009-02-27 16:47:48 -08003187expression.
Russ Cox5958dd62009-03-04 17:19:21 -08003188The scope of any variables declared by that statement
3189extends to the end of the "switch" statement
3190and the variables are initialized once before the statement is entered.
Rob Pike96750f12009-02-27 16:47:48 -08003191</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003192
Robert Griesemerc2d55862009-02-19 16:49:10 -08003193<pre>
3194switch tag {
Russ Cox5958dd62009-03-04 17:19:21 -08003195default: s3()
3196case 0, 1, 2, 3: s1()
3197case 4, 5, 6, 7: s2()
Robert Griesemerc2d55862009-02-19 16:49:10 -08003198}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003199
Rob Pike96750f12009-02-27 16:47:48 -08003200switch x := f(); {
Russ Cox5958dd62009-03-04 17:19:21 -08003201case x &lt; 0: return -x
3202default: return x
Robert Griesemerc2d55862009-02-19 16:49:10 -08003203}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003204
Rob Pike96750f12009-02-27 16:47:48 -08003205switch { // missing expression means "true"
Russ Cox5958dd62009-03-04 17:19:21 -08003206case x < y: f1();
3207case x < z: f2();
3208case x == 4: f3();
Robert Griesemerc2d55862009-02-19 16:49:10 -08003209}
3210</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003211
Rob Pike70c1a102009-03-18 19:23:59 -07003212<h4>Type switches</h4>
3213
Rob Pike5a578492009-03-17 16:48:35 -07003214<p>
Rob Pike70c1a102009-03-18 19:23:59 -07003215A type switch compares types rather than values. It is otherwise similar
3216to an expression switch. It is introduced by special
3217notation in the form of a simple declaration whose right hand side
Rob Pikef5387602009-03-30 16:08:41 -07003218has the form of a type assertion (§Type assertions)
Rob Pike70c1a102009-03-18 19:23:59 -07003219using the reserved word <code>type</code> rather than an actual type.
3220Cases then match literal types against the dynamic type of the expression
Rob Pikef5387602009-03-30 16:08:41 -07003221in the type assertion.
Rob Pike5a578492009-03-17 16:48:35 -07003222</p>
3223
Rob Pike70c1a102009-03-18 19:23:59 -07003224<pre class="grammar">
Rob Pike11417162009-03-24 17:45:53 -07003225TypeSwitchStmt = "switch" [ [ SimpleStmt ] ";" ] TypeSwitchGuard "{" { TypeCaseClause } "}" .
Robert Griesemer091cba82009-03-19 08:39:40 -07003226TypeSwitchGuard = identifier ":=" Expression "." "(" "type" ")" .
3227TypeCaseClause = TypeSwitchCase ":" [ StatementList ] .
Rob Pike94b67eb2009-03-24 17:40:47 -07003228TypeSwitchCase = "case" ( type | "nil" ) | "default" .
Rob Pike5a578492009-03-17 16:48:35 -07003229</pre>
3230
3231<p>
Rob Pike94b67eb2009-03-24 17:40:47 -07003232If the interface value equals <code>nil</code>,
3233only an explict <code>nil</code> case or "default"
3234case will execute.
3235</p>
3236
3237<p>
Rob Pike5a578492009-03-17 16:48:35 -07003238Given a function <code>f</code>
3239that returns a value of interface type,
Rob Pike70c1a102009-03-18 19:23:59 -07003240the following type switch:
Rob Pike5a578492009-03-17 16:48:35 -07003241</p>
3242
3243<pre>
3244switch i := f().(type) {
Rob Pike94b67eb2009-03-24 17:40:47 -07003245case nil:
3246 printString("f() returns nil");
Rob Pike5a578492009-03-17 16:48:35 -07003247case int:
3248 printInt(i); // i is an int
3249case float:
3250 printFloat(i); // i is a float
Rob Pike70c1a102009-03-18 19:23:59 -07003251case func(int) float:
3252 printFunction(i); // i is a function
Rob Pike5a578492009-03-17 16:48:35 -07003253default:
3254 printString("don't know the type");
3255}
3256</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003257
Rob Pike70c1a102009-03-18 19:23:59 -07003258<p>
3259could be rewritten:
3260</p>
3261
3262<pre>
3263v := f();
Rob Pike94b67eb2009-03-24 17:40:47 -07003264if v == nil {
3265 printString("f() returns nil");
3266} else if i, is_int := v.(int); is_int {
Rob Pike70c1a102009-03-18 19:23:59 -07003267 printInt(i); // i is an int
3268} else if i, is_float := v.(float); is_float {
3269 printFloat(i); // i is a float
3270} else if i, is_func := v.(func(int) float); is_func {
3271 printFunction(i); // i is a function
3272} else {
3273 printString("don't know the type");
3274}
3275</pre>
3276
3277<p>
3278In a type switch, the guard is mandatory,
3279there can be only one type per "case", and
3280the "fallthrough" statement is not allowed.
3281</p>
3282
Robert Griesemerc2d55862009-02-19 16:49:10 -08003283<h3>For statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003284
Rob Pike96750f12009-02-27 16:47:48 -08003285<p>
3286A "for" statement specifies repeated execution of a block. The iteration is
3287controlled by a condition, a "for" clause, or a "range" clause.
3288</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003289
Rob Pikeff70f092009-02-20 13:36:14 -08003290<pre class="grammar">
Rob Pike11417162009-03-24 17:45:53 -07003291ForStmt = "for" [ Condition | ForClause | RangeClause ] Block .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003292Condition = Expression .
3293</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003294
Rob Pike96750f12009-02-27 16:47:48 -08003295<p>
3296In its simplest form, a "for" statement specifies the repeated execution of
3297a block as long as a boolean condition evaluates to true.
3298The condition is evaluated before each iteration.
3299If the condition is absent, it is equivalent to <code>true</code>.
3300</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003301
Robert Griesemerc2d55862009-02-19 16:49:10 -08003302<pre>
3303for a &lt; b {
3304 a *= 2
3305}
3306</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003307
Rob Pike96750f12009-02-27 16:47:48 -08003308<p>
3309A "for" statement with a "for" clause is also controlled by its condition, but
3310additionally it may specify an <i>init</i>
3311and a <i>post</i> statement, such as an assignment,
3312an increment or decrement statement. The init statement (but not the post
3313statement) may also be a short variable declaration; the scope of the variables
3314it declares ends at the end of the statement
Russ Cox5958dd62009-03-04 17:19:21 -08003315(§Declarations and scope rules).
Rob Pike96750f12009-02-27 16:47:48 -08003316</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003317
Rob Pikeff70f092009-02-20 13:36:14 -08003318<pre class="grammar">
Rob Pike11417162009-03-24 17:45:53 -07003319ForClause = [ InitStmt ] ";" [ Condition ] ";" [ PostStmt ] .
3320InitStmt = SimpleStmt .
3321PostStmt = SimpleStmt .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003322</pre>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003323
Robert Griesemerc2d55862009-02-19 16:49:10 -08003324<pre>
3325for i := 0; i < 10; i++ {
3326 f(i)
3327}
3328</pre>
Russ Cox5958dd62009-03-04 17:19:21 -08003329
Robert Griesemerc2d55862009-02-19 16:49:10 -08003330<p>
Rob Pike96750f12009-02-27 16:47:48 -08003331If non-empty, the init statement is executed once before evaluating the
3332condition for the first iteration;
3333the post statement is executed after each execution of the block (and
3334only if the block was executed).
3335Any element of the "for" clause may be empty but the semicolons are
3336required unless there is only a condition.
3337If the condition is absent, it is equivalent to <code>true</code>.
3338</p>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003339
Robert Griesemerc2d55862009-02-19 16:49:10 -08003340<pre>
3341for ; cond ; { S() } is the same as for cond { S() }
3342for true { S() } is the same as for { S() }
3343</pre>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003344
Rob Pike96750f12009-02-27 16:47:48 -08003345<p>
3346A "for" statement with a "range" clause
Rob Pike7aee71b2009-04-15 20:28:25 -07003347iterates through all entries of an array, slice, string or map,
3348or values received on a channel.
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003349For each entry it first assigns the current index or key to an iteration
3350variable - or the current (index, element) or (key, value) pair to a pair
Rob Pike96750f12009-02-27 16:47:48 -08003351of iteration variables - and then executes the block.
3352</p>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003353
Rob Pikeff70f092009-02-20 13:36:14 -08003354<pre class="grammar">
Rob Pikeb3408792009-04-15 20:51:17 -07003355RangeClause = ExpressionList ( "=" | ":=" ) "range" Expression .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003356</pre>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003357
Robert Griesemerc2d55862009-02-19 16:49:10 -08003358<p>
Rob Pike7aee71b2009-04-15 20:28:25 -07003359The type of the right-hand expression in the "range" clause must be an
3360array, slice, string or map, or a pointer to an array, slice, string or map;
Rob Pike94b67eb2009-03-24 17:40:47 -07003361or it may be a channel.
Rob Pike7aee71b2009-04-15 20:28:25 -07003362Except for channels,
Rob Pikeb3408792009-04-15 20:51:17 -07003363the identifier list must contain one or two expressions
3364(as in assignments, these must be a
3365variable, pointer indirection, field selector, or index expression)
3366denoting the
Rob Pike96750f12009-02-27 16:47:48 -08003367iteration variables. On each iteration,
Rob Pike7aee71b2009-04-15 20:28:25 -07003368the first variable is set to the string, array or slice index or
Rob Pike96750f12009-02-27 16:47:48 -08003369map key, and the second variable, if present, is set to the corresponding
Rob Pike7aee71b2009-04-15 20:28:25 -07003370string or array element or map value.
Rob Pike96750f12009-02-27 16:47:48 -08003371The types of the array or slice index (always <code>int</code>)
3372and element, or of the map key and value respectively,
3373must be assignment compatible to the iteration variables.
3374</p>
3375<p>
Rob Pike7aee71b2009-04-15 20:28:25 -07003376For strings, the "range" clause iterates over the Unicode code points
3377in the string. On successive iterations, the index variable will be the
Rob Pike55faa5f2009-04-15 21:49:50 -07003378index of successive UTF-8-encoded code points in the string, and
Rob Pike7aee71b2009-04-15 20:28:25 -07003379the second variable, of type <code>int</code>, will be the value of
3380the corresponding code point. If the iteration encounters an invalid
3381UTF-8 sequence, the second variable will be <code>0xFFFD</code>,
3382the Unicode replacement character, and the next iteration will advance
3383a single byte in the string.
3384</p>
3385<p>
Rob Pike94b67eb2009-03-24 17:40:47 -07003386For channels, the identifier list must contain one identifier.
Robert Griesemerce9fbdb2009-04-29 11:45:08 -07003387The iteration receives values sent on the channel until the channel is closed;
Rob Pike94b67eb2009-03-24 17:40:47 -07003388it does not process the zero value sent before the channel is closed.
3389</p>
3390<p>
Rob Pike96750f12009-02-27 16:47:48 -08003391The iteration variables may be declared by the "range" clause (":="), in which
Russ Cox5958dd62009-03-04 17:19:21 -08003392case their scope ends at the end of the "for" statement (§Declarations and
Rob Pike96750f12009-02-27 16:47:48 -08003393scope rules). In this case their types are set to
Russ Cox5958dd62009-03-04 17:19:21 -08003394<code>int</code> and the array element type, or the map key and value types, respectively.
Rob Pike96750f12009-02-27 16:47:48 -08003395If the iteration variables are declared outside the "for" statement,
3396after execution their values will be those of the last iteration.
3397</p>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003398
Robert Griesemerc2d55862009-02-19 16:49:10 -08003399<pre>
3400var a [10]string;
Rob Pike426335f2009-03-02 17:52:52 -08003401m := map[string]int{"mon":0, "tue":1, "wed":2, "thu":3, "fri":4, "sat":5, "sun":6};
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003402
Robert Griesemerc2d55862009-02-19 16:49:10 -08003403for i, s := range a {
3404 // type of i is int
3405 // type of s is string
3406 // s == a[i]
3407 g(i, s)
3408}
3409
3410var key string;
3411var val interface {}; // value type of m is assignment-compatible to val
3412for key, value = range m {
3413 h(key, value)
3414}
3415// key == last map key encountered in iteration
3416// val == map[key]
3417</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003418
Rob Pike96750f12009-02-27 16:47:48 -08003419<p>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003420If map entries that have not yet been processed are deleted during iteration,
3421they will not be processed. If map entries are inserted during iteration, the
Russ Cox5958dd62009-03-04 17:19:21 -08003422behavior is implementation-dependent, but each entry will be processed at most once.
Rob Pike96750f12009-02-27 16:47:48 -08003423</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003424
Robert Griesemerc2d55862009-02-19 16:49:10 -08003425<h3>Go statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003426
Rob Pike96750f12009-02-27 16:47:48 -08003427<p>
Russ Cox5958dd62009-03-04 17:19:21 -08003428A "go" statement starts the execution of a function or method call
Rob Pike96750f12009-02-27 16:47:48 -08003429as an independent concurrent thread of control, or <i>goroutine</i>,
3430within the same address space.
3431</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003432
Rob Pikeff70f092009-02-20 13:36:14 -08003433<pre class="grammar">
Rob Pike11417162009-03-24 17:45:53 -07003434GoStmt = "go" Expression .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003435</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003436
Rob Pike96750f12009-02-27 16:47:48 -08003437<p>
3438The expression must be a call, and
3439unlike with a regular call, program execution does not wait
Robert Griesemerb9f8b9c2008-09-26 13:38:38 -07003440for the invoked function to complete.
Rob Pike96750f12009-02-27 16:47:48 -08003441</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003442
Robert Griesemerc2d55862009-02-19 16:49:10 -08003443<pre>
3444go Server()
3445go func(ch chan <- bool) { for { sleep(10); ch <- true; }} (c)
3446</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003447
3448
Robert Griesemerc2d55862009-02-19 16:49:10 -08003449<h3>Select statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003450
Rob Pike96750f12009-02-27 16:47:48 -08003451<p>
3452A "select" statement chooses which of a set of possible communications
3453will proceed. It looks similar to a "switch" statement but with the
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003454cases all referring to communication operations.
Rob Pike96750f12009-02-27 16:47:48 -08003455</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003456
Rob Pikeff70f092009-02-20 13:36:14 -08003457<pre class="grammar">
Rob Pike11417162009-03-24 17:45:53 -07003458SelectStmt = "select" "{" { CommClause } "}" .
Russ Cox5958dd62009-03-04 17:19:21 -08003459CommClause = CommCase ":" StatementList .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003460CommCase = "case" ( SendExpr | RecvExpr) | "default" .
3461SendExpr = Expression "&lt;-" Expression .
3462RecvExpr = [ Expression ( "=" | ":=" ) ] "&lt;-" Expression .
3463</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003464
Rob Pike96750f12009-02-27 16:47:48 -08003465<p>
Robert Griesemer347cf672008-10-03 14:04:28 -07003466Each communication clause acts as a block for the purpose of scoping
3467(§Declarations and scope rules).
Rob Pike96750f12009-02-27 16:47:48 -08003468</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08003469<p>
Rob Pike96750f12009-02-27 16:47:48 -08003470For all the send and receive expressions in the "select"
3471statement, the channel expression is evaluated. Any expressions
Rob Pike569a1072008-10-03 11:18:45 -07003472that appear on the right hand side of send expressions are also
3473evaluated. If any of the resulting channels can proceed, one is
3474chosen and the corresponding communication and statements are
3475evaluated. Otherwise, if there is a default case, that executes;
Rob Pikecd368a22008-10-02 10:37:12 -07003476if not, the statement blocks until one of the communications can
Rob Pike569a1072008-10-03 11:18:45 -07003477complete. The channels and send expressions are not re-evaluated.
Rob Pike96750f12009-02-27 16:47:48 -08003478A channel pointer may be <code>nil</code>,
3479which is equivalent to that case not
3480being present in the select statement
3481except, if a send, its expression is still evaluated.
3482</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08003483<p>
Rob Pike569a1072008-10-03 11:18:45 -07003484Since all the channels and send expressions are evaluated, any side
3485effects in that evaluation will occur for all the communications
Rob Pike96750f12009-02-27 16:47:48 -08003486in the "select" statement.
3487</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08003488<p>
Rob Pike96750f12009-02-27 16:47:48 -08003489If multiple cases can proceed, a uniform fair choice is made to decide
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003490which single communication will execute.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003491<p>
Rob Pike96750f12009-02-27 16:47:48 -08003492The receive case may declare a new variable using a short variable declaration
3493(§Short variable declarations).
3494The scope of such variables continues to the end of the
3495respective case's statements.
3496</p>
Robert Griesemer2902a822008-09-17 13:57:11 -07003497
Robert Griesemerc2d55862009-02-19 16:49:10 -08003498<pre>
3499var c, c1, c2 chan int;
3500var i1, i2 int;
3501select {
3502case i1 = &lt;-c1:
3503 print("received ", i1, " from c1\n");
3504case c2 &lt;- i2:
3505 print("sent ", i2, " to c2\n");
3506default:
3507 print("no communication\n");
3508}
3509
3510for { // send random sequence of bits to c
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003511 select {
Robert Griesemerc2d55862009-02-19 16:49:10 -08003512 case c &lt;- 0: // note: no statement, no fallthrough, no folding of cases
3513 case c &lt;- 1:
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003514 }
Robert Griesemerc2d55862009-02-19 16:49:10 -08003515}
Robert Griesemerc2d55862009-02-19 16:49:10 -08003516</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003517
Robert Griesemerc2d55862009-02-19 16:49:10 -08003518<font color=red>
Robert Griesemer2902a822008-09-17 13:57:11 -07003519TODO: Make semantics more precise.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003520</font>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003521
3522
Robert Griesemerc2d55862009-02-19 16:49:10 -08003523<h3>Return statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003524
Rob Pike96750f12009-02-27 16:47:48 -08003525<p>
3526A "return" statement terminates execution of the containing function
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003527and optionally provides a result value or values to the caller.
Rob Pike96750f12009-02-27 16:47:48 -08003528</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003529
Rob Pikeff70f092009-02-20 13:36:14 -08003530<pre class="grammar">
Rob Pike11417162009-03-24 17:45:53 -07003531ReturnStmt = "return" [ ExpressionList ] .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003532</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003533
Rob Pike96750f12009-02-27 16:47:48 -08003534<pre>
3535func procedure() {
3536 return
3537}
3538</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003539
Rob Pike96750f12009-02-27 16:47:48 -08003540<p>
3541There are two ways to return values from a function with a result
3542type. The first is to explicitly list the return value or values
Russ Cox5958dd62009-03-04 17:19:21 -08003543in the "return" statement.
3544Normally, the expressions
Rob Pike96750f12009-02-27 16:47:48 -08003545must be single-valued and assignment-compatible to the elements of
3546the result type of the function.
3547</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003548
Robert Griesemerc2d55862009-02-19 16:49:10 -08003549<pre>
3550func simple_f() int {
Rob Pike96750f12009-02-27 16:47:48 -08003551 return 2
Robert Griesemerc2d55862009-02-19 16:49:10 -08003552}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003553
Robert Griesemerc2d55862009-02-19 16:49:10 -08003554func complex_f1() (re float, im float) {
Rob Pike96750f12009-02-27 16:47:48 -08003555 return -7.0, -4.0
Robert Griesemerc2d55862009-02-19 16:49:10 -08003556}
3557</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003558
Rob Pike96750f12009-02-27 16:47:48 -08003559<p>
3560However, if the expression list in the "return" statement is a single call
3561to a multi-valued function, the values returned from the called function
3562will be returned from this one. The result types of the current function
3563and the called function must match.
3564</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003565
Robert Griesemerc2d55862009-02-19 16:49:10 -08003566<pre>
3567func complex_f2() (re float, im float) {
Rob Pike96750f12009-02-27 16:47:48 -08003568 return complex_f1()
3569}
3570</pre>
3571
3572<p>
Russ Cox5958dd62009-03-04 17:19:21 -08003573The second way to return values is to use the elements of the
Rob Pike96750f12009-02-27 16:47:48 -08003574result list of the function as variables. When the function begins
3575execution, these variables are initialized to the zero values for
3576their type (§The zero value). The function can assign them as
3577necessary; if the "return" provides no values, those of the variables
3578will be returned to the caller.
3579</p>
3580
3581<pre>
3582func complex_f3() (re float, im float) {
Robert Griesemerc2d55862009-02-19 16:49:10 -08003583 re = 7.0;
3584 im = 4.0;
3585 return;
3586}
3587</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003588
Russ Cox5958dd62009-03-04 17:19:21 -08003589<p>
3590TODO: Define when return is required.
3591</p>
3592
Robert Griesemerc2d55862009-02-19 16:49:10 -08003593<h3>Break statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003594
Rob Pike96750f12009-02-27 16:47:48 -08003595<p>
3596A "break" statement terminates execution of the innermost
3597"for", "switch" or "select" statement.
3598</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003599
Rob Pikeff70f092009-02-20 13:36:14 -08003600<pre class="grammar">
Rob Pike11417162009-03-24 17:45:53 -07003601BreakStmt = "break" [ Label ].
Robert Griesemerc2d55862009-02-19 16:49:10 -08003602</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003603
Rob Pike96750f12009-02-27 16:47:48 -08003604<p>
3605If there is a label, it must be that of an enclosing
3606"for", "switch" or "select" statement, and that is the one whose execution
3607terminates
3608(§For statements, §Switch statements, §Select statements).
3609</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003610
Robert Griesemerc2d55862009-02-19 16:49:10 -08003611<pre>
3612L: for i < n {
3613 switch i {
Rob Pike96750f12009-02-27 16:47:48 -08003614 case 5: break L
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003615 }
Robert Griesemerc2d55862009-02-19 16:49:10 -08003616}
3617</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003618
Robert Griesemerc2d55862009-02-19 16:49:10 -08003619<h3>Continue statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003620
Rob Pike96750f12009-02-27 16:47:48 -08003621<p>
3622A "continue" statement begins the next iteration of the
3623innermost "for" loop at the post statement (§For statements).
3624</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003625
Rob Pikeff70f092009-02-20 13:36:14 -08003626<pre class="grammar">
Rob Pike11417162009-03-24 17:45:53 -07003627ContinueStmt = "continue" [ Label ].
Robert Griesemerc2d55862009-02-19 16:49:10 -08003628</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003629
Rob Pike96750f12009-02-27 16:47:48 -08003630<p>
3631The optional label is analogous to that of a "break" statement.
3632</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003633
Robert Griesemerc2d55862009-02-19 16:49:10 -08003634<h3>Goto statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003635
Rob Pike96750f12009-02-27 16:47:48 -08003636<p>
3637A "goto" statement transfers control to the statement with the corresponding label.
3638</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003639
Rob Pikeff70f092009-02-20 13:36:14 -08003640<pre class="grammar">
Rob Pike11417162009-03-24 17:45:53 -07003641GotoStmt = "goto" Label .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003642</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003643
Robert Griesemerc2d55862009-02-19 16:49:10 -08003644<pre>
3645goto Error
3646</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003647
Rob Pike96750f12009-02-27 16:47:48 -08003648<p>
3649Executing the "goto" statement must not cause any variables to come into
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003650scope that were not already in scope at the point of the goto. For
3651instance, this example:
Rob Pike96750f12009-02-27 16:47:48 -08003652</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003653
Robert Griesemerc2d55862009-02-19 16:49:10 -08003654<pre>
3655goto L; // BAD
3656v := 3;
3657L:
3658</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003659
Rob Pike96750f12009-02-27 16:47:48 -08003660<p>
3661is erroneous because the jump to label <code>L</code> skips
3662the creation of <code>v</code>.
Russ Cox5958dd62009-03-04 17:19:21 -08003663(TODO: Eliminate in favor of used and not set errors?)
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<h3>Fallthrough statements</h3>
Robert Griesemeraed247f2008-10-08 17:05:30 -07003667
Rob Pike96750f12009-02-27 16:47:48 -08003668<p>
3669A "fallthrough" statement transfers control to the first statement of the
Robert Griesemer091cba82009-03-19 08:39:40 -07003670next case clause in a expression "switch" statement (§Expression switches). It may
3671be used only as the final non-empty statement in a case or default clause in an
3672expression "switch" statement.
Rob Pike96750f12009-02-27 16:47:48 -08003673</p>
Robert Griesemeraed247f2008-10-08 17:05:30 -07003674
Rob Pikeff70f092009-02-20 13:36:14 -08003675<pre class="grammar">
Rob Pike11417162009-03-24 17:45:53 -07003676FallthroughStmt = "fallthrough" .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003677</pre>
Robert Griesemeraed247f2008-10-08 17:05:30 -07003678
3679
Robert Griesemerc2d55862009-02-19 16:49:10 -08003680<h3>Defer statements</h3>
Robert Griesemer4a903e02009-01-27 09:29:40 -08003681
Rob Pike96750f12009-02-27 16:47:48 -08003682<p>
3683A "defer" statement invokes a function whose execution is deferred to the moment
3684the surrounding function returns.
3685</p>
Robert Griesemer4a903e02009-01-27 09:29:40 -08003686
Rob Pikeff70f092009-02-20 13:36:14 -08003687<pre class="grammar">
Rob Pike11417162009-03-24 17:45:53 -07003688DeferStmt = "defer" Expression .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003689</pre>
Robert Griesemer4a903e02009-01-27 09:29:40 -08003690
Rob Pike96750f12009-02-27 16:47:48 -08003691<p>
3692The expression must be a function or method call.
3693Each time the "defer" statement
Robert Griesemer7471eab2009-01-27 14:51:24 -08003694executes, the parameters to the function call are evaluated and saved anew but the
Robert Griesemer4a903e02009-01-27 09:29:40 -08003695function is not invoked. Immediately before the innermost function surrounding
Rob Pike96750f12009-02-27 16:47:48 -08003696the "defer" statement returns, but after its return value (if any) is evaluated,
Robert Griesemer4a903e02009-01-27 09:29:40 -08003697each deferred function is executed with its saved parameters. Deferred functions
3698are executed in LIFO order.
Rob Pike96750f12009-02-27 16:47:48 -08003699</p>
Robert Griesemer4a903e02009-01-27 09:29:40 -08003700
Robert Griesemerc2d55862009-02-19 16:49:10 -08003701<pre>
3702lock(l);
3703defer unlock(l); // unlocking happens before surrounding function returns
Robert Griesemer4a903e02009-01-27 09:29:40 -08003704
Robert Griesemerc2d55862009-02-19 16:49:10 -08003705// prints 3 2 1 0 before surrounding function returns
3706for i := 0; i &lt;= 3; i++ {
3707 defer fmt.Print(i);
3708}
3709</pre>
Robert Griesemer4a903e02009-01-27 09:29:40 -08003710
Rob Pikeff70f092009-02-20 13:36:14 -08003711<hr/>
Robert Griesemer4a903e02009-01-27 09:29:40 -08003712
Rob Pikef27e9f02009-02-23 19:22:05 -08003713<h2>Predeclared functions</h2>
Robert Griesemerc2d55862009-02-19 16:49:10 -08003714<ul>
3715 <li>cap
Rob Pike94b67eb2009-03-24 17:40:47 -07003716 <li>close
3717 <li>closed
Robert Griesemerc2d55862009-02-19 16:49:10 -08003718 <li>len
3719 <li>make
3720 <li>new
3721 <li>panic
3722 <li>panicln
3723 <li>print
3724 <li>println
Robert Griesemerc2d55862009-02-19 16:49:10 -08003725</ul>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003726
Robert Griesemerc2d55862009-02-19 16:49:10 -08003727<h3>Length and capacity</h3>
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -07003728
Rob Pikeff70f092009-02-20 13:36:14 -08003729<pre class="grammar">
Rob Pikedf3183f2009-02-26 16:37:23 -08003730Call Argument type Result
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -07003731
Robert Griesemerc2d55862009-02-19 16:49:10 -08003732len(s) string, *string string length (in bytes)
Rob Pikedf3183f2009-02-26 16:37:23 -08003733 [n]T, *[n]T array length (== n)
3734 []T, *[]T slice length
3735 map[K]T, *map[K]T map length
3736 chan T number of elements in channel buffer
Robert Griesemer4dc25282008-09-09 10:37:19 -07003737
Robert Griesemerc2d55862009-02-19 16:49:10 -08003738cap(s) []T, *[]T capacity of s
Rob Pikedf3183f2009-02-26 16:37:23 -08003739 map[K]T, *map[K]T capacity of s
3740 chan T channel buffer capacity
Robert Griesemerc2d55862009-02-19 16:49:10 -08003741</pre>
Robert Griesemer4dc25282008-09-09 10:37:19 -07003742
Robert Griesemerc2d55862009-02-19 16:49:10 -08003743<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08003744The type of the result is always <code>int</code> and the
3745implementation guarantees that
3746the result always fits into an <code>int</code>.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003747<p>
Robert Griesemer633957b2009-01-06 13:23:20 -08003748The capacity of a slice or map is the number of elements for which there is
Rob Pikedf3183f2009-02-26 16:37:23 -08003749space allocated in the underlying array (for a slice) or map. For a slice
3750<code>s</code>, at any time the following relationship holds:
Robert Griesemer633957b2009-01-06 13:23:20 -08003751
Robert Griesemerc2d55862009-02-19 16:49:10 -08003752<pre>
37530 <= len(s) <= cap(s)
3754</pre>
Robert Griesemer667ef6c2008-09-10 13:00:32 -07003755
Robert Griesemer4dc25282008-09-09 10:37:19 -07003756
Robert Griesemerc2d55862009-02-19 16:49:10 -08003757<h3>Conversions</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003758
Rob Pike96750f12009-02-27 16:47:48 -08003759<p>
Rob Pike96750f12009-02-27 16:47:48 -08003760Conversions look like function calls of the form
3761</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003762
Rob Pikeff70f092009-02-20 13:36:14 -08003763<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08003764T(value)
3765</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003766
Rob Pike96750f12009-02-27 16:47:48 -08003767<p>
Russ Coxe8b43192009-03-03 08:10:25 -08003768where <code>T</code> is a type
3769and <code>value</code> is an expression
3770that can be converted to a value
Rob Pikedf3183f2009-02-26 16:37:23 -08003771of result type <code>T</code>.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003772<p>
Robert Griesemer133c68e2008-09-26 14:04:21 -07003773The following conversion rules apply:
Rob Pike96750f12009-02-27 16:47:48 -08003774</p>
3775<ul>
3776<li>
Robert Griesemer1a304e12009-05-08 10:25:06 -070037771) Between equal types (§Type equality and identity).
3778The conversion always succeeds.
Russ Coxe8b43192009-03-03 08:10:25 -08003779</li>
3780<li>
37812) Between integer types. If the value is a signed quantity, it is
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003782sign extended to implicit infinite precision; otherwise it is zero
3783extended. It is then truncated to fit in the result type size.
Rob Pike96750f12009-02-27 16:47:48 -08003784For example, <code>uint32(int8(0xFF))</code> is <code>0xFFFFFFFF</code>.
3785The conversion always yields a valid value; there is no signal for overflow.
3786</li>
3787<li>
Russ Coxe8b43192009-03-03 08:10:25 -080037883) Between integer and floating point types, or between floating point
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003789types. To avoid overdefining the properties of the conversion, for
Robert Griesemer434c6052008-11-07 13:34:37 -08003790now it is defined as a ``best effort'' conversion. The conversion
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003791always succeeds but the value may be a NaN or other problematic
Robert Griesemerc2d55862009-02-19 16:49:10 -08003792result. <font color=red>TODO: clarify?</font>
Rob Pike96750f12009-02-27 16:47:48 -08003793</li>
3794<li>
Robert Griesemer1a304e12009-05-08 10:25:06 -070037954) Strings permit three special conversions:
Rob Pike96750f12009-02-27 16:47:48 -08003796</li>
3797<li>
Russ Coxe8b43192009-03-03 08:10:25 -080037984a) Converting an integer value yields a string containing the UTF-8
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003799representation of the integer.
3800
Robert Griesemerc2d55862009-02-19 16:49:10 -08003801<pre>
3802string(0x65e5) // "\u65e5"
3803</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003804
Rob Pike96750f12009-02-27 16:47:48 -08003805</li>
3806<li>
Robert Griesemer1a304e12009-05-08 10:25:06 -070038074b) Converting a slice of integers yields a string that is the
3808concatenation of the individual integers converted to strings.
3809If the slice value is <code>nil</code>, the result is the empty string.
3810<pre>
3811string([]int{0x65e5, 0x672c, 0x8a9e}) // "\u65e5\u672c\u8a9e"
3812</pre>
3813</li>
3814<li>
38154c) Converting a slice of bytes yields a string whose successive
3816bytes are those of the slice. If the slice value is <code>nil</code>,
3817the result is the empty string.
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003818
Robert Griesemerc2d55862009-02-19 16:49:10 -08003819<pre>
Robert Griesemer1a304e12009-05-08 10:25:06 -07003820string([]byte{'h', 'e', 'l', 'l', 'o'}) // "hello"
Robert Griesemerc2d55862009-02-19 16:49:10 -08003821</pre>
Rob Pike96750f12009-02-27 16:47:48 -08003822</li>
3823</ul>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003824
Rob Pikedf3183f2009-02-26 16:37:23 -08003825<p>
Rob Pike96750f12009-02-27 16:47:48 -08003826There is no linguistic mechanism to convert between pointers and integers.
3827The <code>unsafe</code> package
3828implements this functionality under
3829restricted circumstances (§Package <code>unsafe</code>).
Rob Pikedf3183f2009-02-26 16:37:23 -08003830</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003831
3832
Robert Griesemerc2d55862009-02-19 16:49:10 -08003833<h3>Allocation</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003834
Rob Pike96750f12009-02-27 16:47:48 -08003835<p>
3836The built-in function <code>new</code> takes a type <code>T</code> and
3837returns a value of type <code>*T</code>.
Robert Griesemera3294712009-01-05 11:17:26 -08003838The memory is initialized as described in the section on initial values
Rob Pike8f2330d2009-02-25 16:20:44 -08003839(§The zero value).
Rob Pike96750f12009-02-27 16:47:48 -08003840</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003841
Robert Griesemerc2d55862009-02-19 16:49:10 -08003842<pre>
3843new(T)
3844</pre>
Robert Griesemer4dc25282008-09-09 10:37:19 -07003845
Rob Pike96750f12009-02-27 16:47:48 -08003846<p>
Robert Griesemer4dc25282008-09-09 10:37:19 -07003847For instance
Rob Pike96750f12009-02-27 16:47:48 -08003848</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003849
Robert Griesemerc2d55862009-02-19 16:49:10 -08003850<pre>
3851type S struct { a int; b float }
3852new(S)
3853</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003854
Rob Pike96750f12009-02-27 16:47:48 -08003855<p>
3856dynamically allocates memory for a variable of type <code>S</code>,
3857initializes it (<code>a=0</code>, <code>b=0.0</code>),
3858and returns a value of type <code>*S</code> containing the address
3859of the memory.
3860</p>
3861
3862<h3>Making slices, maps and channels</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003863
Robert Griesemerc2d55862009-02-19 16:49:10 -08003864<p>
Rob Pike96750f12009-02-27 16:47:48 -08003865Slices, maps and channels are reference types that do not require the
3866extra indirection of an allocation with <code>new</code>.
3867The built-in function <code>make</code> takes a type <code>T</code>,
3868which must be a slice, map or channel type,
3869optionally followed by a type-specific list of expressions.
3870It returns a value of type <code>T</code> (not <code>*T</code>).
Robert Griesemer633957b2009-01-06 13:23:20 -08003871The memory is initialized as described in the section on initial values
Rob Pike8f2330d2009-02-25 16:20:44 -08003872(§The zero value).
Rob Pike96750f12009-02-27 16:47:48 -08003873</p>
Robert Griesemer633957b2009-01-06 13:23:20 -08003874
Robert Griesemerc2d55862009-02-19 16:49:10 -08003875<pre>
3876make(T [, optional list of expressions])
3877</pre>
Robert Griesemer633957b2009-01-06 13:23:20 -08003878
Rob Pike96750f12009-02-27 16:47:48 -08003879<p>
Robert Griesemer633957b2009-01-06 13:23:20 -08003880For instance
Rob Pike96750f12009-02-27 16:47:48 -08003881</p>
Robert Griesemer633957b2009-01-06 13:23:20 -08003882
Robert Griesemerc2d55862009-02-19 16:49:10 -08003883<pre>
3884make(map[string] int)
3885</pre>
Robert Griesemer633957b2009-01-06 13:23:20 -08003886
Rob Pike96750f12009-02-27 16:47:48 -08003887<p>
Robert Griesemer633957b2009-01-06 13:23:20 -08003888creates a new map value and initializes it to an empty map.
Rob Pike96750f12009-02-27 16:47:48 -08003889</p>
Robert Griesemer633957b2009-01-06 13:23:20 -08003890
Rob Pike96750f12009-02-27 16:47:48 -08003891<p>
3892The parameters affect sizes for allocating slices, maps, and
Robert Griesemer633957b2009-01-06 13:23:20 -08003893buffered channels:
Rob Pike96750f12009-02-27 16:47:48 -08003894</p>
Robert Griesemer633957b2009-01-06 13:23:20 -08003895
Robert Griesemerc2d55862009-02-19 16:49:10 -08003896<pre>
3897s := make([]int, 10, 100); # slice with len(s) == 10, cap(s) == 100
3898c := make(chan int, 10); # channel with a buffer size of 10
3899m := make(map[string] int, 100); # map with initial space for 100 elements
3900</pre>
Robert Griesemer633957b2009-01-06 13:23:20 -08003901
Rob Pikeff70f092009-02-20 13:36:14 -08003902<hr/>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003903
Robert Griesemerc2d55862009-02-19 16:49:10 -08003904<h2>Packages</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003905
Rob Pike46596852009-03-02 16:17:29 -08003906<p>
3907Go programs are constructed by linking together <i>packages</i>.
3908A package is in turn constructed from one or more source files that
Rob Pike811dd252009-03-04 20:39:39 -08003909together provide access to a set of types, constants, functions,
Rob Pike46596852009-03-02 16:17:29 -08003910and variables. Those elements may be <i>imported</i> and used in
3911another package.
3912</p>
3913
3914<h3>Source file organization</h3>
3915
3916<p>
3917Each source file consists of a package clause defining the package
3918to which it belongs, followed by a possibly empty set of import
3919declarations that declare packages whose contents it wishes to use,
3920followed by a possibly empty set of declarations of functions,
3921types, variables, and constants. The source text following the
Russ Cox5958dd62009-03-04 17:19:21 -08003922package clause acts as a block for scoping (§Declarations and scope
Rob Pike46596852009-03-02 16:17:29 -08003923rules).
3924</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003925
Rob Pikeff70f092009-02-20 13:36:14 -08003926<pre class="grammar">
Rob Pike46596852009-03-02 16:17:29 -08003927SourceFile = PackageClause { ImportDecl [ ";" ] } { Declaration [ ";" ] } .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003928</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003929
Rob Pike46596852009-03-02 16:17:29 -08003930<h3>Package clause</h3>
3931
Robert Griesemerc2d55862009-02-19 16:49:10 -08003932<p>
Rob Pike46596852009-03-02 16:17:29 -08003933A package clause begins each source file and defines the package
3934to which the file belongs.
3935</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003936
Rob Pikeff70f092009-02-20 13:36:14 -08003937<pre class="grammar">
Rob Pike46596852009-03-02 16:17:29 -08003938PackageClause = "package" PackageName .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003939</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003940
Rob Pike46596852009-03-02 16:17:29 -08003941<pre>
3942package math
3943</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003944
Rob Pike46596852009-03-02 16:17:29 -08003945<p>
3946A set of files sharing the same PackageName form the implementation of a package.
3947An implementation may require that all source files for a package inhabit the same directory.
3948</p>
3949
3950<h3>Import</h3>
3951
3952<p>
3953A source file gains access to exported identifiers (§Exported
3954identifiers) from another package through an import declaration.
3955In the general form, an import declaration provides an identifier
3956that code in the source file may use to access the imported package's
3957contents and a file name referring to the (compiled) implementation of
3958the package. The file name may be relative to a repository of
3959installed packages.
3960</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003961
Rob Pikeff70f092009-02-20 13:36:14 -08003962<pre class="grammar">
Rob Pike46596852009-03-02 16:17:29 -08003963ImportDecl = "import" ( ImportSpec | "(" [ ImportSpecList ] ")" ) .
3964ImportSpecList = ImportSpec { ";" ImportSpec } [ ";" ] .
3965ImportSpec = [ "." | PackageName ] PackageFileName .
3966PackageFileName = StringLit .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003967</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003968
Robert Griesemerc2d55862009-02-19 16:49:10 -08003969<p>
Rob Pike46596852009-03-02 16:17:29 -08003970After an import, in the usual case an exported name <i>N</i> from the imported
3971package <i>P</i> may be accessed by the qualified identifier
3972<i>P</i><code>.</code><i>N</i> (§Qualified identifiers). The actual
3973name <i>P</i> depends on the form of the import declaration. If
3974an explicit package name <code>p1</code> is provided, the qualified
3975identifer will have the form <code>p1.</code><i>N</i>. If no name
3976is provided in the import declaration, <i>P</i> will be the package
3977name declared within the source files of the imported package.
3978Finally, if the import declaration uses an explicit period
3979(<code>.</code>) for the package name, <i>N</i> will appear
3980in the package-level scope of the current file and the qualified name is
3981unnecessary and erroneous. In this form, it is an error if the import introduces
3982a name conflict.
3983</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08003984<p>
Rob Pike46596852009-03-02 16:17:29 -08003985In this table, assume we have compiled a package named
3986<code>math</code>, which exports function <code>Sin</code>, and
3987installed the compiled package in file
3988<code>"lib/math"</code>.
3989</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003990
Rob Pike46596852009-03-02 16:17:29 -08003991<pre class="grammar">
3992Import syntax Local name of Sin
3993
3994import M "lib/math" M.Sin
3995import "lib/math" math.Sin
3996import . "lib/math" Sin
3997</pre>
3998
3999<h3>Multi-file packages</h3>
4000
4001<p>
4002If a package is constructed from multiple source files, all names
4003at package-level scope, not just exported names, are visible to all the
4004files in the package. An import declaration is still necessary to
4005declare intention to use the names,
4006but the imported names do not need a qualified identifer to be
4007accessed.
4008</p>
4009
4010<p>
4011The compilation of a multi-file package may require
4012that the files be compiled and installed in an order that satisfies
4013the resolution of names imported within the package.
4014</p>
4015
4016<p>
4017If source file <code>math1.go</code> contains
4018</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004019<pre>
Rob Pike46596852009-03-02 16:17:29 -08004020package math
4021
4022const twoPi = 6.283185307179586
4023
4024function Sin(x float) float { return ... }
Robert Griesemerc2d55862009-02-19 16:49:10 -08004025</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004026
Robert Griesemerc2d55862009-02-19 16:49:10 -08004027<p>
Rob Pike46596852009-03-02 16:17:29 -08004028and file <code>"math2.go"</code> begins
4029</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004030<pre>
Rob Pike46596852009-03-02 16:17:29 -08004031package math
4032
4033import "lib/math"
Robert Griesemerc2d55862009-02-19 16:49:10 -08004034</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004035
Robert Griesemerc2d55862009-02-19 16:49:10 -08004036<p>
Rob Pike46596852009-03-02 16:17:29 -08004037then, provided <code>"math1.go"</code> is compiled first and
4038installed in <code>"lib/math"</code>, <code>math2.go</code>
4039may refer directly to <code>Sin</code> and <code>twoPi</code>
4040without a qualified identifier.
4041</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004042
Rob Pike46596852009-03-02 16:17:29 -08004043<h3>An example package</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004044
Robert Griesemerc2d55862009-02-19 16:49:10 -08004045<p>
Rob Pike46596852009-03-02 16:17:29 -08004046Here is a complete Go package that implements a concurrent prime sieve.
4047</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004048
Robert Griesemerc2d55862009-02-19 16:49:10 -08004049<pre>
4050package main
4051
Rob Pike46596852009-03-02 16:17:29 -08004052import "fmt"
4053
Robert Griesemerc2d55862009-02-19 16:49:10 -08004054// Send the sequence 2, 3, 4, ... to channel 'ch'.
4055func generate(ch chan <- int) {
4056 for i := 2; ; i++ {
4057 ch <- i // Send 'i' to channel 'ch'.
4058 }
4059}
4060
4061// Copy the values from channel 'in' to channel 'out',
4062// removing those divisible by 'prime'.
Rob Pike94b67eb2009-03-24 17:40:47 -07004063func filter(src chan <- int, dst <-chan int, prime int) {
4064 for i := range src { // Loop over values received from 'src'.
Robert Griesemerc2d55862009-02-19 16:49:10 -08004065 if i % prime != 0 {
Rob Pike94b67eb2009-03-24 17:40:47 -07004066 dst <- i // Send 'i' to channel 'dst'.
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004067 }
4068 }
Robert Griesemerc2d55862009-02-19 16:49:10 -08004069}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004070
Robert Griesemerc2d55862009-02-19 16:49:10 -08004071// The prime sieve: Daisy-chain filter processes together.
4072func sieve() {
4073 ch := make(chan int); // Create a new channel.
4074 go generate(ch); // Start generate() as a subprocess.
4075 for {
4076 prime := <-ch;
Rob Pike46596852009-03-02 16:17:29 -08004077 fmt.Print(prime, "\n");
Robert Griesemerc2d55862009-02-19 16:49:10 -08004078 ch1 := make(chan int);
4079 go filter(ch, ch1, prime);
4080 ch = ch1
4081 }
4082}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004083
Robert Griesemerc2d55862009-02-19 16:49:10 -08004084func main() {
4085 sieve()
4086}
4087</pre>
Robert Griesemer67153582008-12-16 14:45:09 -08004088
Rob Pikeff70f092009-02-20 13:36:14 -08004089<hr/>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004090
4091<h2>Program initialization and execution</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004092
Rob Pike8f2330d2009-02-25 16:20:44 -08004093<h3>The zero value</h3>
4094<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004095When memory is allocated to store a value, either through a declaration
Rob Pike8f2330d2009-02-25 16:20:44 -08004096or <code>new()</code>, and no explicit initialization is provided, the memory is
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004097given a default initialization. Each element of such a value is
Rob Pike8f2330d2009-02-25 16:20:44 -08004098set to the zero value for its type: <code>false</code> for booleans,
4099<code>0</code> for integers, <code>0.0</code> for floats, <code>""</code>
4100for strings, and <code>nil</code> for pointers and interfaces.
Rob Pikeff70f092009-02-20 13:36:14 -08004101This initialization is done recursively, so for instance each element of an
Rob Pike8f2330d2009-02-25 16:20:44 -08004102array of structs will have its fields zeroed if no value is specified.
4103</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004104<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004105These two simple declarations are equivalent:
Rob Pike8f2330d2009-02-25 16:20:44 -08004106</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004107
Robert Griesemerc2d55862009-02-19 16:49:10 -08004108<pre>
4109var i int;
4110var i int = 0;
4111</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004112
Rob Pike8f2330d2009-02-25 16:20:44 -08004113<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004114After
Rob Pike8f2330d2009-02-25 16:20:44 -08004115</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004116
Robert Griesemerc2d55862009-02-19 16:49:10 -08004117<pre>
4118type T struct { i int; f float; next *T };
4119t := new(T);
4120</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004121
Rob Pike8f2330d2009-02-25 16:20:44 -08004122<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004123the following holds:
Rob Pike8f2330d2009-02-25 16:20:44 -08004124</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004125
Robert Griesemerc2d55862009-02-19 16:49:10 -08004126<pre>
4127t.i == 0
4128t.f == 0.0
4129t.next == nil
4130</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004131
Rob Pike96750f12009-02-27 16:47:48 -08004132<p>
4133The same would also be true after
4134</p>
4135
4136<pre>
4137var t T
4138</pre>
4139
Rob Pike8f2330d2009-02-25 16:20:44 -08004140<h3>Program execution</h3>
4141<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004142A package with no imports is initialized by assigning initial values to
Rob Pike96750f12009-02-27 16:47:48 -08004143all its package-level variables in declaration order and then calling any
4144package-level function with the name and signature of
4145</p>
4146<pre>
4147func init()
4148</pre>
4149<p>
4150defined in its source. Since a package may contain more
4151than one source file, there may be more than one
4152<code>init()</code> function in a package, but
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004153only one per source file.
Rob Pike8f2330d2009-02-25 16:20:44 -08004154</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004155<p>
Robert Griesemer566e3b22008-09-26 16:41:50 -07004156Initialization code may contain "go" statements, but the functions
Robert Griesemerd8a764c2009-02-06 17:01:10 -08004157they invoke do not begin execution until initialization of the entire
4158program is complete. Therefore, all initialization code is run in a single
Rob Pike96750f12009-02-27 16:47:48 -08004159goroutine.
Rob Pike8f2330d2009-02-25 16:20:44 -08004160</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004161<p>
Rob Pike96750f12009-02-27 16:47:48 -08004162An <code>init()</code> function cannot be referred to from anywhere
4163in a program. In particular, <code>init()</code> cannot be called explicitly,
4164nor can a pointer to <code>init</code> be assigned to a function variable.
Rob Pike8f2330d2009-02-25 16:20:44 -08004165</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004166<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004167If a package has imports, the imported packages are initialized
Robert Griesemer566e3b22008-09-26 16:41:50 -07004168before initializing the package itself. If multiple packages import
Rob Pike96750f12009-02-27 16:47:48 -08004169a package <code>P</code>, <code>P</code> will be initialized only once.
Rob Pike8f2330d2009-02-25 16:20:44 -08004170</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004171<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004172The importing of packages, by construction, guarantees that there can
4173be no cyclic dependencies in initialization.
Rob Pike8f2330d2009-02-25 16:20:44 -08004174</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004175<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004176A complete program, possibly created by linking multiple packages,
Rob Pike96750f12009-02-27 16:47:48 -08004177must have one package called <code>main</code>, with a function
Rob Pike8f2330d2009-02-25 16:20:44 -08004178</p>
Robert Griesemer4dc25282008-09-09 10:37:19 -07004179
Robert Griesemerc2d55862009-02-19 16:49:10 -08004180<pre>
Rob Pike96750f12009-02-27 16:47:48 -08004181func main() { ... }
Robert Griesemerc2d55862009-02-19 16:49:10 -08004182</pre>
Robert Griesemer4dc25282008-09-09 10:37:19 -07004183
Rob Pike8f2330d2009-02-25 16:20:44 -08004184<p>
Rob Pike96750f12009-02-27 16:47:48 -08004185defined.
4186The function <code>main.main()</code> takes no arguments and returns no value.
Rob Pike8f2330d2009-02-25 16:20:44 -08004187</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004188<p>
Rob Pike96750f12009-02-27 16:47:48 -08004189Program execution begins by initializing the <code>main</code> package and then
Rob Pike8f2330d2009-02-25 16:20:44 -08004190invoking <code>main.main()</code>.
4191</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004192<p>
Rob Pike46596852009-03-02 16:17:29 -08004193When <code>main.main()</code> returns, the program exits.
Rob Pike8f2330d2009-02-25 16:20:44 -08004194</p>
Rob Pike811dd252009-03-04 20:39:39 -08004195<p>
4196Implementation restriction: The compiler assumes package <code>main</code>
4197is created by a single source file and that it is not imported by any other package.
4198</p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004199
Rob Pikeff70f092009-02-20 13:36:14 -08004200<hr/>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004201
Rob Pikef27e9f02009-02-23 19:22:05 -08004202<h2>System considerations</h2>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004203
Rob Pikef27e9f02009-02-23 19:22:05 -08004204<h3>Package <code>unsafe</code></h3>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004205
Robert Griesemerc2d55862009-02-19 16:49:10 -08004206<p>
Rob Pike96750f12009-02-27 16:47:48 -08004207The built-in package <code>unsafe</code>, known to the compiler,
4208provides facilities for low-level programming including operations
4209that violate the type system. A package using <code>unsafe</code>
4210must be vetted manually for type safety. The package provides the
4211following interface:
Rob Pikef27e9f02009-02-23 19:22:05 -08004212</p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004213
Rob Pikeff70f092009-02-20 13:36:14 -08004214<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08004215package unsafe
Robert Griesemer52c02c22009-02-11 13:46:30 -08004216
Robert Griesemerc2d55862009-02-19 16:49:10 -08004217const Maxalign int
Robert Griesemer52c02c22009-02-11 13:46:30 -08004218
Rob Pikef27e9f02009-02-23 19:22:05 -08004219type Pointer *any // "any" is shorthand for any Go type; it is not a real type.
Robert Griesemer52c02c22009-02-11 13:46:30 -08004220
Robert Griesemerc2d55862009-02-19 16:49:10 -08004221func Alignof(variable any) int
4222func Offsetof(selector any) int
4223func Sizeof(variable any) int
4224</pre>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004225
Robert Griesemerc2d55862009-02-19 16:49:10 -08004226<p>
Rob Pikef27e9f02009-02-23 19:22:05 -08004227Any pointer or value of type <code>uintptr</code> can be converted into
4228a <code>Pointer</code> and vice versa.
4229</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004230<p>
Rob Pikef27e9f02009-02-23 19:22:05 -08004231The function <code>Sizeof</code> takes an expression denoting a
Rob Pikecdbf6192009-02-24 17:47:45 -08004232variable of any (complete) type and returns the size of the variable in bytes.
Rob Pikef27e9f02009-02-23 19:22:05 -08004233</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004234<p>
Rob Pikef27e9f02009-02-23 19:22:05 -08004235The function <code>Offsetof</code> takes a selector (§Selectors) denoting a struct
Robert Griesemer52c02c22009-02-11 13:46:30 -08004236field of any type and returns the field offset in bytes relative to the
Rob Pikef27e9f02009-02-23 19:22:05 -08004237struct's address. For a struct <code>s</code> with field <code>f</code>:
4238</p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004239
Robert Griesemerc2d55862009-02-19 16:49:10 -08004240<pre>
Rob Pikef27e9f02009-02-23 19:22:05 -08004241uintptr(unsafe.Pointer(&amp;s)) + uintptr(unsafe.Offsetof(s.f)) == uintptr(unsafe.Pointer(&amp;s.f))
Robert Griesemerc2d55862009-02-19 16:49:10 -08004242</pre>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004243
Robert Griesemerc2d55862009-02-19 16:49:10 -08004244<p>
Rob Pikef27e9f02009-02-23 19:22:05 -08004245Computer architectures may require memory addresses to be <i>aligned</i>;
4246that is, for addresses of a variable to be a multiple of a factor,
4247the variable's type's <i>alignment</i>. The function <code>Alignof</code>
4248takes an expression denoting a variable of any type and returns the
4249alignment of the (type of the) variable in bytes. For a variable
4250<code>x</code>:
4251</p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004252
Robert Griesemerc2d55862009-02-19 16:49:10 -08004253<pre>
4254uintptr(unsafe.Pointer(&amp;x)) % uintptr(unsafe.Alignof(x)) == 0
4255</pre>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004256
Robert Griesemerc2d55862009-02-19 16:49:10 -08004257<p>
Rob Pikef27e9f02009-02-23 19:22:05 -08004258The maximum alignment is given by the constant <code>Maxalign</code>.
4259It usually corresponds to the value of <code>Sizeof(x)</code> for
Russ Cox5958dd62009-03-04 17:19:21 -08004260a variable <code>x</code> of the largest numeric type (8 for a
Rob Pikef27e9f02009-02-23 19:22:05 -08004261<code>float64</code>), but may
4262be smaller on systems with weaker alignment restrictions.
4263</p>
4264<p>
4265Calls to <code>Alignof</code>, <code>Offsetof</code>, and
4266<code>Sizeof</code> are constant expressions of type <code>int</code>.
4267</p>
Robert Griesemer6f8df7a2009-02-11 21:57:15 -08004268
Robert Griesemer52c02c22009-02-11 13:46:30 -08004269
Robert Griesemerc2d55862009-02-19 16:49:10 -08004270<h3>Size and alignment guarantees</h3>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004271
Russ Cox5958dd62009-03-04 17:19:21 -08004272For the numeric types (§Numeric types), the following sizes are guaranteed:
Robert Griesemer52c02c22009-02-11 13:46:30 -08004273
Rob Pikeff70f092009-02-20 13:36:14 -08004274<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08004275type size in bytes
Robert Griesemer52c02c22009-02-11 13:46:30 -08004276
Robert Griesemerc2d55862009-02-19 16:49:10 -08004277byte, uint8, int8 1
4278uint16, int16 2
4279uint32, int32, float32 4
4280uint64, int64, float64 8
4281</pre>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004282
Robert Griesemerc2d55862009-02-19 16:49:10 -08004283<p>
Rob Pikef27e9f02009-02-23 19:22:05 -08004284The following minimal alignment properties are guaranteed:
Rob Pike4501d342009-02-19 17:31:36 -08004285</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004286<ol>
Rob Pikef27e9f02009-02-23 19:22:05 -08004287<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 -08004288
Russ Cox5958dd62009-03-04 17:19:21 -08004289<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 -08004290 of <code>unsafe.Sizeof(x)</code> and <code>unsafe.Maxalign</code>, but at least 1.
Robert Griesemer52c02c22009-02-11 13:46:30 -08004291
Rob Pikef27e9f02009-02-23 19:22:05 -08004292<li>For a variable <code>x</code> of struct type: <code>unsafe.Alignof(x)</code> is the largest of
4293 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 -08004294
Rob Pikef27e9f02009-02-23 19:22:05 -08004295<li>For a variable <code>x</code> of array type: <code>unsafe.Alignof(x)</code> is the same as
4296 <code>unsafe.Alignof(x[0])</code>, but at least 1.
Robert Griesemerc2d55862009-02-19 16:49:10 -08004297</ol>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004298
Rob Pikeff70f092009-02-20 13:36:14 -08004299<hr/>
4300
4301<h2><font color=red>Differences between this doc and implementation - TODO</font></h2>
4302<p>
4303<font color=red>
Rob Pikedf3183f2009-02-26 16:37:23 -08004304Implementation accepts only ASCII digits for digits; doc says Unicode.
4305<br/>
Rob Pikedf3183f2009-02-26 16:37:23 -08004306Implementation does not honor the restriction on goto statements and targets (no intervening declarations).
4307<br/>
4308cap() does not work on maps or chans.
4309<br/>
4310len() does not work on chans.
Robert Griesemer1a304e12009-05-08 10:25:06 -07004311<br>
4312string([]int{...}) conversion is not yet implemented.
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>