blob: c1ab22b7aed2899ff39c03885198f3ef0f4396f7 [file] [log] [blame]
Robert Griesemer53440da2009-10-01 14:08:00 -07001<!-- The Go Programming Language Specification -->
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002
Robert Griesemerc59d2f12008-09-09 10:48:14 -07003<!--
Robert Griesemer19b1d352009-09-24 19:36:48 -07004Todo
5[ ] clarify: two equal lowercase identifiers from different packages denote different objects
Robert Griesemer4b908332009-08-07 17:05:41 -07006[ ] need language about function/method calls and parameter passing rules
Robert Griesemer0a162a12009-08-19 16:44:04 -07007[ ] need to say something about "scope" of selectors?
Robert Griesemer4b908332009-08-07 17:05:41 -07008[ ] clarify what a field name is in struct declarations
9 (struct{T} vs struct {T T} vs struct {t T})
Robert Griesemer7539c852009-07-31 18:05:07 -070010[ ] need explicit language about the result type of operations
11[ ] may want to have some examples for the types of shift operations
Russ Cox60ff8cc2009-10-20 08:27:14 -070012[ ] should string(1<<s) and float(1<<s) be valid?
Robert Griesemer40d6bb52009-04-20 15:32:20 -070013[ ] should probably write something about evaluation order of statements even
14 though obvious
Robert Griesemere1b8cb82009-07-16 20:31:41 -070015[ ] specify iteration direction for range clause
16[ ] review language on implicit dereferencing
Robert Griesemerc59d2f12008-09-09 10:48:14 -070017-->
18
Robert Griesemer40d6bb52009-04-20 15:32:20 -070019
Russ Cox7c4f7cc2009-08-20 11:11:03 -070020<h2 id="Introduction">Introduction</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -070021
Robert Griesemerc2d55862009-02-19 16:49:10 -080022<p>
Rob Pike4501d342009-02-19 17:31:36 -080023This is a reference manual for the Go programming language. For
Robert Griesemer19b1d352009-09-24 19:36:48 -070024more information and other documents, see <a href="http://go/go">go/go</a>.
Rob Pike4501d342009-02-19 17:31:36 -080025</p>
Robert Griesemer67153582008-12-16 14:45:09 -080026
Robert Griesemerc2d55862009-02-19 16:49:10 -080027<p>
Rob Pike4501d342009-02-19 17:31:36 -080028Go is a general-purpose language designed with systems programming
Rob Pike678625d2009-09-15 09:54:22 -070029in mind. It is strongly typed and garbage-collected and has explicit
Rob Pike4501d342009-02-19 17:31:36 -080030support for concurrent programming. Programs are constructed from
31<i>packages</i>, whose properties allow efficient management of
32dependencies. The existing implementations use a traditional
33compile/link model to generate executable binaries.
34</p>
35
Robert Griesemerc2d55862009-02-19 16:49:10 -080036<p>
Rob Pike4501d342009-02-19 17:31:36 -080037The grammar is compact and regular, allowing for easy analysis by
38automatic tools such as integrated development environments.
39</p>
Larry Hosken698c6c02009-09-17 08:05:12 -070040
Russ Cox7c4f7cc2009-08-20 11:11:03 -070041<h2 id="Notation">Notation</h2>
Rob Pike4501d342009-02-19 17:31:36 -080042<p>
Robert Griesemer4d230302008-12-17 15:39:15 -080043The syntax is specified using Extended Backus-Naur Form (EBNF):
Rob Pike4501d342009-02-19 17:31:36 -080044</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -070045
Rob Pikeff70f092009-02-20 13:36:14 -080046<pre class="grammar">
Robert Griesemer88a0c402009-04-23 14:42:21 -070047Production = production_name "=" Expression "." .
Rob Pike4501d342009-02-19 17:31:36 -080048Expression = Alternative { "|" Alternative } .
Robert Griesemerc2d55862009-02-19 16:49:10 -080049Alternative = Term { Term } .
Rob Pike4501d342009-02-19 17:31:36 -080050Term = production_name | token [ "..." token ] | Group | Option | Repetition .
51Group = "(" Expression ")" .
Rob Pikec956e902009-04-14 20:10:49 -070052Option = "[" Expression "]" .
Rob Pike4501d342009-02-19 17:31:36 -080053Repetition = "{" Expression "}" .
Robert Griesemerc2d55862009-02-19 16:49:10 -080054</pre>
Robert Griesemerbbfe3122008-10-09 17:12:09 -070055
Rob Pike4501d342009-02-19 17:31:36 -080056<p>
57Productions are expressions constructed from terms and the following
58operators, in increasing precedence:
59</p>
Rob Pikeff70f092009-02-20 13:36:14 -080060<pre class="grammar">
Rob Pike4501d342009-02-19 17:31:36 -080061| alternation
62() grouping
63[] option (0 or 1 times)
64{} repetition (0 to n times)
Robert Griesemerc2d55862009-02-19 16:49:10 -080065</pre>
Robert Griesemer4d230302008-12-17 15:39:15 -080066
Robert Griesemerc2d55862009-02-19 16:49:10 -080067<p>
Rob Pike4501d342009-02-19 17:31:36 -080068Lower-case production names are used to identify lexical tokens.
69Non-terminals are in CamelCase. Lexical symbols are enclosed in
Rob Pike678625d2009-09-15 09:54:22 -070070double quotes <code>""</code> or back quotes <code>``</code>.
Rob Pike4501d342009-02-19 17:31:36 -080071</p>
72
Robert Griesemerc2d55862009-02-19 16:49:10 -080073<p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -070074The form <code>a ... b</code> represents the set of characters from
Rob Pikef27e9f02009-02-23 19:22:05 -080075<code>a</code> through <code>b</code> as alternatives.
Rob Pike4501d342009-02-19 17:31:36 -080076</p>
77
Russ Cox7c4f7cc2009-08-20 11:11:03 -070078<h2 id="Source_code_representation">Source code representation</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -070079
Robert Griesemerc2d55862009-02-19 16:49:10 -080080<p>
Rob Pikeff70f092009-02-20 13:36:14 -080081Source code is Unicode text encoded in UTF-8. The text is not
82canonicalized, so a single accented code point is distinct from the
83same character constructed from combining an accent and a letter;
84those are treated as two code points. For simplicity, this document
85will use the term <i>character</i> to refer to a Unicode code point.
86</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -080087<p>
Rob Pikeff70f092009-02-20 13:36:14 -080088Each code point is distinct; for instance, upper and lower case letters
89are different characters.
90</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -070091
Russ Cox7c4f7cc2009-08-20 11:11:03 -070092<h3 id="Characters">Characters</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -070093
Robert Griesemerc2d55862009-02-19 16:49:10 -080094<p>
Rob Pike4501d342009-02-19 17:31:36 -080095The following terms are used to denote specific Unicode character classes:
96</p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -070097<pre class="ebnf">
98unicode_char = /* an arbitrary Unicode code point */ .
99unicode_letter = /* a Unicode code point classified as "Letter" */ .
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700100unicode_digit = /* a Unicode code point classified as "Digit" */ .
101</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700102
Rob Pike678625d2009-09-15 09:54:22 -0700103<p>
Robert Griesemerd36d1912009-09-18 11:58:35 -0700104In <a href="http://www.unicode.org/versions/Unicode5.1.0/">The Unicode Standard 5.1</a>,
Rob Pike678625d2009-09-15 09:54:22 -0700105Section 4.5 General Category-Normative
106defines a set of character categories. Go treats
107those characters in category Lu, Ll, Lt, Lm, or Lo as Unicode letters,
108and those in category Nd as Unicode digits.
109</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700110
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700111<h3 id="Letters_and_digits">Letters and digits</h3>
Rob Pikeff70f092009-02-20 13:36:14 -0800112
113<p>
Rob Pikef27e9f02009-02-23 19:22:05 -0800114The underscore character <code>_</code> (U+005F) is considered a letter.
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700115</p>
116<pre class="ebnf">
Robert Griesemerc2d55862009-02-19 16:49:10 -0800117letter = unicode_letter | "_" .
118decimal_digit = "0" ... "9" .
119octal_digit = "0" ... "7" .
120hex_digit = "0" ... "9" | "A" ... "F" | "a" ... "f" .
121</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700122
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700123<h2 id="Lexical_elements">Lexical elements</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700124
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700125<h3 id="Comments">Comments</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700126
Rob Pikeff70f092009-02-20 13:36:14 -0800127<p>
128There are two forms of comments. The first starts at the character
Rob Pikef27e9f02009-02-23 19:22:05 -0800129sequence <code>//</code> and continues through the next newline. The
130second starts at the character sequence <code>/*</code> and continues
131through the character sequence <code>*/</code>. Comments do not nest.
Rob Pikeff70f092009-02-20 13:36:14 -0800132</p>
133
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700134<h3 id="Tokens">Tokens</h3>
Rob Pikeff70f092009-02-20 13:36:14 -0800135
136<p>
137Tokens form the vocabulary of the Go language.
138There are four classes: identifiers, keywords, operators
139and delimiters, and literals. <i>White space</i>, formed from
140blanks, tabs, and newlines, is ignored except as it separates tokens
141that would otherwise combine into a single token. Comments
142behave as white space. While breaking the input into tokens,
143the next token is the longest sequence of characters that form a
144valid token.
145</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700146
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700147<h3 id="Identifiers">Identifiers</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700148
Rob Pikeff70f092009-02-20 13:36:14 -0800149<p>
150Identifiers name program entities such as variables and types.
151An identifier is a sequence of one or more letters and digits.
152The first character in an identifier must be a letter.
153</p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700154<pre class="ebnf">
Robert Griesemerd36d1912009-09-18 11:58:35 -0700155identifier = letter { letter | unicode_digit } .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800156</pre>
Robert Griesemerc2d55862009-02-19 16:49:10 -0800157<pre>
158a
159_x9
160ThisVariableIsExported
161αβ
162</pre>
Robert Griesemer4e56b332009-09-10 10:14:00 -0700163Some identifiers are <a href="#Predeclared_identifiers">predeclared</a>.
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -0700164
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700165<h3 id="Keywords">Keywords</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700166
Rob Pikeff70f092009-02-20 13:36:14 -0800167<p>
168The following keywords are reserved and may not be used as identifiers.
169</p>
170<pre class="grammar">
171break default func interface select
172case defer go map struct
173chan else goto package switch
174const fallthrough if range type
175continue for import return var
176</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700177
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700178<h3 id="Operators_and_Delimiters">Operators and Delimiters</h3>
Rob Pikeff70f092009-02-20 13:36:14 -0800179
180<p>
Robert Griesemerd36d1912009-09-18 11:58:35 -0700181The following character sequences represent <a href="#Operators">operators</a>, delimiters, and other special tokens:
Rob Pikeff70f092009-02-20 13:36:14 -0800182</p>
183<pre class="grammar">
184+ &amp; += &amp;= &amp;&amp; == != ( )
185- | -= |= || &lt; &lt;= [ ]
186* ^ *= ^= &lt;- &gt; &gt;= { }
Robert Griesemer4ed666e2009-08-27 16:45:42 -0700187/ &lt;&lt; /= &lt;&lt;= ++ = := , ;
188% &gt;&gt; %= &gt;&gt;= -- ! ... . :
Rob Pikecd04ec92009-03-11 21:59:05 -0700189 &amp;^ &amp;^=
Rob Pikeff70f092009-02-20 13:36:14 -0800190</pre>
191
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700192<h3 id="Integer_literals">Integer literals</h3>
Rob Pikeff70f092009-02-20 13:36:14 -0800193
194<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700195An integer literal is a sequence of digits representing an
196<a href="#Constants">integer constant</a>.
197An optional prefix sets a non-decimal base: <code>0</code> for octal, <code>0x</code> or
Rob Pikef27e9f02009-02-23 19:22:05 -0800198<code>0X</code> for hexadecimal. In hexadecimal literals, letters
199<code>a-f</code> and <code>A-F</code> represent values 10 through 15.
Rob Pikeff70f092009-02-20 13:36:14 -0800200</p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700201<pre class="ebnf">
Robert Griesemerd36d1912009-09-18 11:58:35 -0700202int_lit = decimal_lit | octal_lit | hex_lit .
203decimal_lit = ( "1" ... "9" ) { decimal_digit } .
204octal_lit = "0" { octal_digit } .
205hex_lit = "0" ( "x" | "X" ) hex_digit { hex_digit } .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800206</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700207
Robert Griesemerc2d55862009-02-19 16:49:10 -0800208<pre>
20942
2100600
2110xBadFace
212170141183460469231731687303715884105727
213</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700214
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700215<h3 id="Floating-point_literals">Floating-point literals</h3>
Rob Pikeff70f092009-02-20 13:36:14 -0800216<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700217A floating-point literal is a decimal representation of a
218<a href="#Constants">floating-point constant</a>.
219It has an integer part, a decimal point, a fractional part,
Rob Pikeff70f092009-02-20 13:36:14 -0800220and an exponent part. The integer and fractional part comprise
Rob Pikef27e9f02009-02-23 19:22:05 -0800221decimal digits; the exponent part is an <code>e</code> or <code>E</code>
Rob Pikeff70f092009-02-20 13:36:14 -0800222followed by an optionally signed decimal exponent. One of the
223integer part or the fractional part may be elided; one of the decimal
224point or the exponent may be elided.
225</p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700226<pre class="ebnf">
Robert Griesemerd36d1912009-09-18 11:58:35 -0700227float_lit = decimals "." [ decimals ] [ exponent ] |
228 decimals exponent |
229 "." decimals [ exponent ] .
230decimals = decimal_digit { decimal_digit } .
231exponent = ( "e" | "E" ) [ "+" | "-" ] decimals .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800232</pre>
Robert Griesemerad711102008-09-11 17:48:20 -0700233
Robert Griesemerc2d55862009-02-19 16:49:10 -0800234<pre>
2350.
2362.71828
2371.e+0
2386.67428e-11
2391E6
240.25
241.12345E+5
242</pre>
Robert Griesemerad711102008-09-11 17:48:20 -0700243
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700244
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700245<h3 id="Character_literals">Character literals</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700246
Rob Pike4501d342009-02-19 17:31:36 -0800247<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700248A character literal represents an <a href="#Constants">integer constant</a>,
249typically a Unicode code point, as one or more characters enclosed in single
Rob Pikeff70f092009-02-20 13:36:14 -0800250quotes. Within the quotes, any character may appear except single
251quote and newline. A single quoted character represents itself,
252while multi-character sequences beginning with a backslash encode
253values in various formats.
Rob Pike4501d342009-02-19 17:31:36 -0800254</p>
Rob Pikeff70f092009-02-20 13:36:14 -0800255<p>
256The simplest form represents the single character within the quotes;
257since Go source text is Unicode characters encoded in UTF-8, multiple
258UTF-8-encoded bytes may represent a single integer value. For
Rob Pikef27e9f02009-02-23 19:22:05 -0800259instance, the literal <code>'a'</code> holds a single byte representing
260a literal <code>a</code>, Unicode U+0061, value <code>0x61</code>, while
261<code>'ä'</code> holds two bytes (<code>0xc3</code> <code>0xa4</code>) representing
262a literal <code>a</code>-dieresis, U+00E4, value <code>0xe4</code>.
Rob Pikeff70f092009-02-20 13:36:14 -0800263</p>
264<p>
265Several backslash escapes allow arbitrary values to be represented
266as ASCII text. There are four ways to represent the integer value
Rob Pikef27e9f02009-02-23 19:22:05 -0800267as a numeric constant: <code>\x</code> followed by exactly two hexadecimal
268digits; <code>\u</code> followed by exactly four hexadecimal digits;
269<code>\U</code> followed by exactly eight hexadecimal digits, and a
270plain backslash <code>\</code> followed by exactly three octal digits.
Rob Pikeff70f092009-02-20 13:36:14 -0800271In each case the value of the literal is the value represented by
272the digits in the corresponding base.
273</p>
274<p>
275Although these representations all result in an integer, they have
276different valid ranges. Octal escapes must represent a value between
Rob Pike678625d2009-09-15 09:54:22 -07002770 and 255 inclusive. Hexadecimal escapes satisfy this condition
278by construction. The escapes <code>\u</code> and <code>\U</code>
Rob Pikeff70f092009-02-20 13:36:14 -0800279represent Unicode code points so within them some values are illegal,
Rob Pikef27e9f02009-02-23 19:22:05 -0800280in particular those above <code>0x10FFFF</code> and surrogate halves.
Rob Pikeff70f092009-02-20 13:36:14 -0800281</p>
282<p>
283After a backslash, certain single-character escapes represent special values:
284</p>
285<pre class="grammar">
286\a U+0007 alert or bell
287\b U+0008 backspace
288\f U+000C form feed
289\n U+000A line feed or newline
290\r U+000D carriage return
291\t U+0009 horizontal tab
292\v U+000b vertical tab
293\\ U+005c backslash
294\' U+0027 single quote (valid escape only within character literals)
295\" U+0022 double quote (valid escape only within string literals)
Robert Griesemerc2d55862009-02-19 16:49:10 -0800296</pre>
Robert Griesemerc2d55862009-02-19 16:49:10 -0800297<p>
Rob Pikeff70f092009-02-20 13:36:14 -0800298All other sequences are illegal inside character literals.
Rob Pike4501d342009-02-19 17:31:36 -0800299</p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700300<pre class="ebnf">
Rob Pikeff70f092009-02-20 13:36:14 -0800301char_lit = "'" ( unicode_value | byte_value ) "'" .
302unicode_value = unicode_char | little_u_value | big_u_value | escaped_char .
303byte_value = octal_byte_value | hex_byte_value .
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700304octal_byte_value = `\` octal_digit octal_digit octal_digit .
305hex_byte_value = `\` "x" hex_digit hex_digit .
306little_u_value = `\` "u" hex_digit hex_digit hex_digit hex_digit .
307big_u_value = `\` "U" hex_digit hex_digit hex_digit hex_digit
Rob Pikeff70f092009-02-20 13:36:14 -0800308 hex_digit hex_digit hex_digit hex_digit .
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700309escaped_char = `\` ( "a" | "b" | "f" | "n" | "r" | "t" | "v" | `\` | "'" | `"` ) .
Rob Pikeff70f092009-02-20 13:36:14 -0800310</pre>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700311
Robert Griesemerc2d55862009-02-19 16:49:10 -0800312<pre>
313'a'
314'ä'
315'本'
316'\t'
317'\000'
318'\007'
319'\377'
320'\x07'
321'\xff'
322'\u12e4'
323'\U00101234'
324</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700325
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700326
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700327<h3 id="String_literals">String literals</h3>
Rob Pikeff70f092009-02-20 13:36:14 -0800328
329<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700330A string literal represents a <a href="#Constants">string constant</a>
331obtained from concatenating a sequence of characters. There are two forms:
332raw string literals and interpreted string literals.
Rob Pikeff70f092009-02-20 13:36:14 -0800333</p>
334<p>
335Raw string literals are character sequences between back quotes
Rob Pikef27e9f02009-02-23 19:22:05 -0800336<code>``</code>. Within the quotes, any character is legal except
Robert Griesemerdb7a6222009-06-18 13:51:14 -0700337back quote. The value of a raw string literal is the
338string composed of the uninterpreted characters between the quotes;
339in particular, backslashes have no special meaning and the string may
340span multiple lines.
Rob Pikeff70f092009-02-20 13:36:14 -0800341</p>
342<p>
343Interpreted string literals are character sequences between double
Rob Pikef27e9f02009-02-23 19:22:05 -0800344quotes <code>&quot;&quot;</code>. The text between the quotes forms the
Rob Pikeff70f092009-02-20 13:36:14 -0800345value of the literal, with backslash escapes interpreted as they
Rob Pikef27e9f02009-02-23 19:22:05 -0800346are in character literals (except that <code>\'</code> is illegal and
347<code>\"</code> is legal). The three-digit octal (<code>\000</code>)
348and two-digit hexadecimal (<code>\x00</code>) escapes represent individual
Rob Pikeff70f092009-02-20 13:36:14 -0800349<i>bytes</i> of the resulting string; all other escapes represent
350the (possibly multi-byte) UTF-8 encoding of individual <i>characters</i>.
Rob Pikef27e9f02009-02-23 19:22:05 -0800351Thus inside a string literal <code>\377</code> and <code>\xFF</code> represent
352a single byte of value <code>0xFF</code>=255, while <code>ÿ</code>,
353<code>\u00FF</code>, <code>\U000000FF</code> and <code>\xc3\xbf</code> represent
Robert Griesemere1e76192009-09-25 14:11:03 -0700354the two bytes <code>0xc3</code> <code>0xbf</code> of the UTF-8 encoding of character
Rob Pikeff70f092009-02-20 13:36:14 -0800355U+00FF.
356</p>
357
Robert Griesemercfe92112009-06-18 13:29:40 -0700358<p>
359A sequence of string literals is concatenated to form a single string.
360</p>
361
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700362<pre class="ebnf">
Robert Griesemercfe92112009-06-18 13:29:40 -0700363StringLit = string_lit { string_lit } .
Rob Pikeff70f092009-02-20 13:36:14 -0800364string_lit = raw_string_lit | interpreted_string_lit .
365raw_string_lit = "`" { unicode_char } "`" .
Robert Griesemerd4d4ff02009-10-19 13:13:59 -0700366interpreted_string_lit = `"` { unicode_value | byte_value } `"` .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800367</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700368
Robert Griesemerc2d55862009-02-19 16:49:10 -0800369<pre>
Robert Griesemerdb7a6222009-06-18 13:51:14 -0700370`abc` // same as "abc"
371`\n
372\n` // same as "\\n\n\\n"
Robert Griesemerc2d55862009-02-19 16:49:10 -0800373"\n"
374""
375"Hello, world!\n"
376"日本語"
377"\u65e5本\U00008a9e"
378"\xff\u00FF"
Robert Griesemercfe92112009-06-18 13:29:40 -0700379"Alea iacta est."
380"Alea " /* The die */ `iacta est` /* is cast */ "." // same as "Alea iacta est."
Robert Griesemerc2d55862009-02-19 16:49:10 -0800381</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700382
Rob Pikeff70f092009-02-20 13:36:14 -0800383<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700384These examples all represent the same string:
Rob Pikeff70f092009-02-20 13:36:14 -0800385</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700386
Robert Griesemerc2d55862009-02-19 16:49:10 -0800387<pre>
Rob Pikeff70f092009-02-20 13:36:14 -0800388"日本語" // UTF-8 input text
389`日本語` // UTF-8 input text as a raw literal
390"\u65e5\u672c\u8a9e" // The explicit Unicode code points
391"\U000065e5\U0000672c\U00008a9e" // The explicit Unicode code points
Robert Griesemerc2d55862009-02-19 16:49:10 -0800392"\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e" // The explicit UTF-8 bytes
393</pre>
Robert Griesemercd499272008-09-29 12:09:00 -0700394
Robert Griesemerc2d55862009-02-19 16:49:10 -0800395<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700396If the source code represents a character as two code points, such as
397a combining form involving an accent and a letter, the result will be
398an error if placed in a character literal (it is not a single code
399point), and will appear as two code points if placed in a string
400literal.
Rob Pikeff70f092009-02-20 13:36:14 -0800401</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700402
Robert Griesemer19b1d352009-09-24 19:36:48 -0700403
404<h2 id="Constants">Constants</h2>
405
406<p>There are <i>boolean constants</i>, <i>integer constants</i>, <i>floating-point constants</i>,
407and <i>string constants</i>. Integer and floating-point constants are
408collectively called <i>numeric constants</i>.
409</p>
Rob Pike678625d2009-09-15 09:54:22 -0700410
411<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700412A constant value is represented by an
413<a href="#Integer_literals">integer</a>,
414<a href="#Floating-point_literals">floating-point</a>,
415<a href="#Character_literals">character</a>, or
416<a href="#String_literals">string</a> literal,
417an identifier denoting a constant,
418a <a href="#Constant_expressions">constant expression</a>, or
419the result value of some built-in functions such as <code>unsafe.Sizeof</code>
420and <code>cap</code> or <code>len</code> applied to an array,
421or <code>len</code> applied to a string constant.
422The boolean truth values are represented by the predeclared constants
423<code>true</code> and <code>false</code>. The predeclared identifier
424<a href="#Iota">iota</a> denotes an integer constant.
Rob Pike678625d2009-09-15 09:54:22 -0700425</p>
426
Robert Griesemer19b1d352009-09-24 19:36:48 -0700427<p>
428Numeric constants represent values of arbitrary precision that
429have no size and cannot overflow.
430</p>
431
432<p>
433Constants may be <a href="#Types">typed</a> or untyped.
434Literal constants, <code>true</code>, <code>false</code>, <code>iota</code>,
435and certain <a href="#Constant_expressions">constant expressions</a>
436containing only untyped constant operands are untyped.
437</p>
438
439<p>
440A constant may be given a type explicitly by a <a href="#Constant_declarations">constant declaration</a>
441or <a href="#Conversions">conversion</a>, or implicitly when used in a
442<a href="#Variable_declarations">variable declaration</a> or an
443<a href="#Assignments">assignment</a> or as an
444operand in an <a href="#Expressions">expression</a>.
445It is an error if the constant value
446cannot be accurately represented as a value of the respective type.
447For instance, <code>3.0</code> can be given any integer type but also any
448floating-point type, while <code>-1e12</code> can be given the types
449<code>float32</code>, <code>float64</code>, or even <code>int64</code> but
450not <code>uint64</code> or <code>string</code>.
451</p>
452
453<p>
454Implementation restriction: A compiler may implement numeric constants by choosing
455an internal representation with at least twice as many bits as any machine type;
456for floating-point values, both the mantissa and exponent must be twice as large.
457</p>
458
459
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700460<h2 id="Types">Types</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700461
Robert Griesemerc2d55862009-02-19 16:49:10 -0800462<p>
Robert Griesemer56809d02009-05-20 11:02:48 -0700463A type determines the set of values and operations specific to values of that
464type. A type may be specified by a (possibly qualified) <i>type name</i>
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700465<a href="#Qualified_identifier">Qualified identifier</a>, §<a href="#Type_declarations">Type declarations</a>) or a <i>type literal</i>,
Robert Griesemer56809d02009-05-20 11:02:48 -0700466which composes a new type from previously declared types.
Rob Pike5af7de32009-02-24 15:17:59 -0800467</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700468
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700469<pre class="ebnf">
Rob Pike8f2330d2009-02-25 16:20:44 -0800470Type = TypeName | TypeLit | "(" Type ")" .
471TypeName = QualifiedIdent.
472TypeLit = ArrayType | StructType | PointerType | FunctionType | InterfaceType |
473 SliceType | MapType | ChannelType .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800474</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -0800475
Robert Griesemerc2d55862009-02-19 16:49:10 -0800476<p>
Robert Griesemerfc61b772009-09-28 14:10:20 -0700477Named instances of the boolean, numeric, and string types are
478<a href="#Predeclared_identifiers">predeclared</a>.
479<i>Composite types</i>&mdash;array, struct, pointer, function,
480interface, slice, map, and channel types&mdash;may be constructed using
481type literals.
Rob Pike5af7de32009-02-24 15:17:59 -0800482</p>
Robert Griesemer7abfcd92008-10-07 17:14:30 -0700483
Rob Pike5af7de32009-02-24 15:17:59 -0800484<p>
Robert Griesemer3b576a72009-06-17 14:31:33 -0700485A type may have a <i>method set</i> associated with it
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700486<a href="#Interface_types">Interface types</a>, §<a href="#Method_declarations">Method declarations</a>).
Robert Griesemer19b1d352009-09-24 19:36:48 -0700487The method set of an <a href="#Interface_types">interface type</a> is its interface.
Robert Griesemer56809d02009-05-20 11:02:48 -0700488The method set of any other named type <code>T</code>
Robert Griesemerfc61b772009-09-28 14:10:20 -0700489consists of all methods with receiver type <code>T</code>.
Robert Griesemer56809d02009-05-20 11:02:48 -0700490The method set of the corresponding pointer type <code>*T</code>
491is the set of all methods with receiver <code>*T</code> or <code>T</code>
492(that is, it also contains the method set of <code>T</code>).
493Any other type has an empty method set.
Robert Griesemerd4d4ff02009-10-19 13:13:59 -0700494In a method set, each method must have a unique name.
Rob Pike5af7de32009-02-24 15:17:59 -0800495</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -0800496<p>
Rob Pike5af7de32009-02-24 15:17:59 -0800497The <i>static type</i> (or just <i>type</i>) of a variable is the
498type defined by its declaration. Variables of interface type
Robert Griesemer19b1d352009-09-24 19:36:48 -0700499also have a distinct <i>dynamic type</i>, which
Rob Pike5af7de32009-02-24 15:17:59 -0800500is the actual type of the value stored in the variable at run-time.
Robert Griesemer19b1d352009-09-24 19:36:48 -0700501The dynamic type may vary during execution but is always assignment compatible
502to the static type of the interface variable. For non-interface
Rob Pike5af7de32009-02-24 15:17:59 -0800503types, the dynamic type is always the static type.
504</p>
Robert Griesemer1f3e8422008-09-29 18:41:30 -0700505
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700506
Robert Griesemer19b1d352009-09-24 19:36:48 -0700507<h3 id="Boolean_types">Boolean types</h3>
508
509A <i>boolean type</i> represents the set of Boolean truth values
510denoted by the predeclared constants <code>true</code>
511and <code>false</code>. The predeclared boolean type is <code>bool</code>.
512
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700513
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700514<h3 id="Numeric_types">Numeric types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700515
Rob Pike5af7de32009-02-24 15:17:59 -0800516<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700517A <i>numeric type</i> represents sets of integer or floating-point values.
518The predeclared architecture-independent numeric types are:
Rob Pike5af7de32009-02-24 15:17:59 -0800519</p>
Robert Griesemerebf14c62008-10-30 14:50:23 -0700520
Rob Pikeff70f092009-02-20 13:36:14 -0800521<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -0800522uint8 the set of all unsigned 8-bit integers (0 to 255)
523uint16 the set of all unsigned 16-bit integers (0 to 65535)
524uint32 the set of all unsigned 32-bit integers (0 to 4294967295)
525uint64 the set of all unsigned 64-bit integers (0 to 18446744073709551615)
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700526
Robert Griesemerc2d55862009-02-19 16:49:10 -0800527int8 the set of all signed 8-bit integers (-128 to 127)
528int16 the set of all signed 16-bit integers (-32768 to 32767)
529int32 the set of all signed 32-bit integers (-2147483648 to 2147483647)
530int64 the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700531
Robert Griesemer19b1d352009-09-24 19:36:48 -0700532float32 the set of all IEEE-754 32-bit floating-point numbers
533float64 the set of all IEEE-754 64-bit floating-point numbers
Rob Pike5af7de32009-02-24 15:17:59 -0800534
535byte familiar alias for uint8
Robert Griesemerc2d55862009-02-19 16:49:10 -0800536</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700537
Rob Pike5af7de32009-02-24 15:17:59 -0800538<p>
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -0800539Integer types are represented in the usual binary format; the value of
540an n-bit integer is n bits wide. A negative signed integer is represented
541as the two's complement of its absolute value.
Rob Pike5af7de32009-02-24 15:17:59 -0800542</p>
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -0800543
Rob Pike5af7de32009-02-24 15:17:59 -0800544<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700545There is also a set of predeclared numeric types with implementation-specific sizes:
Rob Pike5af7de32009-02-24 15:17:59 -0800546</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700547
Rob Pikeff70f092009-02-20 13:36:14 -0800548<pre class="grammar">
Robert Griesemercfe92112009-06-18 13:29:40 -0700549uint either 32 or 64 bits
550int either 32 or 64 bits
551float either 32 or 64 bits
552uintptr an unsigned integer large enough to store the uninterpreted bits of a pointer value
Robert Griesemerc2d55862009-02-19 16:49:10 -0800553</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700554
Robert Griesemerc2d55862009-02-19 16:49:10 -0800555<p>
Rob Pikeda389742009-03-02 19:13:40 -0800556To avoid portability issues all numeric types are distinct except
557<code>byte</code>, which is an alias for <code>uint8</code>.
558Conversions
Robert Griesemer533dfd62009-05-13 16:56:00 -0700559are required when incompatible numeric types are mixed in an expression
Rob Pike5af7de32009-02-24 15:17:59 -0800560or assignment. For instance, <code>int32</code> and <code>int</code>
Russ Cox5958dd62009-03-04 17:19:21 -0800561are not the same type even though they may have the same size on a
Rob Pike5af7de32009-02-24 15:17:59 -0800562particular architecture.
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700563
564
Robert Griesemer19b1d352009-09-24 19:36:48 -0700565<h3 id="String_types">String types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700566
Rob Pike4501d342009-02-19 17:31:36 -0800567<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700568A <i>string type</i> represents the set of string values.
Rob Pike5af7de32009-02-24 15:17:59 -0800569Strings behave like arrays of bytes but are immutable: once created,
570it is impossible to change the contents of a string.
Robert Griesemer19b1d352009-09-24 19:36:48 -0700571The predeclared string type is <code>string</code>.
Rob Pike5af7de32009-02-24 15:17:59 -0800572
573<p>
574The elements of strings have type <code>byte</code> and may be
Robert Griesemerfc61b772009-09-28 14:10:20 -0700575accessed using the usual <a href="#Indexes">indexing operations</a>. It is
Rob Pike678625d2009-09-15 09:54:22 -0700576illegal to take the address of such an element; if
577<code>s[i]</code> is the <i>i</i>th byte of a
Robert Griesemercfe92112009-06-18 13:29:40 -0700578string, <code>&amp;s[i]</code> is invalid. The length of string
579<code>s</code> can be discovered using the built-in function
Robert Griesemer19b1d352009-09-24 19:36:48 -0700580<code>len</code>. The length is a compile-time constant if <code>s</code>
Robert Griesemercfe92112009-06-18 13:29:40 -0700581is a string literal.
Rob Pike4501d342009-02-19 17:31:36 -0800582</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700583
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700584
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700585<h3 id="Array_types">Array types</h3>
Russ Cox461dd912009-03-04 14:44:51 -0800586
587<p>
588An array is a numbered sequence of elements of a single
Robert Griesemer4023dce2009-08-14 17:41:52 -0700589type, called the element type.
590The number of elements is called the length and is never
Russ Cox461dd912009-03-04 14:44:51 -0800591negative.
592</p>
593
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700594<pre class="ebnf">
Russ Cox461dd912009-03-04 14:44:51 -0800595ArrayType = "[" ArrayLength "]" ElementType .
596ArrayLength = Expression .
Robert Griesemer4023dce2009-08-14 17:41:52 -0700597ElementType = Type .
Russ Cox461dd912009-03-04 14:44:51 -0800598</pre>
599
600<p>
Robert Griesemer4ed666e2009-08-27 16:45:42 -0700601The length is part of the array's type and must must be a
602<a href="#Constant_expressions">constant expression</a> that evaluates to a non-negative
Russ Cox461dd912009-03-04 14:44:51 -0800603integer value. The length of array <code>a</code> can be discovered
604using the built-in function <code>len(a)</code>, which is a
605compile-time constant. The elements can be indexed by integer
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700606indices 0 through the <code>len(a)-1</code><a href="#Indexes">Indexes</a>).
Russ Cox461dd912009-03-04 14:44:51 -0800607</p>
608
609<pre>
610[32]byte
611[2*N] struct { x, y int32 }
612[1000]*float64
613</pre>
614
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700615<h3 id="Slice_types">Slice types</h3>
Russ Cox461dd912009-03-04 14:44:51 -0800616
617<p>
618A slice is a reference to a contiguous segment of an array and
619contains a numbered sequence of elements from that array. A slice
620type denotes the set of all slices of arrays of its element type.
621A slice value may be <code>nil</code>.
622</p>
623
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700624<pre class="ebnf">
Russ Cox461dd912009-03-04 14:44:51 -0800625SliceType = "[" "]" ElementType .
626</pre>
627
628<p>
629Like arrays, slices are indexable and have a length. The length of a
630slice <code>s</code> can be discovered by the built-in function
631<code>len(s)</code>; unlike with arrays it may change during
632execution. The elements can be addressed by integer indices 0
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700633through <code>len(s)-1</code><a href="#Indexes">Indexes</a>). The slice index of a
Russ Cox461dd912009-03-04 14:44:51 -0800634given element may be less than the index of the same element in the
635underlying array.
636</p>
637<p>
638A slice, once initialized, is always associated with an underlying
639array that holds its elements. A slice therfore shares storage
640with its array and with other slices of the same array; by contrast,
641distinct arrays always represent distinct storage.
642</p>
643<p>
644The array underlying a slice may extend past the end of the slice.
Russ Cox5958dd62009-03-04 17:19:21 -0800645The <i>capacity</i> is a measure of that extent: it is the sum of
Russ Cox461dd912009-03-04 14:44:51 -0800646the length of the slice and the length of the array beyond the slice;
647a slice of length up to that capacity can be created by `slicing' a new
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700648one from the original slice (§<a href="#Slices">Slices</a>).
Russ Cox461dd912009-03-04 14:44:51 -0800649The capacity of a slice <code>a</code> can be discovered using the
Robert Griesemercfe92112009-06-18 13:29:40 -0700650built-in function <code>cap(a)</code> and the relationship between
651<code>len()</code> and <code>cap()</code> is:
Russ Cox461dd912009-03-04 14:44:51 -0800652</p>
653
654<pre>
6550 <= len(a) <= cap(a)
656</pre>
657
658<p>
659The value of an uninitialized slice is <code>nil</code>.
660The length and capacity of a <code>nil</code> slice
661are 0. A new, initialized slice value for a given element type <code>T</code> is
662made using the built-in function <code>make</code>, which takes a slice type
663and parameters specifying the length and optionally the capacity:
664</p>
665
666<pre>
667make([]T, length)
668make([]T, length, capacity)
669</pre>
Russ Cox5958dd62009-03-04 17:19:21 -0800670
Russ Cox461dd912009-03-04 14:44:51 -0800671<p>
672The <code>make()</code> call allocates a new, hidden array to which the returned
Rob Pike678625d2009-09-15 09:54:22 -0700673slice value refers. That is, executing
Russ Cox461dd912009-03-04 14:44:51 -0800674</p>
675
676<pre>
677make([]T, length, capacity)
678</pre>
679
680<p>
681produces the same slice as allocating an array and slicing it, so these two examples
682result in the same slice:
683</p>
684
685<pre>
686make([]int, 50, 100)
687new([100]int)[0:50]
688</pre>
689
690
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700691<h3 id="Struct_types">Struct types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700692
Rob Pike5af7de32009-02-24 15:17:59 -0800693<p>
694A struct is a sequence of named
695elements, called fields, with various types. A struct type declares
Robert Griesemer4e56b332009-09-10 10:14:00 -0700696an identifier and type for each field. Within a struct, non-<a href="#Blank_identifier">blank</a>
697field identifiers must be unique.
Rob Pike5af7de32009-02-24 15:17:59 -0800698</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700699
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700700<pre class="ebnf">
Russ Cox461dd912009-03-04 14:44:51 -0800701StructType = "struct" "{" [ FieldDeclList ] "}" .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800702FieldDeclList = FieldDecl { ";" FieldDecl } [ ";" ] .
Robert Griesemer4023dce2009-08-14 17:41:52 -0700703FieldDecl = (IdentifierList Type | [ "*" ] TypeName) [ Tag ] .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800704Tag = StringLit .
705</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700706
Robert Griesemerc2d55862009-02-19 16:49:10 -0800707<pre>
708// An empty struct.
709struct {}
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700710
Robert Griesemer4e56b332009-09-10 10:14:00 -0700711// A struct with 6 fields.
Robert Griesemerc2d55862009-02-19 16:49:10 -0800712struct {
713 x, y int;
714 u float;
Robert Griesemer4e56b332009-09-10 10:14:00 -0700715 _ float; // padding
Robert Griesemerc2d55862009-02-19 16:49:10 -0800716 A *[]int;
717 F func();
718}
719</pre>
Robert Griesemer1f3e8422008-09-29 18:41:30 -0700720
Rob Pike5af7de32009-02-24 15:17:59 -0800721<p>
722A field declared with a type but no field identifier is an <i>anonymous field</i>.
723Such a field type must be specified as
Rob Pikeda389742009-03-02 19:13:40 -0800724a type name <code>T</code> or as a pointer to a type name <code>*T</code>,
Ian Lance Taylor3e804ba2009-08-17 11:40:57 -0700725and <code>T</code> itself may not be
726a pointer type. The unqualified type name acts as the field identifier.
Rob Pike5af7de32009-02-24 15:17:59 -0800727</p>
Robert Griesemer1f3e8422008-09-29 18:41:30 -0700728
Robert Griesemerc2d55862009-02-19 16:49:10 -0800729<pre>
730// A struct with four anonymous fields of type T1, *T2, P.T3 and *P.T4
731struct {
732 T1; // the field name is T1
733 *T2; // the field name is T2
Rob Pikeda389742009-03-02 19:13:40 -0800734 P.T3; // the field name is T3
735 *P.T4; // the field name is T4
Russ Cox5958dd62009-03-04 17:19:21 -0800736 x, y int;
Robert Griesemerc2d55862009-02-19 16:49:10 -0800737}
738</pre>
Robert Griesemer1f3e8422008-09-29 18:41:30 -0700739
Rob Pike5af7de32009-02-24 15:17:59 -0800740<p>
Rob Pike678625d2009-09-15 09:54:22 -0700741The unqualified type name of an anonymous field must be distinct from the
742field identifier (or unqualified type name for an anonymous field) of every
Robert Griesemer071c91b2008-10-23 12:04:45 -0700743other field within the struct. The following declaration is illegal:
Rob Pike5af7de32009-02-24 15:17:59 -0800744</p>
Robert Griesemer071c91b2008-10-23 12:04:45 -0700745
Robert Griesemerc2d55862009-02-19 16:49:10 -0800746<pre>
747struct {
748 T; // conflicts with anonymous field *T and *P.T
749 *T; // conflicts with anonymous field T and *P.T
750 *P.T; // conflicts with anonymous field T and *T
751}
752</pre>
Robert Griesemer1f3e8422008-09-29 18:41:30 -0700753
Robert Griesemerc2d55862009-02-19 16:49:10 -0800754<p>
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700755Fields and methods (§<a href="#Method_declarations">Method declarations</a>) of an anonymous field are
756promoted to be ordinary fields and methods of the struct (§<a href="#Selectors">Selectors</a>).
Robert Griesemer56809d02009-05-20 11:02:48 -0700757The following rules apply for a struct type named <code>S</code> and
758a type named <code>T</code>:
Rob Pike5af7de32009-02-24 15:17:59 -0800759</p>
Robert Griesemer56809d02009-05-20 11:02:48 -0700760<ul>
761 <li>If <code>S</code> contains an anonymous field <code>T</code>, the
762 method set of <code>S</code> includes the method set of <code>T</code>.
763 </li>
764
765 <li>If <code>S</code> contains an anonymous field <code>*T</code>, the
766 method set of <code>S</code> includes the method set of <code>*T</code>
767 (which itself includes the method set of <code>T</code>).
768 </li>
769
770 <li>If <code>S</code> contains an anonymous field <code>T</code> or
771 <code>*T</code>, the method set of <code>*S</code> includes the
772 method set of <code>*T</code> (which itself includes the method
773 set of <code>T</code>).
774 </li>
775</ul>
Rob Pike5af7de32009-02-24 15:17:59 -0800776<p>
Robert Griesemer56809d02009-05-20 11:02:48 -0700777A field declaration may be followed by an optional string literal <i>tag</i>,
778which becomes an attribute for all the identifiers in the corresponding
Rob Pike5af7de32009-02-24 15:17:59 -0800779field declaration. The tags are made
Rob Pike8cb91842009-09-15 11:56:39 -0700780visible through a <a href="#Package_unsafe">reflection interface</a>
Rob Pike5af7de32009-02-24 15:17:59 -0800781but are otherwise ignored.
782</p>
Robert Griesemer2e90e542008-10-30 15:52:37 -0700783
Robert Griesemerc2d55862009-02-19 16:49:10 -0800784<pre>
Rob Pike678625d2009-09-15 09:54:22 -0700785// A struct corresponding to the TimeStamp protocol buffer.
Rob Pikecdbf6192009-02-24 17:47:45 -0800786// The tag strings define the protocol buffer field numbers.
Robert Griesemerc2d55862009-02-19 16:49:10 -0800787struct {
Rob Pike678625d2009-09-15 09:54:22 -0700788 microsec uint64 "field 1";
789 serverIP6 uint64 "field 2";
790 process string "field 3";
Robert Griesemerc2d55862009-02-19 16:49:10 -0800791}
792</pre>
Robert Griesemer2e90e542008-10-30 15:52:37 -0700793
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700794<h3 id="Pointer_types">Pointer types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700795
Rob Pike5af7de32009-02-24 15:17:59 -0800796<p>
Robert Griesemer4dc25282008-09-09 10:37:19 -0700797A pointer type denotes the set of all pointers to variables of a given
Rob Pikeda389742009-03-02 19:13:40 -0800798type, called the <i>base type</i> of the pointer.
Rob Pike8f2330d2009-02-25 16:20:44 -0800799A pointer value may be <code>nil</code>.
Rob Pike5af7de32009-02-24 15:17:59 -0800800</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700801
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700802<pre class="ebnf">
Robert Griesemerc2d55862009-02-19 16:49:10 -0800803PointerType = "*" BaseType .
804BaseType = Type .
805</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700806
Robert Griesemerc2d55862009-02-19 16:49:10 -0800807<pre>
808*int
Rob Pike8f2330d2009-02-25 16:20:44 -0800809*map[string] *chan int
Robert Griesemerc2d55862009-02-19 16:49:10 -0800810</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700811
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700812<h3 id="Function_types">Function types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700813
Rob Pike8f2330d2009-02-25 16:20:44 -0800814<p>
Robert Griesemer7231ceb2008-09-08 15:01:04 -0700815A function type denotes the set of all functions with the same parameter
Rob Pike8f2330d2009-02-25 16:20:44 -0800816and result types.
817A function value may be <code>nil</code>.
818</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700819
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700820<pre class="ebnf">
Rob Pike8f2330d2009-02-25 16:20:44 -0800821FunctionType = "func" Signature .
822Signature = Parameters [ Result ] .
Robert Griesemer4023dce2009-08-14 17:41:52 -0700823Result = Parameters | Type .
Rob Pike8f2330d2009-02-25 16:20:44 -0800824Parameters = "(" [ ParameterList ] ")" .
825ParameterList = ParameterDecl { "," ParameterDecl } .
Robert Griesemer4023dce2009-08-14 17:41:52 -0700826ParameterDecl = [ IdentifierList ] ( Type | "..." ) .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800827</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700828
Robert Griesemerc2d55862009-02-19 16:49:10 -0800829<p>
Rob Pike8f2330d2009-02-25 16:20:44 -0800830Within a list of parameters or results, the names (IdentifierList)
831must either all be present or all be absent. If present, each name
832stands for one item (parameter or result) of the specified type; if absent, each
833type stands for one item of that type. Parameter and result
834lists are always parenthesized except that if there is exactly
835one unnamed result that is not a function type it may writen as an unparenthesized type.
Rob Pike8f2330d2009-02-25 16:20:44 -0800836</p>
837<p>
838For the last parameter only, instead of a type one may write
839<code>...</code> to indicate that the function may be invoked with
Rob Pikeda389742009-03-02 19:13:40 -0800840zero or more additional arguments of any
Rob Pike678625d2009-09-15 09:54:22 -0700841type.
Rob Pike8f2330d2009-02-25 16:20:44 -0800842</p>
Robert Griesemer2bfa9572008-10-24 13:13:12 -0700843
Robert Griesemerc2d55862009-02-19 16:49:10 -0800844<pre>
845func ()
846func (x int)
847func () int
848func (string, float, ...)
849func (a, b int, z float) bool
850func (a, b int, z float) (bool)
851func (a, b int, z float, opt ...) (success bool)
852func (int, int, float) (float, *[]int)
Robert Griesemerc2d55862009-02-19 16:49:10 -0800853func (n int) (func (p* T))
854</pre>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -0800855
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700856
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700857<h3 id="Interface_types">Interface types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700858
Rob Pike8f2330d2009-02-25 16:20:44 -0800859<p>
Robert Griesemerd4d4ff02009-10-19 13:13:59 -0700860An interface type specifies a <a href="#Types">method set</a> called its <i>interface</i>.
Robert Griesemer56809d02009-05-20 11:02:48 -0700861A variable of interface type can store a value of any type with a method set
862that is any superset of the interface. Such a type is said to
863<i>implement the interface</i>. An interface value may be <code>nil</code>.
Rob Pike8f2330d2009-02-25 16:20:44 -0800864</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700865
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700866<pre class="ebnf">
Russ Cox461dd912009-03-04 14:44:51 -0800867InterfaceType = "interface" "{" [ MethodSpecList ] "}" .
Rob Pike8f2330d2009-02-25 16:20:44 -0800868MethodSpecList = MethodSpec { ";" MethodSpec } [ ";" ] .
Robert Griesemerd4d4ff02009-10-19 13:13:59 -0700869MethodSpec = MethodName Signature | InterfaceTypeName .
870MethodName = identifier .
Rob Pike8f2330d2009-02-25 16:20:44 -0800871InterfaceTypeName = TypeName .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800872</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700873
Robert Griesemerd4d4ff02009-10-19 13:13:59 -0700874<p>
875As with all method sets, in an interface type, each method must have a unique name.
876</p>
877
Robert Griesemerc2d55862009-02-19 16:49:10 -0800878<pre>
Rob Pike8f2330d2009-02-25 16:20:44 -0800879// A simple File interface
Robert Griesemerc2d55862009-02-19 16:49:10 -0800880interface {
Russ Cox13dac652009-09-28 14:16:33 -0700881 Read(b Buffer) bool;
882 Write(b Buffer) bool;
883 Close();
Robert Griesemerc2d55862009-02-19 16:49:10 -0800884}
885</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700886
Rob Pike8f2330d2009-02-25 16:20:44 -0800887<p>
Robert Griesemer56809d02009-05-20 11:02:48 -0700888More than one type may implement an interface.
Rob Pike8f2330d2009-02-25 16:20:44 -0800889For instance, if two types <code>S1</code> and <code>S2</code>
Robert Griesemer56809d02009-05-20 11:02:48 -0700890have the method set
Rob Pike8f2330d2009-02-25 16:20:44 -0800891</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700892
Robert Griesemerc2d55862009-02-19 16:49:10 -0800893<pre>
894func (p T) Read(b Buffer) bool { return ... }
895func (p T) Write(b Buffer) bool { return ... }
896func (p T) Close() { ... }
897</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700898
Rob Pike8f2330d2009-02-25 16:20:44 -0800899<p>
900(where <code>T</code> stands for either <code>S1</code> or <code>S2</code>)
901then the <code>File</code> interface is implemented by both <code>S1</code> and
902<code>S2</code>, regardless of what other methods
903<code>S1</code> and <code>S2</code> may have or share.
904</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700905
Rob Pike8f2330d2009-02-25 16:20:44 -0800906<p>
907A type implements any interface comprising any subset of its methods
908and may therefore implement several distinct interfaces. For
909instance, all types implement the <i>empty interface</i>:
910</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700911
Robert Griesemerc2d55862009-02-19 16:49:10 -0800912<pre>
Robert Griesemer19b1d352009-09-24 19:36:48 -0700913interface{}
Robert Griesemerc2d55862009-02-19 16:49:10 -0800914</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700915
Rob Pike8f2330d2009-02-25 16:20:44 -0800916<p>
917Similarly, consider this interface specification,
Robert Griesemer4ed666e2009-08-27 16:45:42 -0700918which appears within a <a href="#Type_declarations">type declaration</a>
Rob Pike8f2330d2009-02-25 16:20:44 -0800919to define an interface called <code>Lock</code>:
920</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700921
Robert Griesemerc2d55862009-02-19 16:49:10 -0800922<pre>
923type Lock interface {
Russ Cox13dac652009-09-28 14:16:33 -0700924 Lock();
925 Unlock();
Robert Griesemerc2d55862009-02-19 16:49:10 -0800926}
927</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700928
Rob Pike8f2330d2009-02-25 16:20:44 -0800929<p>
930If <code>S1</code> and <code>S2</code> also implement
931</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700932
Robert Griesemerc2d55862009-02-19 16:49:10 -0800933<pre>
934func (p T) Lock() { ... }
935func (p T) Unlock() { ... }
936</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700937
Robert Griesemerc2d55862009-02-19 16:49:10 -0800938<p>
Rob Pike8f2330d2009-02-25 16:20:44 -0800939they implement the <code>Lock</code> interface as well
940as the <code>File</code> interface.
941</p>
942<p>
943An interface may contain an interface type name <code>T</code>
944in place of a method specification.
Robert Griesemerd4d4ff02009-10-19 13:13:59 -0700945The effect is equivalent to enumerating the methods of <code>T</code> explicitly
Rob Pike8f2330d2009-02-25 16:20:44 -0800946in the interface.
947</p>
Robert Griesemer38c232f2009-02-11 15:09:15 -0800948
Robert Griesemerc2d55862009-02-19 16:49:10 -0800949<pre>
950type ReadWrite interface {
Russ Cox13dac652009-09-28 14:16:33 -0700951 Read(b Buffer) bool;
952 Write(b Buffer) bool;
Robert Griesemerc2d55862009-02-19 16:49:10 -0800953}
Robert Griesemer38c232f2009-02-11 15:09:15 -0800954
Robert Griesemerc2d55862009-02-19 16:49:10 -0800955type File interface {
956 ReadWrite; // same as enumerating the methods in ReadWrite
957 Lock; // same as enumerating the methods in Lock
958 Close();
959}
960</pre>
Robert Griesemer38c232f2009-02-11 15:09:15 -0800961
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700962<h3 id="Map_types">Map types</h3>
Robert Griesemera3294712009-01-05 11:17:26 -0800963
Rob Pike8f2330d2009-02-25 16:20:44 -0800964<p>
965A map is an unordered group of elements of one type, called the
966value type, indexed by a set of unique <i>keys</i> of another type,
Robert Griesemer4023dce2009-08-14 17:41:52 -0700967called the key type.
Rob Pike8f2330d2009-02-25 16:20:44 -0800968A map value may be <code>nil</code>.
969
970</p>
Robert Griesemera3294712009-01-05 11:17:26 -0800971
Robert Griesemerf7ac31362009-07-10 16:06:40 -0700972<pre class="ebnf">
Robert Griesemer19b1d352009-09-24 19:36:48 -0700973MapType = "map" "[" KeyType "]" ElementType .
Robert Griesemer4023dce2009-08-14 17:41:52 -0700974KeyType = Type .
975ValueType = Type .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800976</pre>
Robert Griesemera3294712009-01-05 11:17:26 -0800977
Robert Griesemerc2d55862009-02-19 16:49:10 -0800978<p>
Rob Pike8f2330d2009-02-25 16:20:44 -0800979The comparison operators <code>==</code> and <code>!=</code>
Russ Cox7c4f7cc2009-08-20 11:11:03 -0700980<a href="#Comparison_operators">Comparison operators</a>) must be fully defined for operands of the
Robert Griesemer19b1d352009-09-24 19:36:48 -0700981key type; thus the key type must be a boolean, numeric, string, pointer, function, interface,
Rob Pike8f2330d2009-02-25 16:20:44 -0800982map, or channel type. If the key type is an interface type, these
983comparison operators must be defined for the dynamic key values;
984failure will cause a run-time error.
985
986</p>
Robert Griesemera3294712009-01-05 11:17:26 -0800987
Robert Griesemerc2d55862009-02-19 16:49:10 -0800988<pre>
989map [string] int
990map [*T] struct { x, y float }
991map [string] interface {}
992</pre>
Robert Griesemera3294712009-01-05 11:17:26 -0800993
Rob Pike5af7de32009-02-24 15:17:59 -0800994<p>
Rob Pike8f2330d2009-02-25 16:20:44 -0800995The number of elements is called the length and is never negative.
996The length of a map <code>m</code> can be discovered using the
997built-in function <code>len(m)</code> and may change during execution.
Rob Pike678625d2009-09-15 09:54:22 -0700998Values may be added and removed
999during execution using special forms of <a href="#Assignments">assignment</a>.
Rob Pike5af7de32009-02-24 15:17:59 -08001000</p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001001<p>
Rob Pike678625d2009-09-15 09:54:22 -07001002The value of an uninitialized map is <code>nil</code>.
Rob Pike8f2330d2009-02-25 16:20:44 -08001003A new, empty map value is made using the built-in
1004function <code>make</code>, which takes the map type and an optional
Rob Pikeda389742009-03-02 19:13:40 -08001005capacity hint as arguments:
Rob Pike8f2330d2009-02-25 16:20:44 -08001006</p>
1007
1008<pre>
Rob Pikeda389742009-03-02 19:13:40 -08001009make(map[string] int)
1010make(map[string] int, 100)
Rob Pike8f2330d2009-02-25 16:20:44 -08001011</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001012
Rob Pikeda389742009-03-02 19:13:40 -08001013<p>
1014The initial capacity does not bound its size:
1015maps grow to accommodate the number of items
1016stored in them.
1017</p>
1018
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001019<h3 id="Channel_types">Channel types</h3>
Robert Griesemera3294712009-01-05 11:17:26 -08001020
Rob Pike8f2330d2009-02-25 16:20:44 -08001021<p>
Robert Griesemera3294712009-01-05 11:17:26 -08001022A channel provides a mechanism for two concurrently executing functions
Rob Pike8f2330d2009-02-25 16:20:44 -08001023to synchronize execution and communicate by passing a value of a
Robert Griesemer4023dce2009-08-14 17:41:52 -07001024specified element type.
Robert Griesemere2cb60b2009-06-19 13:03:01 -07001025A value of channel type may be <code>nil</code>.
Rob Pike8f2330d2009-02-25 16:20:44 -08001026</p>
Robert Griesemera3294712009-01-05 11:17:26 -08001027
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001028<pre class="ebnf">
Rob Pike8f2330d2009-02-25 16:20:44 -08001029ChannelType = Channel | SendChannel | RecvChannel .
1030Channel = "chan" ValueType .
1031SendChannel = "chan" "&lt;-" ValueType .
1032RecvChannel = "&lt;-" "chan" ValueType .
Robert Griesemerc2d55862009-02-19 16:49:10 -08001033</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001034
Rob Pike8f2330d2009-02-25 16:20:44 -08001035<p>
1036Upon creation, a channel can be used both to send and to receive values.
Robert Griesemera3294712009-01-05 11:17:26 -08001037By conversion or assignment, a channel may be constrained only to send or
Rob Pike8f2330d2009-02-25 16:20:44 -08001038to receive. This constraint is called a channel's <i>direction</i>; either
1039<i>send</i>, <i>receive</i>, or <i>bi-directional</i> (unconstrained).
1040</p>
Robert Griesemera3294712009-01-05 11:17:26 -08001041
Robert Griesemerc2d55862009-02-19 16:49:10 -08001042<pre>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07001043chan T // can be used to send and receive values of type T
Robert Griesemere1e76192009-09-25 14:11:03 -07001044chan&lt;- float // can only be used to send floats
Rob Pike46596852009-03-02 16:17:29 -08001045&lt;-chan int // can only be used to receive ints
Robert Griesemerc2d55862009-02-19 16:49:10 -08001046</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001047
Rob Pike8f2330d2009-02-25 16:20:44 -08001048<p>
1049The value of an uninitialized channel is <code>nil</code>. A new, initialized channel
1050value is made using the built-in function <code>make</code>,
Robert Griesemer633957b2009-01-06 13:23:20 -08001051which takes the channel type and an optional capacity as arguments:
Rob Pike8f2330d2009-02-25 16:20:44 -08001052</p>
Robert Griesemera3294712009-01-05 11:17:26 -08001053
Rob Pike94b67eb2009-03-24 17:40:47 -07001054
Robert Griesemerc2d55862009-02-19 16:49:10 -08001055<pre>
Rob Pikeda389742009-03-02 19:13:40 -08001056make(chan int, 100)
Robert Griesemerc2d55862009-02-19 16:49:10 -08001057</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001058
Rob Pike8f2330d2009-02-25 16:20:44 -08001059<p>
1060The capacity, in number of elements, sets the size of the buffer in the channel. If the
Rob Pike678625d2009-09-15 09:54:22 -07001061capacity is greater than zero, the channel is asynchronous: provided the
Rob Pike8f2330d2009-02-25 16:20:44 -08001062buffer is not full, sends can succeed without blocking. If the capacity is zero
1063or absent, the communication succeeds only when both a sender and receiver are ready.
1064</p>
Robert Griesemera3294712009-01-05 11:17:26 -08001065
Rob Pike94b67eb2009-03-24 17:40:47 -07001066<p>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07001067A channel may be closed and tested for closure with the built-in functions
1068<a href="#Close_and_closed"><code>close</code> and <code>closed</code></a>.
Rob Pike94b67eb2009-03-24 17:40:47 -07001069</p>
1070
Rob Pike83cbca52009-08-21 14:18:08 -07001071<h2 id="Properties_of_types_and_values">Properties of types and values</h2>
Robert Griesemer434c6052008-11-07 13:34:37 -08001072
Rob Pike4501d342009-02-19 17:31:36 -08001073<p>
Robert Griesemerfc61b772009-09-28 14:10:20 -07001074Two types are either <i>identical</i> or <i>different</i>, and they are
1075either <i>compatible</i> or <i>incompatible</i>.
1076Identical types are always compatible, but compatible types need not be identical.
Robert Griesemer19b1d352009-09-24 19:36:48 -07001077</p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001078
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001079<h3 id="Type_identity_and_compatibility">Type identity and compatibility</h3>
Robert Griesemer533dfd62009-05-13 16:56:00 -07001080
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001081<h4 id="Type_identity">Type identity</h4>
Rob Pike8f2330d2009-02-25 16:20:44 -08001082
Robert Griesemerc2d55862009-02-19 16:49:10 -08001083<p>
Robert Griesemer533dfd62009-05-13 16:56:00 -07001084Two named types are identical if their type names originate in the same
Robert Griesemeraeaab592009-08-31 17:30:55 -07001085type declaration (§<a href="#Declarations_and_scope">Declarations and scope</a>). A named and an unnamed type
Robert Griesemer533dfd62009-05-13 16:56:00 -07001086are never identical. Two unnamed types are identical if the corresponding
1087type literals have the same literal structure and corresponding components have
1088identical types. In detail:
Rob Pike4501d342009-02-19 17:31:36 -08001089</p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001090
Robert Griesemerc2d55862009-02-19 16:49:10 -08001091<ul>
Robert Griesemer533dfd62009-05-13 16:56:00 -07001092 <li>Two array types are identical if they have identical element types and
1093 the same array length.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001094
Robert Griesemer533dfd62009-05-13 16:56:00 -07001095 <li>Two slice types are identical if they have identical element types.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001096
Robert Griesemer533dfd62009-05-13 16:56:00 -07001097 <li>Two struct types are identical if they have the same sequence of fields,
1098 and if corresponding fields have the same names and identical types.
Robert Griesemer56809d02009-05-20 11:02:48 -07001099 Two anonymous fields are considered to have the same name.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001100
Robert Griesemer533dfd62009-05-13 16:56:00 -07001101 <li>Two pointer types are identical if they have identical base types.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001102
Robert Griesemer533dfd62009-05-13 16:56:00 -07001103 <li>Two function types are identical if they have the same number of parameters
1104 and result values and if corresponding parameter and result types are
Rob Pike678625d2009-09-15 09:54:22 -07001105 identical. All "..." parameters are defined to have identical type.
Robert Griesemer533dfd62009-05-13 16:56:00 -07001106 Parameter and result names are not required to match.</li>
Robert Griesemera3294712009-01-05 11:17:26 -08001107
Robert Griesemer533dfd62009-05-13 16:56:00 -07001108 <li>Two interface types are identical if they have the same set of methods
1109 with the same names and identical function types. The order
1110 of the methods is irrelevant.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001111
Robert Griesemer533dfd62009-05-13 16:56:00 -07001112 <li>Two map types are identical if they have identical key and value types.</li>
Robert Griesemer434c6052008-11-07 13:34:37 -08001113
Robert Griesemer533dfd62009-05-13 16:56:00 -07001114 <li>Two channel types are identical if they have identical value types and
1115 the same direction.</li>
Robert Griesemerc2d55862009-02-19 16:49:10 -08001116</ul>
Robert Griesemer434c6052008-11-07 13:34:37 -08001117
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001118<h4 id="Type_compatibility">Type compatibility</h4>
Robert Griesemer533dfd62009-05-13 16:56:00 -07001119
Robert Griesemerc2d55862009-02-19 16:49:10 -08001120<p>
Robert Griesemer533dfd62009-05-13 16:56:00 -07001121Type compatibility is less stringent than type identity: a named and an unnamed
1122type are compatible if the respective type literals are compatible.
1123In all other respects, the definition of type compatibility is the
1124same as for type identity listed above but with ``compatible''
1125substituted for ``identical''.
Rob Pike4501d342009-02-19 17:31:36 -08001126</p>
Robert Griesemer434c6052008-11-07 13:34:37 -08001127
Robert Griesemerc2d55862009-02-19 16:49:10 -08001128<p>
Rob Pike8f2330d2009-02-25 16:20:44 -08001129Given the declarations
1130</p>
Robert Griesemer434c6052008-11-07 13:34:37 -08001131
Robert Griesemerc2d55862009-02-19 16:49:10 -08001132<pre>
1133type (
1134 T0 []string;
Rob Pike678625d2009-09-15 09:54:22 -07001135 T1 []string;
Robert Griesemerc2d55862009-02-19 16:49:10 -08001136 T2 struct { a, b int };
1137 T3 struct { a, c int };
Rob Pike678625d2009-09-15 09:54:22 -07001138 T4 func (int, float) *T0;
1139 T5 func (x int, y float) *[]string;
Robert Griesemerc2d55862009-02-19 16:49:10 -08001140)
1141</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -08001142
Rob Pike8f2330d2009-02-25 16:20:44 -08001143<p>
Robert Griesemer533dfd62009-05-13 16:56:00 -07001144these types are identical:
Rob Pike8f2330d2009-02-25 16:20:44 -08001145</p>
Robert Griesemer434c6052008-11-07 13:34:37 -08001146
Robert Griesemerc2d55862009-02-19 16:49:10 -08001147<pre>
1148T0 and T0
1149[]int and []int
1150struct { a, b *T5 } and struct { a, b *T5 }
Robert Griesemer533dfd62009-05-13 16:56:00 -07001151func (x int, y float) *[]string and func (int, float) (result *[]string)
Robert Griesemerc2d55862009-02-19 16:49:10 -08001152</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -08001153
Rob Pike8f2330d2009-02-25 16:20:44 -08001154<p>
Robert Griesemer533dfd62009-05-13 16:56:00 -07001155<code>T0</code> and <code>T1</code> are neither identical nor compatible
1156because they are named types with distinct declarations.
1157</p>
1158
1159<p>
1160These types are compatible:
1161</p>
1162
1163<pre>
1164T0 and T0
1165T0 and []string
1166T3 and struct { a int; c int }
1167T4 and func (x int, y float) *[]string
1168</pre>
1169
1170<p>
1171<code>T2</code> and <code>struct { a, c int }</code> are incompatible because
1172they have different field names.
Rob Pike8f2330d2009-02-25 16:20:44 -08001173</p>
Robert Griesemer434c6052008-11-07 13:34:37 -08001174
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001175<h3 id="Assignment_compatibility">Assignment compatibility</h3>
Rob Pike5af7de32009-02-24 15:17:59 -08001176
Rob Pike5af7de32009-02-24 15:17:59 -08001177<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07001178A value <code>v</code> of static type <code>V</code> is <i>assignment compatible</i>
1179with a type <code>T</code> if one of the following conditions applies:
Rob Pike5af7de32009-02-24 15:17:59 -08001180</p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07001181
Rob Pike5af7de32009-02-24 15:17:59 -08001182<ul>
1183<li>
Robert Griesemer19b1d352009-09-24 19:36:48 -07001184<code>V</code> is compatible with <code>T</code>.
Rob Pike5af7de32009-02-24 15:17:59 -08001185</li>
1186<li>
Robert Griesemer19b1d352009-09-24 19:36:48 -07001187<code>T</code> is an interface type and
1188<code>V</code> <a href="#Interface_types">implements</a> <code>T</code>.
Robert Griesemere2cb60b2009-06-19 13:03:01 -07001189</li>
1190<li>
Robert Griesemer19b1d352009-09-24 19:36:48 -07001191<code>V</code> is a pointer to an array and <code>T</code> is a slice type
1192with compatible element type and at least one of <code>V</code> or <code>T</code> is unnamed.
1193After assignment, the slice variable refers to the original array; the elements are not
1194copied.
Rob Pike5af7de32009-02-24 15:17:59 -08001195</li>
Robert Griesemer4e56b332009-09-10 10:14:00 -07001196<li>
Robert Griesemer19b1d352009-09-24 19:36:48 -07001197<code>V</code> is a bidirectional channel and <code>T</code> is a channel type
1198with compatible element type and at least one of <code>V</code> or <code>T</code> is unnamed.
Robert Griesemer4e56b332009-09-10 10:14:00 -07001199</li>
Rob Pike5af7de32009-02-24 15:17:59 -08001200</ul>
1201
Robert Griesemer19b1d352009-09-24 19:36:48 -07001202<p>
Robert Griesemer326ef132009-09-28 19:21:15 -07001203If <code>T</code> is a struct type, either all fields of <code>T</code>
1204must be <a href="#Exported_identifiers">exported</a>, or the assignment must be in
1205the same package in which <code>T</code> is declared.
1206In other words, a struct value can be assigned to a struct variable only if
1207every field of the struct may be legally assigned individually by the program.
1208</p>
1209
1210<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07001211An untyped <a href="#Constants">constant</a> <code>v</code>
1212is assignment compatible with type <code>T</code> if <code>v</code>
1213can be represented accurately as a value of type <code>T</code>.
1214</p>
1215
1216<p>
1217The predeclared identifier <code>nil</code> is assignment compatible with any
1218pointer, function, slice, map, channel, or interface type and
Robert Griesemer997851e2009-09-25 15:36:25 -07001219represents the <a href="#The_zero_value">zero value</a> for that type.
Robert Griesemer19b1d352009-09-24 19:36:48 -07001220</p>
1221
1222<p>
1223Any value may be assigned to the <a href="#Blank_identifier">blank identifier</a>.
1224</p>
1225
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001226<h3 id="Comparison_compatibility">Comparison compatibility</h3>
Rob Pike5af7de32009-02-24 15:17:59 -08001227
1228<p>
Rob Pike678625d2009-09-15 09:54:22 -07001229Except as noted, values of any type may be compared to other values of
1230<a href="#Type_compatibility">compatible static type</a>.
1231Values of numeric and string type may be compared using the
1232full range of <a href="#Comparison_operators;">comparison operators</a>;
Rob Pike5af7de32009-02-24 15:17:59 -08001233booleans may be compared only for equality or inequality.
1234</p>
1235
1236<p>
1237Values of composite type may be
1238compared for equality or inequality using the <code>==</code> and
1239<code>!=</code> operators, with the following provisos:
1240</p>
1241<ul>
1242<li>
1243Arrays and structs may not be compared to anything.
1244</li>
1245<li>
Rob Pikeda389742009-03-02 19:13:40 -08001246A slice value may only be compared explicitly against <code>nil</code>.
1247A slice value is equal to <code>nil</code> if it has been assigned the explicit
Rob Pike678625d2009-09-15 09:54:22 -07001248value <code>nil</code>, if it is uninitialized, or if it has
1249been assigned another slice value equal to <code>nil</code>·
Rob Pike5af7de32009-02-24 15:17:59 -08001250</li>
1251<li>
1252Similarly, an interface value is equal to <code>nil</code> if it has
Rob Pike678625d2009-09-15 09:54:22 -07001253been assigned the explicit value <code>nil</code>, if it is uninitialized,
1254or if it has been assigned another interface value equal to <code>nil</code>.
Rob Pike5af7de32009-02-24 15:17:59 -08001255</li>
1256<li>
1257For types that can be compared to <code>nil</code>,
1258two values of the same type are equal if they both equal <code>nil</code>,
1259unequal if one equals <code>nil</code> and one does not.
1260</li>
1261<li>
1262Pointer values are equal if they point to the same location.
1263</li>
1264<li>
Rob Pikeda389742009-03-02 19:13:40 -08001265Function values are equal if they refer to the same function.
Rob Pike5af7de32009-02-24 15:17:59 -08001266</li>
1267<li>
Rob Pikeda389742009-03-02 19:13:40 -08001268Channel and map values are equal if they were created by the same call to <code>make</code>
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001269<a href="#Making_slices">Making slices</a>, maps, and channels).
Rob Pike83cbca52009-08-21 14:18:08 -07001270When comparing two values of channel type, the channel value types
1271must be compatible but the channel direction is ignored.
Rob Pike5af7de32009-02-24 15:17:59 -08001272</li>
1273<li>
Robert Griesemer533dfd62009-05-13 16:56:00 -07001274Interface values may be compared if they have compatible static types.
Rob Pikeda389742009-03-02 19:13:40 -08001275They will be equal only if they have the same dynamic type and the underlying values are equal.
Rob Pike5af7de32009-02-24 15:17:59 -08001276</li>
1277</ul>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001278
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001279<h2 id="Blocks">Blocks</h2>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001280
1281<p>
Robert Griesemer0a162a12009-08-19 16:44:04 -07001282A <i>block</i> is a sequence of declarations and statements within matching
1283brace brackets.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001284</p>
1285
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001286<pre class="ebnf">
Robert Griesemer0a162a12009-08-19 16:44:04 -07001287Block = "{" StatementList "}" .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001288</pre>
Russ Cox5958dd62009-03-04 17:19:21 -08001289
Rob Pikea9ed30f2009-02-23 19:26:07 -08001290<p>
Robert Griesemer0a162a12009-08-19 16:44:04 -07001291In addition to explicit blocks in the source code, there are implicit blocks:
Rob Pikea9ed30f2009-02-23 19:26:07 -08001292</p>
1293
1294<ol>
Robert Griesemer0a162a12009-08-19 16:44:04 -07001295 <li>The <i>universe block</i> encompasses all Go source text.</li>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001296
Robert Griesemer4e56b332009-09-10 10:14:00 -07001297 <li>Each <a href="#Packages">package</a> has a <i>package block</i> containing all
Robert Griesemer0a162a12009-08-19 16:44:04 -07001298 Go source text for that package.</li>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001299
Robert Griesemer0a162a12009-08-19 16:44:04 -07001300 <li>Each file has a <i>file block</i> containing all Go source text
1301 in that file.</li>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001302
Robert Griesemer0a162a12009-08-19 16:44:04 -07001303 <li>Each <code>if</code>, <code>for</code>, and <code>switch</code>
1304 statement is considered to be in its own implicit block.</li>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001305
Russ Cox16b95ba2009-08-20 10:22:52 -07001306 <li>Each clause in a <code>switch</code> or <code>select</code> statement
Robert Griesemer0a162a12009-08-19 16:44:04 -07001307 acts as an implicit block.</li>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001308</ol>
1309
Robert Griesemer0a162a12009-08-19 16:44:04 -07001310<p>
Robert Griesemer4e56b332009-09-10 10:14:00 -07001311Blocks nest and influence <a href="#Declarations_and_scope">scoping</a>.
Robert Griesemer0a162a12009-08-19 16:44:04 -07001312</p>
1313
1314
Robert Griesemeraeaab592009-08-31 17:30:55 -07001315<h2 id="Declarations_and_scope">Declarations and scope</h2>
Robert Griesemer0a162a12009-08-19 16:44:04 -07001316
1317<p>
Robert Griesemer4e56b332009-09-10 10:14:00 -07001318A declaration binds a non-<a href="#Blank_identifier">blank</a>
1319identifier to a constant, type, variable, function, or package.
Robert Griesemer0a162a12009-08-19 16:44:04 -07001320Every identifier in a program must be declared.
1321No identifier may be declared twice in the same block, and
1322no identifier may be declared in both the file and package block.
1323</p>
1324
1325<pre class="ebnf">
1326Declaration = ConstDecl | TypeDecl | VarDecl .
1327TopLevelDecl = Declaration | FunctionDecl | MethodDecl .
1328</pre>
1329
1330<p>
1331The <i>scope</i> of a declared identifier is the extent of source text in which
1332the identifier denotes the specified constant, type, variable, function, or package.
1333</p>
1334
1335<p>
1336Go is lexically scoped using blocks:
1337</p>
1338
1339<ol>
1340 <li>The scope of a predeclared identifier is the universe block.</li>
1341
1342 <li>The scope of an identifier denoting a constant, type, variable,
1343 or function declared at top level (outside any function) is the
1344 package block.</li>
1345
1346 <li>The scope of an imported package identifier is the file block
1347 of the file containing the import declaration.</li>
1348
1349 <li>The scope of an identifier denoting a function parameter or
1350 result variable is the function body.</li>
1351
1352 <li>The scope of a constant or variable identifier declared
1353 inside a function begins at the end of the ConstSpec or VarSpec
1354 and ends at the end of the innermost containing block.</li>
1355
1356 <li>The scope of a type identifier declared inside a function
Russ Cox16b95ba2009-08-20 10:22:52 -07001357 begins at the identifier in the TypeSpec
Robert Griesemer0a162a12009-08-19 16:44:04 -07001358 and ends at the end of the innermost containing block.</li>
1359</ol>
1360
1361<p>
1362An identifier declared in a block may be redeclared in an inner block.
1363While the identifier of the inner declaration is in scope, it denotes
1364the entity declared by the inner declaration.
1365</p>
1366
1367<p>
Robert Griesemer506c0082009-09-08 15:41:14 -07001368The <a href="#Package_clause">package clause</a> is not a declaration; the package name
Robert Griesemer0a162a12009-08-19 16:44:04 -07001369does not appear in any scope. Its purpose is to identify the files belonging
Robert Griesemer997851e2009-09-25 15:36:25 -07001370to the same <a href="#Packages">package</a> and to specify the default package name for import
Robert Griesemer0a162a12009-08-19 16:44:04 -07001371declarations.
1372</p>
1373
1374
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001375<h3 id="Label_scopes">Label scopes</h3>
Robert Griesemer0a162a12009-08-19 16:44:04 -07001376
1377<p>
Robert Griesemer506c0082009-09-08 15:41:14 -07001378Labels are declared by <a href="#Labeled_statements">labeled statements</a> and are
Robert Griesemer0a162a12009-08-19 16:44:04 -07001379used in the <code>break</code>, <code>continue</code>, and <code>goto</code>
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001380statements (§<a href="#Break_statements">Break statements</a>, §<a href="#Continue_statements">Continue statements</a>, §<a href="#Goto_statements">Goto statements</a>).
Robert Griesemer0a162a12009-08-19 16:44:04 -07001381In contrast to other identifiers, labels are not block scoped and do
1382not conflict with identifiers that are not labels. The scope of a label
1383is the body of the function in which it is declared and excludes
1384the body of any nested function.
1385</p>
1386
1387
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001388<h3 id="Predeclared_identifiers">Predeclared identifiers</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001389
1390<p>
Robert Griesemer0a162a12009-08-19 16:44:04 -07001391The following identifiers are implicitly declared in the universe block:
Rob Pikea9ed30f2009-02-23 19:26:07 -08001392</p>
1393<pre class="grammar">
1394Basic types:
Russ Cox5958dd62009-03-04 17:19:21 -08001395 bool byte float32 float64 int8 int16 int32 int64
1396 string uint8 uint16 uint32 uint64
Rob Pikea9ed30f2009-02-23 19:26:07 -08001397
Rob Pike5af7de32009-02-24 15:17:59 -08001398Architecture-specific convenience types:
Rob Pikea9ed30f2009-02-23 19:26:07 -08001399 float int uint uintptr
1400
1401Constants:
Robert Griesemer19b1d352009-09-24 19:36:48 -07001402 true false iota
1403
1404Zero value:
1405 nil
Rob Pikea9ed30f2009-02-23 19:26:07 -08001406
1407Functions:
Robert Griesemer4ed666e2009-08-27 16:45:42 -07001408 cap close closed len make new panic panicln print println
Rob Pikea9ed30f2009-02-23 19:26:07 -08001409</pre>
1410
Robert Griesemeraeaab592009-08-31 17:30:55 -07001411
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001412<h3 id="Exported_identifiers">Exported identifiers</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001413
1414<p>
Robert Griesemeraeaab592009-08-31 17:30:55 -07001415An identifier may be <i>exported</i> to permit access to it from another package
1416using a <a href="#Qualified_identifiers">qualified identifier</a>. An identifier
1417is exported if both:
Rob Pikea9ed30f2009-02-23 19:26:07 -08001418</p>
1419<ol>
Robert Griesemeraeaab592009-08-31 17:30:55 -07001420 <li>the first character of the identifier's name is a Unicode upper case letter (Unicode class "Lu"); and
Rob Pike678625d2009-09-15 09:54:22 -07001421 <li>the identifier is declared in the <a href="#Blocks">package block</a> or denotes a field or method of a type
Robert Griesemeraeaab592009-08-31 17:30:55 -07001422 declared in that block.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001423</ol>
1424<p>
Robert Griesemeraeaab592009-08-31 17:30:55 -07001425All other identifiers are not exported.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001426</p>
1427
Robert Griesemeraeaab592009-08-31 17:30:55 -07001428
Robert Griesemer4e56b332009-09-10 10:14:00 -07001429<h3 id="Blank_identifier">Blank identifier</h3>
1430
1431<p>
1432The <i>blank identifier</i>, represented by the underscore character <code>_</code>, may be used in a declaration like
1433any other identifier but the declaration does not introduce a new binding.
1434</p>
1435
1436
Robert Griesemer19b1d352009-09-24 19:36:48 -07001437<h3 id="Constant_declarations">Constant declarations</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001438
1439<p>
1440A constant declaration binds a list of identifiers (the names of
Robert Griesemer506c0082009-09-08 15:41:14 -07001441the constants) to the values of a list of <a href="#Constant_expressions">constant expressions</a>.
1442The number of identifiers must be equal
1443to the number of expressions, and the <i>n</i>th identifier on
1444the left is bound to the value of the <i>n</i>th expression on the
Rob Pikea9ed30f2009-02-23 19:26:07 -08001445right.
1446</p>
1447
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001448<pre class="ebnf">
Rob Pikea9ed30f2009-02-23 19:26:07 -08001449ConstDecl = "const" ( ConstSpec | "(" [ ConstSpecList ] ")" ) .
1450ConstSpecList = ConstSpec { ";" ConstSpec } [ ";" ] .
Robert Griesemer4023dce2009-08-14 17:41:52 -07001451ConstSpec = IdentifierList [ [ Type ] "=" ExpressionList ] .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001452
1453IdentifierList = identifier { "," identifier } .
1454ExpressionList = Expression { "," Expression } .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001455</pre>
1456
1457<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07001458If the type is present, all constants take the type specified, and
1459the expressions must be <a href="#Assignment_compatibility">assignment compatible</a> with that type.
Robert Griesemer4023dce2009-08-14 17:41:52 -07001460If the type is omitted, the constants take the
Robert Griesemer19b1d352009-09-24 19:36:48 -07001461individual types of the corresponding expressions.
1462If the expression values are untyped <a href="#Constants">constants</a>,
1463the declared constants remain untyped and the constant identifiers
1464denote the constant values. For instance, if the expression is a
1465floating-point literal, the constant identifier denotes a floating-point
1466constant, even if the literal's fractional part is zero.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001467</p>
1468
1469<pre>
1470const Pi float64 = 3.14159265358979323846
Robert Griesemer19b1d352009-09-24 19:36:48 -07001471const zero = 0.0 // untyped floating-point constant
Rob Pikea9ed30f2009-02-23 19:26:07 -08001472const (
1473 size int64 = 1024;
Robert Griesemer19b1d352009-09-24 19:36:48 -07001474 eof = -1; // untyped integer constant
Rob Pikea9ed30f2009-02-23 19:26:07 -08001475)
Robert Griesemer19b1d352009-09-24 19:36:48 -07001476const a, b, c = 3, 4, "foo" // a = 3, b = 4, c = "foo", untyped integer and string constants
Rob Pikea9ed30f2009-02-23 19:26:07 -08001477const u, v float = 0, 3 // u = 0.0, v = 3.0
1478</pre>
1479
1480<p>
1481Within a parenthesized <code>const</code> declaration list the
1482expression list may be omitted from any but the first declaration.
1483Such an empty list is equivalent to the textual substitution of the
Rob Pike678625d2009-09-15 09:54:22 -07001484first preceding non-empty expression list and its type if any.
Russ Cox5958dd62009-03-04 17:19:21 -08001485Omitting the list of expressions is therefore equivalent to
1486repeating the previous list. The number of identifiers must be equal
1487to the number of expressions in the previous list.
Robert Griesemer506c0082009-09-08 15:41:14 -07001488Together with the <a href="#Iota"><code>iota</code> constant generator</a>
1489this mechanism permits light-weight declaration of sequential values:
Rob Pikea9ed30f2009-02-23 19:26:07 -08001490</p>
1491
1492<pre>
1493const (
1494 Sunday = iota;
1495 Monday;
1496 Tuesday;
1497 Wednesday;
1498 Thursday;
1499 Friday;
1500 Partyday;
1501 numberOfDays; // this constant is not exported
1502)
1503</pre>
1504
1505
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001506<h3 id="Iota">Iota</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001507
1508<p>
1509Within a constant declaration, the predeclared pseudo-constant
Robert Griesemer19b1d352009-09-24 19:36:48 -07001510<code>iota</code> represents successive untyped integer <a href="#Constants">
1511constants</a>. It is reset to 0 whenever the reserved word <code>const</code>
1512appears in the source and increments with each semicolon. It can be used to construct a
Rob Pikea9ed30f2009-02-23 19:26:07 -08001513set of related constants:
1514</p>
1515
1516<pre>
Robert Griesemer4e56b332009-09-10 10:14:00 -07001517const ( // iota is reset to 0
Rob Pikea9ed30f2009-02-23 19:26:07 -08001518 c0 = iota; // c0 == 0
1519 c1 = iota; // c1 == 1
1520 c2 = iota // c2 == 2
1521)
1522
1523const (
Robert Griesemer4ed666e2009-08-27 16:45:42 -07001524 a = 1 &lt;&lt; iota; // a == 1 (iota has been reset)
1525 b = 1 &lt;&lt; iota; // b == 2
1526 c = 1 &lt;&lt; iota; // c == 4
Rob Pikea9ed30f2009-02-23 19:26:07 -08001527)
1528
1529const (
Robert Griesemer19b1d352009-09-24 19:36:48 -07001530 u = iota * 42; // u == 0 (untyped integer constant)
1531 v float = iota * 42; // v == 42.0 (float constant)
1532 w = iota * 42; // w == 84 (untyped integer constant)
Rob Pikea9ed30f2009-02-23 19:26:07 -08001533)
1534
1535const x = iota; // x == 0 (iota has been reset)
1536const y = iota; // y == 0 (iota has been reset)
1537</pre>
1538
1539<p>
1540Within an ExpressionList, the value of each <code>iota</code> is the same because
1541it is only incremented at a semicolon:
1542</p>
1543
1544<pre>
1545const (
Robert Griesemer4ed666e2009-08-27 16:45:42 -07001546 bit0, mask0 = 1 &lt;&lt; iota, 1 &lt;&lt; iota - 1; // bit0 == 1, mask0 == 0
Rob Pikea9ed30f2009-02-23 19:26:07 -08001547 bit1, mask1; // bit1 == 2, mask1 == 1
Robert Griesemer4e56b332009-09-10 10:14:00 -07001548 _, _; // skips iota == 2
1549 bit3, mask3; // bit3 == 8, mask3 == 7
Rob Pikea9ed30f2009-02-23 19:26:07 -08001550)
1551</pre>
1552
1553<p>
1554This last example exploits the implicit repetition of the
1555last non-empty expression list.
1556</p>
1557
1558
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001559<h3 id="Type_declarations">Type declarations</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001560
1561<p>
Robert Griesemerfc61b772009-09-28 14:10:20 -07001562A type declaration binds an identifier, the <i>type name</i>, to a new type
1563that has the same definition (element, fields, channel direction, etc.) as
1564an existing type. The new type is
1565<a href="#Properties_of_types_and_values">compatible</a> with, but
1566<a href="#Properties_of_types_and_values">different</a> from, the existing type.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001567</p>
1568
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001569<pre class="ebnf">
Rob Pikea9ed30f2009-02-23 19:26:07 -08001570TypeDecl = "type" ( TypeSpec | "(" [ TypeSpecList ] ")" ) .
1571TypeSpecList = TypeSpec { ";" TypeSpec } [ ";" ] .
Robert Griesemerbdec3302009-08-31 17:57:14 -07001572TypeSpec = identifier Type .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001573</pre>
1574
1575<pre>
Robert Griesemerbdec3302009-08-31 17:57:14 -07001576type IntArray [16]int
Rob Pikea9ed30f2009-02-23 19:26:07 -08001577
1578type (
1579 Point struct { x, y float };
1580 Polar Point
1581)
1582
1583type TreeNode struct {
1584 left, right *TreeNode;
Russ Cox461dd912009-03-04 14:44:51 -08001585 value *Comparable;
Rob Pikea9ed30f2009-02-23 19:26:07 -08001586}
1587
Rob Pike678625d2009-09-15 09:54:22 -07001588type Cipher interface {
1589 BlockSize() int;
1590 Encrypt(src, dst []byte);
1591 Decrypt(src, dst []byte);
Rob Pikea9ed30f2009-02-23 19:26:07 -08001592}
1593</pre>
1594
Robert Griesemerfc61b772009-09-28 14:10:20 -07001595<p>
1596The declared type does not inherit any <a href="#Method_declarations">methods</a>
1597bound to the existing type, but the <a href="#Types">method set</a>
1598of elements of a composite type is not changed:
1599</p>
1600
1601<pre>
1602// A Mutex is a data type with two methods Lock and Unlock.
1603type Mutex struct { /* Mutex fields */ }
1604func (m *Mutex) Lock() { /* Lock implementation */ }
1605func (m *Mutex) Unlock() { /* Unlock implementation */ }
1606
1607// NewMutex has the same composition as Mutex but its method set is empty.
1608type NewMutex Mutex
1609
1610// PrintableMutex has no methods bound to it, but the method set contains
1611// the methods Lock and Unlock bound to its anonymous field Mutex.
1612type PrintableMutex struct {
1613 Mutex;
1614}
1615</pre>
1616
1617<p>
1618A type declaration may be used to define a different boolean, numeric, or string
1619type and attach methods to it:
1620</p>
1621
1622<pre>
1623type TimeZone int
1624
1625const (
1626 EST TimeZone = -(5 + iota);
1627 CST;
1628 MST;
1629 PST;
1630)
1631
1632func (tz TimeZone) String() string {
1633 return fmt.Sprintf("GMT+%dh", tz);
1634}
1635</pre>
1636
1637
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001638<h3 id="Variable_declarations">Variable declarations</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001639
1640<p>
1641A variable declaration creates a variable, binds an identifier to it and
1642gives it a type and optionally an initial value.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001643</p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001644<pre class="ebnf">
Rob Pikea9ed30f2009-02-23 19:26:07 -08001645VarDecl = "var" ( VarSpec | "(" [ VarSpecList ] ")" ) .
1646VarSpecList = VarSpec { ";" VarSpec } [ ";" ] .
Robert Griesemer4023dce2009-08-14 17:41:52 -07001647VarSpec = IdentifierList ( Type [ "=" ExpressionList ] | "=" ExpressionList ) .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001648</pre>
1649
1650<pre>
1651var i int
1652var U, V, W float
1653var k = 0
Rob Pike678625d2009-09-15 09:54:22 -07001654var x, y float = -1, -2
Rob Pikea9ed30f2009-02-23 19:26:07 -08001655var (
1656 i int;
1657 u, v, s = 2.0, 3.0, "bar"
1658)
Robert Griesemer4e56b332009-09-10 10:14:00 -07001659var re, im = complexSqrt(-1)
1660var _, found = entries[name]; // map lookup; only interested in "found"
Rob Pikea9ed30f2009-02-23 19:26:07 -08001661</pre>
1662
1663<p>
Robert Griesemeref45e642009-08-21 11:25:00 -07001664If a list of expressions is given, the variables are initialized
Rob Pike678625d2009-09-15 09:54:22 -07001665by assigning the expressions to the variables (§<a href="#Assignments">Assignments</a>)
1666in order; all expressions must be consumed and all variables initialized from them.
Robert Griesemer506c0082009-09-08 15:41:14 -07001667Otherwise, each variable is initialized to its <a href="#The_zero_value"><i>zero value</i></a>.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001668</p>
Robert Griesemeref45e642009-08-21 11:25:00 -07001669
Rob Pikea9ed30f2009-02-23 19:26:07 -08001670<p>
Robert Griesemeref45e642009-08-21 11:25:00 -07001671If the type is present, each variable is given that type.
1672Otherwise, the types are deduced from the assignment
1673of the expression list.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001674</p>
Robert Griesemeref45e642009-08-21 11:25:00 -07001675
Rob Pikea9ed30f2009-02-23 19:26:07 -08001676<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07001677If the type is absent and the corresponding expression evaluates to an
1678untyped <a href="#Constants">constant</a>, the type of the declared variable
1679is <code>bool</code>, <code>int</code>, <code>float</code>, or <code>string</code>
1680respectively, depending on whether the value is a boolean, integer,
1681floating-point, or string constant:
Rob Pikea9ed30f2009-02-23 19:26:07 -08001682</p>
1683
1684<pre>
Robert Griesemer19b1d352009-09-24 19:36:48 -07001685var b = true // t has type bool
Rob Pikea9ed30f2009-02-23 19:26:07 -08001686var i = 0 // i has type int
Robert Griesemer19b1d352009-09-24 19:36:48 -07001687var f = 3.0 // f has type float
Robert Griesemeref45e642009-08-21 11:25:00 -07001688var s = "OMDB" // s has type string
Rob Pikea9ed30f2009-02-23 19:26:07 -08001689</pre>
1690
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001691<h3 id="Short_variable_declarations">Short variable declarations</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001692
Robert Griesemer997851e2009-09-25 15:36:25 -07001693<p>
Robert Griesemeref45e642009-08-21 11:25:00 -07001694A <i>short variable declaration</i> uses the syntax:
Robert Griesemer997851e2009-09-25 15:36:25 -07001695</p>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001696
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001697<pre class="ebnf">
Robert Griesemere1b8cb82009-07-16 20:31:41 -07001698ShortVarDecl = IdentifierList ":=" ExpressionList .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001699</pre>
1700
Robert Griesemer997851e2009-09-25 15:36:25 -07001701<p>
Robert Griesemeref45e642009-08-21 11:25:00 -07001702It is a shorthand for a regular variable declaration with
1703initializer expressions but no types:
Robert Griesemer997851e2009-09-25 15:36:25 -07001704</p>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001705
1706<pre class="grammar">
1707"var" IdentifierList = ExpressionList .
1708</pre>
1709
1710<pre>
1711i, j := 0, 10;
1712f := func() int { return 7; }
Rob Pikef5387602009-03-30 16:08:41 -07001713ch := make(chan int);
Russ Cox5958dd62009-03-04 17:19:21 -08001714r, w := os.Pipe(fd); // os.Pipe() returns two values
Rob Pike678625d2009-09-15 09:54:22 -07001715_, y, _ := coord(p); // coord() returns three values; only interested in y coordinate
Rob Pikea9ed30f2009-02-23 19:26:07 -08001716</pre>
1717
1718<p>
Robert Griesemeref45e642009-08-21 11:25:00 -07001719Unlike regular variable declarations, a short variable declaration may redeclare variables provided they
Rob Pike2a1683a2009-04-19 20:04:15 -07001720were originally declared in the same block with the same type, and at
Robert Griesemer4e56b332009-09-10 10:14:00 -07001721least one of the non-<a href="#Blank_identifier">blank</a> variables is new. As a consequence, redeclaration
Rob Pike2a1683a2009-04-19 20:04:15 -07001722can only appear in a multi-variable short declaration.
1723Redeclaration does not introduce a new
1724variable; it just assigns a new value to the original.
1725</p>
1726
1727<pre>
1728field1, offset := nextField(str, 0);
1729field2, offset := nextField(str, offset); // redeclares offset
1730</pre>
1731
1732<p>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001733Short variable declarations may appear only inside functions.
1734In some contexts such as the initializers for <code>if</code>,
1735<code>for</code>, or <code>switch</code> statements,
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001736they can be used to declare local temporary variables (§<a href="#Statements">Statements</a>).
Rob Pikea9ed30f2009-02-23 19:26:07 -08001737</p>
1738
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001739<h3 id="Function_declarations">Function declarations</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001740
1741<p>
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001742A function declaration binds an identifier to a function (§<a href="#Function_types">Function types</a>).
Rob Pikea9ed30f2009-02-23 19:26:07 -08001743</p>
1744
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001745<pre class="ebnf">
Robert Griesemer0a162a12009-08-19 16:44:04 -07001746FunctionDecl = "func" identifier Signature [ Body ] .
1747Body = Block.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001748</pre>
1749
Robert Griesemer4023dce2009-08-14 17:41:52 -07001750<p>
1751A function declaration may omit the body. Such a declaration provides the
1752signature for a function implemented outside Go, such as an assembly routine.
1753</p>
1754
Rob Pikea9ed30f2009-02-23 19:26:07 -08001755<pre>
1756func min(x int, y int) int {
1757 if x &lt; y {
1758 return x;
1759 }
1760 return y;
1761}
Robert Griesemer4023dce2009-08-14 17:41:52 -07001762
1763func flushICache(begin, end uintptr) // implemented externally
Rob Pikea9ed30f2009-02-23 19:26:07 -08001764</pre>
1765
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001766<h3 id="Method_declarations">Method declarations</h3>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001767
1768<p>
1769A method declaration binds an identifier to a method,
1770which is a function with a <i>receiver</i>.
1771</p>
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001772<pre class="ebnf">
Robert Griesemerda961882009-09-17 11:01:50 -07001773MethodDecl = "func" Receiver MethodName Signature [ Body ] .
Robert Griesemerfc61b772009-09-28 14:10:20 -07001774Receiver = "(" [ identifier ] [ "*" ] BaseTypeName ")" .
Robert Griesemerfc61b772009-09-28 14:10:20 -07001775BaseTypeName = identifier .
Rob Pikea9ed30f2009-02-23 19:26:07 -08001776</pre>
1777
1778<p>
Robert Griesemer56809d02009-05-20 11:02:48 -07001779The receiver type must be of the form <code>T</code> or <code>*T</code> where
1780<code>T</code> is a type name. <code>T</code> is called the
1781<i>receiver base type</i> or just <i>base type</i>.
1782The base type must not be a pointer or interface type and must be
Stephen Ma5db1d382009-09-02 20:09:25 -07001783declared in the same package as the method.
Rob Pikea9ed30f2009-02-23 19:26:07 -08001784The method is said to be <i>bound</i> to the base type
1785and is visible only within selectors for that type
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001786<a href="#Type_declarations">Type declarations</a>, §<a href="#Selectors">Selectors</a>).
Rob Pikea9ed30f2009-02-23 19:26:07 -08001787</p>
1788
1789<p>
Rob Pikea9ed30f2009-02-23 19:26:07 -08001790Given type <code>Point</code>, the declarations
1791</p>
1792
1793<pre>
1794func (p *Point) Length() float {
1795 return Math.sqrt(p.x * p.x + p.y * p.y);
1796}
1797
1798func (p *Point) Scale(factor float) {
1799 p.x = p.x * factor;
1800 p.y = p.y * factor;
1801}
1802</pre>
1803
1804<p>
Rob Pike678625d2009-09-15 09:54:22 -07001805bind the methods <code>Length</code> and <code>Scale</code>,
1806with receiver type <code>*Point</code>,
Rob Pikea9ed30f2009-02-23 19:26:07 -08001807to the base type <code>Point</code>.
1808</p>
1809
1810<p>
Robert Griesemer56809d02009-05-20 11:02:48 -07001811If the receiver's value is not referenced inside the the body of the method,
Rob Pikea9ed30f2009-02-23 19:26:07 -08001812its identifier may be omitted in the declaration. The same applies in
1813general to parameters of functions and methods.
1814</p>
1815
1816<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08001817The type of a method is the type of a function with the receiver as first
1818argument. For instance, the method <code>Scale</code> has type
1819</p>
1820
1821<pre>
1822(p *Point, factor float)
1823</pre>
1824
1825<p>
1826However, a function declared this way is not a method.
1827</p>
1828
Rob Pikea9ed30f2009-02-23 19:26:07 -08001829
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001830<h2 id="Expressions">Expressions</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001831
Rob Pikedf3183f2009-02-26 16:37:23 -08001832<p>
1833An expression specifies the computation of a value by applying
Robert Griesemer19b1d352009-09-24 19:36:48 -07001834operators and functions to operands.
Rob Pikedf3183f2009-02-26 16:37:23 -08001835</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001836
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001837<h3 id="Operands">Operands</h3>
Robert Griesemerad711102008-09-11 17:48:20 -07001838
Robert Griesemer997851e2009-09-25 15:36:25 -07001839<p>
Robert Griesemerad711102008-09-11 17:48:20 -07001840Operands denote the elementary values in an expression.
Robert Griesemer997851e2009-09-25 15:36:25 -07001841</p>
Robert Griesemerad711102008-09-11 17:48:20 -07001842
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001843<pre class="ebnf">
Rob Pike01cadde2009-09-15 15:56:44 -07001844Operand = Literal | QualifiedIdent | MethodExpr | "(" Expression ")" .
Rob Pikedf3183f2009-02-26 16:37:23 -08001845Literal = BasicLit | CompositeLit | FunctionLit .
1846BasicLit = int_lit | float_lit | char_lit | StringLit .
Robert Griesemerc2d55862009-02-19 16:49:10 -08001847</pre>
Robert Griesemerad711102008-09-11 17:48:20 -07001848
1849
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001850<h3 id="Qualified_identifiers">Qualified identifiers</h3>
Robert Griesemerad711102008-09-11 17:48:20 -07001851
Robert Griesemerc2d55862009-02-19 16:49:10 -08001852<p>
Robert Griesemer4e56b332009-09-10 10:14:00 -07001853A qualified identifier is a non-<a href="#Blank_identifier">blank</a> identifier qualified by a package name prefix.
Rob Pikedf3183f2009-02-26 16:37:23 -08001854</p>
Robert Griesemer337af312008-11-17 18:11:36 -08001855
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001856<pre class="ebnf">
Russ Cox16b95ba2009-08-20 10:22:52 -07001857QualifiedIdent = [ PackageName "." ] identifier .
Robert Griesemerc2d55862009-02-19 16:49:10 -08001858</pre>
Robert Griesemerad711102008-09-11 17:48:20 -07001859
Rob Pikedf3183f2009-02-26 16:37:23 -08001860<p>
Robert Griesemeraeaab592009-08-31 17:30:55 -07001861A qualified identifier accesses an identifier in a separate package.
1862The identifier must be <a href="#Exported_identifiers">exported</a> by that
1863package, which means that it must begin with a Unicode upper case letter.
Rob Pikedf3183f2009-02-26 16:37:23 -08001864</p>
Rob Pikedf3183f2009-02-26 16:37:23 -08001865
1866<pre>
Rob Pike678625d2009-09-15 09:54:22 -07001867math.Sin
Rob Pikedf3183f2009-02-26 16:37:23 -08001868</pre>
Robert Griesemerad711102008-09-11 17:48:20 -07001869
Robert Griesemer4e56b332009-09-10 10:14:00 -07001870<p>
Robert Griesemer90cc4a52009-10-22 09:41:38 -07001871<span class="alert">TODO: Unify this section with Selectors - it's the same syntax.</span>
Robert Griesemer4e56b332009-09-10 10:14:00 -07001872</p>
1873
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001874<h3 id="Composite_literals">Composite literals</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001875
Rob Pikedf3183f2009-02-26 16:37:23 -08001876<p>
1877Composite literals construct values for structs, arrays, slices, and maps
1878and create a new value each time they are evaluated.
1879They consist of the type of the value
Robert Griesemer838cf122009-05-22 10:25:06 -07001880followed by a brace-bound list of composite elements. An element may be
1881a single expression or a key-value pair.
Rob Pikedf3183f2009-02-26 16:37:23 -08001882</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001883
Robert Griesemerf7ac31362009-07-10 16:06:40 -07001884<pre class="ebnf">
Robert Griesemer838cf122009-05-22 10:25:06 -07001885CompositeLit = LiteralType "{" [ ElementList ] "}" .
Rob Pikedf3183f2009-02-26 16:37:23 -08001886LiteralType = StructType | ArrayType | "[" "..." "]" ElementType |
Robert Griesemer164a7bc2009-09-30 12:00:25 -07001887 SliceType | MapType | TypeName | "(" LiteralType ")" .
Robert Griesemer838cf122009-05-22 10:25:06 -07001888ElementList = Element { "," Element } [ "," ] .
1889Element = [ Key ":" ] Value .
Rob Pike678625d2009-09-15 09:54:22 -07001890Key = FieldName | Index .
1891FieldName = identifier .
Robert Griesemer838cf122009-05-22 10:25:06 -07001892Value = Expression .
Robert Griesemerc2d55862009-02-19 16:49:10 -08001893</pre>
Robert Griesemer0976e342008-09-03 13:37:44 -07001894
Rob Pikedf3183f2009-02-26 16:37:23 -08001895<p>
Robert Griesemer838cf122009-05-22 10:25:06 -07001896The LiteralType must be a struct, array, slice, or map type
1897(the grammar enforces this constraint except when the type is given
1898as a TypeName).
Robert Griesemer326ef132009-09-28 19:21:15 -07001899The types of the expressions must be <a href="#Assignment_compatibility">assignment compatible</a> with
Russ Cox7a5e97b2009-03-03 15:40:30 -08001900the respective field, element, and key types of the LiteralType;
1901there is no additional conversion.
Robert Griesemer838cf122009-05-22 10:25:06 -07001902The key is interpreted as a field name for struct literals,
Rob Pike678625d2009-09-15 09:54:22 -07001903an index expression for array and slice literals, and a key for map literals.
Robert Griesemer838cf122009-05-22 10:25:06 -07001904For map literals, all elements must have a key. It is an error
1905to specify multiple elements with the same field name or
1906constant key value.
Rob Pikedf3183f2009-02-26 16:37:23 -08001907</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001908
Robert Griesemer838cf122009-05-22 10:25:06 -07001909<p>
1910For struct literals the following rules apply:
Robert Griesemercfe92112009-06-18 13:29:40 -07001911</p>
Robert Griesemer838cf122009-05-22 10:25:06 -07001912<ul>
Rob Pike678625d2009-09-15 09:54:22 -07001913 <li>A literal that does not contain any keys must
Robert Griesemer838cf122009-05-22 10:25:06 -07001914 list an element for each struct field in the
1915 order in which the fields are declared.
1916 </li>
1917 <li>If any element has a key, every element must have a key.
1918 </li>
Rob Pike678625d2009-09-15 09:54:22 -07001919 <li>A literal that contains keys does not need to
Robert Griesemer838cf122009-05-22 10:25:06 -07001920 have an element for each struct field. Omitted fields
1921 get the zero value for that field.
1922 </li>
1923 <li>A literal may omit the element list; such a literal evaluates
1924 to the zero value for its type.
1925 </li>
1926 <li>It is an error to specify an element for a non-exported
1927 field of a struct belonging to a different package.
1928 </li>
1929</ul>
Robert Griesemer838cf122009-05-22 10:25:06 -07001930
1931<p>
1932Given the declarations
1933</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08001934<pre>
Robert Griesemer838cf122009-05-22 10:25:06 -07001935type Point struct { x, y, z float }
1936type Line struct { p, q Point }
Robert Griesemerc2d55862009-02-19 16:49:10 -08001937</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001938
Rob Pikedf3183f2009-02-26 16:37:23 -08001939<p>
1940one may write
1941</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001942
Robert Griesemerc2d55862009-02-19 16:49:10 -08001943<pre>
Robert Griesemer838cf122009-05-22 10:25:06 -07001944origin := Point{}; // zero value for Point
1945line := Line{origin, Point{y: -4, z: 12.3}}; // zero value for line.q.x
Rob Pike37ab8382009-03-18 22:58:36 -07001946</pre>
1947
Robert Griesemercfe92112009-06-18 13:29:40 -07001948<p>
1949For array and slice literals the following rules apply:
1950</p>
Robert Griesemer838cf122009-05-22 10:25:06 -07001951<ul>
1952 <li>Each element has an associated integer index marking
1953 its position in the array.
1954 </li>
1955 <li>An element with a key uses the key as its index; the
1956 key must be a constant integer expression.
1957 </li>
1958 <li>An element without a key uses the previous element's index plus one.
1959 If the first element has no key, its index is zero.
1960 </li>
1961</ul>
Robert Griesemer838cf122009-05-22 10:25:06 -07001962
Rob Pike37ab8382009-03-18 22:58:36 -07001963<p>
Russ Cox7c4f7cc2009-08-20 11:11:03 -07001964Taking the address of a composite literal (§<a href="#Address_operators">Address operators</a>)
Rob Pike2a5af742009-03-20 17:03:48 -07001965generates a unique pointer to an instance of the literal's value.
Rob Pike37ab8382009-03-18 22:58:36 -07001966</p>
Rob Pike37ab8382009-03-18 22:58:36 -07001967<pre>
Robert Griesemer838cf122009-05-22 10:25:06 -07001968var pointer *Point = &amp;Point{y: 1000};
Robert Griesemerc2d55862009-02-19 16:49:10 -08001969</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001970
Rob Pikedf3183f2009-02-26 16:37:23 -08001971<p>
Robert Griesemera3294712009-01-05 11:17:26 -08001972The length of an array literal is the length specified in the LiteralType.
1973If fewer elements than the length are provided in the literal, the missing
Rob Pike8f2330d2009-02-25 16:20:44 -08001974elements are set to the zero value for the array element type.
Robert Griesemer838cf122009-05-22 10:25:06 -07001975It is an error to provide elements with index values outside the index range
1976of the array. The notation <code>...</code> specifies an array length equal
1977to the maximum element index plus one.
Rob Pikedf3183f2009-02-26 16:37:23 -08001978</p>
Robert Griesemerb90b2132008-09-19 15:49:55 -07001979
Robert Griesemerc2d55862009-02-19 16:49:10 -08001980<pre>
Rob Pike426335f2009-03-02 17:52:52 -08001981buffer := [10]string{}; // len(buffer) == 10
Robert Griesemer838cf122009-05-22 10:25:06 -07001982intSet := [6]int{1, 2, 3, 5}; // len(intSet) == 6
Rob Pike426335f2009-03-02 17:52:52 -08001983days := [...]string{"Sat", "Sun"}; // len(days) == 2
Robert Griesemerc2d55862009-02-19 16:49:10 -08001984</pre>
Robert Griesemer91bbd642009-01-07 09:31:35 -08001985
Rob Pikedf3183f2009-02-26 16:37:23 -08001986<p>
1987A slice literal describes the entire underlying array literal.
Rob Pike678625d2009-09-15 09:54:22 -07001988Thus, the length and capacity of a slice literal are the maximum
Robert Griesemer838cf122009-05-22 10:25:06 -07001989element index plus one. A slice literal has the form
Rob Pikedf3183f2009-02-26 16:37:23 -08001990</p>
Robert Griesemer91bbd642009-01-07 09:31:35 -08001991
Robert Griesemerc2d55862009-02-19 16:49:10 -08001992<pre>
Rob Pike426335f2009-03-02 17:52:52 -08001993[]T{x1, x2, ... xn}
Robert Griesemerc2d55862009-02-19 16:49:10 -08001994</pre>
Robert Griesemer91bbd642009-01-07 09:31:35 -08001995
Rob Pikedf3183f2009-02-26 16:37:23 -08001996<p>
1997and is a shortcut for a slice operation applied to an array literal:
1998</p>
Robert Griesemer91bbd642009-01-07 09:31:35 -08001999
Robert Griesemerc2d55862009-02-19 16:49:10 -08002000<pre>
Rob Pike426335f2009-03-02 17:52:52 -08002001[n]T{x1, x2, ... xn}[0 : n]
Robert Griesemerc2d55862009-02-19 16:49:10 -08002002</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002003
Rob Pikedf3183f2009-02-26 16:37:23 -08002004<p>
Russ Cox7a5e97b2009-03-03 15:40:30 -08002005A parsing ambiguity arises when a composite literal using the
2006TypeName form of the LiteralType appears in the condition of an
2007"if", "for", or "switch" statement, because the braces surrounding
2008the expressions in the literal are confused with those introducing
2009a block of statements. To resolve the ambiguity in this rare case,
2010the composite literal must appear within
2011parentheses.
2012</p>
2013
2014<pre>
2015if x == (T{a,b,c}[i]) { ... }
2016if (x == T{a,b,c}[i]) { ... }
2017</pre>
2018
Robert Griesemer838cf122009-05-22 10:25:06 -07002019<p>
2020Examples of valid array, slice, and map literals:
2021</p>
2022
2023<pre>
2024// list of prime numbers
2025primes := []int{2, 3, 5, 7, 9, 11, 13, 17, 19, 991};
2026
2027// vowels[ch] is true if ch is a vowel
2028vowels := [128]bool{'a': true, 'e': true, 'i': true, 'o': true, 'u': true, 'y': true};
2029
2030// the array [10]float{-1, 0, 0, 0, -0.1, -0.1, 0, 0, 0, -1};
2031filter := [10]float{-1, 4: -0.1, -0.1, 9: -1};
2032
2033// frequencies in Hz for equal-tempered scale (A4 = 440Hz)
2034noteFrequency := map[string]float{
2035 "C0": 16.35, "D0": 18.35, "E0": 20.60, "F0": 21.83,
2036 "G0": 24.50, "A0": 27.50, "B0": 30.87,
2037}
2038</pre>
2039
2040
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002041<h3 id="Function_literals">Function literals</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002042
Rob Pikedf3183f2009-02-26 16:37:23 -08002043<p>
2044A function literal represents an anonymous function.
2045It consists of a specification of the function type and a function body.
2046</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002047
Robert Griesemerf7ac31362009-07-10 16:06:40 -07002048<pre class="ebnf">
Robert Griesemer0a162a12009-08-19 16:44:04 -07002049FunctionLit = FunctionType Body .
Robert Griesemerc2d55862009-02-19 16:49:10 -08002050</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002051
Robert Griesemerc2d55862009-02-19 16:49:10 -08002052<pre>
Rob Pikedf3183f2009-02-26 16:37:23 -08002053func (a, b int, z float) bool { return a*b &lt; int(z) }
Robert Griesemerc2d55862009-02-19 16:49:10 -08002054</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002055
Rob Pikedf3183f2009-02-26 16:37:23 -08002056<p>
2057A function literal can be assigned to a variable or invoked directly.
2058</p>
Robert Griesemer7231ceb2008-09-08 15:01:04 -07002059
Robert Griesemerc2d55862009-02-19 16:49:10 -08002060<pre>
Rob Pikedf3183f2009-02-26 16:37:23 -08002061f := func(x, y int) int { return x + y }
2062func(ch chan int) { ch &lt;- ACK } (reply_chan)
Robert Griesemerc2d55862009-02-19 16:49:10 -08002063</pre>
Robert Griesemer7231ceb2008-09-08 15:01:04 -07002064
Rob Pikedf3183f2009-02-26 16:37:23 -08002065<p>
2066Function literals are <i>closures</i>: they may refer to variables
Robert Griesemerd8a764c2009-02-06 17:01:10 -08002067defined in a surrounding function. Those variables are then shared between
2068the surrounding function and the function literal, and they survive as long
Rob Pikedf3183f2009-02-26 16:37:23 -08002069as they are accessible.
2070</p>
Robert Griesemer7231ceb2008-09-08 15:01:04 -07002071
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002072
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002073<h3 id="Primary_expressions">Primary expressions</h3>
Russ Cox5958dd62009-03-04 17:19:21 -08002074
Robert Griesemer164a7bc2009-09-30 12:00:25 -07002075<p>
2076Primary expressions are the operands for unary and binary expressions.
2077</p>
2078
Robert Griesemerf7ac31362009-07-10 16:06:40 -07002079<pre class="ebnf">
Robert Griesemerc2d55862009-02-19 16:49:10 -08002080PrimaryExpr =
2081 Operand |
Robert Griesemer5eb36242009-09-16 11:05:14 -07002082 Conversion |
Robert Griesemer164a7bc2009-09-30 12:00:25 -07002083 BuiltinCall |
Robert Griesemerc2d55862009-02-19 16:49:10 -08002084 PrimaryExpr Selector |
2085 PrimaryExpr Index |
2086 PrimaryExpr Slice |
Russ Cox5958dd62009-03-04 17:19:21 -08002087 PrimaryExpr TypeAssertion |
Robert Griesemerc2d55862009-02-19 16:49:10 -08002088 PrimaryExpr Call .
Robert Griesemer57b34612008-10-10 12:45:44 -07002089
Russ Cox5958dd62009-03-04 17:19:21 -08002090Selector = "." identifier .
2091Index = "[" Expression "]" .
2092Slice = "[" Expression ":" Expression "]" .
2093TypeAssertion = "." "(" Type ")" .
2094Call = "(" [ ExpressionList ] ")" .
Robert Griesemerc2d55862009-02-19 16:49:10 -08002095</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002096
2097
Robert Griesemerc2d55862009-02-19 16:49:10 -08002098<pre>
2099x
21002
2101(s + ".txt")
2102f(3.1415, true)
Rob Pike426335f2009-03-02 17:52:52 -08002103Point{1, 2}
Robert Griesemerc2d55862009-02-19 16:49:10 -08002104m["foo"]
2105s[i : j + 1]
2106obj.color
2107Math.sin
2108f.p[i].x()
2109</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002110
2111
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002112<h3 id="Selectors">Selectors</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002113
Rob Pikedf3183f2009-02-26 16:37:23 -08002114<p>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002115A primary expression of the form
Rob Pikedf3183f2009-02-26 16:37:23 -08002116</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002117
Robert Griesemerc2d55862009-02-19 16:49:10 -08002118<pre>
2119x.f
2120</pre>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002121
Robert Griesemerc2d55862009-02-19 16:49:10 -08002122<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002123denotes the field or method <code>f</code> of the value denoted by <code>x</code>
2124(or of <code>*x</code> if
2125<code>x</code> is of pointer type). The identifier <code>f</code>
2126is called the (field or method)
Robert Griesemer4e56b332009-09-10 10:14:00 -07002127<i>selector</i>; it must not be the <a href="#Blank_identifier">blank identifier</a>.
Rob Pikedf3183f2009-02-26 16:37:23 -08002128The type of the expression is the type of <code>f</code>.
2129</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002130<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002131A selector <code>f</code> may denote a field or method <code>f</code> of
2132a type <code>T</code>, or it may refer
2133to a field or method <code>f</code> of a nested anonymous field of
2134<code>T</code>.
2135The number of anonymous fields traversed
2136to reach <code>f</code> is called its <i>depth</i> in <code>T</code>.
2137The depth of a field or method <code>f</code>
2138declared in <code>T</code> is zero.
2139The depth of a field or method <code>f</code> declared in
2140an anonymous field <code>A</code> in <code>T</code> is the
2141depth of <code>f</code> in <code>A</code> plus one.
2142</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002143<p>
Robert Griesemer071c91b2008-10-23 12:04:45 -07002144The following rules apply to selectors:
Rob Pikedf3183f2009-02-26 16:37:23 -08002145</p>
2146<ol>
2147<li>
2148For a value <code>x</code> of type <code>T</code> or <code>*T</code>
2149where <code>T</code> is not an interface type,
2150<code>x.f</code> denotes the field or method at the shallowest depth
2151in <code>T</code> where there
2152is such an <code>f</code>.
2153If there is not exactly one <code>f</code> with shallowest depth, the selector
Robert Griesemer071c91b2008-10-23 12:04:45 -07002154expression is illegal.
Rob Pikedf3183f2009-02-26 16:37:23 -08002155</li>
2156<li>
2157For a variable <code>x</code> of type <code>I</code> or <code>*I</code>
2158where <code>I</code> is an interface type,
2159<code>x.f</code> denotes the actual method with name <code>f</code> of the value assigned
2160to <code>x</code> if there is such a method.
2161If no value or <code>nil</code> was assigned to <code>x</code>, <code>x.f</code> is illegal.
2162</li>
2163<li>
2164In all other cases, <code>x.f</code> is illegal.
2165</ol>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002166<p>
Rob Pike678625d2009-09-15 09:54:22 -07002167Selectors automatically dereference pointers.
Rob Pikedf3183f2009-02-26 16:37:23 -08002168If <code>x</code> is of pointer type, <code>x.y</code>
2169is shorthand for <code>(*x).y</code>; if <code>y</code>
2170is also of pointer type, <code>x.y.z</code> is shorthand
2171for <code>(*(*x).y).z</code>, and so on.
2172If <code>*x</code> is of pointer type, dereferencing
2173must be explicit;
2174only one level of automatic dereferencing is provided.
2175For an <code>x</code> of type <code>T</code> containing an
2176anonymous field declared as <code>*A</code>,
2177<code>x.f</code> is a shortcut for <code>(*x.A).f</code>.
2178</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002179<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002180For example, given the declarations:
2181</p>
Robert Griesemer071c91b2008-10-23 12:04:45 -07002182
Robert Griesemerc2d55862009-02-19 16:49:10 -08002183<pre>
2184type T0 struct {
2185 x int;
2186}
Robert Griesemer071c91b2008-10-23 12:04:45 -07002187
Robert Griesemerc2d55862009-02-19 16:49:10 -08002188func (recv *T0) M0()
Robert Griesemer071c91b2008-10-23 12:04:45 -07002189
Robert Griesemerc2d55862009-02-19 16:49:10 -08002190type T1 struct {
2191 y int;
2192}
Robert Griesemer071c91b2008-10-23 12:04:45 -07002193
Robert Griesemerc2d55862009-02-19 16:49:10 -08002194func (recv T1) M1()
Robert Griesemer071c91b2008-10-23 12:04:45 -07002195
Robert Griesemerc2d55862009-02-19 16:49:10 -08002196type T2 struct {
2197 z int;
2198 T1;
2199 *T0;
2200}
Robert Griesemer071c91b2008-10-23 12:04:45 -07002201
Robert Griesemerc2d55862009-02-19 16:49:10 -08002202func (recv *T2) M2()
Robert Griesemer071c91b2008-10-23 12:04:45 -07002203
Robert Griesemerc2d55862009-02-19 16:49:10 -08002204var p *T2; // with p != nil and p.T1 != nil
2205</pre>
Robert Griesemer071c91b2008-10-23 12:04:45 -07002206
Rob Pikedf3183f2009-02-26 16:37:23 -08002207<p>
2208one may write:
2209</p>
Robert Griesemer071c91b2008-10-23 12:04:45 -07002210
Robert Griesemerc2d55862009-02-19 16:49:10 -08002211<pre>
2212p.z // (*p).z
2213p.y // ((*p).T1).y
2214p.x // (*(*p).T0).x
Robert Griesemer071c91b2008-10-23 12:04:45 -07002215
Robert Griesemerc2d55862009-02-19 16:49:10 -08002216p.M2 // (*p).M2
2217p.M1 // ((*p).T1).M1
2218p.M0 // ((*p).T0).M0
2219</pre>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002220
2221
Robert Griesemer90cc4a52009-10-22 09:41:38 -07002222<span class="alert">
Robert Griesemer071c91b2008-10-23 12:04:45 -07002223TODO: Specify what happens to receivers.
Robert Griesemer90cc4a52009-10-22 09:41:38 -07002224</span>
Robert Griesemer7abfcd92008-10-07 17:14:30 -07002225
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002226
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002227<h3 id="Indexes">Indexes</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002228
Rob Pikedf3183f2009-02-26 16:37:23 -08002229<p>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002230A primary expression of the form
Rob Pikedf3183f2009-02-26 16:37:23 -08002231</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002232
Robert Griesemerc2d55862009-02-19 16:49:10 -08002233<pre>
2234a[x]
2235</pre>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002236
Rob Pike4501d342009-02-19 17:31:36 -08002237<p>
Rob Pike678625d2009-09-15 09:54:22 -07002238denotes the element of the array, slice, string or map <code>a</code> indexed by <code>x</code>.
Rob Pikedf3183f2009-02-26 16:37:23 -08002239The value <code>x</code> is called the
Rob Pike678625d2009-09-15 09:54:22 -07002240<i>index</i> or <i>map key</i>, respectively. The following
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002241rules apply:
Rob Pike4501d342009-02-19 17:31:36 -08002242</p>
Russ Coxeaa92e02009-06-25 14:43:55 -07002243
Robert Griesemerc2d55862009-02-19 16:49:10 -08002244<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002245For <code>a</code> of type <code>A</code> or <code>*A</code>
Robert Griesemer506c0082009-09-08 15:41:14 -07002246where <code>A</code> is an <a href="#Array_types">array type</a>,
2247or for <code>a</code> of type <code>S</code> where <code>S</code> is a <a href="#Slice_types">slice type</a>:
Rob Pike4501d342009-02-19 17:31:36 -08002248</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002249<ul>
Rob Pikedf3183f2009-02-26 16:37:23 -08002250 <li><code>x</code> must be an integer value and <code>0 &lt;= x &lt; len(a)</code>
2251 <li><code>a[x]</code> is the array element at index <code>x</code> and the type of
2252 <code>a[x]</code> is the element type of <code>A</code>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002253</ul>
Russ Coxeaa92e02009-06-25 14:43:55 -07002254
Robert Griesemerc2d55862009-02-19 16:49:10 -08002255<p>
Russ Coxeaa92e02009-06-25 14:43:55 -07002256For <code>a</code> of type <code>T</code>
Robert Griesemer19b1d352009-09-24 19:36:48 -07002257where <code>T</code> is a <a href="#String_types">string type</a>:
Russ Coxeaa92e02009-06-25 14:43:55 -07002258</p>
2259<ul>
2260 <li><code>x</code> must be an integer value and <code>0 &lt;= x &lt; len(a)</code>
2261 <li><code>a[x]</code> is the byte at index <code>x</code> and the type of
2262 <code>a[x]</code> is <code>byte</code>
2263 <li><code>a[x]</code> may not be assigned to
2264</ul>
2265
2266<p>
2267For <code>a</code> of type <code>M</code>
Robert Griesemer506c0082009-09-08 15:41:14 -07002268where <code>M</code> is a <a href="#Map_types">map type</a>:
Rob Pike4501d342009-02-19 17:31:36 -08002269</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002270<ul>
Robert Griesemer533dfd62009-05-13 16:56:00 -07002271 <li><code>x</code>'s type must be compatible with the key type of <code>M</code>
Rob Pikedf3183f2009-02-26 16:37:23 -08002272 and the map must contain an entry with key <code>x</code> (but see special forms below)
2273 <li><code>a[x]</code> is the map value with key <code>x</code>
2274 and the type of <code>a[x]</code> is the value type of <code>M</code>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002275</ul>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07002276
Robert Griesemerc2d55862009-02-19 16:49:10 -08002277<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002278Otherwise <code>a[x]</code> is illegal. If the index or key is out of range evaluating
2279an otherwise legal index expression, a run-time exception occurs.
2280</p>
Robert Griesemer7abfcd92008-10-07 17:14:30 -07002281
Rob Pikedf3183f2009-02-26 16:37:23 -08002282<p>
2283However, if an index expression on a map <code>a</code> of type <code>map[K] V</code>
Rob Piked5537072009-08-22 00:04:04 -07002284is used in an assignment or initialization of the form
Rob Pikedf3183f2009-02-26 16:37:23 -08002285</p>
2286
2287<pre>
2288r, ok = a[x]
2289r, ok := a[x]
Rob Piked5537072009-08-22 00:04:04 -07002290var r, ok = a[x]
Rob Pikedf3183f2009-02-26 16:37:23 -08002291</pre>
2292
2293<p>
2294the result of the index expression is a pair of values with types
2295<code>(K, bool)</code>.
2296If the key is present in the map,
2297the expression returns the pair <code>(a[x], true)</code>;
2298otherwise it returns <code>(Z, false)</code> where <code>Z</code> is
Robert Griesemer506c0082009-09-08 15:41:14 -07002299the <a href="#The_zero_value">zero value</a> for <code>V</code>.
Rob Pikedf3183f2009-02-26 16:37:23 -08002300No run-time exception occurs in this case.
2301The index expression in this construct thus acts like a function call
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002302returning a value and a boolean indicating success. (§<a href="#Assignments">Assignments</a>)
Rob Pikedf3183f2009-02-26 16:37:23 -08002303</p>
2304
2305<p>
2306Similarly, if an assignment to a map has the special form
2307</p>
2308
2309<pre>
2310a[x] = r, ok
2311</pre>
2312
2313<p>
2314and boolean <code>ok</code> has the value <code>false</code>,
2315the entry for key <code>x</code> is deleted from the map; if
2316<code>ok</code> is <code>true</code>, the construct acts like
2317a regular assignment to an element of the map.
2318</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002319
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002320<h3 id="Slices">Slices</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002321
Rob Pikedf3183f2009-02-26 16:37:23 -08002322<p>
Rob Pike8f2330d2009-02-25 16:20:44 -08002323Strings, arrays, and slices can be <i>sliced</i> to construct substrings or descriptors
Robert Griesemer633957b2009-01-06 13:23:20 -08002324of subarrays. The index expressions in the slice select which elements appear
2325in the result. The result has indexes starting at 0 and length equal to the
Rob Pikedf3183f2009-02-26 16:37:23 -08002326difference in the index values in the slice. After slicing the array <code>a</code>
2327</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002328
Robert Griesemerc2d55862009-02-19 16:49:10 -08002329<pre>
Rob Pike426335f2009-03-02 17:52:52 -08002330a := [4]int{1, 2, 3, 4};
Robert Griesemerc2d55862009-02-19 16:49:10 -08002331s := a[1:3];
2332</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002333
Rob Pikedf3183f2009-02-26 16:37:23 -08002334<p>
Russ Cox5958dd62009-03-04 17:19:21 -08002335the slice <code>s</code> has type <code>[]int</code>, length 2, capacity 3, and elements
Rob Pikedf3183f2009-02-26 16:37:23 -08002336</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002337
Robert Griesemerc2d55862009-02-19 16:49:10 -08002338<pre>
2339s[0] == 2
2340s[1] == 3
2341</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002342
Rob Pikedf3183f2009-02-26 16:37:23 -08002343<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07002344The slice length must not be negative.
Russ Cox5958dd62009-03-04 17:19:21 -08002345For arrays or strings, the indexes
Rob Pike811dd252009-03-04 20:39:39 -08002346<code>lo</code> and <code>hi</code> must satisfy
23470 &lt;= <code>lo</code> &lt;= <code>hi</code> &lt;= length;
Russ Cox5958dd62009-03-04 17:19:21 -08002348for slices, the upper bound is the capacity rather than the length.
Robert Griesemer19b1d352009-09-24 19:36:48 -07002349</p>
2350
Robert Griesemerc2d55862009-02-19 16:49:10 -08002351<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07002352If the sliced operand is a string or slice, the result of the slice operation
2353is a string or slice of the same type.
2354If the sliced operand is an array, the result of the slice operation is a slice
2355with the same element type as the array.
Rob Pikedf3183f2009-02-26 16:37:23 -08002356</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002357
2358
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002359<h3 id="Type_assertions">Type assertions</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002360
Rob Pikedf3183f2009-02-26 16:37:23 -08002361<p>
2362For an expression <code>x</code> and a type <code>T</code>, the primary expression
2363</p>
Robert Griesemer434c6052008-11-07 13:34:37 -08002364
Robert Griesemerc2d55862009-02-19 16:49:10 -08002365<pre>
2366x.(T)
2367</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -08002368
Robert Griesemerc2d55862009-02-19 16:49:10 -08002369<p>
Russ Coxb89a54e2009-05-20 18:16:04 -07002370asserts that <code>x</code> is not the zero interface value
2371and that the value stored in <code>x</code> is of type <code>T</code>.
Russ Cox5958dd62009-03-04 17:19:21 -08002372The notation <code>x.(T)</code> is called a <i>type assertion</i>.
2373The type of <code>x</code> must be an interface type.
Rob Pikedf3183f2009-02-26 16:37:23 -08002374</p>
2375<p>
Russ Cox5958dd62009-03-04 17:19:21 -08002376More precisely, if <code>T</code> is not an interface type, <code>x.(T)</code> asserts
Rob Pikedf3183f2009-02-26 16:37:23 -08002377that the dynamic type of <code>x</code> is identical to the type <code>T</code>
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002378<a href="#Type_identity_and_compatibility">Type identity and compatibility</a>).
Russ Cox5958dd62009-03-04 17:19:21 -08002379If <code>T</code> is an interface type, <code>x.(T)</code> asserts that the dynamic type
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002380of <code>T</code> implements the interface <code>T</code><a href="#Interface_types">Interface types</a>).
Rob Pikedf3183f2009-02-26 16:37:23 -08002381</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002382<p>
Russ Cox5958dd62009-03-04 17:19:21 -08002383If the type assertion holds, the value of the expression is the value
2384stored 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 -08002385exception occurs. In other words, even though the dynamic type of <code>x</code>
Russ Cox5958dd62009-03-04 17:19:21 -08002386is known only at run-time, the type of <code>x.(T)</code> is
Rob Pikedf3183f2009-02-26 16:37:23 -08002387known to be <code>T</code> in a correct program.
2388</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002389<p>
Rob Piked5537072009-08-22 00:04:04 -07002390If a type assertion is used in an assignment or initialization of the form
Rob Pikedf3183f2009-02-26 16:37:23 -08002391</p>
Robert Griesemer434c6052008-11-07 13:34:37 -08002392
Robert Griesemerc2d55862009-02-19 16:49:10 -08002393<pre>
2394v, ok = x.(T)
2395v, ok := x.(T)
Rob Piked5537072009-08-22 00:04:04 -07002396var v, ok = x.(T)
Robert Griesemerc2d55862009-02-19 16:49:10 -08002397</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -08002398
Robert Griesemerc2d55862009-02-19 16:49:10 -08002399<p>
Russ Cox5958dd62009-03-04 17:19:21 -08002400the result of the assertion is a pair of values with types <code>(T, bool)</code>.
2401If the assertion holds, the expression returns the pair <code>(x.(T), true)</code>;
Rob Pikedf3183f2009-02-26 16:37:23 -08002402otherwise, the expression returns <code>(Z, false)</code> where <code>Z</code>
Robert Griesemer506c0082009-09-08 15:41:14 -07002403is the <a href="#The_zero_value">zero value</a> for type <code>T</code>.
Rob Pikedf3183f2009-02-26 16:37:23 -08002404No run-time exception occurs in this case.
Russ Cox5958dd62009-03-04 17:19:21 -08002405The type assertion in this construct thus acts like a function call
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002406returning a value and a boolean indicating success. (§<a href="#Assignments">Assignments</a>)
Rob Pikedf3183f2009-02-26 16:37:23 -08002407</p>
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -07002408
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002409
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002410<h3 id="Calls">Calls</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002411
Robert Griesemerc2d55862009-02-19 16:49:10 -08002412<p>
Rob Pike96750f12009-02-27 16:47:48 -08002413Given an expression <code>f</code> of function type
2414<code>F</code>,
Rob Pikedf3183f2009-02-26 16:37:23 -08002415</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002416
Robert Griesemerc2d55862009-02-19 16:49:10 -08002417<pre>
Rob Pike96750f12009-02-27 16:47:48 -08002418f(a1, a2, ... an)
Robert Griesemerc2d55862009-02-19 16:49:10 -08002419</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002420
Robert Griesemerc2d55862009-02-19 16:49:10 -08002421<p>
Rob Pike96750f12009-02-27 16:47:48 -08002422calls <code>f</code> with arguments <code>a1, a2, ... an</code>.
2423The arguments must be single-valued expressions
Robert Griesemer326ef132009-09-28 19:21:15 -07002424<a href="#Assignment_compatibility">assignment compatible</a> with the parameter types of
Rob Pikedf3183f2009-02-26 16:37:23 -08002425<code>F</code> and are evaluated before the function is called.
2426The type of the expression is the result type
2427of <code>F</code>.
2428A method invocation is similar but the method itself
2429is specified as a selector upon a value of the receiver type for
2430the method.
2431</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002432
Robert Griesemerc2d55862009-02-19 16:49:10 -08002433<pre>
Rob Pikedf3183f2009-02-26 16:37:23 -08002434Atan2(x, y) // function call
2435var pt *Point;
2436pt.Scale(3.5) // method call with receiver pt
Robert Griesemerc2d55862009-02-19 16:49:10 -08002437</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002438
Rob Pikedf3183f2009-02-26 16:37:23 -08002439<p>
Robert Griesemer56809d02009-05-20 11:02:48 -07002440A method call <code>x.m()</code> is valid if the method set of
Rob Pike678625d2009-09-15 09:54:22 -07002441(the type of) <code>x</code> contains <code>m</code> and the
2442argument list is compatible with the parameter list of <code>m</code>.
2443If <code>x</code> is <a href="#Address_operators">addressable</a> and <code>&amp;x</code>'s method
Robert Griesemer56809d02009-05-20 11:02:48 -07002444set contains <code>m</code>, <code>x.m()</code> is shorthand
2445for <code>(&amp;x).m()</code>:
Rob Pikedf3183f2009-02-26 16:37:23 -08002446</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002447
Robert Griesemerc2d55862009-02-19 16:49:10 -08002448<pre>
Rob Pikedf3183f2009-02-26 16:37:23 -08002449var p Point;
2450p.Scale(3.5)
Robert Griesemerc2d55862009-02-19 16:49:10 -08002451</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002452
Robert Griesemerc2d55862009-02-19 16:49:10 -08002453<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002454There is no distinct method type and there are no method literals.
Rob Pikedf3183f2009-02-26 16:37:23 -08002455</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002456
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002457<h3 id="Passing_arguments_to_..._parameters">Passing arguments to <code>...</code> parameters</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002458
Robert Griesemerc2d55862009-02-19 16:49:10 -08002459<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002460When a function <code>f</code> has a <code>...</code> parameter,
2461it is always the last formal parameter. Within calls to <code>f</code>,
2462the arguments before the <code>...</code> are treated normally.
2463After those, an arbitrary number (including zero) of trailing
2464arguments may appear in the call and are bound to the <code>...</code>
2465parameter.
2466</p>
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002467
Rob Pikedf3183f2009-02-26 16:37:23 -08002468<p>
2469Within <code>f</code>, the <code>...</code> parameter has static
2470type <code>interface{}</code> (the empty interface). For each call,
2471its dynamic type is a structure whose sequential fields are the
2472trailing arguments of the call. That is, the actual arguments
2473provided for a <code>...</code> parameter are wrapped into a struct
2474that is passed to the function instead of the actual arguments.
Rob Pike8cb91842009-09-15 11:56:39 -07002475Using the <a href="#Package_unsafe">reflection</a> interface, <code>f</code> may
Rob Pikedf3183f2009-02-26 16:37:23 -08002476unpack the elements of the dynamic type to recover the actual
2477arguments.
2478</p>
2479
2480<p>
2481Given the function and call
2482</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002483<pre>
Rob Pikefb24d792009-05-08 11:21:20 -07002484func Fprintf(f io.Writer, format string, args ...)
Rob Pikedf3183f2009-02-26 16:37:23 -08002485Fprintf(os.Stdout, "%s %d", "hello", 23);
Robert Griesemerc2d55862009-02-19 16:49:10 -08002486</pre>
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002487
Rob Pikedf3183f2009-02-26 16:37:23 -08002488<p>
2489Within <code>Fprintf</code>, the dynamic type of <code>args</code> for this
2490call will be, schematically,
2491<code> struct { string; int }</code>.
2492</p>
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002493
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002494
Rob Pikedf3183f2009-02-26 16:37:23 -08002495<p>
2496As a special case, if a function passes its own <code>...</code> parameter as the argument
2497for a <code>...</code> in a call to another function with a <code>...</code> parameter,
2498the parameter is not wrapped again but passed directly. In short, a formal <code>...</code>
2499parameter is passed unchanged as an actual <code>...</code> parameter.
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002500
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002501<h3 id="Operators">Operators</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002502
Rob Pikedf3183f2009-02-26 16:37:23 -08002503<p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002504Operators combine operands into expressions.
Rob Pikedf3183f2009-02-26 16:37:23 -08002505</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002506
Robert Griesemerf7ac31362009-07-10 16:06:40 -07002507<pre class="ebnf">
Russ Cox5958dd62009-03-04 17:19:21 -08002508Expression = UnaryExpr | Expression binary_op UnaryExpr .
Rob Pikedf3183f2009-02-26 16:37:23 -08002509UnaryExpr = PrimaryExpr | unary_op UnaryExpr .
Robert Griesemer57b34612008-10-10 12:45:44 -07002510
Rob Pikedf3183f2009-02-26 16:37:23 -08002511binary_op = log_op | com_op | rel_op | add_op | mul_op .
2512log_op = "||" | "&amp;&amp;" .
2513com_op = "&lt;-" .
2514rel_op = "==" | "!=" | "&lt;" | "&lt;=" | ">" | ">=" .
2515add_op = "+" | "-" | "|" | "^" .
Robert Griesemer4ed666e2009-08-27 16:45:42 -07002516mul_op = "*" | "/" | "%" | "&lt;&lt;" | "&gt;&gt;" | "&amp;" | "&amp;^" .
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002517
Rob Pikedf3183f2009-02-26 16:37:23 -08002518unary_op = "+" | "-" | "!" | "^" | "*" | "&amp;" | "&lt;-" .
Robert Griesemerc2d55862009-02-19 16:49:10 -08002519</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002520
Robert Griesemerc2d55862009-02-19 16:49:10 -08002521<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07002522Comparisons are discussed <a href="#Comparison_operators">elsewhere</a>.
2523For other binary operators, the operand types must be identical
Rob Pike83cbca52009-08-21 14:18:08 -07002524<a href="#Properties_of_types_and_values">Properties of types and values</a>)
Robert Griesemer19b1d352009-09-24 19:36:48 -07002525unless the operation involves channels, shifts, or untyped <a href="#Constants">constants</a>.
2526For operations involving constants only, see the section on
2527<a href="#Constant_expressions">constant expressions</a>.
Rob Pike4501d342009-02-19 17:31:36 -08002528</p>
Robert Griesemerc8e18762008-09-12 12:26:22 -07002529
Rob Pike83cbca52009-08-21 14:18:08 -07002530<p>
Robert Griesemerb691e082009-10-28 18:17:24 -07002531In a channel send, the first operand is always a channel and the second
2532must be a value <a href="#Assignment_compatibility">assignment compatible</a>
2533with the channel's element type.
Rob Pike83cbca52009-08-21 14:18:08 -07002534</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002535
Rob Pike83cbca52009-08-21 14:18:08 -07002536<p>
2537Except for shift operations,
Robert Griesemer19b1d352009-09-24 19:36:48 -07002538if one operand is an untyped <a href="#Constants">constant</a>
2539and the other operand is not, the constant is <a href="#Conversions">converted</a>
2540to the type of the other operand.
Rob Pike83cbca52009-08-21 14:18:08 -07002541</p>
Robert Griesemer7539c852009-07-31 18:05:07 -07002542
Rob Pike83cbca52009-08-21 14:18:08 -07002543<p>
2544The right operand in a shift operation must have unsigned integer type
Robert Griesemer19b1d352009-09-24 19:36:48 -07002545or be an untyped constant that can be converted to unsigned integer type.
Rob Pike83cbca52009-08-21 14:18:08 -07002546</p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002547
Rob Pike83cbca52009-08-21 14:18:08 -07002548<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07002549If the left operand of a non-constant shift operation is an untyped constant,
2550the type of constant is what it would be if the shift operation were replaced by
2551the left operand alone.
Rob Pike83cbca52009-08-21 14:18:08 -07002552</p>
Robert Griesemera6b546f2008-10-20 11:46:40 -07002553
Rob Pike83cbca52009-08-21 14:18:08 -07002554<pre>
2555var s uint = 33;
2556var i = 1&lt;&lt;s; // 1 has type int
2557var j = int32(1&lt;&lt;s); // 1 has type int32; j == 0
2558var u = uint64(1&lt;&lt;s); // 1 has type uint64; u == 1&lt;&lt;33
2559var f = float(1&lt;&lt;s); // illegal: 1 has type float, cannot shift
2560var g = float(1&lt;&lt;33); // legal; 1&lt;&lt;33 is a constant shift operation; g == 1&lt;&lt;33
2561</pre>
Robert Griesemer18b05c12009-01-26 09:34:19 -08002562
Robert Griesemerd36d1912009-09-18 11:58:35 -07002563<h3 id="Operator_precedence">Operator precedence</h3>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002564<p>
Russ Coxec9b0422009-07-09 16:44:13 -07002565Unary operators have the highest precedence.
2566As the <code>++</code> and <code>--</code> operators form
Rob Pikedf3183f2009-02-26 16:37:23 -08002567statements, not expressions, they fall
Russ Coxec9b0422009-07-09 16:44:13 -07002568outside the operator hierarchy.
Rob Pikedf3183f2009-02-26 16:37:23 -08002569As a consequence, statement <code>*p++</code> is the same as <code>(*p)++</code>.
2570<p>
2571There are six precedence levels for binary operators.
2572Multiplication operators bind strongest, followed by addition
Robert Griesemerd36d1912009-09-18 11:58:35 -07002573operators, comparison operators, <code>&lt;-</code> (channel send),
Rob Pikedf3183f2009-02-26 16:37:23 -08002574<code>&amp;&amp;</code> (logical and), and finally <code>||</code> (logical or):
2575</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002576
Rob Pikeff70f092009-02-20 13:36:14 -08002577<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08002578Precedence Operator
Robert Griesemer4ed666e2009-08-27 16:45:42 -07002579 6 * / % &lt;&lt; &gt;&gt; &amp; &amp;^
Robert Griesemerc2d55862009-02-19 16:49:10 -08002580 5 + - | ^
2581 4 == != &lt; &lt;= > >=
2582 3 &lt;-
2583 2 &amp;&amp;
2584 1 ||
2585</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002586
Robert Griesemerc2d55862009-02-19 16:49:10 -08002587<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002588Binary operators of the same precedence associate from left to right.
Robert Griesemerd36d1912009-09-18 11:58:35 -07002589For instance, <code>x / y * z</code> is the same as <code>(x / y) * z</code>.
Rob Pikedf3183f2009-02-26 16:37:23 -08002590</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002591
Robert Griesemerc2d55862009-02-19 16:49:10 -08002592<pre>
2593+x
259423 + 3*x[i]
2595x &lt;= f()
Robert Griesemer4ed666e2009-08-27 16:45:42 -07002596^a &gt;&gt; b
Robert Griesemerc2d55862009-02-19 16:49:10 -08002597f() || g()
Robert Griesemerd36d1912009-09-18 11:58:35 -07002598x == y+1 &amp;&amp; &lt;-chan_ptr > 0
Robert Griesemerc2d55862009-02-19 16:49:10 -08002599</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002600
2601
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002602<h3 id="Arithmetic_operators">Arithmetic operators</h3>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002603<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07002604Arithmetic operators apply to numeric values and yield a result of the same
Rob Pikedf3183f2009-02-26 16:37:23 -08002605type as the first operand. The four standard arithmetic operators (<code>+</code>,
Robert Griesemer19b1d352009-09-24 19:36:48 -07002606<code>-</code>, <code>*</code>, <code>/</code>) apply to integer and
2607floating-point types; <code>+</code> also applies
2608to strings. All other arithmetic operators apply to integers only.
Rob Pikedf3183f2009-02-26 16:37:23 -08002609</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002610
Rob Pikeff70f092009-02-20 13:36:14 -08002611<pre class="grammar">
Rob Pike307ec212009-03-12 15:53:56 -07002612+ sum integers, floats, strings
2613- difference integers, floats
2614* product integers, floats
2615/ quotient integers, floats
2616% remainder integers
Robert Griesemerc2d55862009-02-19 16:49:10 -08002617
Rob Pike307ec212009-03-12 15:53:56 -07002618&amp; bitwise and integers
2619| bitwise or integers
2620^ bitwise xor integers
2621&amp;^ bit clear (and not) integers
Robert Griesemerc2d55862009-02-19 16:49:10 -08002622
Robert Griesemer4ed666e2009-08-27 16:45:42 -07002623&lt;&lt; left shift integer &lt;&lt; unsigned integer
2624&gt;&gt; right shift integer &gt;&gt; unsigned integer
Robert Griesemerc2d55862009-02-19 16:49:10 -08002625</pre>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002626
Rob Pikedf3183f2009-02-26 16:37:23 -08002627<p>
2628Strings can be concatenated using the <code>+</code> operator
2629or the <code>+=</code> assignment operator:
2630</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002631
Robert Griesemerc2d55862009-02-19 16:49:10 -08002632<pre>
Rob Pikedf3183f2009-02-26 16:37:23 -08002633s := "hi" + string(c);
2634s += " and good bye";
Robert Griesemerc2d55862009-02-19 16:49:10 -08002635</pre>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002636
Robert Griesemerc2d55862009-02-19 16:49:10 -08002637<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002638String addition creates a new string by concatenating the operands.
2639</p>
2640<p>
2641For integer values, <code>/</code> and <code>%</code> satisfy the following relationship:
2642</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002643
Robert Griesemerc2d55862009-02-19 16:49:10 -08002644<pre>
2645(a / b) * b + a % b == a
2646</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002647
Rob Pikedf3183f2009-02-26 16:37:23 -08002648<p>
2649with <code>(a / b)</code> truncated towards zero.
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002650Examples:
Rob Pikedf3183f2009-02-26 16:37:23 -08002651</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002652
Robert Griesemerc2d55862009-02-19 16:49:10 -08002653<pre>
2654 x y x / y x % y
2655 5 3 1 2
2656-5 3 -1 -2
2657 5 -3 -1 2
2658-5 -3 1 -2
2659</pre>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002660
Rob Pikedf3183f2009-02-26 16:37:23 -08002661<p>
2662If the dividend is positive and the divisor is a constant power of 2,
Rob Pikef3a33bc2009-09-14 17:39:17 -07002663the division may be replaced by a right shift, and computing the remainder may
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002664be replaced by a bitwise "and" operation:
Rob Pikedf3183f2009-02-26 16:37:23 -08002665</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002666
Robert Griesemerc2d55862009-02-19 16:49:10 -08002667<pre>
Robert Griesemer4ed666e2009-08-27 16:45:42 -07002668 x x / 4 x % 4 x &gt;&gt; 2 x &amp; 3
Robert Griesemerc2d55862009-02-19 16:49:10 -08002669 11 2 3 2 3
2670-11 -2 -3 -3 1
2671</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002672
Rob Pikedf3183f2009-02-26 16:37:23 -08002673<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002674The shift operators shift the left operand by the shift count specified by the
2675right operand. They implement arithmetic shifts if the left operand is a signed
Rob Pikedf3183f2009-02-26 16:37:23 -08002676integer and logical shifts if it is an unsigned integer. The shift count must
2677be an unsigned integer. There is no upper limit on the shift count. Shifts behave
2678as if the left operand is shifted <code>n</code> times by 1 for a shift
2679count of <code>n</code>.
Robert Griesemer4ed666e2009-08-27 16:45:42 -07002680As a result, <code>x &lt;&lt; 1</code> is the same as <code>x*2</code>
2681and <code>x &gt;&gt; 1</code> is the same as
Rob Pikedf3183f2009-02-26 16:37:23 -08002682<code>x/2</code> truncated towards negative infinity.
2683</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002684
Rob Pikedf3183f2009-02-26 16:37:23 -08002685<p>
2686For integer operands, the unary operators
2687<code>+</code>, <code>-</code>, and <code>^</code> are defined as
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002688follows:
Rob Pikedf3183f2009-02-26 16:37:23 -08002689</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002690
Rob Pikeff70f092009-02-20 13:36:14 -08002691<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08002692+x is 0 + x
2693-x negation is 0 - x
Russ Coxd83dc4f2009-05-29 16:04:16 -07002694^x bitwise complement is m ^ x with m = "all bits set to 1" for unsigned x
2695 and m = -1 for signed x
Robert Griesemerc2d55862009-02-19 16:49:10 -08002696</pre>
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002697
Russ Cox5958dd62009-03-04 17:19:21 -08002698<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07002699For floating-point numbers,
Russ Cox5958dd62009-03-04 17:19:21 -08002700<code>+x</code> is the same as <code>x</code>,
2701while <code>-x</code> is the negation of <code>x</code>.
2702</p>
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002703
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002704<h3 id="Integer_overflow">Integer overflow</h3>
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002705
Rob Pikedf3183f2009-02-26 16:37:23 -08002706<p>
2707For unsigned integer values, the operations <code>+</code>,
2708<code>-</code>, <code>*</code>, and <code>&lt;&lt;</code> are
2709computed modulo 2<sup><i>n</i></sup>, where <i>n</i> is the bit width of
2710the unsigned integer's type
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002711<a href="#Numeric_types">Numeric types</a>). Loosely speaking, these unsigned integer operations
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002712discard high bits upon overflow, and programs may rely on ``wrap around''.
Rob Pikedf3183f2009-02-26 16:37:23 -08002713</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002714<p>
Rob Pikedf3183f2009-02-26 16:37:23 -08002715For signed integers, the operations <code>+</code>,
2716<code>-</code>, <code>*</code>, and <code>&lt;&lt;</code> may legally
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002717overflow and the resulting value exists and is deterministically defined
2718by the signed integer representation, the operation, and its operands.
Rob Pikedf3183f2009-02-26 16:37:23 -08002719No exception is raised as a result of overflow. A
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002720compiler may not optimize code under the assumption that overflow does
Rob Pikedf3183f2009-02-26 16:37:23 -08002721not occur. For instance, it may not assume that <code>x &lt; x + 1</code> is always true.
2722</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002723
2724
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002725<h3 id="Comparison_operators">Comparison operators</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002726
Rob Pike5af7de32009-02-24 15:17:59 -08002727<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07002728Comparison operators yield a value of type <code>bool</code>.
Rob Pike5af7de32009-02-24 15:17:59 -08002729The operators <code>==</code> and <code>!=</code> apply, at least in some cases,
Robert Griesemer19b1d352009-09-24 19:36:48 -07002730to operands of all types except arrays and structs.
2731All other comparison operators apply only to numeric and string values.
Rob Pike5af7de32009-02-24 15:17:59 -08002732</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002733
Rob Pikeff70f092009-02-20 13:36:14 -08002734<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08002735== equal
2736!= not equal
2737< less
2738<= less or equal
2739> greater
2740>= greater or equal
2741</pre>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002742
Rob Pike5af7de32009-02-24 15:17:59 -08002743<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07002744Operands of numeric type are compared in the usual way.
Rob Pike5af7de32009-02-24 15:17:59 -08002745</p>
2746<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07002747Operands of string type are compared byte-wise (lexically).
Rob Pike5af7de32009-02-24 15:17:59 -08002748</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002749<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07002750Operands of boolean type are equal if they are either both <code>true</code>
2751or both <code>false</code>.
Rob Pike5af7de32009-02-24 15:17:59 -08002752</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002753<p>
Rob Pike5af7de32009-02-24 15:17:59 -08002754The rules for comparison of composite types are described in the
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002755section on §<a href="#Comparison_compatibility">Comparison compatibility</a>.
Rob Pike5af7de32009-02-24 15:17:59 -08002756</p>
Robert Griesemera3294712009-01-05 11:17:26 -08002757
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002758
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002759<h3 id="Logical_operators">Logical operators</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002760
Rob Pikedf3183f2009-02-26 16:37:23 -08002761<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07002762Logical operators apply to <a href="#Boolean_types">boolean</a> values
2763and yield a result of the same type as the operands.
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002764The right operand is evaluated conditionally.
Rob Pikedf3183f2009-02-26 16:37:23 -08002765</p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002766
Rob Pikeff70f092009-02-20 13:36:14 -08002767<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08002768&amp;&amp; conditional and p &amp;&amp; q is "if p then q else false"
2769|| conditional or p || q is "if p then true else q"
2770! not !p is "not p"
2771</pre>
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -07002772
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002773
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002774<h3 id="Address_operators">Address operators</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002775
Rob Pikedf3183f2009-02-26 16:37:23 -08002776<p>
Rob Pike678625d2009-09-15 09:54:22 -07002777The address-of operator <code>&amp;</code> generates the address of its operand,
2778which must be <i>addressable</i>,
2779that is, either a variable, pointer indirection, array or slice indexing
2780operation,
2781or a field selector of an addressable struct operand.
2782A function result variable is not addressable.
Robert Griesemer90cc4a52009-10-22 09:41:38 -07002783(<span class="alert">TODO: remove this restriction.</span>)
Rob Pike678625d2009-09-15 09:54:22 -07002784Given an operand of pointer type, the pointer indirection
2785operator <code>*</code> retrieves the value pointed
Rob Pikeafee1c52009-03-20 17:41:25 -07002786to by the operand.
2787</p>
2788
2789<pre>
2790&amp;x
2791&amp;a[f(2)]
2792*p
2793*pf(x)
2794</pre>
2795
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002796<h3 id="Communication_operators">Communication operators</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002797
Robert Griesemerc2d55862009-02-19 16:49:10 -08002798<p>
Rob Pike678625d2009-09-15 09:54:22 -07002799The term <i>channel</i> means "value of <a href="#Channel_types">channel type</a>".
Rob Pikedf3183f2009-02-26 16:37:23 -08002800</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002801<p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002802The send operation uses the binary operator "&lt;-", which operates on
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002803a channel and a value (expression):
Rob Pikedf3183f2009-02-26 16:37:23 -08002804</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002805
Robert Griesemerc2d55862009-02-19 16:49:10 -08002806<pre>
Robert Griesemere1e76192009-09-25 14:11:03 -07002807ch &lt;- 3
Robert Griesemerc2d55862009-02-19 16:49:10 -08002808</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002809
Rob Pikedf3183f2009-02-26 16:37:23 -08002810<p>
2811The send operation sends the value on the channel. Both the channel
2812and the expression are evaluated before communication begins.
2813Communication blocks until the send can proceed, at which point the
Rob Pike678625d2009-09-15 09:54:22 -07002814value is transmitted on the channel.
2815A send on an unbuffered channel can proceed if a receiver is ready.
2816A send on a buffered channel can proceed if there is room in the buffer.
Rob Pikedf3183f2009-02-26 16:37:23 -08002817</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002818<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002819If the send operation appears in an expression context, the value
2820of the expression is a boolean and the operation is non-blocking.
2821The value of the boolean reports true if the communication succeeded,
Rob Pikedf3183f2009-02-26 16:37:23 -08002822false if it did not. (The channel and
2823the expression to be sent are evaluated regardless.)
2824These two examples are equivalent:
2825</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002826
Robert Griesemerc2d55862009-02-19 16:49:10 -08002827<pre>
Robert Griesemere1e76192009-09-25 14:11:03 -07002828ok := ch &lt;- 3;
Robert Griesemerc2d55862009-02-19 16:49:10 -08002829if ok { print("sent") } else { print("not sent") }
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002830
Robert Griesemere1e76192009-09-25 14:11:03 -07002831if ch &lt;- 3 { print("sent") } else { print("not sent") }
Robert Griesemerc2d55862009-02-19 16:49:10 -08002832</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002833
Rob Pikedf3183f2009-02-26 16:37:23 -08002834<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002835In other words, if the program tests the value of a send operation,
2836the send is non-blocking and the value of the expression is the
2837success of the operation. If the program does not test the value,
2838the operation blocks until it succeeds.
Rob Pikedf3183f2009-02-26 16:37:23 -08002839</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002840<p>
2841The receive operation uses the prefix unary operator "&lt;-".
Rob Pikedf3183f2009-02-26 16:37:23 -08002842The value of the expression is the value received, whose type
2843is the element type of the channel.
2844</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002845
Robert Griesemerc2d55862009-02-19 16:49:10 -08002846<pre>
Robert Griesemere1e76192009-09-25 14:11:03 -07002847&lt;-ch
Robert Griesemerc2d55862009-02-19 16:49:10 -08002848</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002849
Rob Pikedf3183f2009-02-26 16:37:23 -08002850<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002851The expression blocks until a value is available, which then can
Rob Pikedf3183f2009-02-26 16:37:23 -08002852be assigned to a variable or used like any other expression.
2853If the receive expression does not save the value, the value is
2854discarded.
2855</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002856
Robert Griesemerc2d55862009-02-19 16:49:10 -08002857<pre>
Robert Griesemere1e76192009-09-25 14:11:03 -07002858v1 := &lt;-ch
2859v2 = &lt;-ch
2860f(&lt;-ch)
2861&lt;-strobe // wait until clock pulse
Robert Griesemerc2d55862009-02-19 16:49:10 -08002862</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002863
Rob Pikedf3183f2009-02-26 16:37:23 -08002864<p>
Rob Piked5537072009-08-22 00:04:04 -07002865If a receive expression is used in an assignment or initialization of the form
Rob Pikedf3183f2009-02-26 16:37:23 -08002866</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002867
Robert Griesemerc2d55862009-02-19 16:49:10 -08002868<pre>
Robert Griesemere1e76192009-09-25 14:11:03 -07002869x, ok = &lt;-ch
2870x, ok := &lt;-ch
2871var x, ok = &lt;-ch
Robert Griesemerc2d55862009-02-19 16:49:10 -08002872</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002873
Rob Pikedf3183f2009-02-26 16:37:23 -08002874<p>
2875the receive operation becomes non-blocking.
Rob Pike678625d2009-09-15 09:54:22 -07002876If the operation can proceed, the boolean variable
Rob Pikedf3183f2009-02-26 16:37:23 -08002877<code>ok</code> will be set to <code>true</code>
2878and the value stored in <code>x</code>; otherwise
2879<code>ok</code> is set
2880to <code>false</code> and <code>x</code> is set to the
Russ Cox7c4f7cc2009-08-20 11:11:03 -07002881zero value for its type (§<a href="#The_zero_value">The zero value</a>).
Rob Pikedf3183f2009-02-26 16:37:23 -08002882</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002883
Rob Pikedf3183f2009-02-26 16:37:23 -08002884<p>
Robert Griesemer90cc4a52009-10-22 09:41:38 -07002885<span class="alert">TODO: Probably in a separate section, communication semantics
2886need to be presented regarding send, receive, select, and goroutines.</span>
Rob Pikedf3183f2009-02-26 16:37:23 -08002887</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002888
Rob Pike01cadde2009-09-15 15:56:44 -07002889<h3 id="Method_expressions">Method expressions</h3>
2890
2891<p>
2892If <code>M</code> is in the method set of type <code>T</code>,
2893<code>T.M</code> is a function that is callable as a regular function
2894with the same arguments as <code>M</code> prefixed by an additional
2895argument that is the receiver of the method.
2896</p>
2897
Robert Griesemerda961882009-09-17 11:01:50 -07002898<pre class="ebnf">
Rob Pike01cadde2009-09-15 15:56:44 -07002899MethodExpr = ReceiverType "." MethodName .
2900ReceiverType = TypeName | "(" "*" TypeName ")" .
Rob Pike01cadde2009-09-15 15:56:44 -07002901</pre>
2902
2903<p>
2904Consider a struct type <code>T</code> with two methods,
2905<code>Mv</code>, whose receiver is of type <code>T</code>, and
2906<code>Mp</code>, whose receiver is of type <code>*T</code>.
2907</p>
2908
2909<pre>
2910type T struct {
2911 a int;
2912}
2913func (tv T) Mv(a int) int { return 0 } // value receiver
2914func (tp *T) Mp(f float) float { return 1 } // pointer receiver
2915var t T;
2916</pre>
2917
2918<p>
2919The expression
2920</p>
2921
2922<pre>
2923T.Mv
2924</pre>
2925
2926<p>
2927yields a function equivalent to <code>Mv</code> but
2928with an explicit receiver as its first argument; it has signature
2929</p>
2930
2931<pre>
2932func (tv T, a int) int
2933</pre>
2934
2935<p>
2936That function may be called normally with an explicit receiver, so
2937these three invocations are equivalent:
2938</p>
2939
2940<pre>
2941t.Mv(7)
2942T.Mv(t, 7)
2943f := T.Mv; f(t, 7)
2944</pre>
2945
2946<p>
2947Similarly, the expression
2948</p>
2949
2950<pre>
2951(*T).Mp
2952</pre>
2953
2954<p>
2955yields a function value representing <code>Mp</code> with signature
2956</p>
2957
2958<pre>
2959func (tp *T, f float) float
2960</pre>
2961
2962<p>
2963For a method with a value receiver, one can derive a function
2964with an explicit pointer receiver, so
2965</p>
2966
2967<pre>
2968(*T).Mv
2969</pre>
2970
2971<p>
2972yields a function value representing <code>Mv</code> with signature
2973</p>
2974
2975<pre>
2976func (tv *T, f int) int
2977</pre>
2978
2979<p>
2980Such a function indirects through the receiver to create a value
2981to pass as the receiver to the underlying method;
2982the method does not overwrite the value whose address is passed in
2983the function call.
2984</p>
2985
2986<p>
2987The final case, a value-receiver function for a pointer-receiver method,
2988is illegal because pointer-receiver methods are not in the method set
2989of the value type.
2990</p>
2991
2992<p>
2993Function values derived from methods are called with function call syntax;
2994the receiver is provided as the first argument to the call.
2995That is, given <code>f := T.Mv</code>, <code>f</code> is invoked
2996as <code>f(t, 7)</code> not <code>t.f(7)</code>.
2997To construct a function that binds the receiver, use a
Robert Griesemerd36d1912009-09-18 11:58:35 -07002998<a href="#Function_literals">closure</a>.
Rob Pike01cadde2009-09-15 15:56:44 -07002999</p>
3000
3001<p>
3002It is legal to derive a function value from a method of an interface type.
3003The resulting function takes an explicit receiver of that interface type.
3004</p>
3005
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003006<h3 id="Conversions">Conversions</h3>
3007
3008<p>
3009Conversions are expressions of the form <code>T(x)</code>
3010where <code>T</code> is a type and <code>x</code> is an expression
3011that can be converted to type <code>T</code>.
3012</p>
3013
3014<pre class="ebnf">
3015Conversion = LiteralType "(" Expression ")" .
3016</pre>
3017
3018<p>
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003019In general, a conversion succeeds if the value of <code>x</code> is
3020<a href="#Assignment_compatibility">assignment compatible</a> with type <code>T</code>,
3021or if the value would be assignment compatible with type <code>T</code> if the
3022value's type, or <code>T</code>, or any of their component types were unnamed.
3023Usually, such a conversion changes the type but not the representation of the value
3024of <code>x</code> and thus has no run-time cost.
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003025</p>
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003026
3027<p>
3028Specific rules apply to conversions where <code>T</code> is a numeric or string type.
3029These conversions may change the representation of a value and incur a run-time cost.
3030</p>
3031
3032<h4>Conversions between integer types</h4>
3033<p>
3034If the value is a signed quantity, it is
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003035sign extended to implicit infinite precision; otherwise it is zero
3036extended. It is then truncated to fit in the result type's size.
3037For example, if <code>x := uint16(0x10F0)</code>, then <code>uint32(int8(x)) == 0xFFFFFFF0</code>.
3038The conversion always yields a valid value; there is no indication of overflow.
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003039</p>
3040
3041<h4>Conversions involving floating point types</h4>
3042<ol>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003043<li>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003044When converting a floating-point number to an integer, the fraction is discarded
3045(truncation towards zero).
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003046</li>
3047<li>
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003048When converting a number to a floating-point type, the result value is rounded
3049to the precision specified by the floating point type.
3050For instance, the value of a variable <code>x</code> of type <code>float32</code>
3051may be stored using additional precision beyond that of an IEEE-754 32-bit number,
3052but float32(x) represents the result of rounding <code>x</code>'s value to
305332-bit precision. Similarly, <code>x + 0.1</code> may use more than 32 bits
3054of precision, <code>but float32(x + 0.1)</code> does not.
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003055</li>
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003056</ol>
3057
3058<p>
3059In all conversions involving floating-point values, if the result type cannot
3060represent the value the conversion succeeds but the result value is
3061implementation-dependent.
3062</p>
3063
3064<h4>Conversions to a string type</h4>
3065<ol>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003066<li>
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003067Converting an integer value yields a string containing the UTF-8
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003068representation of the integer.
3069
3070<pre>
3071string(0x65e5) // "\u65e5" == "日" == "\xe6\x97\xa5"
3072</pre>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003073</li>
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003074
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003075<li>
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003076Converting a slice of integers yields a string that is the
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003077concatenation of the individual integers converted to strings.
3078If the slice value is <code>nil</code>, the result is the empty string.
3079<pre>
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003080string([]int{0x767d, 0x9d6c, 0x7fd4}) // "\u767d\u9d6c\u7fd4" == "白鵬翔"
3081</pre>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003082</li>
3083
3084<li>
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003085Converting a slice of bytes yields a string whose successive
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003086bytes are those of the slice. If the slice value is <code>nil</code>,
3087the result is the empty string.
3088
3089<pre>
3090string([]byte{'h', 'e', 'l', 'l', 'o'}) // "hello"
3091</pre>
3092</li>
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003093</ol>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07003094
3095<p>
3096There is no linguistic mechanism to convert between pointers and integers.
3097The package <a href="#Package_unsafe"><code>unsafe</code></a>
3098implements this functionality under
3099restricted circumstances.
3100</p>
3101
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003102<h3 id="Constant_expressions">Constant expressions</h3>
Robert Griesemerb90b2132008-09-19 15:49:55 -07003103
Rob Pikef27e9f02009-02-23 19:22:05 -08003104<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07003105Constant expressions may contain only <a href="#Constants">constant</a>
3106operands and are evaluated at compile-time.
Rob Pikef27e9f02009-02-23 19:22:05 -08003107</p>
3108
3109<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07003110Untyped boolean, numeric, and string constants may be used as operands
3111wherever it is legal to use an operand of boolean, numeric, or string type,
3112respectively. Except for shift operations, if the operands of a binary operation
3113are an untyped integer constant and an untyped floating-point constant,
3114the integer constant is converted to an untyped floating-point constant
3115(relevant for <code>/</code> and <code>%</code>).
Robert Griesemer19b1d352009-09-24 19:36:48 -07003116</p>
Robert Griesemer997851e2009-09-25 15:36:25 -07003117
3118<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07003119Applying an operator to untyped constants results in an untyped
3120constant of the same kind (that is, a boolean, integer, floating-point, or
3121string constant), except for
3122<a href="#Comparison_operators">comparison operators</a> which result in
3123a constant of type <code>bool</code>.
3124</p>
3125
3126<p>
3127Constant expressions are always evaluated exactly; intermediate values and the
3128constants themselves may require precision significantly larger than supported
3129by any predeclared type in the language. The following are legal declarations:
Rob Pikef27e9f02009-02-23 19:22:05 -08003130</p>
3131
3132<pre>
Robert Griesemer4ed666e2009-08-27 16:45:42 -07003133const Huge = 1 &lt;&lt; 100;
3134const Four int8 = Huge &gt;&gt; 98;
Rob Pikef27e9f02009-02-23 19:22:05 -08003135</pre>
3136
3137<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07003138The values of <i>typed</i> constants must always be accurately representable as values
3139of the constant type. The following constant expressions are illegal:
Rob Pike21d03492009-03-24 19:16:42 -07003140</p>
3141
3142<pre>
Robert Griesemer19b1d352009-09-24 19:36:48 -07003143uint(-1) // -1 overflows uint
3144int(3.14) // 3.14 truncated to integer
3145int64(Huge) // 1&lt;&lt;100 overflows int64
3146Four * 300 // 300 overflows int8
3147Four * 100 // 400 overflows int8
Rob Pike21d03492009-03-24 19:16:42 -07003148</pre>
3149
3150<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07003151The mask used by the unary bitwise complement operator <code>^</code> matches
Rob Pike678625d2009-09-15 09:54:22 -07003152the rule for non-constants: the mask is all 1s for unsigned constants
Robert Griesemer19b1d352009-09-24 19:36:48 -07003153and -1 for signed and untyped constants.
Rob Pike21d03492009-03-24 19:16:42 -07003154</p>
3155
3156<pre>
Robert Griesemer19b1d352009-09-24 19:36:48 -07003157^1 // untyped integer constant, equal to -2
Rob Pike21d03492009-03-24 19:16:42 -07003158uint8(^1) // error, same as uint8(-2), out of range
3159^uint8(1) // typed uint8 constant, same as 0xFF ^ uint8(1) = uint8(0xFE)
3160int8(^1) // same as int8(-2)
Russ Coxd83dc4f2009-05-29 16:04:16 -07003161^int8(1) // same as -1 ^ int8(1) = -2
Rob Pike21d03492009-03-24 19:16:42 -07003162</pre>
3163
3164<p>
Robert Griesemer90cc4a52009-10-22 09:41:38 -07003165<span class="alert">
Rob Pike21d03492009-03-24 19:16:42 -07003166TODO: perhaps ^ should be disallowed on non-uints instead of assuming twos complement.
3167Also it may be possible to make typed constants more like variables, at the cost of fewer
3168overflow etc. errors being caught.
Robert Griesemer90cc4a52009-10-22 09:41:38 -07003169</span>
Rob Pike21d03492009-03-24 19:16:42 -07003170</p>
3171
Robert Griesemer19b1d352009-09-24 19:36:48 -07003172
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003173<h3 id="Order_of_evaluation">Order of evaluation</h3>
Rob Pikec956e902009-04-14 20:10:49 -07003174
3175<p>
3176When evaluating the elements of an assignment or expression,
3177all function calls, method calls and
3178communication operations are evaluated in lexical left-to-right
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07003179order.
3180</p>
3181
3182<p>
3183Floating-point operations within a single expression are evaluated according to
3184the associativity of the operators. Explicit parentheses affect the evaluation
3185by overriding the default associativity.
3186In the expression <code>x + (y + z)</code> the addition <code>y + z</code>
3187is performed before adding <code>x</code>.
Rob Pikec956e902009-04-14 20:10:49 -07003188</p>
3189
3190<p>
Robert Griesemer4f185492009-05-01 17:00:16 -07003191For example, in the assignment
Rob Pikec956e902009-04-14 20:10:49 -07003192</p>
3193<pre>
Robert Griesemere1e76192009-09-25 14:11:03 -07003194y[f()], ok = g(h(), i() + x[j()], &lt;-c), k()
Rob Pikec956e902009-04-14 20:10:49 -07003195</pre>
3196<p>
Robert Griesemer4f185492009-05-01 17:00:16 -07003197the function calls and communication happen in the order
3198<code>f()</code>, <code>h()</code>, <code>i()</code>, <code>j()</code>,
Robert Griesemere1e76192009-09-25 14:11:03 -07003199<code>&lt;-c</code>, <code>g()</code>, and <code>k()</code>.
Robert Griesemer4f185492009-05-01 17:00:16 -07003200However, the order of those events compared to the evaluation
3201and indexing of <code>x</code> and the evaluation
3202of <code>y</code> is not specified.
Rob Pikec956e902009-04-14 20:10:49 -07003203</p>
3204
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003205<h2 id="Statements">Statements</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003206
Rob Pike96750f12009-02-27 16:47:48 -08003207<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003208Statements control execution.
Rob Pike96750f12009-02-27 16:47:48 -08003209</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003210
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003211<pre class="ebnf">
Robert Griesemerdea43942009-03-16 17:36:52 -07003212Statement =
Rob Pikef3a33bc2009-09-14 17:39:17 -07003213 Declaration | LabeledStmt | SimpleStmt |
3214 GoStmt | ReturnStmt | BreakStmt | ContinueStmt | GotoStmt |
Rob Pike11417162009-03-24 17:45:53 -07003215 FallthroughStmt | Block | IfStmt | SwitchStmt | SelectStmt | ForStmt |
3216 DeferStmt .
Robert Griesemer7abfcd92008-10-07 17:14:30 -07003217
Rob Pikef3a33bc2009-09-14 17:39:17 -07003218SimpleStmt = EmptyStmt | ExpressionStmt | IncDecStmt | Assignment | ShortVarDecl .
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003219
Rob Pike96750f12009-02-27 16:47:48 -08003220StatementList = Statement { Separator Statement } .
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003221Separator = [ ";" ] .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003222</pre>
Robert Griesemer7271e042008-10-09 20:05:24 -07003223
Robert Griesemerc2d55862009-02-19 16:49:10 -08003224<p>
Rob Pike96750f12009-02-27 16:47:48 -08003225Elements of a list of statements are separated by semicolons,
3226which may be omitted only if the previous statement:
Rob Pike4501d342009-02-19 17:31:36 -08003227</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08003228<ul>
Robert Griesemer506c0082009-09-08 15:41:14 -07003229 <li>ends with the closing parenthesis ")" of a list of <a href="#Declarations_and_scope">declarations</a>; or</li>
Rob Pike736a1ae2009-04-02 23:03:41 -07003230 <li>ends with a closing brace "}" that is not part of an expression.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003231</ul>
Robert Griesemer7271e042008-10-09 20:05:24 -07003232
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003233
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003234<h3 id="Empty_statements">Empty statements</h3>
Robert Griesemeraed247f2008-10-08 17:05:30 -07003235
Rob Pike96750f12009-02-27 16:47:48 -08003236<p>
Robert Griesemeraed247f2008-10-08 17:05:30 -07003237The empty statement does nothing.
Rob Pike96750f12009-02-27 16:47:48 -08003238</p>
Robert Griesemeraed247f2008-10-08 17:05:30 -07003239
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003240<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003241EmptyStmt = .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003242</pre>
Robert Griesemeraed247f2008-10-08 17:05:30 -07003243
Rob Pike96750f12009-02-27 16:47:48 -08003244<p>
3245A statement list can always in effect be terminated with a semicolon by
3246adding an empty statement.
3247</p>
3248
Robert Griesemeraed247f2008-10-08 17:05:30 -07003249
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003250<h3 id="Labeled_statements">Labeled statements</h3>
Robert Griesemerdea43942009-03-16 17:36:52 -07003251
3252<p>
3253A labeled statement may be the target of a <code>goto</code>,
3254<code>break</code> or <code>continue</code> statement.
3255</p>
3256
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003257<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003258LabeledStmt = Label ":" Statement .
Robert Griesemerdea43942009-03-16 17:36:52 -07003259Label = identifier .
3260</pre>
3261
3262<pre>
3263Error: log.Fatal("error encountered")
3264</pre>
3265
3266
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003267<h3 id="Expression_statements">Expression statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003268
Rob Pike96750f12009-02-27 16:47:48 -08003269<p>
3270Function calls, method calls, and channel operations
3271can appear in statement context.
3272</p>
3273
3274
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003275<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003276ExpressionStmt = Expression .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003277</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003278
Robert Griesemerc2d55862009-02-19 16:49:10 -08003279<pre>
3280f(x+y)
Robert Griesemere1e76192009-09-25 14:11:03 -07003281&lt;-ch
Robert Griesemerc2d55862009-02-19 16:49:10 -08003282</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003283
3284
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003285<h3 id="IncDec_statements">IncDec statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003286
Rob Pike96750f12009-02-27 16:47:48 -08003287<p>
Robert Griesemer52a54802008-09-30 13:02:50 -07003288The "++" and "--" statements increment or decrement their operands
Robert Griesemer19b1d352009-09-24 19:36:48 -07003289by the untyped <a href="#Constants">constant</a> <code>1</code>.
3290As with an assignment, the operand must be a variable, pointer indirection,
3291field selector or index expression.
Rob Pike96750f12009-02-27 16:47:48 -08003292</p>
Robert Griesemer52a54802008-09-30 13:02:50 -07003293
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003294<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003295IncDecStmt = Expression ( "++" | "--" ) .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003296</pre>
Russ Cox5958dd62009-03-04 17:19:21 -08003297
Rob Pike96750f12009-02-27 16:47:48 -08003298<p>
Robert Griesemer506c0082009-09-08 15:41:14 -07003299The following <a href="#Assignments">assignment statements</a> are semantically
Robert Griesemer52a54802008-09-30 13:02:50 -07003300equivalent:
Rob Pike96750f12009-02-27 16:47:48 -08003301</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003302
Rob Pikeff70f092009-02-20 13:36:14 -08003303<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08003304IncDec statement Assignment
3305x++ x += 1
3306x-- x -= 1
3307</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003308
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003309<h3 id="Assignments">Assignments</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003310
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003311<pre class="ebnf">
Robert Griesemerc2d55862009-02-19 16:49:10 -08003312Assignment = ExpressionList assign_op ExpressionList .
Rob Pikeff70f092009-02-20 13:36:14 -08003313
Robert Griesemerc2d55862009-02-19 16:49:10 -08003314assign_op = [ add_op | mul_op ] "=" .
3315</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003316
Rob Pike96750f12009-02-27 16:47:48 -08003317<p>
Rob Pike678625d2009-09-15 09:54:22 -07003318Each left-hand side operand must be <a href="#Address_operators">addressable</a>,
3319a map index expresssion,
3320or the <a href="#Blank_identifier">blank identifier</a>.
Rob Pike96750f12009-02-27 16:47:48 -08003321</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003322
Robert Griesemerc2d55862009-02-19 16:49:10 -08003323<pre>
3324x = 1
3325*p = f()
3326a[i] = 23
Robert Griesemere1e76192009-09-25 14:11:03 -07003327k = &lt;-ch
Robert Griesemerc2d55862009-02-19 16:49:10 -08003328</pre>
Rob Pike96750f12009-02-27 16:47:48 -08003329
3330<p>
3331An <i>assignment operation</i> <code>x</code> <i>op</i><code>=</code>
3332<code>y</code> where <i>op</i> is a binary arithmetic operation is equivalent
3333to <code>x</code> <code>=</code> <code>x</code> <i>op</i>
3334<code>y</code> but evalutates <code>x</code>
3335only once. The <i>op</i><code>=</code> construct is a single token.
Rob Pike678625d2009-09-15 09:54:22 -07003336In assignment operations, both the left- and right-hand expression lists
3337must contain exactly one single-valued expression.
Rob Pike96750f12009-02-27 16:47:48 -08003338</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003339
Robert Griesemerc2d55862009-02-19 16:49:10 -08003340<pre>
Robert Griesemer4ed666e2009-08-27 16:45:42 -07003341a[i] &lt;&lt;= 2
Rob Pike678625d2009-09-15 09:54:22 -07003342i &amp;^= 1&lt;&lt;n
Robert Griesemerc2d55862009-02-19 16:49:10 -08003343</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003344
Rob Pike96750f12009-02-27 16:47:48 -08003345<p>
3346A tuple assignment assigns the individual elements of a multi-valued
3347operation to a list of variables. There are two forms. In the
3348first, the right hand operand is a single multi-valued expression
Robert Griesemer506c0082009-09-08 15:41:14 -07003349such as a function evaluation or <a href="#Channel_types">channel</a> or
3350<a href="#Map_types">map</a> operation or a <a href="#Type_assertions">type assertion</a>.
Russ Cox5958dd62009-03-04 17:19:21 -08003351The number of operands on the left
Robert Griesemer4e56b332009-09-10 10:14:00 -07003352hand side must match the number of values. For instance, if
Rob Pike96750f12009-02-27 16:47:48 -08003353<code>f</code> is a function returning two values,
3354</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003355
Robert Griesemerc2d55862009-02-19 16:49:10 -08003356<pre>
3357x, y = f()
3358</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003359
Rob Pike96750f12009-02-27 16:47:48 -08003360<p>
3361assigns the first value to <code>x</code> and the second to <code>y</code>.
Rob Pike678625d2009-09-15 09:54:22 -07003362The <a href="#Blank_identifier">blank identifier</a> provides a
Robert Griesemer4e56b332009-09-10 10:14:00 -07003363way to ignore values returned by a multi-valued expression:
Rob Pike96750f12009-02-27 16:47:48 -08003364</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003365
Robert Griesemer4e56b332009-09-10 10:14:00 -07003366<pre>
3367x, _ = f() // ignore second value returned by f()
3368</pre>
3369
Rob Pike96750f12009-02-27 16:47:48 -08003370<p>
3371In the second form, the number of operands on the left must equal the number
Robert Griesemeref45e642009-08-21 11:25:00 -07003372of expressions on the right, each of which must be single-valued, and the
3373<i>n</i>th expression on the right is assigned to the <i>n</i>th
3374operand on the left.
Russ Cox5958dd62009-03-04 17:19:21 -08003375The expressions on the right are evaluated before assigning to
3376any of the operands on the left, but otherwise the evaluation
Rob Pike678625d2009-09-15 09:54:22 -07003377order is unspecified beyond <a href="#Order_of_evaluation">the usual rules</a>.
Rob Pike96750f12009-02-27 16:47:48 -08003378</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003379
Robert Griesemerc2d55862009-02-19 16:49:10 -08003380<pre>
Rob Pike96750f12009-02-27 16:47:48 -08003381a, b = b, a // exchange a and b
Robert Griesemerc2d55862009-02-19 16:49:10 -08003382</pre>
Rob Pike96750f12009-02-27 16:47:48 -08003383
3384<p>
Robert Griesemer19b1d352009-09-24 19:36:48 -07003385In assignments, each value must be
Robert Griesemer4ed666e2009-08-27 16:45:42 -07003386<a href="#Assignment_compatibility">assignment compatible</a> with the type of the
Robert Griesemer19b1d352009-09-24 19:36:48 -07003387operand to which it is assigned. If an untyped <a href="#Constants">constant</a>
3388is assigned to a variable of interface type, the constant is <a href="#Conversions">converted</a>
3389to type <code>bool</code>, <code>int</code>, <code>float</code>, or <code>string</code>
3390respectively, depending on whether the value is a boolean, integer, floating-point,
3391or string constant.
Rob Pike96750f12009-02-27 16:47:48 -08003392</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003393
3394
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003395<h3 id="If_statements">If statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003396
Rob Pike96750f12009-02-27 16:47:48 -08003397<p>
3398"If" statements specify the conditional execution of two branches
3399according to the value of a boolean expression. If the expression
3400evaluates to true, the "if" branch is executed, otherwise, if
3401present, the "else" branch is executed. A missing condition
3402is equivalent to <code>true</code>.
3403</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003404
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003405<pre class="ebnf">
Rob Pikef3a33bc2009-09-14 17:39:17 -07003406IfStmt = "if" [ SimpleStmt ";" ] [ Expression ] Block [ "else" Statement ] .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003407</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003408
Robert Griesemerc2d55862009-02-19 16:49:10 -08003409<pre>
3410if x > 0 {
3411 return true;
3412}
3413</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003414
Russ Cox5958dd62009-03-04 17:19:21 -08003415<p>
Russ Cox16b95ba2009-08-20 10:22:52 -07003416The expression may be preceded by a simple statement, which
3417executes before the expression is evaluated.
Russ Cox5958dd62009-03-04 17:19:21 -08003418</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003419
Robert Griesemerc2d55862009-02-19 16:49:10 -08003420<pre>
3421if x := f(); x < y {
3422 return x;
3423} else if x > z {
3424 return z;
3425} else {
3426 return y;
3427}
3428</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003429
3430
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003431<h3 id="Switch_statements">Switch statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003432
Rob Pike96750f12009-02-27 16:47:48 -08003433<p>
3434"Switch" statements provide multi-way execution.
Rob Pike5a578492009-03-17 16:48:35 -07003435An expression or type specifier is compared to the "cases"
3436inside the "switch" to determine which branch
3437to execute.
Rob Pikeafee1c52009-03-20 17:41:25 -07003438</p>
Robert Griesemer091cba82009-03-19 08:39:40 -07003439
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003440<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003441SwitchStmt = ExprSwitchStmt | TypeSwitchStmt .
Robert Griesemer091cba82009-03-19 08:39:40 -07003442</pre>
3443
Rob Pikeafee1c52009-03-20 17:41:25 -07003444<p>
Rob Pike5a578492009-03-17 16:48:35 -07003445There are two forms: expression switches and type switches.
Rob Pike5a578492009-03-17 16:48:35 -07003446In an expression switch, the cases contain expressions that are compared
3447against the value of the switch expression.
3448In a type switch, the cases contain types that are compared against the
3449type of a specially annotated switch expression.
Rob Pike96750f12009-02-27 16:47:48 -08003450</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003451
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003452<h4 id="Expression_switches">Expression switches</h4>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003453
Rob Pike96750f12009-02-27 16:47:48 -08003454<p>
Rob Pike5a578492009-03-17 16:48:35 -07003455In an expression switch,
3456the switch expression is evaluated and
3457the case expressions, which need not be constants,
Robert Griesemer4f185492009-05-01 17:00:16 -07003458are evaluated left-to-right and top-to-bottom; the first one that equals the
Rob Pike5a578492009-03-17 16:48:35 -07003459switch expression
Rob Pike96750f12009-02-27 16:47:48 -08003460triggers execution of the statements of the associated case;
3461the other cases are skipped.
Rob Pike5a578492009-03-17 16:48:35 -07003462If no case matches and there is a "default" case,
3463its statements are executed.
Rob Pike96750f12009-02-27 16:47:48 -08003464There can be at most one default case and it may appear anywhere in the
3465"switch" statement.
Rob Pike70c1a102009-03-18 19:23:59 -07003466A missing expression is equivalent to
3467the expression <code>true</code>.
Rob Pike96750f12009-02-27 16:47:48 -08003468</p>
Rob Pike70c1a102009-03-18 19:23:59 -07003469
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003470<pre class="ebnf">
Rob Pikef3a33bc2009-09-14 17:39:17 -07003471ExprSwitchStmt = "switch" [ SimpleStmt ";" ] [ Expression ] "{" { ExprCaseClause } "}" .
Robert Griesemer091cba82009-03-19 08:39:40 -07003472ExprCaseClause = ExprSwitchCase ":" [ StatementList ] .
3473ExprSwitchCase = "case" ExpressionList | "default" .
Rob Pike70c1a102009-03-18 19:23:59 -07003474</pre>
3475
Rob Pike96750f12009-02-27 16:47:48 -08003476<p>
3477In a case or default clause,
3478the last statement only may be a "fallthrough" statement
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003479<a href="#Fallthrough_statement">Fallthrough statement</a>) to
Rob Pike96750f12009-02-27 16:47:48 -08003480indicate that control should flow from the end of this clause to
Robert Griesemeraed247f2008-10-08 17:05:30 -07003481the first statement of the next clause.
Rob Pike96750f12009-02-27 16:47:48 -08003482Otherwise control flows to the end of the "switch" statement.
3483</p>
Robert Griesemer0a162a12009-08-19 16:44:04 -07003484
Robert Griesemerc2d55862009-02-19 16:49:10 -08003485<p>
Russ Cox16b95ba2009-08-20 10:22:52 -07003486The expression may be preceded by a simple statement, which
3487executes before the expression is evaluated.
Rob Pike96750f12009-02-27 16:47:48 -08003488</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003489
Robert Griesemerc2d55862009-02-19 16:49:10 -08003490<pre>
3491switch tag {
Rob Pike65ec16b2009-05-29 15:46:03 -07003492default: s3()
3493case 0, 1, 2, 3: s1()
3494case 4, 5, 6, 7: s2()
Robert Griesemerc2d55862009-02-19 16:49:10 -08003495}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003496
Rob Pike96750f12009-02-27 16:47:48 -08003497switch x := f(); {
Rob Pike65ec16b2009-05-29 15:46:03 -07003498case x &lt; 0: return -x
3499default: return x
Robert Griesemerc2d55862009-02-19 16:49:10 -08003500}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003501
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003502switch { // missing expression means "true"
3503case x &lt; y: f1();
3504case x &lt; z: f2();
Russ Cox5958dd62009-03-04 17:19:21 -08003505case x == 4: f3();
Robert Griesemerc2d55862009-02-19 16:49:10 -08003506}
3507</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003508
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003509<h4 id="Type_switches">Type switches</h4>
Rob Pike70c1a102009-03-18 19:23:59 -07003510
Rob Pike5a578492009-03-17 16:48:35 -07003511<p>
Rob Pike70c1a102009-03-18 19:23:59 -07003512A type switch compares types rather than values. It is otherwise similar
Robert Griesemer1f95f0d2009-08-27 16:44:17 -07003513to an expression switch. It is marked by a special switch expression that
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003514has the form of a <a href="#Type_assertions">type assertion</a>
Rob Pike70c1a102009-03-18 19:23:59 -07003515using the reserved word <code>type</code> rather than an actual type.
3516Cases then match literal types against the dynamic type of the expression
Rob Pikef5387602009-03-30 16:08:41 -07003517in the type assertion.
Rob Pike5a578492009-03-17 16:48:35 -07003518</p>
3519
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003520<pre class="ebnf">
Rob Pikef3a33bc2009-09-14 17:39:17 -07003521TypeSwitchStmt = "switch" [ SimpleStmt ";" ] TypeSwitchGuard "{" { TypeCaseClause } "}" .
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003522TypeSwitchGuard = [ identifier ":=" ] Expression "." "(" "type" ")" .
Robert Griesemer091cba82009-03-19 08:39:40 -07003523TypeCaseClause = TypeSwitchCase ":" [ StatementList ] .
Rob Pike678625d2009-09-15 09:54:22 -07003524TypeSwitchCase = "case" TypeList | "default" .
3525TypeList = Type { "," Type } .
Rob Pike5a578492009-03-17 16:48:35 -07003526</pre>
3527
3528<p>
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003529The TypeSwitchGuard may include a
3530<a href="#Short_variable_declarations">short variable declaration</a>.
3531When that form is used, the variable is declared in each clause.
3532In clauses with a case listing exactly one type, the variable
3533has that type; otherwise, the variable has the type of the expression
3534in the TypeSwitchGuard.
Rob Pike94b67eb2009-03-24 17:40:47 -07003535</p>
3536
3537<p>
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003538The type in a case may be <code>nil</code>
3539<a href="#Predeclared_identifiers">Predeclared identifiers</a>);
3540that case is used when the expression in the TypeSwitchGuard
Robert Griesemer1f95f0d2009-08-27 16:44:17 -07003541is a <code>nil</code> interface value.
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003542</p>
3543
3544<p>
3545Given a function <code>f</code> that returns
3546a value of type <code>interface{}</code>,
Rob Pike70c1a102009-03-18 19:23:59 -07003547the following type switch:
Rob Pike5a578492009-03-17 16:48:35 -07003548</p>
3549
3550<pre>
3551switch i := f().(type) {
Rob Pike94b67eb2009-03-24 17:40:47 -07003552case nil:
3553 printString("f() returns nil");
Rob Pike5a578492009-03-17 16:48:35 -07003554case int:
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003555 printInt(i); // i is an int
Rob Pike5a578492009-03-17 16:48:35 -07003556case float:
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003557 printFloat(i); // i is a float
Rob Pike70c1a102009-03-18 19:23:59 -07003558case func(int) float:
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003559 printFunction(i); // i is a function
3560case bool, string:
3561 printString("type is bool or string"); // i is an interface{}
Rob Pike5a578492009-03-17 16:48:35 -07003562default:
3563 printString("don't know the type");
3564}
3565</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003566
Rob Pike70c1a102009-03-18 19:23:59 -07003567<p>
3568could be rewritten:
3569</p>
3570
3571<pre>
3572v := f();
Rob Pike94b67eb2009-03-24 17:40:47 -07003573if v == nil {
3574 printString("f() returns nil");
3575} else if i, is_int := v.(int); is_int {
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003576 printInt(i); // i is an int
Rob Pike70c1a102009-03-18 19:23:59 -07003577} else if i, is_float := v.(float); is_float {
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003578 printFloat(i); // i is a float
Rob Pike70c1a102009-03-18 19:23:59 -07003579} else if i, is_func := v.(func(int) float); is_func {
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003580 printFunction(i); // i is a function
Rob Pike70c1a102009-03-18 19:23:59 -07003581} else {
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003582 i1, is_bool := v.(bool);
3583 i2, is_string := v.(string);
3584 if is_bool || is_string {
3585 i := v;
3586 printString("type is bool or string"); // i is an interface{}
3587 } else {
3588 i := v;
3589 printString("don't know the type"); // i is an interface{}
3590 }
Rob Pike70c1a102009-03-18 19:23:59 -07003591}
3592</pre>
3593
Robert Griesemeraeaab592009-08-31 17:30:55 -07003594<p>
Russ Cox16b95ba2009-08-20 10:22:52 -07003595The type switch guard may be preceded by a simple statement, which
3596executes before the guard is evaluated.
Robert Griesemer1f95f0d2009-08-27 16:44:17 -07003597</p>
Robert Griesemer9ecd30a2009-08-27 14:22:51 -07003598
3599<p>
3600The "fallthrough" statement is not permitted in a type switch.
Russ Cox16b95ba2009-08-20 10:22:52 -07003601</p>
3602
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003603<h3 id="For_statements">For statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003604
Rob Pike96750f12009-02-27 16:47:48 -08003605<p>
3606A "for" statement specifies repeated execution of a block. The iteration is
3607controlled by a condition, a "for" clause, or a "range" clause.
3608</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003609
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003610<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003611ForStmt = "for" [ Condition | ForClause | RangeClause ] Block .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003612Condition = Expression .
3613</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003614
Rob Pike96750f12009-02-27 16:47:48 -08003615<p>
3616In its simplest form, a "for" statement specifies the repeated execution of
3617a block as long as a boolean condition evaluates to true.
3618The condition is evaluated before each iteration.
3619If the condition is absent, it is equivalent to <code>true</code>.
3620</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003621
Robert Griesemerc2d55862009-02-19 16:49:10 -08003622<pre>
3623for a &lt; b {
3624 a *= 2
3625}
3626</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003627
Rob Pike96750f12009-02-27 16:47:48 -08003628<p>
Rob Pike678625d2009-09-15 09:54:22 -07003629A "for" statement with a ForClause is also controlled by its condition, but
Rob Pike96750f12009-02-27 16:47:48 -08003630additionally it may specify an <i>init</i>
3631and a <i>post</i> statement, such as an assignment,
Russ Cox16b95ba2009-08-20 10:22:52 -07003632an increment or decrement statement. The init statement may be a
Robert Griesemer4ed666e2009-08-27 16:45:42 -07003633<a href="#Short_variable_declarations">short variable declaration</a>, but the post statement must not.
Rob Pike96750f12009-02-27 16:47:48 -08003634</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003635
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003636<pre class="ebnf">
Rob Pikef3a33bc2009-09-14 17:39:17 -07003637ForClause = InitStmt ";" [ Condition ] ";" PostStmt .
Rob Pike11417162009-03-24 17:45:53 -07003638InitStmt = SimpleStmt .
3639PostStmt = SimpleStmt .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003640</pre>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003641
Robert Griesemerc2d55862009-02-19 16:49:10 -08003642<pre>
3643for i := 0; i < 10; i++ {
3644 f(i)
3645}
3646</pre>
Russ Cox5958dd62009-03-04 17:19:21 -08003647
Robert Griesemerc2d55862009-02-19 16:49:10 -08003648<p>
Rob Pike96750f12009-02-27 16:47:48 -08003649If non-empty, the init statement is executed once before evaluating the
3650condition for the first iteration;
3651the post statement is executed after each execution of the block (and
3652only if the block was executed).
Rob Pike678625d2009-09-15 09:54:22 -07003653Any element of the ForClause may be empty but the semicolons are
Rob Pike96750f12009-02-27 16:47:48 -08003654required unless there is only a condition.
3655If the condition is absent, it is equivalent to <code>true</code>.
3656</p>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003657
Robert Griesemerc2d55862009-02-19 16:49:10 -08003658<pre>
Rob Pike678625d2009-09-15 09:54:22 -07003659for cond { S() } is the same as for ; cond ; { S() }
3660for { S() } is the same as for true { S() }
Robert Griesemerc2d55862009-02-19 16:49:10 -08003661</pre>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003662
Rob Pike96750f12009-02-27 16:47:48 -08003663<p>
3664A "for" statement with a "range" clause
Rob Pike7aee71b2009-04-15 20:28:25 -07003665iterates through all entries of an array, slice, string or map,
3666or values received on a channel.
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003667For each entry it first assigns the current index or key to an iteration
3668variable - or the current (index, element) or (key, value) pair to a pair
Rob Pike96750f12009-02-27 16:47:48 -08003669of iteration variables - and then executes the block.
3670</p>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003671
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003672<pre class="ebnf">
Rob Pikeb3408792009-04-15 20:51:17 -07003673RangeClause = ExpressionList ( "=" | ":=" ) "range" Expression .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003674</pre>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003675
Robert Griesemerc2d55862009-02-19 16:49:10 -08003676<p>
Rob Pike7aee71b2009-04-15 20:28:25 -07003677The type of the right-hand expression in the "range" clause must be an
Rob Pike678625d2009-09-15 09:54:22 -07003678array, slice, string or map, or a pointer to an array;
Rob Pike94b67eb2009-03-24 17:40:47 -07003679or it may be a channel.
Rob Pike7aee71b2009-04-15 20:28:25 -07003680Except for channels,
Rob Pikeb3408792009-04-15 20:51:17 -07003681the identifier list must contain one or two expressions
3682(as in assignments, these must be a
3683variable, pointer indirection, field selector, or index expression)
3684denoting the
Rob Pike96750f12009-02-27 16:47:48 -08003685iteration variables. On each iteration,
Rob Pike7aee71b2009-04-15 20:28:25 -07003686the first variable is set to the string, array or slice index or
Rob Pike96750f12009-02-27 16:47:48 -08003687map key, and the second variable, if present, is set to the corresponding
Rob Pike7aee71b2009-04-15 20:28:25 -07003688string or array element or map value.
Rob Pike96750f12009-02-27 16:47:48 -08003689The types of the array or slice index (always <code>int</code>)
3690and element, or of the map key and value respectively,
Robert Griesemer326ef132009-09-28 19:21:15 -07003691must be <a href="#Assignment_compatibility">assignment compatible</a> with
3692the type of the iteration variables.
Rob Pike96750f12009-02-27 16:47:48 -08003693</p>
3694<p>
Rob Pike7aee71b2009-04-15 20:28:25 -07003695For strings, the "range" clause iterates over the Unicode code points
3696in the string. On successive iterations, the index variable will be the
Rob Pike678625d2009-09-15 09:54:22 -07003697index of the first byte of successive UTF-8-encoded code points in the string, and
Rob Pike7aee71b2009-04-15 20:28:25 -07003698the second variable, of type <code>int</code>, will be the value of
3699the corresponding code point. If the iteration encounters an invalid
3700UTF-8 sequence, the second variable will be <code>0xFFFD</code>,
3701the Unicode replacement character, and the next iteration will advance
3702a single byte in the string.
3703</p>
3704<p>
Rob Pike94b67eb2009-03-24 17:40:47 -07003705For channels, the identifier list must contain one identifier.
Robert Griesemerce9fbdb2009-04-29 11:45:08 -07003706The iteration receives values sent on the channel until the channel is closed;
Rob Pike94b67eb2009-03-24 17:40:47 -07003707it does not process the zero value sent before the channel is closed.
3708</p>
3709<p>
Rob Pike96750f12009-02-27 16:47:48 -08003710The iteration variables may be declared by the "range" clause (":="), in which
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003711case their scope ends at the end of the "for" statement (§<a href="#Declarations_and">Declarations and</a>
Rob Pike96750f12009-02-27 16:47:48 -08003712scope rules). In this case their types are set to
Russ Cox5958dd62009-03-04 17:19:21 -08003713<code>int</code> and the array element type, or the map key and value types, respectively.
Rob Pike96750f12009-02-27 16:47:48 -08003714If the iteration variables are declared outside the "for" statement,
3715after execution their values will be those of the last iteration.
3716</p>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003717
Robert Griesemerc2d55862009-02-19 16:49:10 -08003718<pre>
3719var a [10]string;
Rob Pike426335f2009-03-02 17:52:52 -08003720m := map[string]int{"mon":0, "tue":1, "wed":2, "thu":3, "fri":4, "sat":5, "sun":6};
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003721
Robert Griesemerc2d55862009-02-19 16:49:10 -08003722for i, s := range a {
3723 // type of i is int
3724 // type of s is string
3725 // s == a[i]
3726 g(i, s)
3727}
3728
3729var key string;
Robert Griesemer19b1d352009-09-24 19:36:48 -07003730var val interface {}; // value type of m is assignment compatible to val
Rob Pike678625d2009-09-15 09:54:22 -07003731for key, val = range m {
3732 h(key, val)
Robert Griesemerc2d55862009-02-19 16:49:10 -08003733}
3734// key == last map key encountered in iteration
3735// val == map[key]
3736</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003737
Rob Pike96750f12009-02-27 16:47:48 -08003738<p>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08003739If map entries that have not yet been processed are deleted during iteration,
3740they will not be processed. If map entries are inserted during iteration, the
Russ Cox5958dd62009-03-04 17:19:21 -08003741behavior is implementation-dependent, but each entry will be processed at most once.
Rob Pike96750f12009-02-27 16:47:48 -08003742</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003743
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003744<h3 id="Go_statements">Go statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003745
Rob Pike96750f12009-02-27 16:47:48 -08003746<p>
Russ Cox5958dd62009-03-04 17:19:21 -08003747A "go" statement starts the execution of a function or method call
Rob Pike96750f12009-02-27 16:47:48 -08003748as an independent concurrent thread of control, or <i>goroutine</i>,
3749within the same address space.
3750</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003751
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003752<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003753GoStmt = "go" Expression .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003754</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003755
Rob Pike96750f12009-02-27 16:47:48 -08003756<p>
3757The expression must be a call, and
3758unlike with a regular call, program execution does not wait
Robert Griesemerb9f8b9c2008-09-26 13:38:38 -07003759for the invoked function to complete.
Rob Pike96750f12009-02-27 16:47:48 -08003760</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003761
Robert Griesemerc2d55862009-02-19 16:49:10 -08003762<pre>
3763go Server()
Robert Griesemere1e76192009-09-25 14:11:03 -07003764go func(ch chan&lt;- bool) { for { sleep(10); ch &lt;- true; }} (c)
Robert Griesemerc2d55862009-02-19 16:49:10 -08003765</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003766
3767
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003768<h3 id="Select_statements">Select statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003769
Rob Pike96750f12009-02-27 16:47:48 -08003770<p>
3771A "select" statement chooses which of a set of possible communications
3772will proceed. It looks similar to a "switch" statement but with the
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003773cases all referring to communication operations.
Rob Pike96750f12009-02-27 16:47:48 -08003774</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003775
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003776<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003777SelectStmt = "select" "{" { CommClause } "}" .
Russ Cox5958dd62009-03-04 17:19:21 -08003778CommClause = CommCase ":" StatementList .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003779CommCase = "case" ( SendExpr | RecvExpr) | "default" .
3780SendExpr = Expression "&lt;-" Expression .
3781RecvExpr = [ Expression ( "=" | ":=" ) ] "&lt;-" Expression .
3782</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003783
Rob Pike96750f12009-02-27 16:47:48 -08003784<p>
Rob Pike96750f12009-02-27 16:47:48 -08003785For all the send and receive expressions in the "select"
Rob Pike9d7538b2009-09-16 11:49:35 -07003786statement, the channel expressions are evaluated, along with
3787any expressions that appear on the right hand side of send expressions,
3788in top-to-bottom order.
3789If any of the resulting operations can proceed, one is
Rob Pike569a1072008-10-03 11:18:45 -07003790chosen and the corresponding communication and statements are
3791evaluated. Otherwise, if there is a default case, that executes;
Rob Pikecd368a22008-10-02 10:37:12 -07003792if not, the statement blocks until one of the communications can
Rob Pike569a1072008-10-03 11:18:45 -07003793complete. The channels and send expressions are not re-evaluated.
Rob Pike96750f12009-02-27 16:47:48 -08003794A channel pointer may be <code>nil</code>,
3795which is equivalent to that case not
3796being present in the select statement
3797except, if a send, its expression is still evaluated.
3798</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08003799<p>
Rob Pike569a1072008-10-03 11:18:45 -07003800Since all the channels and send expressions are evaluated, any side
3801effects in that evaluation will occur for all the communications
Rob Pike96750f12009-02-27 16:47:48 -08003802in the "select" statement.
3803</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08003804<p>
Rob Pike96750f12009-02-27 16:47:48 -08003805If multiple cases can proceed, a uniform fair choice is made to decide
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003806which single communication will execute.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003807<p>
Robert Griesemer4ed666e2009-08-27 16:45:42 -07003808The receive case may declare a new variable using a
3809<a href="#Short_variable_declarations">short variable declaration</a>.
Rob Pike96750f12009-02-27 16:47:48 -08003810</p>
Robert Griesemer2902a822008-09-17 13:57:11 -07003811
Robert Griesemerc2d55862009-02-19 16:49:10 -08003812<pre>
3813var c, c1, c2 chan int;
3814var i1, i2 int;
3815select {
3816case i1 = &lt;-c1:
3817 print("received ", i1, " from c1\n");
3818case c2 &lt;- i2:
3819 print("sent ", i2, " to c2\n");
3820default:
3821 print("no communication\n");
3822}
3823
3824for { // send random sequence of bits to c
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003825 select {
Robert Griesemerc2d55862009-02-19 16:49:10 -08003826 case c &lt;- 0: // note: no statement, no fallthrough, no folding of cases
3827 case c &lt;- 1:
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003828 }
Robert Griesemerc2d55862009-02-19 16:49:10 -08003829}
Robert Griesemerc2d55862009-02-19 16:49:10 -08003830</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003831
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003832
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003833<h3 id="Return_statements">Return statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003834
Rob Pike96750f12009-02-27 16:47:48 -08003835<p>
3836A "return" statement terminates execution of the containing function
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003837and optionally provides a result value or values to the caller.
Rob Pike96750f12009-02-27 16:47:48 -08003838</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003839
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003840<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003841ReturnStmt = "return" [ ExpressionList ] .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003842</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003843
Robert Griesemer4b908332009-08-07 17:05:41 -07003844<p>
3845In a function without a result type, a "return" statement must not
3846specify any result values.
3847</p>
Rob Pike96750f12009-02-27 16:47:48 -08003848<pre>
Robert Griesemer4b908332009-08-07 17:05:41 -07003849func no_result() {
Rob Pike96750f12009-02-27 16:47:48 -08003850 return
3851}
3852</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003853
Rob Pike96750f12009-02-27 16:47:48 -08003854<p>
Robert Griesemer4b908332009-08-07 17:05:41 -07003855There are three ways to return values from a function with a result
3856type:
Rob Pike96750f12009-02-27 16:47:48 -08003857</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003858
Robert Griesemer4b908332009-08-07 17:05:41 -07003859<ol>
3860 <li>The return value or values may be explicitly listed
3861 in the "return" statement. Each expression must be single-valued
Robert Griesemer326ef132009-09-28 19:21:15 -07003862 and <a href="#Assignment_compatibility">assignment compatible</a>
3863 with the type of the corresponding element of the function's
3864 result type.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003865<pre>
3866func simple_f() int {
Rob Pike96750f12009-02-27 16:47:48 -08003867 return 2
Robert Griesemerc2d55862009-02-19 16:49:10 -08003868}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003869
Robert Griesemerc2d55862009-02-19 16:49:10 -08003870func complex_f1() (re float, im float) {
Rob Pike96750f12009-02-27 16:47:48 -08003871 return -7.0, -4.0
Robert Griesemerc2d55862009-02-19 16:49:10 -08003872}
3873</pre>
Robert Griesemer4b908332009-08-07 17:05:41 -07003874 </li>
3875 <li>The expression list in the "return" statement may be a single
3876 call to a multi-valued function. The effect is as if each value
3877 returned from that function were assigned to a temporary
3878 variable with the type of the respective value, followed by a
3879 "return" statement listing these variables, at which point the
3880 rules of the previous case apply.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003881<pre>
3882func complex_f2() (re float, im float) {
Rob Pike96750f12009-02-27 16:47:48 -08003883 return complex_f1()
3884}
3885</pre>
Robert Griesemer4b908332009-08-07 17:05:41 -07003886 </li>
3887 <li>The expression list may be empty if the functions's result
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003888 type specifies names for its result parameters (§<a href="#Function_Types">Function Types</a>).
Robert Griesemer4b908332009-08-07 17:05:41 -07003889 The result parameters act as ordinary local variables that are
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003890 initialized to the zero values for their type (§<a href="#The_zero_value">The zero value</a>)
Robert Griesemer4b908332009-08-07 17:05:41 -07003891 and the function may assign values to them as necessary.
3892 The "return" statement returns the values of these variables.
Rob Pike96750f12009-02-27 16:47:48 -08003893<pre>
3894func complex_f3() (re float, im float) {
Robert Griesemerc2d55862009-02-19 16:49:10 -08003895 re = 7.0;
3896 im = 4.0;
3897 return;
3898}
3899</pre>
Robert Griesemer4b908332009-08-07 17:05:41 -07003900 </li>
3901</ol>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003902
Russ Cox5958dd62009-03-04 17:19:21 -08003903<p>
Robert Griesemer90cc4a52009-10-22 09:41:38 -07003904<span class="alert">
Robert Griesemer4b908332009-08-07 17:05:41 -07003905TODO: Define when return is required.<br />
3906TODO: Language about result parameters needs to go into a section on
3907 function/method invocation<br />
Robert Griesemer90cc4a52009-10-22 09:41:38 -07003908</span>
Russ Cox5958dd62009-03-04 17:19:21 -08003909</p>
3910
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003911<h3 id="Break_statements">Break statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003912
Rob Pike96750f12009-02-27 16:47:48 -08003913<p>
3914A "break" statement terminates execution of the innermost
3915"for", "switch" or "select" statement.
3916</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003917
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003918<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003919BreakStmt = "break" [ Label ].
Robert Griesemerc2d55862009-02-19 16:49:10 -08003920</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003921
Rob Pike96750f12009-02-27 16:47:48 -08003922<p>
3923If there is a label, it must be that of an enclosing
3924"for", "switch" or "select" statement, and that is the one whose execution
3925terminates
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003926<a href="#For_statements">For statements</a>, §<a href="#Switch_statements">Switch statements</a>, §<a href="#Select_statements">Select statements</a>).
Rob Pike96750f12009-02-27 16:47:48 -08003927</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003928
Robert Griesemerc2d55862009-02-19 16:49:10 -08003929<pre>
3930L: for i < n {
3931 switch i {
Rob Pike96750f12009-02-27 16:47:48 -08003932 case 5: break L
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003933 }
Robert Griesemerc2d55862009-02-19 16:49:10 -08003934}
3935</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003936
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003937<h3 id="Continue_statements">Continue statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003938
Rob Pike96750f12009-02-27 16:47:48 -08003939<p>
3940A "continue" statement begins the next iteration of the
Rob Pike678625d2009-09-15 09:54:22 -07003941innermost "for" loop at its post statement (§<a href="#For_statements">For statements</a>).
Rob Pike96750f12009-02-27 16:47:48 -08003942</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003943
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003944<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003945ContinueStmt = "continue" [ Label ].
Robert Griesemerc2d55862009-02-19 16:49:10 -08003946</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003947
Rob Pike96750f12009-02-27 16:47:48 -08003948<p>
3949The optional label is analogous to that of a "break" statement.
3950</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003951
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003952<h3 id="Goto_statements">Goto statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003953
Rob Pike96750f12009-02-27 16:47:48 -08003954<p>
3955A "goto" statement transfers control to the statement with the corresponding label.
3956</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003957
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003958<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003959GotoStmt = "goto" Label .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003960</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003961
Robert Griesemerc2d55862009-02-19 16:49:10 -08003962<pre>
3963goto Error
3964</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003965
Rob Pike96750f12009-02-27 16:47:48 -08003966<p>
3967Executing the "goto" statement must not cause any variables to come into
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003968scope that were not already in scope at the point of the goto. For
3969instance, this example:
Rob Pike96750f12009-02-27 16:47:48 -08003970</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003971
Robert Griesemerc2d55862009-02-19 16:49:10 -08003972<pre>
3973goto L; // BAD
3974v := 3;
3975L:
3976</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003977
Rob Pike96750f12009-02-27 16:47:48 -08003978<p>
3979is erroneous because the jump to label <code>L</code> skips
3980the creation of <code>v</code>.
Robert Griesemer90cc4a52009-10-22 09:41:38 -07003981(<span class="alert">TODO: Eliminate in favor of used and not set errors?</span>)
Rob Pike96750f12009-02-27 16:47:48 -08003982</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003983
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003984<h3 id="Fallthrough_statements">Fallthrough statements</h3>
Robert Griesemeraed247f2008-10-08 17:05:30 -07003985
Rob Pike96750f12009-02-27 16:47:48 -08003986<p>
3987A "fallthrough" statement transfers control to the first statement of the
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003988next case clause in a expression "switch" statement (§<a href="#Expression_switches">Expression switches</a>). It may
Robert Griesemer091cba82009-03-19 08:39:40 -07003989be used only as the final non-empty statement in a case or default clause in an
3990expression "switch" statement.
Rob Pike96750f12009-02-27 16:47:48 -08003991</p>
Robert Griesemeraed247f2008-10-08 17:05:30 -07003992
Robert Griesemerf7ac31362009-07-10 16:06:40 -07003993<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07003994FallthroughStmt = "fallthrough" .
Robert Griesemerc2d55862009-02-19 16:49:10 -08003995</pre>
Robert Griesemeraed247f2008-10-08 17:05:30 -07003996
3997
Russ Cox7c4f7cc2009-08-20 11:11:03 -07003998<h3 id="Defer_statements">Defer statements</h3>
Robert Griesemer4a903e02009-01-27 09:29:40 -08003999
Rob Pike96750f12009-02-27 16:47:48 -08004000<p>
4001A "defer" statement invokes a function whose execution is deferred to the moment
4002the surrounding function returns.
4003</p>
Robert Griesemer4a903e02009-01-27 09:29:40 -08004004
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004005<pre class="ebnf">
Rob Pike11417162009-03-24 17:45:53 -07004006DeferStmt = "defer" Expression .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004007</pre>
Robert Griesemer4a903e02009-01-27 09:29:40 -08004008
Rob Pike96750f12009-02-27 16:47:48 -08004009<p>
4010The expression must be a function or method call.
4011Each time the "defer" statement
Robert Griesemer7471eab2009-01-27 14:51:24 -08004012executes, the parameters to the function call are evaluated and saved anew but the
Rob Pike678625d2009-09-15 09:54:22 -07004013function is not invoked.
4014Deferred function calls are executed in LIFO order
4015immediately before the surrounding function returns,
4016but after the return values, if any, have been evaluated.
Rob Pike96750f12009-02-27 16:47:48 -08004017</p>
Robert Griesemer4a903e02009-01-27 09:29:40 -08004018
Robert Griesemerc2d55862009-02-19 16:49:10 -08004019<pre>
4020lock(l);
4021defer unlock(l); // unlocking happens before surrounding function returns
Robert Griesemer4a903e02009-01-27 09:29:40 -08004022
Robert Griesemerc2d55862009-02-19 16:49:10 -08004023// prints 3 2 1 0 before surrounding function returns
4024for i := 0; i &lt;= 3; i++ {
4025 defer fmt.Print(i);
4026}
4027</pre>
Robert Griesemer4a903e02009-01-27 09:29:40 -08004028
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004029<h2 id="Built-in_functions">Built-in functions</h2>
4030
4031<p>
4032A small number of built-in functions are
4033<a href="#Predeclared_identifiers">predeclared</a>.
4034They are called like any other function but some of them
4035accept a type instead of an expression as the first argument.
4036</p>
4037
4038<pre class="ebnf">
4039BuiltinCall = identifier "(" [ BuiltinArgs ] ")" .
4040BuiltinArgs = Type [ "," ExpressionList ] | ExpressionList .
4041</pre>
4042
4043<h3 id="Close_and_closed">Close and closed</h3>
4044
4045<p>
4046For a channel <code>c</code>, the predefined function <code>close(c)</code>
4047marks the channel as unable to accept more
4048values through a send operation. After any previously
4049sent values have been received, receive operations will return
4050the zero value for the channel's type. After at least one such zero value has been
4051received, <code>closed(c)</code> returns true.
4052</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004053
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004054<h3 id="Length_and_capacity">Length and capacity</h3>
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -07004055
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004056<p>
4057The built-in functions <code>len</code> and <code>cap</code> take arguments
4058of various types and return a result of type <code>int</code>.
4059The implementation guarantees that the result always fits into an <code>int</code>.
4060</p>
4061
Rob Pikeff70f092009-02-20 13:36:14 -08004062<pre class="grammar">
Robert Griesemerd3ffc5e2009-09-03 10:35:09 -07004063Call Argument type Result
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -07004064
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004065len(s) string type string length in bytes
Rob Pikedf3183f2009-02-26 16:37:23 -08004066 [n]T, *[n]T array length (== n)
Russ Coxfe537952009-08-20 10:47:40 -07004067 []T slice length
Rob Pike678625d2009-09-15 09:54:22 -07004068 map[K]T map length (number of defined keys)
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004069 chan T number of elements queued in channel buffer
Robert Griesemer4dc25282008-09-09 10:37:19 -07004070
Russ Coxfe537952009-08-20 10:47:40 -07004071cap(s) [n]T, *[n]T array length (== n)
4072 []T slice capacity
Rob Pikedf3183f2009-02-26 16:37:23 -08004073 chan T channel buffer capacity
Robert Griesemerc2d55862009-02-19 16:49:10 -08004074</pre>
Robert Griesemer4dc25282008-09-09 10:37:19 -07004075
Robert Griesemerc2d55862009-02-19 16:49:10 -08004076<p>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004077The capacity of a slice is the number of elements for which there is
4078space allocated in the underlying array.
4079At any time the following relationship holds:
4080</p>
Robert Griesemer633957b2009-01-06 13:23:20 -08004081
Robert Griesemerc2d55862009-02-19 16:49:10 -08004082<pre>
40830 <= len(s) <= cap(s)
4084</pre>
Robert Griesemer667ef6c2008-09-10 13:00:32 -07004085
Robert Griesemer4dc25282008-09-09 10:37:19 -07004086
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004087<h3 id="Allocation">Allocation</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004088
Rob Pike96750f12009-02-27 16:47:48 -08004089<p>
4090The built-in function <code>new</code> takes a type <code>T</code> and
4091returns a value of type <code>*T</code>.
Robert Griesemera3294712009-01-05 11:17:26 -08004092The memory is initialized as described in the section on initial values
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004093<a href="#The_zero_value">The zero value</a>).
Rob Pike96750f12009-02-27 16:47:48 -08004094</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004095
Robert Griesemerc2d55862009-02-19 16:49:10 -08004096<pre>
4097new(T)
4098</pre>
Robert Griesemer4dc25282008-09-09 10:37:19 -07004099
Rob Pike96750f12009-02-27 16:47:48 -08004100<p>
Robert Griesemer4dc25282008-09-09 10:37:19 -07004101For instance
Rob Pike96750f12009-02-27 16:47:48 -08004102</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004103
Robert Griesemerc2d55862009-02-19 16:49:10 -08004104<pre>
4105type S struct { a int; b float }
4106new(S)
4107</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004108
Rob Pike96750f12009-02-27 16:47:48 -08004109<p>
4110dynamically allocates memory for a variable of type <code>S</code>,
4111initializes it (<code>a=0</code>, <code>b=0.0</code>),
4112and returns a value of type <code>*S</code> containing the address
4113of the memory.
4114</p>
4115
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004116<h3 id="Making_slices_maps_and_channels">Making slices, maps and channels</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004117
Robert Griesemerc2d55862009-02-19 16:49:10 -08004118<p>
Rob Pike96750f12009-02-27 16:47:48 -08004119Slices, maps and channels are reference types that do not require the
4120extra indirection of an allocation with <code>new</code>.
4121The built-in function <code>make</code> takes a type <code>T</code>,
4122which must be a slice, map or channel type,
4123optionally followed by a type-specific list of expressions.
4124It returns a value of type <code>T</code> (not <code>*T</code>).
Robert Griesemer633957b2009-01-06 13:23:20 -08004125The memory is initialized as described in the section on initial values
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004126<a href="#The_zero_value">The zero value</a>).
Rob Pike96750f12009-02-27 16:47:48 -08004127</p>
Robert Griesemer633957b2009-01-06 13:23:20 -08004128
Robert Griesemerc2d55862009-02-19 16:49:10 -08004129<pre>
4130make(T [, optional list of expressions])
4131</pre>
Robert Griesemer633957b2009-01-06 13:23:20 -08004132
Rob Pike96750f12009-02-27 16:47:48 -08004133<p>
Robert Griesemer633957b2009-01-06 13:23:20 -08004134For instance
Rob Pike96750f12009-02-27 16:47:48 -08004135</p>
Robert Griesemer633957b2009-01-06 13:23:20 -08004136
Robert Griesemerc2d55862009-02-19 16:49:10 -08004137<pre>
4138make(map[string] int)
4139</pre>
Robert Griesemer633957b2009-01-06 13:23:20 -08004140
Rob Pike96750f12009-02-27 16:47:48 -08004141<p>
Robert Griesemer633957b2009-01-06 13:23:20 -08004142creates a new map value and initializes it to an empty map.
Rob Pike96750f12009-02-27 16:47:48 -08004143</p>
Robert Griesemer633957b2009-01-06 13:23:20 -08004144
Rob Pike96750f12009-02-27 16:47:48 -08004145<p>
4146The parameters affect sizes for allocating slices, maps, and
Robert Griesemer633957b2009-01-06 13:23:20 -08004147buffered channels:
Rob Pike96750f12009-02-27 16:47:48 -08004148</p>
Robert Griesemer633957b2009-01-06 13:23:20 -08004149
Robert Griesemerc2d55862009-02-19 16:49:10 -08004150<pre>
4151s := make([]int, 10, 100); # slice with len(s) == 10, cap(s) == 100
Russ Coxfe537952009-08-20 10:47:40 -07004152s := make([]int, 10); # slice with len(s) == cap(s) == 10
Robert Griesemerc2d55862009-02-19 16:49:10 -08004153c := make(chan int, 10); # channel with a buffer size of 10
4154m := make(map[string] int, 100); # map with initial space for 100 elements
4155</pre>
Robert Griesemer633957b2009-01-06 13:23:20 -08004156
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004157
4158<h3 id="Bootstrapping">Bootstrapping</h3>
4159
Robert Griesemer5eb36242009-09-16 11:05:14 -07004160<p>
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004161Current implementations provide several built-in functions useful during
4162bootstrapping. These functions are documented for completeness but are not
4163guaranteed to stay in the language. They do not return a result.
Robert Griesemer5eb36242009-09-16 11:05:14 -07004164</p>
4165
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004166<pre class="grammar">
Robert Griesemerd4d4ff02009-10-19 13:13:59 -07004167Function Behavior
Robert Griesemer164a7bc2009-09-30 12:00:25 -07004168
4169print prints all arguments; formatting of arguments is implementation-specific
4170println like print but prints spaces between arguments and a newline at the end
4171panic like print, aborts execution after printing
4172panicln like println, aborts execution after printing
4173</pre>
4174
4175
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004176<h2 id="Packages">Packages</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004177
Rob Pike46596852009-03-02 16:17:29 -08004178<p>
4179Go programs are constructed by linking together <i>packages</i>.
Robert Griesemer326ef132009-09-28 19:21:15 -07004180A package in turn is constructed from one or more source files
4181that together declare constants, types, variables and functions
4182belonging to the package and which are accessible in all files
4183of the same package. Those elements may be
4184<a href="#Exported_identifiers">exported</a> and used in another package.
Rob Pike46596852009-03-02 16:17:29 -08004185</p>
4186
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004187<h3 id="Source_file_organization">Source file organization</h3>
Rob Pike46596852009-03-02 16:17:29 -08004188
4189<p>
4190Each source file consists of a package clause defining the package
4191to which it belongs, followed by a possibly empty set of import
4192declarations that declare packages whose contents it wishes to use,
4193followed by a possibly empty set of declarations of functions,
Robert Griesemer0a162a12009-08-19 16:44:04 -07004194types, variables, and constants.
Rob Pike46596852009-03-02 16:17:29 -08004195</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004196
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004197<pre class="ebnf">
Robert Griesemer0a162a12009-08-19 16:44:04 -07004198SourceFile = PackageClause { ImportDecl [ ";" ] } { TopLevelDecl [ ";" ] } .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004199</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004200
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004201<h3 id="Package_clause">Package clause</h3>
Rob Pike46596852009-03-02 16:17:29 -08004202
Robert Griesemerc2d55862009-02-19 16:49:10 -08004203<p>
Rob Pike46596852009-03-02 16:17:29 -08004204A package clause begins each source file and defines the package
4205to which the file belongs.
4206</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004207
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004208<pre class="ebnf">
Robert Griesemer4e56b332009-09-10 10:14:00 -07004209PackageClause = "package" PackageName .
4210PackageName = identifier .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004211</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004212
Robert Griesemer4e56b332009-09-10 10:14:00 -07004213<p>
4214The PackageName must not be the <a href="#Blank_identifier">blank identifier</a>.
4215</p>
4216
Rob Pike46596852009-03-02 16:17:29 -08004217<pre>
4218package math
4219</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004220
Rob Pike46596852009-03-02 16:17:29 -08004221<p>
4222A set of files sharing the same PackageName form the implementation of a package.
4223An implementation may require that all source files for a package inhabit the same directory.
4224</p>
4225
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004226<h3 id="Import_declarations">Import declarations</h3>
Rob Pike46596852009-03-02 16:17:29 -08004227
4228<p>
Rob Pike3aec2e42009-09-25 17:00:22 -07004229An import declaration states that the source file containing the
4230declaration uses identifiers
4231<a href="#Exported_identifiers">exported</a> by the <i>imported</i>
4232package and enables access to them. The import names an
4233identifier (PackageName) to be used for access and an ImportPath
4234that specifies the package to be imported.
Rob Pike46596852009-03-02 16:17:29 -08004235</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004236
Robert Griesemerf7ac31362009-07-10 16:06:40 -07004237<pre class="ebnf">
Rob Pike46596852009-03-02 16:17:29 -08004238ImportDecl = "import" ( ImportSpec | "(" [ ImportSpecList ] ")" ) .
4239ImportSpecList = ImportSpec { ";" ImportSpec } [ ";" ] .
Robert Griesemer997851e2009-09-25 15:36:25 -07004240ImportSpec = [ "." | PackageName ] ImportPath .
4241ImportPath = StringLit .
Robert Griesemerc2d55862009-02-19 16:49:10 -08004242</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004243
Robert Griesemerc2d55862009-02-19 16:49:10 -08004244<p>
Rob Pike3aec2e42009-09-25 17:00:22 -07004245The PackageName is used in <a href="#Qualified_identifiers">qualified identifiers</a>
4246to access the exported identifiers of the package within the importing source file.
4247It is declared in the <a href="#Blocks">file block</a>.
4248If the PackageName is omitted, it defaults to the identifier specified in the
4249<a href="#Package_clauses">package clause</a> of the imported package.
4250If an explicit period (<code>.</code>) appears instead of a name, all the
4251package's exported identifiers will be declared in the current file's
4252file block and can be accessed without a qualifier.
Rob Pike46596852009-03-02 16:17:29 -08004253</p>
Russ Cox16b95ba2009-08-20 10:22:52 -07004254
Robert Griesemerc2d55862009-02-19 16:49:10 -08004255<p>
Rob Pike3aec2e42009-09-25 17:00:22 -07004256The interpretation of the ImportPath is implementation-dependent but
4257it is typically a substring of the full file name of the compiled
4258package and may be relative to a repository of installed packages.
4259</p>
4260
4261<p>
4262Assume we have compiled a package containing the package clause
4263<code>package math</code>, which exports function <code>Sin</code>, and
4264installed the compiled package in the file identified by
Rob Pike46596852009-03-02 16:17:29 -08004265<code>"lib/math"</code>.
Rob Pike3aec2e42009-09-25 17:00:22 -07004266This table illustrates how <code>Sin</code> may be accessed in files
4267that import the package after the
4268various types of import declaration.
Rob Pike46596852009-03-02 16:17:29 -08004269</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004270
Rob Pike46596852009-03-02 16:17:29 -08004271<pre class="grammar">
Robert Griesemer997851e2009-09-25 15:36:25 -07004272Import declaration Local name of Sin
Rob Pike46596852009-03-02 16:17:29 -08004273
Rob Pike46596852009-03-02 16:17:29 -08004274import "lib/math" math.Sin
Robert Griesemer997851e2009-09-25 15:36:25 -07004275import M "lib/math" M.Sin
Rob Pike46596852009-03-02 16:17:29 -08004276import . "lib/math" Sin
4277</pre>
4278
Robert Griesemer997851e2009-09-25 15:36:25 -07004279<p>
Rob Pike3aec2e42009-09-25 17:00:22 -07004280An import declaration declares a dependency relation between
4281the importing and imported package.
Robert Griesemer997851e2009-09-25 15:36:25 -07004282It is illegal for a package to import itself or to import a package without
4283referring to any of its exported identifiers. To import a package solely for
4284its side-effects (initialization), use the <a href="#Blank_identifier">blank</a>
4285identifier as explicit package name:
4286</p>
4287
4288<pre>
4289import _ "lib/math"
4290</pre>
4291
4292
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004293<h3 id="An_example_package">An example package</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004294
Robert Griesemerc2d55862009-02-19 16:49:10 -08004295<p>
Rob Pike46596852009-03-02 16:17:29 -08004296Here is a complete Go package that implements a concurrent prime sieve.
4297</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004298
Robert Griesemerc2d55862009-02-19 16:49:10 -08004299<pre>
4300package main
4301
Rob Pike46596852009-03-02 16:17:29 -08004302import "fmt"
4303
Robert Griesemerc2d55862009-02-19 16:49:10 -08004304// Send the sequence 2, 3, 4, ... to channel 'ch'.
Robert Griesemere1e76192009-09-25 14:11:03 -07004305func generate(ch chan&lt;- int) {
Robert Griesemerc2d55862009-02-19 16:49:10 -08004306 for i := 2; ; i++ {
Robert Griesemere1e76192009-09-25 14:11:03 -07004307 ch &lt;- i; // Send 'i' to channel 'ch'.
Robert Griesemerc2d55862009-02-19 16:49:10 -08004308 }
4309}
4310
4311// Copy the values from channel 'in' to channel 'out',
4312// removing those divisible by 'prime'.
Robert Griesemere1e76192009-09-25 14:11:03 -07004313func filter(src &lt;-chan int, dst chan&lt;- int, prime int) {
4314 for i := range src { // Loop over values received from 'src'.
4315 if i%prime != 0 {
4316 dst &lt;- i; // Send 'i' to channel 'dst'.
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004317 }
4318 }
Robert Griesemerc2d55862009-02-19 16:49:10 -08004319}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004320
Robert Griesemerc2d55862009-02-19 16:49:10 -08004321// The prime sieve: Daisy-chain filter processes together.
4322func sieve() {
Robert Griesemere1e76192009-09-25 14:11:03 -07004323 ch := make(chan int); // Create a new channel.
4324 go generate(ch); // Start generate() as a subprocess.
Robert Griesemerc2d55862009-02-19 16:49:10 -08004325 for {
Robert Griesemere1e76192009-09-25 14:11:03 -07004326 prime := &lt;-ch;
Rob Pike46596852009-03-02 16:17:29 -08004327 fmt.Print(prime, "\n");
Robert Griesemerc2d55862009-02-19 16:49:10 -08004328 ch1 := make(chan int);
4329 go filter(ch, ch1, prime);
Robert Griesemere1e76192009-09-25 14:11:03 -07004330 ch = ch1;
Robert Griesemerc2d55862009-02-19 16:49:10 -08004331 }
4332}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004333
Robert Griesemerc2d55862009-02-19 16:49:10 -08004334func main() {
Robert Griesemere1e76192009-09-25 14:11:03 -07004335 sieve();
Robert Griesemerc2d55862009-02-19 16:49:10 -08004336}
4337</pre>
Robert Griesemer67153582008-12-16 14:45:09 -08004338
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004339<h2 id="Program_initialization_and_execution">Program initialization and execution</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004340
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004341<h3 id="The_zero_value">The zero value</h3>
Rob Pike8f2330d2009-02-25 16:20:44 -08004342<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004343When memory is allocated to store a value, either through a declaration
Rob Pike678625d2009-09-15 09:54:22 -07004344or <code>make()</code> or <code>new()</code> call,
4345and no explicit initialization is provided, the memory is
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004346given a default initialization. Each element of such a value is
Robert Griesemeref45e642009-08-21 11:25:00 -07004347set to the <i>zero value</i> for its type: <code>false</code> for booleans,
Rob Pike8f2330d2009-02-25 16:20:44 -08004348<code>0</code> for integers, <code>0.0</code> for floats, <code>""</code>
Robert Griesemer19b1d352009-09-24 19:36:48 -07004349for strings, and <code>nil</code> for pointers, functions, interfaces, slices, channels, and maps.
Rob Pikeff70f092009-02-20 13:36:14 -08004350This initialization is done recursively, so for instance each element of an
Rob Pike8f2330d2009-02-25 16:20:44 -08004351array of structs will have its fields zeroed if no value is specified.
4352</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004353<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004354These two simple declarations are equivalent:
Rob Pike8f2330d2009-02-25 16:20:44 -08004355</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004356
Robert Griesemerc2d55862009-02-19 16:49:10 -08004357<pre>
4358var i int;
4359var i int = 0;
4360</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004361
Rob Pike8f2330d2009-02-25 16:20:44 -08004362<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004363After
Rob Pike8f2330d2009-02-25 16:20:44 -08004364</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004365
Robert Griesemerc2d55862009-02-19 16:49:10 -08004366<pre>
4367type T struct { i int; f float; next *T };
4368t := new(T);
4369</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004370
Rob Pike8f2330d2009-02-25 16:20:44 -08004371<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004372the following holds:
Rob Pike8f2330d2009-02-25 16:20:44 -08004373</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004374
Robert Griesemerc2d55862009-02-19 16:49:10 -08004375<pre>
4376t.i == 0
4377t.f == 0.0
4378t.next == nil
4379</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004380
Rob Pike96750f12009-02-27 16:47:48 -08004381<p>
4382The same would also be true after
4383</p>
4384
4385<pre>
4386var t T
4387</pre>
4388
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004389<h3 id="Program_execution">Program execution</h3>
Rob Pike8f2330d2009-02-25 16:20:44 -08004390<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004391A package with no imports is initialized by assigning initial values to
Rob Pike8cb91842009-09-15 11:56:39 -07004392all its package-level variables
Rob Pike678625d2009-09-15 09:54:22 -07004393and then calling any
Rob Pike96750f12009-02-27 16:47:48 -08004394package-level function with the name and signature of
4395</p>
4396<pre>
4397func init()
4398</pre>
4399<p>
4400defined in its source. Since a package may contain more
4401than one source file, there may be more than one
4402<code>init()</code> function in a package, but
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004403only one per source file.
Rob Pike8f2330d2009-02-25 16:20:44 -08004404</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004405<p>
Rob Pike8cb91842009-09-15 11:56:39 -07004406Within a package, package-level variables are initialized,
4407and constant values are determined, in
4408data-dependent order: if the initializer of <code>A</code>
4409depends on the value of <code>B</code>, <code>A</code>
4410will be set after <code>B</code>.
4411It is an error if such dependencies form a cycle.
4412Dependency analysis is done lexically: <code>A</code>
4413depends on <code>B</code> if the value of <code>A</code>
4414contains a mention of <code>B</code>, contains a value
4415whose initializer
4416mentions <code>B</code>, or mentions a function that
4417mentions <code>B</code>, recursively.
4418If two items are not interdependent, they will be initialized
4419in the order they appear in the source.
Rob Pike01cadde2009-09-15 15:56:44 -07004420Since the dependency analysis is done per package, it can produce
4421unspecified results if <code>A</code>'s initializer calls a function defined
Rob Pike8cb91842009-09-15 11:56:39 -07004422in another package that refers to <code>B</code>.
4423</p>
4424<p>
Robert Griesemer566e3b22008-09-26 16:41:50 -07004425Initialization code may contain "go" statements, but the functions
Robert Griesemerd8a764c2009-02-06 17:01:10 -08004426they invoke do not begin execution until initialization of the entire
4427program is complete. Therefore, all initialization code is run in a single
Rob Pike96750f12009-02-27 16:47:48 -08004428goroutine.
Rob Pike8f2330d2009-02-25 16:20:44 -08004429</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004430<p>
Rob Pike96750f12009-02-27 16:47:48 -08004431An <code>init()</code> function cannot be referred to from anywhere
4432in a program. In particular, <code>init()</code> cannot be called explicitly,
4433nor can a pointer to <code>init</code> be assigned to a function variable.
Rob Pike8f2330d2009-02-25 16:20:44 -08004434</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004435<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004436If a package has imports, the imported packages are initialized
Robert Griesemer566e3b22008-09-26 16:41:50 -07004437before initializing the package itself. If multiple packages import
Rob Pike96750f12009-02-27 16:47:48 -08004438a package <code>P</code>, <code>P</code> will be initialized only once.
Rob Pike8f2330d2009-02-25 16:20:44 -08004439</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004440<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004441The importing of packages, by construction, guarantees that there can
4442be no cyclic dependencies in initialization.
Rob Pike8f2330d2009-02-25 16:20:44 -08004443</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004444<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07004445A complete program, possibly created by linking multiple packages,
Rob Pike96750f12009-02-27 16:47:48 -08004446must have one package called <code>main</code>, with a function
Rob Pike8f2330d2009-02-25 16:20:44 -08004447</p>
Robert Griesemer4dc25282008-09-09 10:37:19 -07004448
Robert Griesemerc2d55862009-02-19 16:49:10 -08004449<pre>
Rob Pike96750f12009-02-27 16:47:48 -08004450func main() { ... }
Robert Griesemerc2d55862009-02-19 16:49:10 -08004451</pre>
Robert Griesemer4dc25282008-09-09 10:37:19 -07004452
Rob Pike8f2330d2009-02-25 16:20:44 -08004453<p>
Rob Pike96750f12009-02-27 16:47:48 -08004454defined.
4455The function <code>main.main()</code> takes no arguments and returns no value.
Rob Pike8f2330d2009-02-25 16:20:44 -08004456</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004457<p>
Rob Pike96750f12009-02-27 16:47:48 -08004458Program execution begins by initializing the <code>main</code> package and then
Rob Pike8f2330d2009-02-25 16:20:44 -08004459invoking <code>main.main()</code>.
4460</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004461<p>
Rob Pike46596852009-03-02 16:17:29 -08004462When <code>main.main()</code> returns, the program exits.
Rob Pike8f2330d2009-02-25 16:20:44 -08004463</p>
Rob Pike811dd252009-03-04 20:39:39 -08004464<p>
4465Implementation restriction: The compiler assumes package <code>main</code>
Robert Griesemer4023dce2009-08-14 17:41:52 -07004466is not imported by any other package.
Rob Pike811dd252009-03-04 20:39:39 -08004467</p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004468
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004469<h2 id="System_considerations">System considerations</h2>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004470
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004471<h3 id="Package_unsafe">Package <code>unsafe</code></h3>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004472
Robert Griesemerc2d55862009-02-19 16:49:10 -08004473<p>
Rob Pike96750f12009-02-27 16:47:48 -08004474The built-in package <code>unsafe</code>, known to the compiler,
4475provides facilities for low-level programming including operations
4476that violate the type system. A package using <code>unsafe</code>
4477must be vetted manually for type safety. The package provides the
4478following interface:
Rob Pikef27e9f02009-02-23 19:22:05 -08004479</p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004480
Rob Pikeff70f092009-02-20 13:36:14 -08004481<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08004482package unsafe
Robert Griesemer52c02c22009-02-11 13:46:30 -08004483
Robert Griesemer98b4f6a2009-05-12 21:37:46 -07004484type ArbitraryType int // shorthand for an arbitrary Go type; it is not a real type
4485type Pointer *ArbitraryType
Robert Griesemer52c02c22009-02-11 13:46:30 -08004486
Robert Griesemer98b4f6a2009-05-12 21:37:46 -07004487func Alignof(variable ArbitraryType) int
4488func Offsetof(selector ArbitraryType) int
Robert Griesemer98b4f6a2009-05-12 21:37:46 -07004489func Sizeof(variable ArbitraryType) int
Rob Pike678625d2009-09-15 09:54:22 -07004490
4491func Reflect(val interface {}) (typ runtime.Type, addr uintptr)
4492func Typeof(val interface {}) reflect.Type
4493func Unreflect(typ runtime.Type, addr uintptr) interface{}
Robert Griesemerc2d55862009-02-19 16:49:10 -08004494</pre>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004495
Robert Griesemerc2d55862009-02-19 16:49:10 -08004496<p>
Rob Pikef27e9f02009-02-23 19:22:05 -08004497Any pointer or value of type <code>uintptr</code> can be converted into
4498a <code>Pointer</code> and vice versa.
4499</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004500<p>
Rob Pikef27e9f02009-02-23 19:22:05 -08004501The function <code>Sizeof</code> takes an expression denoting a
Robert Griesemer4023dce2009-08-14 17:41:52 -07004502variable of any type and returns the size of the variable in bytes.
Rob Pikef27e9f02009-02-23 19:22:05 -08004503</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004504<p>
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004505The function <code>Offsetof</code> takes a selector (§<a href="#Selectors">Selectors</a>) denoting a struct
Robert Griesemer52c02c22009-02-11 13:46:30 -08004506field of any type and returns the field offset in bytes relative to the
Rob Pike678625d2009-09-15 09:54:22 -07004507struct's address.
4508For a struct <code>s</code> with field <code>f</code>:
Rob Pikef27e9f02009-02-23 19:22:05 -08004509</p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004510
Robert Griesemerc2d55862009-02-19 16:49:10 -08004511<pre>
Rob Pikef27e9f02009-02-23 19:22:05 -08004512uintptr(unsafe.Pointer(&amp;s)) + uintptr(unsafe.Offsetof(s.f)) == uintptr(unsafe.Pointer(&amp;s.f))
Robert Griesemerc2d55862009-02-19 16:49:10 -08004513</pre>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004514
Robert Griesemerc2d55862009-02-19 16:49:10 -08004515<p>
Rob Pikef27e9f02009-02-23 19:22:05 -08004516Computer architectures may require memory addresses to be <i>aligned</i>;
4517that is, for addresses of a variable to be a multiple of a factor,
4518the variable's type's <i>alignment</i>. The function <code>Alignof</code>
4519takes an expression denoting a variable of any type and returns the
4520alignment of the (type of the) variable in bytes. For a variable
4521<code>x</code>:
4522</p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004523
Robert Griesemerc2d55862009-02-19 16:49:10 -08004524<pre>
4525uintptr(unsafe.Pointer(&amp;x)) % uintptr(unsafe.Alignof(x)) == 0
4526</pre>
Robert Griesemer533dfd62009-05-13 16:56:00 -07004527
Rob Pikef27e9f02009-02-23 19:22:05 -08004528<p>
4529Calls to <code>Alignof</code>, <code>Offsetof</code>, and
Rob Pike678625d2009-09-15 09:54:22 -07004530<code>Sizeof</code> are compile-time constant expressions of type <code>int</code>.
Rob Pikef27e9f02009-02-23 19:22:05 -08004531</p>
Robert Griesemer98b4f6a2009-05-12 21:37:46 -07004532<p>
Rob Pike678625d2009-09-15 09:54:22 -07004533The functions <code>unsafe.Typeof</code>,
4534<code>unsafe.Reflect</code>,
Russ Cox13dac652009-09-28 14:16:33 -07004535and <code>unsafe.Unreflect</code> allow access at run time to the dynamic
Rob Pike678625d2009-09-15 09:54:22 -07004536types and values stored in interfaces.
4537<code>Typeof</code> returns a representation of
4538<code>val</code>'s
4539dynamic type as a <code>runtime.Type</code>.
4540<code>Reflect</code> allocates a copy of
4541<code>val</code>'s dynamic
4542value and returns both the type and the address of the copy.
4543<code>Unreflect</code> inverts <code>Reflect</code>,
4544creating an
4545interface value from a type and address.
4546The <code>reflect</code> package built on these primitives
4547provides a safe, more convenient way to inspect interface values.
Robert Griesemer98b4f6a2009-05-12 21:37:46 -07004548</p>
Robert Griesemer6f8df7a2009-02-11 21:57:15 -08004549
Robert Griesemer52c02c22009-02-11 13:46:30 -08004550
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004551<h3 id="Size_and_alignment_guarantees">Size and alignment guarantees</h3>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004552
Robert Griesemer997851e2009-09-25 15:36:25 -07004553<p>
Russ Cox7c4f7cc2009-08-20 11:11:03 -07004554For the numeric types (§<a href="#Numeric_types">Numeric types</a>), the following sizes are guaranteed:
Robert Griesemer997851e2009-09-25 15:36:25 -07004555</p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004556
Rob Pikeff70f092009-02-20 13:36:14 -08004557<pre class="grammar">
Robert Griesemerc2d55862009-02-19 16:49:10 -08004558type size in bytes
Robert Griesemer52c02c22009-02-11 13:46:30 -08004559
Robert Griesemerc2d55862009-02-19 16:49:10 -08004560byte, uint8, int8 1
4561uint16, int16 2
4562uint32, int32, float32 4
4563uint64, int64, float64 8
4564</pre>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004565
Robert Griesemerc2d55862009-02-19 16:49:10 -08004566<p>
Rob Pikef27e9f02009-02-23 19:22:05 -08004567The following minimal alignment properties are guaranteed:
Rob Pike4501d342009-02-19 17:31:36 -08004568</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08004569<ol>
Rob Pikef27e9f02009-02-23 19:22:05 -08004570<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 -08004571
Russ Cox5958dd62009-03-04 17:19:21 -08004572<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 -08004573 of <code>unsafe.Sizeof(x)</code> and <code>unsafe.Maxalign</code>, but at least 1.
Robert Griesemer52c02c22009-02-11 13:46:30 -08004574
Rob Pikef27e9f02009-02-23 19:22:05 -08004575<li>For a variable <code>x</code> of struct type: <code>unsafe.Alignof(x)</code> is the largest of
4576 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 -08004577
Rob Pikef27e9f02009-02-23 19:22:05 -08004578<li>For a variable <code>x</code> of array type: <code>unsafe.Alignof(x)</code> is the same as
4579 <code>unsafe.Alignof(x[0])</code>, but at least 1.
Robert Griesemerc2d55862009-02-19 16:49:10 -08004580</ol>
Robert Griesemer52c02c22009-02-11 13:46:30 -08004581
Robert Griesemer90cc4a52009-10-22 09:41:38 -07004582<h2 id="Implementation_differences"><span class="alert">Implementation differences - TODO</span></h2>
Robert Griesemer237c8ab2009-09-01 14:07:30 -07004583<ul>
Robert Griesemer90cc4a52009-10-22 09:41:38 -07004584 <li><span class="alert">Implementation does not honor the restriction on goto statements and targets (no intervening declarations).</span></li>
4585 <li><span class="alert">Gccgo does not implement the blank identifier.</span></li>
4586 <li><span class="alert">Method expressions are not implemented.</span></li>
Robert Griesemer237c8ab2009-09-01 14:07:30 -07004587</ul>