blob: 368f5c51bd0a20a8c83f056ed27919ee5a4a7edd [file] [log] [blame]
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002
Robert Griesemerc59d2f12008-09-09 10:48:14 -07003<!--
Robert Griesemer7471eab2009-01-27 14:51:24 -08004Biggest open issues:
Robert Griesemer6f8df7a2009-02-11 21:57:15 -08005[ ] General iterators
Robert Griesemer7471eab2009-01-27 14:51:24 -08006[ ] Conversions:
7 - current situation is messy
8 - 2 (3?) different notations for the same thing
9 - unclear when a type guard is needed
10 - unclear where conversions can be applied
11 - for type T int; can we say T(3.0) ?
12 - do we need channel conversion (channel direction)
13[ ] Semantics of type declaration:
14 - creating a new type (status quo), or only a new type name?
15 - also: declaration type T S; strips methods of S. why/why not?
16
17
Robert Griesemer1593ab62009-01-16 15:36:46 -080018Decisions in need of integration into the doc:
19[ ] pair assignment is required to get map, and receive ok.
20[ ] len() returns an int, new(array_type, n) n must be an int
Robert Griesemer57b34612008-10-10 12:45:44 -070021
22
Robert Griesemer57b34612008-10-10 12:45:44 -070023Todo's:
Robert Griesemerd8a764c2009-02-06 17:01:10 -080024[ ] there is some funny-ness regarding ';' and empty statements and label decls
Robert Griesemer9f4a27c2009-01-16 15:44:08 -080025[ ] document illegality of package-external tuple assignments to structs
Robert Griesemer6f8df7a2009-02-11 21:57:15 -080026 w/ private fields: P.T(1, 2) illegal since same as P.T(a: 1, b: 2) for
Robert Griesemer9f4a27c2009-01-16 15:44:08 -080027 a T struct { a b int }.
Robert Griesemerc59d2f12008-09-09 10:48:14 -070028[ ] clarification on interface types, rules
Robert Griesemer57b34612008-10-10 12:45:44 -070029[ ] clarify tuples
30[ ] need to talk about precise int/floats clearly
31[ ] iant suggests to use abstract/precise int for len(), cap() - good idea
32 (issue: what happens in len() + const - what is the type?)
Robert Griesemer1593ab62009-01-16 15:36:46 -080033[ ] cleanup convert() vs T() vs x.(T) - convert() should go away?
Robert Griesemer1593ab62009-01-16 15:36:46 -080034[ ] fix "else" part of if statement
35[ ] cleanup: 6g allows: interface { f F } where F is a function type.
36 fine, but then we should also allow: func f F {}, where F is a function type.
Robert Griesemer57b34612008-10-10 12:45:44 -070037
38
Robert Griesemer7471eab2009-01-27 14:51:24 -080039Wish list:
40[ ] enum facility (enum symbols that are not mixable with ints) or some other
41 mechanism to obtain type-safety which we don't have with int-only tags
42[ ] Gri: built-in assert() - alternatively: allow entire expressions
43 as statements so we can write: some_condition || panic(); (along these lines)
44[ ] Helper syntax for composite types: allow names/keys/indices for
45 structs/maps/arrays, remove need for type in elements of composites
46
47
48Smaller issues:
Robert Griesemer1593ab62009-01-16 15:36:46 -080049[ ] need for type switch? (or use type guard with ok in tuple assignment?)
Robert Griesemerebf14c62008-10-30 14:50:23 -070050[ ] Is . import implemented / do we still need it?
Robert Griesemer7271e042008-10-09 20:05:24 -070051[ ] Do we allow empty statements? If so, do we allow empty statements after a label?
52 and if so, does a label followed by an empty statement (a semicolon) still denote
Robert Griesemer6f8df7a2009-02-11 21:57:15 -080053 a for loop that is following, and can break L be used inside it?
Robert Griesemerac055792008-09-26 11:15:14 -070054
Robert Griesemer57b34612008-10-10 12:45:44 -070055
56Closed:
Robert Griesemer6f8df7a2009-02-11 21:57:15 -080057[x] Russ: If we use x.(T) for all conversions, we could use T() for "construction"
58 and type literals - would resolve the parsing ambiguity of T{} in if's -
59 switching to () for literals, conversion discussion still open
Robert Griesemerd8a764c2009-02-06 17:01:10 -080060[x] Russ: consider re-introducing "func" for function type. Make function literals
61 behave like slices, etc. Require no &'s to get a function value (solves issue
62 of func{} vs &func{} vs &func_name).
Robert Griesemer7471eab2009-01-27 14:51:24 -080063[x] onreturn/undo statement - now: defer statement
64[x] comparison of non-basic types: what do we allow? what do we allow in interfaces
65 what about maps (require ==, copy and hash)
66 maybe: no maps with non-basic type keys, and no interface comparison unless
67 with nil[x]
Robert Griesemer18b05c12009-01-26 09:34:19 -080068[x] clarify slice rules
69[x] what are the permissible ranges for the indices in slices? The spec
70 doesn't correspond to the implementation. The spec is wrong when it
71 comes to the first index i: it should allow (at least) the range 0 <= i <= len(a).
72 also: document different semantics for strings and arrays (strings cannot be grown).
Robert Griesemer1593ab62009-01-16 15:36:46 -080073[x] reopening & and func issue: Seems inconsistent as both &func(){} and func(){} are
74 permitted. Suggestion: func literals are pointers. We need to use & for all other
75 functions. This would be in consistency with the declaration of function pointer
76 variables and the use of '&' to convert methods into function pointers.
77 - covered by other entry
78[x] composite types should uniformly create an instance instead of a pointer - fixed
79[x] like to have assert() in the language, w/ option to disable code gen for it
80 - added to wish list
81[x] convert should not be used for composite literals anymore,
82 in fact, convert() should go away - made a todo
83[x] type switch or some form of type test needed - duplicate entry
84[x] provide composite literal notation to address array indices: []int{ 0: x1, 1: x2, ... }
85 and struct field names (both seem easy to do). - under "Missing" list
86[x] passing a "..." arg to another "..." parameter doesn't wrap the argument again
87 (so "..." args can be passed down easily) - this is documented
88[x] consider syntactic notation for composite literals to make them parseable w/o type information
89 (require ()'s in control clauses) - use heuristics for now
90[x] do we need anything on package vs file names? - current package scheme workable for now
91[x] what is the meaning of typeof() - we don't have it
92[x] old-style export decls (still needed, but ideally should go away)
Robert Griesemer83c17602009-01-16 14:12:50 -080093[x] packages of multiple files - we have a working approach
94[x] partial export of structs, methods
Robert Griesemer91bbd642009-01-07 09:31:35 -080095[x] new as it is now is weird - need to go back to previous semantics and introduce
96 literals for slices, maps, channels - done
97[x] determine if really necessary to disallow array assignment - allow array assignment
Robert Griesemer30a1a8c2008-12-16 11:38:56 -080098[x] semantics of statements - we just need to fill in the language, the semantics is mostly clear
99[x] range statement: to be defined more reasonably
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -0800100[x] need to be specific on (unsigned) integer operations: one must be able
101 to rely on wrap-around on overflow
Robert Griesemer7354b862008-12-04 17:33:37 -0800102[x] global var decls: "var a, b, c int = 0, 0, 0" is ok, but "var a, b, c = 0, 0, 0" is not
103 (seems inconsistent with "var a = 0", and ":=" notation)
104[x] const decls: "const a, b = 1, 2" is not allowed - why not? Should be symmetric to vars.
Robert Griesemerf618f892008-11-03 10:52:28 -0800105[x] new(arraytype, n1, n2): spec only talks about length, not capacity
106 (should only use new(arraytype, n) - this will allow later
107 extension to multi-dim arrays w/o breaking the language) - documented
Robert Griesemerebf14c62008-10-30 14:50:23 -0700108[x] should we have a shorter list of alias types? (byte, int, uint, float) - done
109[x] reflection support
110[x] syntax for var args
Robert Griesemer47121652008-10-23 17:19:56 -0700111[x] Do composite literals create a new literal each time (gri thinks yes) (Russ is putting in a change
112 to this effect, essentially)
Robert Griesemerb5e0cc72008-10-10 16:34:44 -0700113[x] comparison operators: can we compare interfaces?
Robert Griesemer57b34612008-10-10 12:45:44 -0700114[x] can we add methods to types defined in another package? (probably not)
Robert Griesemer7abfcd92008-10-07 17:14:30 -0700115[x] optional semicolons: too complicated and unclear
Robert Griesemer347cf672008-10-03 14:04:28 -0700116[x] anonymous types are written using a type name, which can be a qualified identifier.
117 this might be a problem when referring to such a field using the type name.
Robert Griesemer52a54802008-09-30 13:02:50 -0700118[x] nil and interfaces - can we test for nil, what does it mean, etc.
119[x] talk about underflow/overflow of 2's complement numbers (defined vs not defined).
Robert Griesemerc59b2a32008-09-30 10:57:59 -0700120[x] change wording on array composite literals: the types are always fixed arrays
121 for array composites
Robert Griesemera1065fa2008-09-29 20:37:46 -0700122[x] meaning of nil
Robert Griesemerac055792008-09-26 11:15:14 -0700123[x] remove "any"
124[x] methods for all types
125[x] should binary <- be at lowest precedence level? when is a send/receive non-blocking? (NO - 9/19/08)
126[x] func literal like a composite type - should probably require the '&' to get address (NO)
127[x] & needed to get a function pointer from a function? (NO - there is the "func" keyword - 9/19/08)
Robert Griesemer1593ab62009-01-16 15:36:46 -0800128
129Timeline (9/5/08):
130- threads: 1 month
131- reflection code: 2 months
132- proto buf support: 3 months
133- GC: 6 months
134- debugger
135- Jan 1, 2009: enough support to write interesting programs
Robert Griesemerc59d2f12008-09-09 10:48:14 -0700136-->
137
Robert Griesemerc2d55862009-02-19 16:49:10 -0800138<h2>Introduction</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700139
Robert Griesemerc2d55862009-02-19 16:49:10 -0800140<p>
Rob Pike4501d342009-02-19 17:31:36 -0800141This is a reference manual for the Go programming language. For
142more information and other documents, see <a
143href="/">the Go home page</a>.
144</p>
Robert Griesemer67153582008-12-16 14:45:09 -0800145
Robert Griesemerc2d55862009-02-19 16:49:10 -0800146<p>
Rob Pike4501d342009-02-19 17:31:36 -0800147Go is a general-purpose language designed with systems programming
148in mind. It is strongly typed and garbage-collected, and has explicit
149support for concurrent programming. Programs are constructed from
150<i>packages</i>, whose properties allow efficient management of
151dependencies. The existing implementations use a traditional
152compile/link model to generate executable binaries.
153</p>
154
Robert Griesemerc2d55862009-02-19 16:49:10 -0800155<p>
Rob Pike4501d342009-02-19 17:31:36 -0800156The grammar is compact and regular, allowing for easy analysis by
157automatic tools such as integrated development environments.
158</p>
Rob Pikefd1f3832009-02-19 19:49:56 -0800159<hr>
Robert Griesemerc2d55862009-02-19 16:49:10 -0800160<h2>Notation</h2>
Rob Pike4501d342009-02-19 17:31:36 -0800161<p>
Robert Griesemer4d230302008-12-17 15:39:15 -0800162The syntax is specified using Extended Backus-Naur Form (EBNF):
Rob Pike4501d342009-02-19 17:31:36 -0800163</p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700164
Robert Griesemerc2d55862009-02-19 16:49:10 -0800165<pre>
Rob Pike4501d342009-02-19 17:31:36 -0800166Production = production_name "=" Expression .
167Expression = Alternative { "|" Alternative } .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800168Alternative = Term { Term } .
Rob Pike4501d342009-02-19 17:31:36 -0800169Term = production_name | token [ "..." token ] | Group | Option | Repetition .
170Group = "(" Expression ")" .
171Option = "[" Expression ")" .
172Repetition = "{" Expression "}" .
Robert Griesemerc2d55862009-02-19 16:49:10 -0800173</pre>
Robert Griesemerbbfe3122008-10-09 17:12:09 -0700174
Rob Pike4501d342009-02-19 17:31:36 -0800175<p>
176Productions are expressions constructed from terms and the following
177operators, in increasing precedence:
178</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -0800179<pre>
Rob Pike4501d342009-02-19 17:31:36 -0800180| alternation
181() grouping
182[] option (0 or 1 times)
183{} repetition (0 to n times)
Robert Griesemerc2d55862009-02-19 16:49:10 -0800184</pre>
Robert Griesemer4d230302008-12-17 15:39:15 -0800185
Robert Griesemerc2d55862009-02-19 16:49:10 -0800186<p>
Rob Pike4501d342009-02-19 17:31:36 -0800187Lower-case production names are used to identify lexical tokens.
188Non-terminals are in CamelCase. Lexical symbols are enclosed in
189double quotes <tt>""</tt> (the double quote symbol is written as
190<tt>'"'</tt>).
191</p>
192
Robert Griesemerc2d55862009-02-19 16:49:10 -0800193<p>
Rob Pike4501d342009-02-19 17:31:36 -0800194The form <tt>"a ... b"</tt> represents the set of characters from
195<tt>a</tt> through <tt>b</tt> as alternatives.
196</p>
197
Robert Griesemerc2d55862009-02-19 16:49:10 -0800198<p>
Robert Griesemer57b34612008-10-10 12:45:44 -0700199Where possible, recursive productions are used to express evaluation order
Rob Pike4501d342009-02-19 17:31:36 -0800200and operator precedence syntactically.
201</p>
Rob Pikefd1f3832009-02-19 19:49:56 -0800202<hr>
Robert Griesemer7abfcd92008-10-07 17:14:30 -0700203
Robert Griesemerc2d55862009-02-19 16:49:10 -0800204<h2>Source code representation</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700205
206Source code is Unicode text encoded in UTF-8.
Robert Griesemerc2d55862009-02-19 16:49:10 -0800207<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700208Tokenization follows the usual rules. Source text is case-sensitive.
Robert Griesemerc2d55862009-02-19 16:49:10 -0800209<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700210White space is blanks, newlines, carriage returns, or tabs.
Robert Griesemerc2d55862009-02-19 16:49:10 -0800211<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700212Comments are // to end of line or /* */ without nesting and are treated as white space.
Robert Griesemerc2d55862009-02-19 16:49:10 -0800213<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700214Some Unicode characters (e.g., the character U+00E4) may be representable in
215two forms, as a single code point or as two code points. For simplicity of
Robert Griesemer83c17602009-01-16 14:12:50 -0800216implementation, Go treats these as distinct characters: each Unicode code
217point is a single character in Go.
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700218
219
Robert Griesemerc2d55862009-02-19 16:49:10 -0800220<h3>Characters</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700221
Robert Griesemerc2d55862009-02-19 16:49:10 -0800222<p>
Rob Pike4501d342009-02-19 17:31:36 -0800223The following terms are used to denote specific Unicode character classes:
224</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -0800225<ul>
226 <li>unicode_char an arbitrary Unicode code point
227 <li>unicode_letter a Unicode code point classified as "Letter"
228 <li>capital_letter a Unicode code point classified as "Letter, uppercase"
229</ul>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700230
Robert Griesemer83c17602009-01-16 14:12:50 -0800231(The Unicode Standard, Section 4.5 General Category - Normative.)
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700232
233
Robert Griesemerc2d55862009-02-19 16:49:10 -0800234<h3>Letters and digits</h3>
235<pre>
236letter = unicode_letter | "_" .
237decimal_digit = "0" ... "9" .
238octal_digit = "0" ... "7" .
239hex_digit = "0" ... "9" | "A" ... "F" | "a" ... "f" .
240</pre>
241<hr>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700242
Robert Griesemerc2d55862009-02-19 16:49:10 -0800243<h2>Vocabulary</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700244
245Tokens make up the vocabulary of the Go language. They consist of
246identifiers, numbers, strings, operators, and delimitors.
247
248
Robert Griesemerc2d55862009-02-19 16:49:10 -0800249<h3>Identifiers</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700250
251An identifier is a name for a program entity such as a variable, a
252type, a function, etc.
Robert Griesemerc2d55862009-02-19 16:49:10 -0800253<pre>
254identifier = letter { letter | decimal_digit } .
255</pre>
Robert Griesemer83c17602009-01-16 14:12:50 -0800256Exported identifiers (§Exported identifiers) start with a capital_letter.
Robert Griesemerc2d55862009-02-19 16:49:10 -0800257<pre>
258a
259_x9
260ThisVariableIsExported
261αβ
262</pre>
Robert Griesemer83c17602009-01-16 14:12:50 -0800263Some identifiers are predeclared (§Predeclared identifiers).
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -0700264
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700265
Robert Griesemerc2d55862009-02-19 16:49:10 -0800266<h3>Numeric literals</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700267
Robert Griesemerad711102008-09-11 17:48:20 -0700268An integer literal represents a mathematically ideal integer constant
269of arbitrary precision, or 'ideal int'.
Robert Griesemerc2d55862009-02-19 16:49:10 -0800270<pre>
271int_lit = decimal_int | octal_int | hex_int .
272decimal_int = ( "1" ... "9" ) { decimal_digit } .
273octal_int = "0" { octal_digit } .
274hex_int = "0" ( "x" | "X" ) hex_digit { hex_digit } .
275</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700276
Robert Griesemerc2d55862009-02-19 16:49:10 -0800277<pre>
27842
2790600
2800xBadFace
281170141183460469231731687303715884105727
282</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700283
Robert Griesemerad711102008-09-11 17:48:20 -0700284A floating point literal represents a mathematically ideal floating point
285constant of arbitrary precision, or 'ideal float'.
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700286
Robert Griesemerc2d55862009-02-19 16:49:10 -0800287<pre>
288float_lit =
289 decimals "." [ decimals ] [ exponent ] |
290 decimals exponent |
291 "." decimals [ exponent ] .
292decimals = decimal_digit { decimal_digit } .
293exponent = ( "e" | "E" ) [ "+" | "-" ] decimals .
294</pre>
Robert Griesemerad711102008-09-11 17:48:20 -0700295
Robert Griesemerc2d55862009-02-19 16:49:10 -0800296<pre>
2970.
2982.71828
2991.e+0
3006.67428e-11
3011E6
302.25
303.12345E+5
304</pre>
Robert Griesemerad711102008-09-11 17:48:20 -0700305
306Numeric literals are unsigned. A negative constant is formed by
307applying the unary prefix operator "-" (§Arithmetic operators).
Robert Griesemerc2d55862009-02-19 16:49:10 -0800308<p>
Robert Griesemerad711102008-09-11 17:48:20 -0700309An 'ideal number' is either an 'ideal int' or an 'ideal float'.
Robert Griesemerc2d55862009-02-19 16:49:10 -0800310<p>
Robert Griesemerad711102008-09-11 17:48:20 -0700311Only when an ideal number (or an arithmetic expression formed
312solely from ideal numbers) is bound to a variable or used in an expression
313or constant of fixed-size integers or floats it is required to fit
314a particular size. In other words, ideal numbers and arithmetic
315upon them are not subject to overflow; only use of them in assignments
316or expressions involving fixed-size numbers may cause overflow, and thus
317an error (§Expressions).
Robert Griesemerc2d55862009-02-19 16:49:10 -0800318<p>
Robert Griesemerad711102008-09-11 17:48:20 -0700319Implementation restriction: A compiler may implement ideal numbers
320by choosing a "sufficiently large" internal representation of such
321numbers.
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700322
323
Robert Griesemerc2d55862009-02-19 16:49:10 -0800324<h3>Character and string literals</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700325
Rob Pike4501d342009-02-19 17:31:36 -0800326<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700327Character and string literals are almost the same as in C, with the
328following differences:
Rob Pike4501d342009-02-19 17:31:36 -0800329</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -0800330<ul>
331 <li>The encoding is UTF-8
332 <li>`` strings exist; they do not interpret backslashes
333 <li>Octal character escapes are always 3 digits ("\077" not "\77")
334 <li>Hexadecimal character escapes are always 2 digits ("\x07" not "\x7")
335</ul>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700336
Robert Griesemercae03422008-09-04 16:59:31 -0700337The rules are:
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700338
Robert Griesemerc2d55862009-02-19 16:49:10 -0800339<pre>
340escaped_char = "\" ( "a" | "b" | "f" | "n" | "r" | "t" | "v" | "\" | "'" | """ ) .
341</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700342
Robert Griesemerc2d55862009-02-19 16:49:10 -0800343<p>
Rob Pike4501d342009-02-19 17:31:36 -0800344A unicode_value takes one of four forms:
345</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -0800346<ul>
347 <li>The UTF-8 encoding of a Unicode code point. Since Go source
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700348text is in UTF-8, this is the obvious translation from input
349text into Unicode characters.
Robert Griesemerc2d55862009-02-19 16:49:10 -0800350
351 <li>The usual list of C backslash escapes: "\n", "\t", etc.
Robert Griesemercae03422008-09-04 16:59:31 -0700352Within a character or string literal, only the corresponding quote character
353is a legal escape (this is not explicitly reflected in the above syntax).
Robert Griesemerc2d55862009-02-19 16:49:10 -0800354
355 <li>A `little u' value, such as "\u12AB". This represents the Unicode
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700356code point with the corresponding hexadecimal value. It always
357has exactly 4 hexadecimal digits.
Robert Griesemerc2d55862009-02-19 16:49:10 -0800358
359 <li>A `big U' value, such as "\U00101234". This represents the
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700360Unicode code point with the corresponding hexadecimal value.
361It always has exactly 8 hexadecimal digits.
Robert Griesemerc2d55862009-02-19 16:49:10 -0800362</ul>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700363
364Some values that can be represented this way are illegal because they
365are not valid Unicode code points. These include values above
3660x10FFFF and surrogate halves.
Robert Griesemerc2d55862009-02-19 16:49:10 -0800367<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700368An octal_byte_value contains three octal digits. A hex_byte_value
369contains two hexadecimal digits. (Note: This differs from C but is
370simpler.)
Robert Griesemerc2d55862009-02-19 16:49:10 -0800371<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700372It is erroneous for an octal_byte_value to represent a value larger than 255.
373(By construction, a hex_byte_value cannot.)
Robert Griesemerc2d55862009-02-19 16:49:10 -0800374<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700375A character literal is a form of unsigned integer constant. Its value
376is that of the Unicode code point represented by the text between the
377quotes.
378
Robert Griesemerc2d55862009-02-19 16:49:10 -0800379<pre>
380'a'
381'ä'
382'本'
383'\t'
384'\000'
385'\007'
386'\377'
387'\x07'
388'\xff'
389'\u12e4'
390'\U00101234'
391</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700392
393String literals come in two forms: double-quoted and back-quoted.
394Double-quoted strings have the usual properties; back-quoted strings
395do not interpret backslashes at all.
396
Robert Griesemerc2d55862009-02-19 16:49:10 -0800397<pre>
398string_lit = raw_string_lit | interpreted_string_lit .
399raw_string_lit = "`" { unicode_char } "`" .
400interpreted_string_lit = """ { unicode_value | byte_value } """ .
401</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700402
Robert Griesemercd499272008-09-29 12:09:00 -0700403A string literal has type "string" (§Strings). Its value is constructed
404by taking the byte values formed by the successive elements of the
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700405literal. For byte_values, these are the literal bytes; for
406unicode_values, these are the bytes of the UTF-8 encoding of the
407corresponding Unicode code points. Note that
408 "\u00FF"
409and
410 "\xFF"
411are
412different strings: the first contains the two-byte UTF-8 expansion of
413the value 255, while the second contains a single byte of value 255.
414The same rules apply to raw string literals, except the contents are
415uninterpreted UTF-8.
416
Robert Griesemerc2d55862009-02-19 16:49:10 -0800417<pre>
418`abc`
419`\n`
420"hello, world\n"
421"\n"
422""
423"Hello, world!\n"
424"日本語"
425"\u65e5本\U00008a9e"
426"\xff\u00FF"
427</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700428
429These examples all represent the same string:
430
Robert Griesemerc2d55862009-02-19 16:49:10 -0800431<pre>
432"日本語" // UTF-8 input text
433`日本語` // UTF-8 input text as a raw literal
434"\u65e5\u672c\u8a9e" // The explicit Unicode code points
435"\U000065e5\U0000672c\U00008a9e" // The explicit Unicode code points
436"\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e" // The explicit UTF-8 bytes
437</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700438
Robert Griesemercd499272008-09-29 12:09:00 -0700439Adjacent strings separated only by whitespace (including comments)
440are concatenated into a single string. The following two lines
441represent the same string:
442
Robert Griesemerc2d55862009-02-19 16:49:10 -0800443<pre>
444"Alea iacta est."
445"Alea " /* The die */ `iacta est` /* is cast */ "."
446</pre>
Robert Griesemercd499272008-09-29 12:09:00 -0700447
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700448The language does not canonicalize Unicode text or evaluate combining
449forms. The text of source code is passed uninterpreted.
Robert Griesemerc2d55862009-02-19 16:49:10 -0800450<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700451If the source code represents a character as two code points, such as
452a combining form involving an accent and a letter, the result will be
453an error if placed in a character literal (it is not a single code
454point), and will appear as two code points if placed in a string
455literal.
456
457
Robert Griesemerc2d55862009-02-19 16:49:10 -0800458<h3>Operators and delimitors</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700459
460The following special character sequences serve as operators or delimitors:
461
Robert Griesemerc2d55862009-02-19 16:49:10 -0800462<pre>
463+ &amp; += &amp;= &amp;&amp; == != ( )
464- | -= |= || < <= [ ]
465* ^ *= ^= <- > >= { }
466/ << /= <<= ++ = := , ;
467% >> %= >>= -- ! ... . :
468</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700469
470
Robert Griesemerc2d55862009-02-19 16:49:10 -0800471<h3>Reserved words</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700472
473The following words are reserved and must not be used as identifiers:
474
Robert Griesemerc2d55862009-02-19 16:49:10 -0800475<pre>
476break default func interface select
477case defer go map struct
478chan else goto package switch
479const fallthrough if range type
480continue for import return var
481</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700482
Robert Griesemerc2d55862009-02-19 16:49:10 -0800483<hr>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700484
Robert Griesemerc2d55862009-02-19 16:49:10 -0800485<h2>Declarations and scope rules</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700486
Robert Griesemer7abfcd92008-10-07 17:14:30 -0700487A declaration ``binds'' an identifier to a language entity (such as
Robert Griesemer347cf672008-10-03 14:04:28 -0700488a package, constant, type, struct field, variable, parameter, result,
489function, method) and specifies properties of that entity such as its type.
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700490
Robert Griesemerc2d55862009-02-19 16:49:10 -0800491<pre>
492Declaration = ConstDecl | TypeDecl | VarDecl | FunctionDecl | MethodDecl .
493</pre>
Robert Griesemerbbfe3122008-10-09 17:12:09 -0700494
Robert Griesemer347cf672008-10-03 14:04:28 -0700495Every identifier in a program must be declared; some identifiers, such as "int"
Robert Griesemer337af312008-11-17 18:11:36 -0800496and "true", are predeclared (§Predeclared identifiers).
Robert Griesemerc2d55862009-02-19 16:49:10 -0800497<p>
Robert Griesemer347cf672008-10-03 14:04:28 -0700498The ``scope'' of an identifier is the extent of source text within which the
Robert Griesemer7abfcd92008-10-07 17:14:30 -0700499identifier denotes the bound entity. No identifier may be declared twice in a
500single scope. Go is lexically scoped: An identifier denotes the entity it is
501bound to only within the scope of the identifier.
Robert Griesemerc2d55862009-02-19 16:49:10 -0800502<p>
Robert Griesemer7abfcd92008-10-07 17:14:30 -0700503For instance, for a variable named "x", the scope of identifier "x" is the
504extent of source text within which "x" denotes that particular variable.
505It is illegal to declare another identifier "x" within the same scope.
Robert Griesemerc2d55862009-02-19 16:49:10 -0800506<p>
Robert Griesemer7abfcd92008-10-07 17:14:30 -0700507The scope of an identifier depends on the entity declared. The scope for
508an identifier always excludes scopes redeclaring the identifier in nested
509blocks. An identifier declared in a nested block is said to ``shadow'' the
510same identifier declared in an outer block.
Robert Griesemer347cf672008-10-03 14:04:28 -0700511
Robert Griesemerc2d55862009-02-19 16:49:10 -0800512<ol>
513 <li> The scope of predeclared identifiers is the entire source file.
Robert Griesemer347cf672008-10-03 14:04:28 -0700514
Robert Griesemerc2d55862009-02-19 16:49:10 -0800515 <li> The scope of an identifier denoting a type, function or package
516 extends textually from the point of the identifier in the declaration
517 to the end of the innermost surrounding block.
Robert Griesemer347cf672008-10-03 14:04:28 -0700518
Robert Griesemerc2d55862009-02-19 16:49:10 -0800519 <li> The scope of a constant or variable extends textually from
520 after the declaration to the end of the innermost surrounding
521 block. If the variable is declared in the init statement of an
522 if, for, or switch statement, the innermost surrounding block
523 is the block associated with the respective statement.
Robert Griesemer347cf672008-10-03 14:04:28 -0700524
Robert Griesemerc2d55862009-02-19 16:49:10 -0800525 <li> The scope of a parameter or result identifier is the body of the
526 corresponding function.
Robert Griesemer7abfcd92008-10-07 17:14:30 -0700527
Robert Griesemerc2d55862009-02-19 16:49:10 -0800528 <li> The scope of a field or method identifier is selectors for the
529 corresponding type containing the field or method (§Selectors).
Robert Griesemer7abfcd92008-10-07 17:14:30 -0700530
Robert Griesemerc2d55862009-02-19 16:49:10 -0800531 <li> The scope of a label is the body of the innermost surrounding
532 function and does not intersect with any non-label scope. Thus,
533 each function has its own private label scope.
534</ol>
Robert Griesemer347cf672008-10-03 14:04:28 -0700535
Robert Griesemerc2d55862009-02-19 16:49:10 -0800536<h3>Predeclared identifiers</h3>
Robert Griesemer337af312008-11-17 18:11:36 -0800537
538The following identifiers are predeclared:
539
540All basic types:
541
Robert Griesemerc2d55862009-02-19 16:49:10 -0800542<pre>
543bool, byte, uint8, uint16, uint32, uint64, int8, int16, int32, int64,
544float32, float64, string
545</pre>
Robert Griesemer337af312008-11-17 18:11:36 -0800546
547A set of platform-specific convenience types:
548
Robert Griesemerc2d55862009-02-19 16:49:10 -0800549<pre>
550uint, int, float, uintptr
551</pre>
Robert Griesemer337af312008-11-17 18:11:36 -0800552
553The predeclared constants:
554
Robert Griesemerc2d55862009-02-19 16:49:10 -0800555<pre>
556true, false, iota, nil
557</pre>
Robert Griesemer337af312008-11-17 18:11:36 -0800558
559The predeclared functions (note: this list is likely to change):
560
Robert Griesemerc2d55862009-02-19 16:49:10 -0800561<pre>
562cap(), convert(), len(), make(), new(), panic(), panicln(), print(), println(), typeof(), ...
563</pre>
Robert Griesemer337af312008-11-17 18:11:36 -0800564
565
Robert Griesemerc2d55862009-02-19 16:49:10 -0800566<h3>Exported identifiers</h3>
Robert Griesemer337af312008-11-17 18:11:36 -0800567
Robert Griesemer83c17602009-01-16 14:12:50 -0800568Identifiers that start with a capital_letter (§Identifiers) are ``exported'',
569thus making the identifiers accessible outside the current package. A file
570belonging to another package may then import the package (§Packages) and access
571exported identifiers via qualified identifiers (§Qualified identifiers).
Robert Griesemerc2d55862009-02-19 16:49:10 -0800572<p>
Robert Griesemer83c17602009-01-16 14:12:50 -0800573All other identifiers are ``internal''; they are only visible in files
574belonging to the same package which declares them.
Robert Griesemerc2d55862009-02-19 16:49:10 -0800575<p>
576<font color=red>
Robert Griesemer83c17602009-01-16 14:12:50 -0800577TODO: This should be made clearer. For instance, function-local identifiers
578are never exported, but non-global fields/methods may be exported.
Robert Griesemerc2d55862009-02-19 16:49:10 -0800579</font>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700580
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700581
Robert Griesemerc2d55862009-02-19 16:49:10 -0800582<h3>Const declarations</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700583
Robert Griesemer7abfcd92008-10-07 17:14:30 -0700584A constant declaration binds an identifier to the value of a constant
585expression (§Constant expressions).
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700586
Robert Griesemerc2d55862009-02-19 16:49:10 -0800587<pre>
588ConstDecl = "const" ( ConstSpec | "(" [ ConstSpecList ] ")" ) .
589ConstSpecList = ConstSpec { ";" ConstSpec } [ ";" ] .
590ConstSpec = IdentifierList [ CompleteType ] [ "=" ExpressionList ] .
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700591
Robert Griesemerc2d55862009-02-19 16:49:10 -0800592IdentifierList = identifier { "," identifier } .
593ExpressionList = Expression { "," Expression } .
594</pre>
Robert Griesemer7354b862008-12-04 17:33:37 -0800595
596A constant declaration binds a list of identifiers (the names of the constants)
597to the values of a list of constant expressions. The number of identifiers must
598be equal to the number of expressions, with the i'th identifier on the left
599corresponding to the i'th expression on the right. If CompleteType is omitted,
600the types of the constants are the types of the corresponding expressions;
601different expressions may have different types. If CompleteType is present,
602the type of all constants is the type specified, and the types of all
603expressions in ExpressionList must be assignment-compatible with the
604constant type.
605
Robert Griesemerc2d55862009-02-19 16:49:10 -0800606<pre>
607const Pi float64 = 3.14159265358979323846
608const E = 2.718281828
609const (
610 size int64 = 1024;
611 eof = -1;
612)
613const a, b, c = 3, 4, "foo" // a = 3, b = 4, c = "foo"
614const u, v float = 0, 3 // u = 0.0, v = 3.0
615</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700616
Robert Griesemer7354b862008-12-04 17:33:37 -0800617As a special case, within a parenthesized "const" declaration list the
618ExpressionList may be omitted from any but the first declaration. Such an empty
619ExpressionList is equivalent to the textual substitution of the first preceding
620non-empty ExpressionList in the same "const" declaration list.
621That is, omitting the list of expressions is equivalent to repeating the
622previous list. The number of identifiers must be equal to the number of
623expressions in the previous list.
Robert Griesemerc2d55862009-02-19 16:49:10 -0800624<p>
Robert Griesemer7354b862008-12-04 17:33:37 -0800625Together with the "iota" constant generator implicit repetition of
626ExpressionLists permit light-weight declaration of enumerated values (§Iota):
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700627
Robert Griesemerc2d55862009-02-19 16:49:10 -0800628<pre>
629const (
630 Sunday = iota;
631 Monday;
632 Tuesday;
633 Wednesday;
634 Thursday;
635 Friday;
636 Partyday;
637 numberOfDays; // this constant in not exported
638)
639</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700640
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700641The initializing expression for a numeric constant is evaluated
642using the principles described in the section on numeric literals:
643constants are mathematical values given a size only upon assignment
644to a variable. Intermediate values, and the constants themselves,
645may require precision significantly larger than any concrete type
646in the language. Thus the following is legal:
647
Robert Griesemerc2d55862009-02-19 16:49:10 -0800648<pre>
649const Huge = 1 << 100;
650const Four int8 = Huge >> 98;
651</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700652
653A given numeric constant expression is, however, defined to be
654either an integer or a floating point value, depending on the syntax
655of the literals it comprises (123 vs. 1.0e4). This is because the
656nature of the arithmetic operations depends on the type of the
657values; for example, 3/2 is an integer division yielding 1, while
6583./2. is a floating point division yielding 1.5. Thus
659
Robert Griesemerc2d55862009-02-19 16:49:10 -0800660<pre>
661const x = 3./2. + 3/2;
662</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700663
664yields a floating point constant of value 2.5 (1.5 + 1); its
665constituent expressions are evaluated using different rules for
666division.
Robert Griesemerc2d55862009-02-19 16:49:10 -0800667<p>
Robert Griesemer7354b862008-12-04 17:33:37 -0800668If the type is missing from a numeric constant declaration, the constant
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700669represents a value of abitrary precision, either integer or floating
670point, determined by the type of the initializing expression. Such
671a constant may be assigned to any variable that can represent its
672value accurately, regardless of type. For instance, 3 can be
Robert Griesemer7354b862008-12-04 17:33:37 -0800673assigned to any integer variable but also to any floating point variable,
674while 1e12 can be assigned to a "float32", "float64", or even "int64".
675It is erroneous to assign a value with a non-zero fractional part
676to an integer, or if the assignment would overflow or underflow.
677
678
Robert Griesemerc2d55862009-02-19 16:49:10 -0800679<h3>Iota</h3>
Robert Griesemer7354b862008-12-04 17:33:37 -0800680
681Within a constant declaration, the predeclared operand "iota" represents
682successive elements of an integer sequence. It is reset to 0 whenever the
683reserved word "const" appears in the source and increments with each
684semicolon. For instance, "iota" can be used to construct a set of related
685constants:
686
Robert Griesemerc2d55862009-02-19 16:49:10 -0800687<pre>
688const ( // iota is set to 0
689 enum0 = iota; // sets enum0 to 0, etc.
690 enum1 = iota;
691 enum2 = iota
692)
Robert Griesemer7354b862008-12-04 17:33:37 -0800693
Robert Griesemerc2d55862009-02-19 16:49:10 -0800694const (
695 a = 1 << iota; // a == 1 (iota has been reset)
696 b = 1 << iota; // b == 2
697 c = 1 << iota; // c == 4
698)
Robert Griesemer7354b862008-12-04 17:33:37 -0800699
Robert Griesemerc2d55862009-02-19 16:49:10 -0800700const (
701 u = iota * 42; // u == 0 (ideal integer)
702 v float = iota * 42; // v == 42.0 (float)
703 w = iota * 42; // w == 84 (ideal integer)
704)
Robert Griesemer7354b862008-12-04 17:33:37 -0800705
Robert Griesemerc2d55862009-02-19 16:49:10 -0800706const x = iota; // x == 0 (iota has been reset)
707const y = iota; // y == 0 (iota has been reset)
708</pre>
Robert Griesemer7354b862008-12-04 17:33:37 -0800709
710Within an ExpressionList, the value of all "iota"'s is the same because "iota"
711is only incremented at each semicolon:
712
Robert Griesemerc2d55862009-02-19 16:49:10 -0800713<pre>
714const (
715 base0, mask0 int64 = 1 << iota, i << iota - 1; // base0 == 1, mask0 = 0
716 base1, mask1 int64 = 1 << iota, i << iota - 1; // base1 == 2, mask1 = 1
717 base2, mask2 int64 = 1 << iota, i << iota - 1; // base2 == 4, mask2 = 3
718)
719</pre>
Robert Griesemer7354b862008-12-04 17:33:37 -0800720
721Since the ExpressionList in constant declarations repeats implicitly
722if omitted, some of the examples above can be abbreviated:
723
Robert Griesemerc2d55862009-02-19 16:49:10 -0800724<pre>
725const (
726 enum0 = iota;
727 enum1;
728 enum2
729)
Robert Griesemer7354b862008-12-04 17:33:37 -0800730
Robert Griesemerc2d55862009-02-19 16:49:10 -0800731const (
732 a = 1 << iota;
733 b;
734 c;
735)
Robert Griesemer7354b862008-12-04 17:33:37 -0800736
Robert Griesemerc2d55862009-02-19 16:49:10 -0800737const (
738 u = iota * 42;
739 v float;
740 w;
741)
Robert Griesemer7354b862008-12-04 17:33:37 -0800742
Robert Griesemerc2d55862009-02-19 16:49:10 -0800743const (
744 base0, mask0 int64 = 1 << iota, i << iota - 1;
745 base1, mask1 int64;
746 base2, mask2 int64;
747)
748</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700749
750
Robert Griesemerc2d55862009-02-19 16:49:10 -0800751<h3>Type declarations</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700752
Robert Griesemer7abfcd92008-10-07 17:14:30 -0700753A type declaration specifies a new type and binds an identifier to it.
Robert Griesemer434c6052008-11-07 13:34:37 -0800754The identifier is called the ``type name''; it denotes the type.
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700755
Robert Griesemerc2d55862009-02-19 16:49:10 -0800756<pre>
757TypeDecl = "type" ( TypeSpec | "(" [ TypeSpecList ] ")" ) .
758TypeSpecList = TypeSpec { ";" TypeSpec } [ ";" ] .
759TypeSpec = identifier Type .
760</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700761
Robert Griesemer7abfcd92008-10-07 17:14:30 -0700762A struct or interface type may be forward-declared (§Struct types,
763§Interface types). A forward-declared type is incomplete (§Types)
764until it is fully declared. The full declaration must must follow
765within the same block containing the forward declaration.
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700766
Robert Griesemerc2d55862009-02-19 16:49:10 -0800767<pre>
768type IntArray [16] int
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700769
Robert Griesemerc2d55862009-02-19 16:49:10 -0800770type (
771 Point struct { x, y float };
772 Polar Point
773)
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700774
Robert Griesemerc2d55862009-02-19 16:49:10 -0800775type TreeNode struct {
776 left, right *TreeNode;
777 value Point;
778}
779
780type Comparable interface {
781 cmp(Comparable) int
782}
783</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700784
785
Robert Griesemerc2d55862009-02-19 16:49:10 -0800786<h3>Variable declarations</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700787
Robert Griesemer7abfcd92008-10-07 17:14:30 -0700788A variable declaration creates a variable, binds an identifier to it and
789gives it a type. It may optionally give the variable an initial value.
790The variable type must be a complete type (§Types).
791In some forms of declaration the type of the initial value defines the type
792of the variable.
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700793
Robert Griesemerc2d55862009-02-19 16:49:10 -0800794<pre>
795VarDecl = "var" ( VarSpec | "(" [ VarSpecList ] ")" ) .
796VarSpecList = VarSpec { ";" VarSpec } [ ";" ] .
797VarSpec = IdentifierList ( CompleteType [ "=" ExpressionList ] | "=" ExpressionList ) .
798</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700799
Robert Griesemerc2d55862009-02-19 16:49:10 -0800800<pre>
801var i int
802var U, V, W float
803var k = 0
804var x, y float = -1.0, -2.0
805var (
806 i int;
807 u, v, s = 2.0, 3.0, "bar"
808)
809</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700810
811If the expression list is present, it must have the same number of elements
812as there are variables in the variable specification.
Robert Griesemerc2d55862009-02-19 16:49:10 -0800813<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700814If the variable type is omitted, an initialization expression (or expression
815list) must be present, and the variable type is the type of the expression
816value (in case of a list of variables, the variables assume the types of the
817corresponding expression values).
Robert Griesemerc2d55862009-02-19 16:49:10 -0800818<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700819If the variable type is omitted, and the corresponding initialization expression
820is a constant expression of abstract int or floating point type, the type
821of the variable is "int" or "float" respectively:
822
Robert Griesemerc2d55862009-02-19 16:49:10 -0800823<pre>
824var i = 0 // i has int type
825var f = 3.1415 // f has float type
826</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700827
828The syntax
829
Robert Griesemerc2d55862009-02-19 16:49:10 -0800830<pre>
831SimpleVarDecl = IdentifierList ":=" ExpressionList .
832</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700833
834is shorthand for
835
Robert Griesemerc2d55862009-02-19 16:49:10 -0800836<pre>
837"var" IdentifierList = ExpressionList .
838</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700839
Robert Griesemerc2d55862009-02-19 16:49:10 -0800840<pre>
841i, j := 0, 10;
842f := func() int { return 7; }
843ch := new(chan int);
844</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700845
846Also, in some contexts such as "if", "for", or "switch" statements,
847this construct can be used to declare local temporary variables.
848
Robert Griesemerc2d55862009-02-19 16:49:10 -0800849<hr>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700850
Robert Griesemerc2d55862009-02-19 16:49:10 -0800851<h2>Types</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700852
Robert Griesemer434c6052008-11-07 13:34:37 -0800853A type specifies the set of values that variables of that type may assume
854and the operators that are applicable.
Robert Griesemerc2d55862009-02-19 16:49:10 -0800855<p>
Robert Griesemera3294712009-01-05 11:17:26 -0800856A type may be specified by a type name (§Type declarations) or a type literal.
857A type literal is a syntactic construct that explicitly specifies the
858composition of a new type in terms of other (already declared) types.
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700859
Robert Griesemerc2d55862009-02-19 16:49:10 -0800860<pre>
861Type = TypeName | TypeLit .
862TypeName = QualifiedIdent.
863TypeLit =
864 ArrayType | StructType | PointerType | FunctionType | InterfaceType |
865 SliceType | MapType | ChannelType .
866</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -0800867
Robert Griesemera3294712009-01-05 11:17:26 -0800868Some types are predeclared and denoted by their type names; these are called
869``basic types''. Generally (except for strings) they are not composed of more
870elementary types; instead they model elementary machine data types.
Robert Griesemerc2d55862009-02-19 16:49:10 -0800871<p>
Robert Griesemera3294712009-01-05 11:17:26 -0800872All other types are called ``composite types'; they are composed from other
873(basic or composite) types and denoted by their type names or by type literals.
874There are arrays, structs, pointers, functions, interfaces, slices, maps, and
875channels.
Robert Griesemerc2d55862009-02-19 16:49:10 -0800876<p>
Robert Griesemera3294712009-01-05 11:17:26 -0800877At a given point in the source code, a type may be ``complete'' or
878''incomplete''. Array and struct types are complete when they are fully declared.
879All other types are always complete (although their components, such as the base
880type of a pointer type, may be incomplete). Incomplete types are subject to usage
881restrictions; for instance the type of a variable must be complete where the
882variable is declared.
Robert Griesemer7abfcd92008-10-07 17:14:30 -0700883
Robert Griesemerc2d55862009-02-19 16:49:10 -0800884<pre>
885CompleteType = Type .
886</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700887
Robert Griesemera1065fa2008-09-29 20:37:46 -0700888The ``interface'' of a type is the set of methods bound to it
889(§Method declarations). The interface of a pointer type is the interface
890of the pointer base type (§Pointer types). All types have an interface;
891if they have no methods associated with them, their interface is
892called the ``empty'' interface.
Robert Griesemerc2d55862009-02-19 16:49:10 -0800893<p>
Robert Griesemera1065fa2008-09-29 20:37:46 -0700894The ``static type'' (or simply ``type'') of a variable is the type defined by
895the variable's declaration. The ``dynamic type'' of a variable is the actual
Robert Griesemer434c6052008-11-07 13:34:37 -0800896type of the value stored in a variable at run-time. Except for variables of
Robert Griesemera1065fa2008-09-29 20:37:46 -0700897interface type, the dynamic type of a variable is always its static type.
Robert Griesemerc2d55862009-02-19 16:49:10 -0800898<p>
Robert Griesemera1065fa2008-09-29 20:37:46 -0700899Variables of interface type may hold values with different dynamic types
900during execution. However, its dynamic type is always compatible with
901the static type of the interface variable (§Interface types).
Robert Griesemer7abfcd92008-10-07 17:14:30 -0700902
Robert Griesemer1f3e8422008-09-29 18:41:30 -0700903
Robert Griesemerc2d55862009-02-19 16:49:10 -0800904<h3>Basic types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700905
906Go defines a number of basic types, referred to by their predeclared
907type names. These include traditional arithmetic types, booleans,
Robert Griesemer4dc25282008-09-09 10:37:19 -0700908and strings.
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700909
910
Robert Griesemerc2d55862009-02-19 16:49:10 -0800911<h3>Arithmetic types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700912
Robert Griesemerebf14c62008-10-30 14:50:23 -0700913The following list enumerates all platform-independent numeric types:
914
Robert Griesemerc2d55862009-02-19 16:49:10 -0800915<pre>
916byte same as uint8 (for convenience)
Robert Griesemerebf14c62008-10-30 14:50:23 -0700917
Robert Griesemerc2d55862009-02-19 16:49:10 -0800918uint8 the set of all unsigned 8-bit integers (0 to 255)
919uint16 the set of all unsigned 16-bit integers (0 to 65535)
920uint32 the set of all unsigned 32-bit integers (0 to 4294967295)
921uint64 the set of all unsigned 64-bit integers (0 to 18446744073709551615)
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700922
Robert Griesemerc2d55862009-02-19 16:49:10 -0800923int8 the set of all signed 8-bit integers (-128 to 127)
924int16 the set of all signed 16-bit integers (-32768 to 32767)
925int32 the set of all signed 32-bit integers (-2147483648 to 2147483647)
926int64 the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700927
Robert Griesemerc2d55862009-02-19 16:49:10 -0800928float32 the set of all valid IEEE-754 32-bit floating point numbers
929float64 the set of all valid IEEE-754 64-bit floating point numbers
930</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700931
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -0800932Integer types are represented in the usual binary format; the value of
933an n-bit integer is n bits wide. A negative signed integer is represented
934as the two's complement of its absolute value.
935
936<!--
937The representation of signed integers and their exact range is
938implementation-specific, but the set of all positive values (including zero)
939of a signed integer type is always a subset of the corresponding unsigned
940integer type (thus, a positive signed integer can always be converted into
941its corresponding unsigned type without loss).
942-->
943
Robert Griesemerebf14c62008-10-30 14:50:23 -0700944Additionally, Go declares a set of platform-specific numeric types for
945convenience:
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700946
Robert Griesemerc2d55862009-02-19 16:49:10 -0800947<pre>
948uint at least 32 bits, at most the size of the largest uint type
949int at least 32 bits, at most the size of the largest int type
950float at least 32 bits, at most the size of the largest float type
951uintptr smallest uint type large enough to store the uninterpreted
952 bits of a pointer value
953</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700954
Robert Griesemerebf14c62008-10-30 14:50:23 -0700955For instance, int might have the same size as int32 on a 32-bit
956architecture, or int64 on a 64-bit architecture.
Robert Griesemerc2d55862009-02-19 16:49:10 -0800957<p>
Robert Griesemera3294712009-01-05 11:17:26 -0800958Except for "byte", which is an alias for "uint8", all numeric types
Robert Griesemerebf14c62008-10-30 14:50:23 -0700959are different from each other to avoid portability issues. Conversions
960are required when different numeric types are mixed in an expression or assignment.
Robert Griesemera3294712009-01-05 11:17:26 -0800961For instance, "int32" and "int" are not the same type even though they may have
Robert Griesemerebf14c62008-10-30 14:50:23 -0700962the same size on a particular platform.
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700963
964
Robert Griesemerc2d55862009-02-19 16:49:10 -0800965<h3>Booleans</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700966
Robert Griesemer4dc25282008-09-09 10:37:19 -0700967The type "bool" comprises the truth values true and false, which are
968available through the two predeclared constants, "true" and "false".
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700969
970
Robert Griesemerc2d55862009-02-19 16:49:10 -0800971<h3>Strings</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700972
Rob Pike4501d342009-02-19 17:31:36 -0800973<p>
Robert Griesemera3294712009-01-05 11:17:26 -0800974The "string" type represents the set of string values (strings).
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700975Strings behave like arrays of bytes, with the following properties:
Rob Pike4501d342009-02-19 17:31:36 -0800976</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -0800977<ul>
978<li>They are immutable: after creation, it is not possible to change the
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700979contents of a string.
Robert Griesemerc2d55862009-02-19 16:49:10 -0800980<li>No internal pointers: it is illegal to create a pointer to an inner
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700981element of a string.
Robert Griesemerc2d55862009-02-19 16:49:10 -0800982<li>They can be indexed: given string "s1", "s1[i]" is a byte value.
983<li>They can be concatenated: given strings "s1" and "s2", "s1 + s2" is a value
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700984combining the elements of "s1" and "s2" in sequence.
Robert Griesemerc2d55862009-02-19 16:49:10 -0800985<li>Known length: the length of a string "s1" can be obtained by calling
Robert Griesemer4dc25282008-09-09 10:37:19 -0700986"len(s1)". The length of a string is the number
987of bytes within. Unlike in C, there is no terminal NUL byte.
Robert Griesemerc2d55862009-02-19 16:49:10 -0800988<li>Creation 1: a string can be created from an integer value by a conversion;
Robert Griesemer133c68e2008-09-26 14:04:21 -0700989the result is a string containing the UTF-8 encoding of that code point
990(§Conversions).
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700991"string('x')" yields "x"; "string(0x1234)" yields the equivalent of "\u1234"
992
Robert Griesemerc2d55862009-02-19 16:49:10 -0800993<li>Creation 2: a string can by created from an array of integer values (maybe
Robert Griesemer133c68e2008-09-26 14:04:21 -0700994just array of bytes) by a conversion (§Conversions):
Robert Griesemerc2d55862009-02-19 16:49:10 -0800995<pre>
996a [3]byte; a[0] = 'a'; a[1] = 'b'; a[2] = 'c'; string(a) == "abc";
997</pre>
998</ul>
Robert Griesemerdf49fb32008-08-28 17:47:53 -0700999
1000
Robert Griesemerc2d55862009-02-19 16:49:10 -08001001<h3>Array types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001002
Robert Griesemera3294712009-01-05 11:17:26 -08001003An array is a composite type consisting of a number of elements all of the
1004same type, called the element type. The element type must be a complete type
1005(§Types). The number of elements of an array is called its length; it is never
1006negative. The elements of an array are designated by indices
1007which are integers from 0 through the length - 1.
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001008
Robert Griesemerc2d55862009-02-19 16:49:10 -08001009<pre>
1010ArrayType = "[" ArrayLength "]" ElementType .
1011ArrayLength = Expression .
1012ElementType = CompleteType .
1013</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001014
Robert Griesemera3294712009-01-05 11:17:26 -08001015The array length and its value are part of the array type. The array length
1016must be a constant expression (§Constant expressions) that evaluates to an
1017integer value >= 0.
Robert Griesemerc2d55862009-02-19 16:49:10 -08001018<p>
Robert Griesemera3294712009-01-05 11:17:26 -08001019The number of elements of an array "a" can be discovered using the built-in
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001020function
1021
Robert Griesemerc2d55862009-02-19 16:49:10 -08001022<pre>
1023len(a)
1024</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001025
Robert Griesemera3294712009-01-05 11:17:26 -08001026The length of arrays is known at compile-time, and the result of a call to
1027"len(a)" is a compile-time constant.
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001028
Robert Griesemerc2d55862009-02-19 16:49:10 -08001029<pre>
1030[32]byte
1031[2*N] struct { x, y int32 }
1032[1000]*float64
1033</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001034
Robert Griesemer1593ab62009-01-16 15:36:46 -08001035Assignment compatibility: Arrays can be assigned to variables of equal type
1036and to slice variables with equal element type. When assigning to a slice
1037variable, the array is not copied but a slice comprising the entire array
1038is created.
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001039
1040
Robert Griesemerc2d55862009-02-19 16:49:10 -08001041<h3>Struct types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001042
Robert Griesemer4dc25282008-09-09 10:37:19 -07001043A struct is a composite type consisting of a fixed number of elements,
Robert Griesemer7abfcd92008-10-07 17:14:30 -07001044called fields, with possibly different types. A struct type declares
1045an identifier and type for each field. Within a struct type no field
1046identifier may be declared twice and all field types must be complete
1047types (§Types).
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001048
Robert Griesemerc2d55862009-02-19 16:49:10 -08001049<pre>
1050StructType = "struct" [ "{" [ FieldDeclList ] "}" ] .
1051FieldDeclList = FieldDecl { ";" FieldDecl } [ ";" ] .
1052FieldDecl = (IdentifierList CompleteType | [ "*" ] TypeName) [ Tag ] .
1053Tag = StringLit .
1054</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001055
Robert Griesemerc2d55862009-02-19 16:49:10 -08001056<pre>
1057// An empty struct.
1058struct {}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001059
Robert Griesemerc2d55862009-02-19 16:49:10 -08001060// A struct with 5 fields.
1061struct {
1062 x, y int;
1063 u float;
1064 A *[]int;
1065 F func();
1066}
1067</pre>
Robert Griesemer1f3e8422008-09-29 18:41:30 -07001068
Robert Griesemer071c91b2008-10-23 12:04:45 -07001069A struct may contain ``anonymous fields'', which are declared with a type
1070but no explicit field identifier. An anonymous field type must be specified as
1071a type name "T", or as a pointer to a type name ``*T'', and T itself may not be
Robert Griesemer83c17602009-01-16 14:12:50 -08001072a pointer or interface type. The unqualified type name acts as the field identifier.
Robert Griesemer1f3e8422008-09-29 18:41:30 -07001073
Robert Griesemerc2d55862009-02-19 16:49:10 -08001074<pre>
1075// A struct with four anonymous fields of type T1, *T2, P.T3 and *P.T4
1076struct {
1077 T1; // the field name is T1
1078 *T2; // the field name is T2
1079 P.T3; // the field name is the unqualified type name T3
1080 *P.T4; // the field name is the unqualified type name T4
1081 x, y int;
1082}
1083</pre>
Robert Griesemer1f3e8422008-09-29 18:41:30 -07001084
Robert Griesemer7abfcd92008-10-07 17:14:30 -07001085The unqualified type name of an anonymous field must not conflict with the
1086field identifier (or unqualified type name for an anonymous field) of any
Robert Griesemer071c91b2008-10-23 12:04:45 -07001087other field within the struct. The following declaration is illegal:
1088
Robert Griesemerc2d55862009-02-19 16:49:10 -08001089<pre>
1090struct {
1091 T; // conflicts with anonymous field *T and *P.T
1092 *T; // conflicts with anonymous field T and *P.T
1093 *P.T; // conflicts with anonymous field T and *T
1094}
1095</pre>
Robert Griesemer1f3e8422008-09-29 18:41:30 -07001096
Robert Griesemerc59b2a32008-09-30 10:57:59 -07001097Fields and methods (§Method declarations) of an anonymous field become directly
1098accessible as fields and methods of the struct without the need to provide the
Robert Griesemera6b546f2008-10-20 11:46:40 -07001099type name of the respective anonymous field (§Selectors).
Robert Griesemerc2d55862009-02-19 16:49:10 -08001100<p>
Robert Griesemer2e90e542008-10-30 15:52:37 -07001101A field declaration may be followed by an optional string literal tag which
1102becomes an ``attribute'' for all the identifiers in the corresponding
1103field declaration. The tags are available via the reflection library but
1104are ignored otherwise. A tag may contain arbitrary application-specific
Robert Griesemerf618f892008-11-03 10:52:28 -08001105information.
Robert Griesemer2e90e542008-10-30 15:52:37 -07001106
Robert Griesemerc2d55862009-02-19 16:49:10 -08001107<pre>
1108// A struct corresponding to the EventIdMessage protocol buffer.
1109// The tag strings contain the protocol buffer field tags.
1110struct {
1111 time_usec uint64 "1";
1112 server_ip uint32 "2";
1113 process_id uint32 "3";
1114}
1115</pre>
Robert Griesemer2e90e542008-10-30 15:52:37 -07001116
Robert Griesemer7abfcd92008-10-07 17:14:30 -07001117Forward declaration:
1118A struct type consisting of only the reserved word "struct" may be used in
1119a type declaration; it declares an incomplete struct type (§Type declarations).
1120This allows the construction of mutually recursive types such as:
1121
Robert Griesemerc2d55862009-02-19 16:49:10 -08001122<pre>
1123type S2 struct // forward declaration of S2
1124type S1 struct { s2 *S2 }
1125type S2 struct { s1 *S1 }
1126</pre>
Robert Griesemer7abfcd92008-10-07 17:14:30 -07001127
Robert Griesemer4dc25282008-09-09 10:37:19 -07001128Assignment compatibility: Structs are assignment compatible to variables of
1129equal type only.
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001130
1131
Robert Griesemerc2d55862009-02-19 16:49:10 -08001132<h3>Pointer types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001133
Robert Griesemer4dc25282008-09-09 10:37:19 -07001134A pointer type denotes the set of all pointers to variables of a given
1135type, called the ``base type'' of the pointer, and the value "nil".
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001136
Robert Griesemerc2d55862009-02-19 16:49:10 -08001137<pre>
1138PointerType = "*" BaseType .
1139BaseType = Type .
1140</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001141
Robert Griesemerc2d55862009-02-19 16:49:10 -08001142<pre>
1143*int
1144map[string] chan
1145</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001146
Robert Griesemer7abfcd92008-10-07 17:14:30 -07001147The pointer base type may be denoted by an identifier referring to an
1148incomplete type (§Types), possibly declared via a forward declaration.
1149This allows the construction of recursive and mutually recursive types
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001150such as:
1151
Robert Griesemerc2d55862009-02-19 16:49:10 -08001152<pre>
1153type S struct { s *S }
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001154
Robert Griesemerc2d55862009-02-19 16:49:10 -08001155type S2 struct // forward declaration of S2
1156type S1 struct { s2 *S2 }
1157type S2 struct { s1 *S1 }
1158</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001159
Robert Griesemer4dc25282008-09-09 10:37:19 -07001160Assignment compatibility: A pointer is assignment compatible to a variable
1161of pointer type, only if both types are equal.
Robert Griesemerc2d55862009-02-19 16:49:10 -08001162<p>
Robert Griesemer7471eab2009-01-27 14:51:24 -08001163Comparisons: A variable of pointer type can be compared against "nil" with the
1164operators "==" and "!=" (§Comparison operators). The variable is
1165"nil" only if "nil" is assigned explicitly to the variable (§Assignments), or
1166if the variable has not been modified since creation (§Program initialization
1167and execution).
Robert Griesemerc2d55862009-02-19 16:49:10 -08001168<p>
Robert Griesemer7471eab2009-01-27 14:51:24 -08001169Two variables of equal pointer type can be tested for equality with the
1170operators "==" and "!=" (§Comparison operators). The pointers are equal
1171if they point to the same location.
1172
Robert Griesemer4dc25282008-09-09 10:37:19 -07001173Pointer arithmetic of any kind is not permitted.
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001174
1175
Robert Griesemerc2d55862009-02-19 16:49:10 -08001176<h3>Function types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001177
Robert Griesemer7231ceb2008-09-08 15:01:04 -07001178A function type denotes the set of all functions with the same parameter
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08001179and result types, and the value "nil".
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001180
Robert Griesemerc2d55862009-02-19 16:49:10 -08001181<pre>
1182FunctionType = "func" Signature .
1183Signature = "(" [ ParameterList ] ")" [ Result ] .
1184ParameterList = ParameterDecl { "," ParameterDecl } .
1185ParameterDecl = [ IdentifierList ] ( Type | "..." ) .
1186Result = Type | "(" ParameterList ")" .
1187</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001188
Robert Griesemerac055792008-09-26 11:15:14 -07001189In ParameterList, the parameter names (IdentifierList) either must all be
1190present, or all be absent. If the parameters are named, each name stands
1191for one parameter of the specified type. If the parameters are unnamed, each
1192type stands for one parameter of that type.
Robert Griesemerc2d55862009-02-19 16:49:10 -08001193<p>
Robert Griesemer2bfa9572008-10-24 13:13:12 -07001194For the last incoming parameter only, instead of a parameter type one
1195may write "...". The ellipsis indicates that the last parameter stands
1196for an arbitrary number of additional arguments of any type (including
1197no additional arguments). If the parameters are named, the identifier
1198list immediately preceding "..." must contain only one identifier (the
1199name of the last parameter).
1200
Robert Griesemerc2d55862009-02-19 16:49:10 -08001201<pre>
1202func ()
1203func (x int)
1204func () int
1205func (string, float, ...)
1206func (a, b int, z float) bool
1207func (a, b int, z float) (bool)
1208func (a, b int, z float, opt ...) (success bool)
1209func (int, int, float) (float, *[]int)
1210</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001211
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08001212If the result type of a function is itself a function type, the result type
1213must be parenthesized to resolve a parsing ambiguity:
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001214
Robert Griesemerc2d55862009-02-19 16:49:10 -08001215<pre>
1216func (n int) (func (p* T))
1217</pre>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08001218
1219Assignment compatibility: A function can be assigned to a function
1220variable only if both function types are equal.
Robert Griesemerc2d55862009-02-19 16:49:10 -08001221<p>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08001222Comparisons: A variable of function type can be compared against "nil" with the
1223operators "==" and "!=" (§Comparison operators). The variable is
1224"nil" only if "nil" is assigned explicitly to the variable (§Assignments), or
1225if the variable has not been modified since creation (§Program initialization
1226and execution).
Robert Griesemerc2d55862009-02-19 16:49:10 -08001227<p>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08001228Two variables of equal function type can be tested for equality with the
1229operators "==" and "!=" (§Comparison operators). The variables are equal
1230if they refer to the same function.
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001231
1232
Robert Griesemerc2d55862009-02-19 16:49:10 -08001233<h3>Interface types</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001234
Robert Griesemera1065fa2008-09-29 20:37:46 -07001235Type interfaces may be specified explicitly by interface types.
1236An interface type denotes the set of all types that implement at least
1237the set of methods specified by the interface type, and the value "nil".
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001238
Robert Griesemerc2d55862009-02-19 16:49:10 -08001239<pre>
1240InterfaceType = "interface" [ "{" [ MethodSpecList ] "}" ] .
1241MethodSpecList = MethodSpec { ";" MethodSpec } [ ";" ] .
1242MethodSpec = IdentifierList Signature | TypeName .
1243</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001244
Robert Griesemerc2d55862009-02-19 16:49:10 -08001245<pre>
1246// An interface specifying a basic File type.
1247interface {
1248 Read, Write (b Buffer) bool;
1249 Close ();
1250}
1251</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001252
Robert Griesemer434c6052008-11-07 13:34:37 -08001253Any type (including interface types) whose interface has, possibly as a
1254subset, the complete set of methods of an interface I is said to implement
1255interface I. For instance, if two types S1 and S2 have the methods
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001256
Robert Griesemerc2d55862009-02-19 16:49:10 -08001257<pre>
1258func (p T) Read(b Buffer) bool { return ... }
1259func (p T) Write(b Buffer) bool { return ... }
1260func (p T) Close() { ... }
1261</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001262
1263(where T stands for either S1 or S2) then the File interface is
1264implemented by both S1 and S2, regardless of what other methods
1265S1 and S2 may have or share.
1266
1267All types implement the empty interface:
1268
Robert Griesemerc2d55862009-02-19 16:49:10 -08001269<pre>
1270interface {}
1271</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001272
1273In general, a type implements an arbitrary number of interfaces.
Robert Griesemer434c6052008-11-07 13:34:37 -08001274For instance, consider the interface
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001275
Robert Griesemerc2d55862009-02-19 16:49:10 -08001276<pre>
1277type Lock interface {
1278 Lock, Unlock ();
1279}
1280</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001281
Robert Griesemer434c6052008-11-07 13:34:37 -08001282If S1 and S2 also implement
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001283
Robert Griesemerc2d55862009-02-19 16:49:10 -08001284<pre>
1285func (p T) Lock() { ... }
1286func (p T) Unlock() { ... }
1287</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001288
1289they implement the Lock interface as well as the File interface.
Robert Griesemerc2d55862009-02-19 16:49:10 -08001290<p>
Robert Griesemer38c232f2009-02-11 15:09:15 -08001291An interface may contain a type name T in place of a method specification.
1292T must denote another, complete (and not forward-declared) interface type.
1293Using this notation is equivalent to enumerating the methods of T explicitly
1294in the interface containing T.
1295
Robert Griesemerc2d55862009-02-19 16:49:10 -08001296<pre>
1297type ReadWrite interface {
1298 Read, Write (b Buffer) bool;
1299}
Robert Griesemer38c232f2009-02-11 15:09:15 -08001300
Robert Griesemerc2d55862009-02-19 16:49:10 -08001301type File interface {
1302 ReadWrite; // same as enumerating the methods in ReadWrite
1303 Lock; // same as enumerating the methods in Lock
1304 Close();
1305}
1306</pre>
Robert Griesemer38c232f2009-02-11 15:09:15 -08001307
Robert Griesemer7abfcd92008-10-07 17:14:30 -07001308Forward declaration:
1309A interface type consisting of only the reserved word "interface" may be used in
1310a type declaration; it declares an incomplete interface type (§Type declarations).
1311This allows the construction of mutually recursive types such as:
1312
Robert Griesemerc2d55862009-02-19 16:49:10 -08001313<pre>
1314type T2 interface
1315type T1 interface {
1316 foo(T2) int;
1317}
1318type T2 interface {
1319 bar(T1) int;
1320}
1321</pre>
Robert Griesemer7abfcd92008-10-07 17:14:30 -07001322
Robert Griesemer7abfcd92008-10-07 17:14:30 -07001323Assignment compatibility: A value can be assigned to an interface variable
Robert Griesemerb5e0cc72008-10-10 16:34:44 -07001324if the static type of the value implements the interface or if the value is "nil".
Robert Griesemerc2d55862009-02-19 16:49:10 -08001325<p>
Robert Griesemer18b05c12009-01-26 09:34:19 -08001326Comparisons: A variable of interface type can be compared against "nil" with the
1327operators "==" and "!=" (§Comparison operators). The variable is
1328"nil" only if "nil" is assigned explicitly to the variable (§Assignments), or
1329if the variable has not been modified since creation (§Program initialization
1330and execution).
Robert Griesemerc2d55862009-02-19 16:49:10 -08001331<p>
Robert Griesemer18b05c12009-01-26 09:34:19 -08001332Two variables of interface type can be tested for equality with the
1333operators "==" and "!=" (§Comparison operators) if both variables have the
1334same static type. They are equal if both their dynamic types and values are
Robert Griesemer7471eab2009-01-27 14:51:24 -08001335equal. If the dynamic types are equal but the values do not support comparison,
1336a run-time error occurs.
Robert Griesemer18b05c12009-01-26 09:34:19 -08001337
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001338
Robert Griesemerc2d55862009-02-19 16:49:10 -08001339<h3>Slice types</h3>
Robert Griesemera3294712009-01-05 11:17:26 -08001340
Robert Griesemer633957b2009-01-06 13:23:20 -08001341A slice type denotes the set of all slices (segments) of arrays
Robert Griesemera3294712009-01-05 11:17:26 -08001342(§Array types) of a given element type, and the value "nil".
1343The number of elements of a slice is called its length; it is never negative.
1344The elements of a slice are designated by indices which are
1345integers from 0 through the length - 1.
1346
Robert Griesemerc2d55862009-02-19 16:49:10 -08001347<pre>
1348SliceType = "[" "]" ElementType .
1349</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001350
1351Syntactically and semantically, arrays and slices look and behave very
1352similarly, but with one important difference: A slice is a descriptor
1353of an array segment; in particular, different variables of a slice type may
1354refer to different (and possibly overlapping) segments of the same underlying
1355array. Thus, with respect to the underlying array, slices behave like
1356references. In contrast, two different variables of array type always
1357denote two different arrays.
Robert Griesemerc2d55862009-02-19 16:49:10 -08001358<p>
Robert Griesemera3294712009-01-05 11:17:26 -08001359For slices, the actual array underlying the slice may extend past the current
1360slice length; the maximum length a slice may assume is called its capacity.
1361The capacity of any slice "a" can be discovered using the built-in function
1362
Robert Griesemerc2d55862009-02-19 16:49:10 -08001363<pre>
1364cap(a)
1365</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001366
1367and the following relationship between "len()" and "cap()" holds:
1368
Robert Griesemerc2d55862009-02-19 16:49:10 -08001369<pre>
13700 <= len(a) <= cap(a)
1371</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001372
1373The value of an uninitialized slice is "nil", and its length and capacity
Robert Griesemer633957b2009-01-06 13:23:20 -08001374are 0. A new, initialized slice value for a given element type T is
1375made using the built-in function "make", which takes a slice type
Robert Griesemera3294712009-01-05 11:17:26 -08001376and parameters specifying the length and optionally the capacity:
1377
Robert Griesemerc2d55862009-02-19 16:49:10 -08001378<pre>
1379make([]T, length)
1380make([]T, length, capacity)
1381</pre>
Robert Griesemer633957b2009-01-06 13:23:20 -08001382
1383The "make()" call allocates a new underlying array to which the returned
1384slice value refers. More precisely, calling "make"
1385
Robert Griesemerc2d55862009-02-19 16:49:10 -08001386<pre>
1387make([]T, length, capacity)
1388</pre>
Robert Griesemer633957b2009-01-06 13:23:20 -08001389
1390is effectively the same as allocating an array and slicing it
1391
Robert Griesemerc2d55862009-02-19 16:49:10 -08001392<pre>
1393new([capacity]T)[0 : length]
1394</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001395
1396Assignment compatibility: Slices are assignment compatible to variables
1397of the same type.
Robert Griesemerc2d55862009-02-19 16:49:10 -08001398<p>
Robert Griesemera3294712009-01-05 11:17:26 -08001399Indexing: Given a (pointer to) a slice variable "a", a slice element is
1400specified with an index operation:
1401
Robert Griesemerc2d55862009-02-19 16:49:10 -08001402<pre>
1403a[i]
1404</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001405
1406This denotes the slice element at index "i". "i" must be within bounds,
Robert Griesemerc2d55862009-02-19 16:49:10 -08001407that is "0 &lt;= i &lt; len(a)".
1408<p>
Robert Griesemera3294712009-01-05 11:17:26 -08001409Slicing: Given a a slice variable "a", a sub-slice is created with a slice
1410operation:
1411
Robert Griesemerc2d55862009-02-19 16:49:10 -08001412<pre>
1413a[i : j]
1414</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001415
1416This creates the sub-slice consisting of the elements "a[i]" through "a[j - 1]"
Robert Griesemer18b05c12009-01-26 09:34:19 -08001417(that is, excluding "a[j]"). The values "i" and "j" must satisfy the condition
Robert Griesemerc2d55862009-02-19 16:49:10 -08001418"0 &lt;= i &lt;= j &lt;= cap(a)". The length of the new slice is "j - i". The capacity of
Robert Griesemera3294712009-01-05 11:17:26 -08001419the slice is "cap(a) - i"; thus if "i" is 0, the slice capacity does not change
1420as a result of a slice operation. The type of a sub-slice is the same as the
Robert Griesemer18b05c12009-01-26 09:34:19 -08001421type of the slice. Unlike the capacity, the length of a sub-slice may be larger
1422than the length of the original slice.
Robert Griesemerc2d55862009-02-19 16:49:10 -08001423<p>
Robert Griesemer18b05c12009-01-26 09:34:19 -08001424Comparisons: A variable of slice type can be compared against "nil" with the
1425operators "==" and "!=" (§Comparison operators). The variable is
1426"nil" only if "nil" is assigned explicitly to the variable (§Assignments), or
1427if the variable has not been modified since creation (§Program initialization
1428and execution).
Robert Griesemera3294712009-01-05 11:17:26 -08001429
1430
Robert Griesemerc2d55862009-02-19 16:49:10 -08001431<h3>Map types</h3>
Robert Griesemera3294712009-01-05 11:17:26 -08001432
1433A map is a composite type consisting of a variable number of entries
1434called (key, value) pairs. For a given map, the keys and values must
1435each be of a specific complete type (§Types) called the key and value type,
1436respectively. The number of entries in a map is called its length; it is never
1437negative.
1438
Robert Griesemerc2d55862009-02-19 16:49:10 -08001439<pre>
1440MapType = "map" "[" KeyType "]" ValueType .
1441KeyType = CompleteType .
1442ValueType = CompleteType .
1443</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001444
Robert Griesemer7471eab2009-01-27 14:51:24 -08001445The comparison operators "==" and "!=" (§Comparison operators) must be defined
1446for operands of the key type; thus the key type must be a basic, pointer,
1447interface, or channel type. If the key type is an interface type,
1448the dynamic key types must support these comparison operators. In this case,
1449inserting a map value with a key that does not support testing for equality
1450is a run-time error.
Robert Griesemerc2d55862009-02-19 16:49:10 -08001451<p>
Robert Griesemera3294712009-01-05 11:17:26 -08001452Upon creation, a map is empty and values may be added and removed
1453during execution.
1454
Robert Griesemerc2d55862009-02-19 16:49:10 -08001455<pre>
1456map [string] int
1457map [*T] struct { x, y float }
1458map [string] interface {}
1459</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001460
1461The length of a map "m" can be discovered using the built-in function
1462
Robert Griesemerc2d55862009-02-19 16:49:10 -08001463<pre>
1464len(m)
1465</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001466
Robert Griesemer7471eab2009-01-27 14:51:24 -08001467The value of an uninitialized map is "nil". A new, empty map value for given
1468map type M is made using the built-in function "make" which takes the map type
1469and an optional capacity as arguments:
Robert Griesemera3294712009-01-05 11:17:26 -08001470
Robert Griesemerc2d55862009-02-19 16:49:10 -08001471<pre>
1472my_map := make(M, 100);
1473</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001474
1475The map capacity is an allocation hint for more efficient incremental growth
1476of the map.
Robert Griesemerc2d55862009-02-19 16:49:10 -08001477<p>
Robert Griesemera3294712009-01-05 11:17:26 -08001478Assignment compatibility: A map type is assignment compatible to a variable of
1479map type only if both types are equal.
Robert Griesemerc2d55862009-02-19 16:49:10 -08001480<p>
Robert Griesemer18b05c12009-01-26 09:34:19 -08001481Comparisons: A variable of map type can be compared against "nil" with the
1482operators "==" and "!=" (§Comparison operators). The variable is
1483"nil" only if "nil" is assigned explicitly to the variable (§Assignments), or
1484if the variable has not been modified since creation (§Program initialization
1485and execution).
Robert Griesemera3294712009-01-05 11:17:26 -08001486
1487
Robert Griesemerc2d55862009-02-19 16:49:10 -08001488<h3>Channel types</h3>
Robert Griesemera3294712009-01-05 11:17:26 -08001489
1490A channel provides a mechanism for two concurrently executing functions
1491to synchronize execution and exchange values of a specified type. This
Robert Griesemerc2d55862009-02-19 16:49:10 -08001492type must be a complete type (§Types). <font color=red>(TODO could it be incomplete?)</font>
Robert Griesemera3294712009-01-05 11:17:26 -08001493
Robert Griesemerc2d55862009-02-19 16:49:10 -08001494<pre>
1495ChannelType = Channel | SendChannel | RecvChannel .
1496Channel = "chan" ValueType .
1497SendChannel = "chan" "&lt;-" ValueType .
1498RecvChannel = "&lt;-" "chan" ValueType .
1499</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001500
1501Upon creation, a channel can be used both to send and to receive.
1502By conversion or assignment, a channel may be constrained only to send or
1503to receive. This constraint is called a channel's ``direction''; either
1504bi-directional (unconstrained), send, or receive.
1505
Robert Griesemerc2d55862009-02-19 16:49:10 -08001506<pre>
1507chan T // can send and receive values of type T
1508chan &lt;- float // can only be used to send floats
1509&lt;-chan int // can only receive ints
1510</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001511
1512The value of an uninitialized channel is "nil". A new, initialized channel
Robert Griesemer633957b2009-01-06 13:23:20 -08001513value for a given element type T is made using the built-in function "make",
1514which takes the channel type and an optional capacity as arguments:
Robert Griesemera3294712009-01-05 11:17:26 -08001515
Robert Griesemerc2d55862009-02-19 16:49:10 -08001516<pre>
1517my_chan = make(chan int, 100);
1518</pre>
Robert Griesemera3294712009-01-05 11:17:26 -08001519
1520The capacity sets the size of the buffer in the communication channel. If the
1521capacity is greater than zero, the channel is asynchronous and, provided the
1522buffer is not full, sends can succeed without blocking. If the capacity is zero,
1523the communication succeeds only when both a sender and receiver are ready.
Robert Griesemerc2d55862009-02-19 16:49:10 -08001524<p>
Robert Griesemer18b05c12009-01-26 09:34:19 -08001525Assignment compatibility: A value of type channel can be assigned to a variable
1526of type channel only if a) both types are equal (§Type equality), or b) both
1527have equal channel value types and the value is a bidirectional channel.
Robert Griesemerc2d55862009-02-19 16:49:10 -08001528<p>
Robert Griesemer18b05c12009-01-26 09:34:19 -08001529Comparisons: A variable of channel type can be compared against "nil" with the
1530operators "==" and "!=" (§Comparison operators). The variable is
1531"nil" only if "nil" is assigned explicitly to the variable (§Assignments), or
1532if the variable has not been modified since creation (§Program initialization
1533and execution).
Robert Griesemerc2d55862009-02-19 16:49:10 -08001534<p>
Robert Griesemer18b05c12009-01-26 09:34:19 -08001535Two variables of channel type can be tested for equality with the
1536operators "==" and "!=" (§Comparison operators) if both variables have
1537the same ValueType. They are equal if both values were created by the same
1538"make" call (§Making slices, maps, and channels).
Robert Griesemera3294712009-01-05 11:17:26 -08001539
1540
Robert Griesemerc2d55862009-02-19 16:49:10 -08001541<h3>Type equality</h3>
Robert Griesemer434c6052008-11-07 13:34:37 -08001542
Rob Pike4501d342009-02-19 17:31:36 -08001543<p>
Robert Griesemer434c6052008-11-07 13:34:37 -08001544Types may be ``different'', ``structurally equal'', or ``identical''.
1545Go is a type-safe language; generally different types cannot be mixed
1546in binary operations, and values cannot be assigned to variables of different
1547types. However, values may be assigned to variables of structually
1548equal types. Finally, type guards succeed only if the dynamic type
1549is identical to or implements the type tested against (§Type guards).
Robert Griesemerc2d55862009-02-19 16:49:10 -08001550<p>
Robert Griesemer434c6052008-11-07 13:34:37 -08001551Structural type equality (equality for short) is defined by these rules:
Robert Griesemerc2d55862009-02-19 16:49:10 -08001552<p>
Robert Griesemer434c6052008-11-07 13:34:37 -08001553Two type names denote equal types if the types in the corresponding declarations
1554are equal. Two type literals specify equal types if they have the same
1555literal structure and corresponding components have equal types. Loosely
1556speaking, two types are equal if their values have the same layout in memory.
1557More precisely:
Rob Pike4501d342009-02-19 17:31:36 -08001558</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08001559<ul>
1560 <li>Two array types are equal if they have equal element types and if they
Robert Griesemera3294712009-01-05 11:17:26 -08001561 have the same array length.
Robert Griesemer434c6052008-11-07 13:34:37 -08001562
Robert Griesemerc2d55862009-02-19 16:49:10 -08001563 <li>Two struct types are equal if they have the same number of fields in the
Robert Griesemer77ccfb02009-02-05 16:11:14 -08001564 same order, corresponding fields either have both the same name or
1565 are both anonymous, and corresponding field types are identical.
Robert Griesemer434c6052008-11-07 13:34:37 -08001566
Robert Griesemerc2d55862009-02-19 16:49:10 -08001567 <li>Two pointer types are equal if they have equal base types.
Robert Griesemer434c6052008-11-07 13:34:37 -08001568
Robert Griesemerc2d55862009-02-19 16:49:10 -08001569 <li>Two function types are equal if they have the same number of parameters
Robert Griesemer434c6052008-11-07 13:34:37 -08001570 and result values and if corresponding parameter and result types are
1571 equal (a "..." parameter is equal to another "..." parameter).
1572 Note that parameter and result names do not have to match.
1573
Robert Griesemerc2d55862009-02-19 16:49:10 -08001574 <li>Two slice types are equal if they have equal element types.
Robert Griesemera3294712009-01-05 11:17:26 -08001575
Robert Griesemerc2d55862009-02-19 16:49:10 -08001576 <li>Two channel types are equal if they have equal value types and
Robert Griesemer434c6052008-11-07 13:34:37 -08001577 the same direction.
1578
Robert Griesemerc2d55862009-02-19 16:49:10 -08001579 <li>Two map types are equal if they have equal key and value types.
Robert Griesemer434c6052008-11-07 13:34:37 -08001580
Robert Griesemerc2d55862009-02-19 16:49:10 -08001581 <li>Two interface types are equal if they have the same set of methods
Robert Griesemer434c6052008-11-07 13:34:37 -08001582 with the same names and equal function types. Note that the order
1583 of the methods in the respective type declarations is irrelevant.
Robert Griesemerc2d55862009-02-19 16:49:10 -08001584</ul>
Robert Griesemer434c6052008-11-07 13:34:37 -08001585
Robert Griesemerc2d55862009-02-19 16:49:10 -08001586<p>
Robert Griesemer434c6052008-11-07 13:34:37 -08001587Type identity is defined by these rules:
Rob Pike4501d342009-02-19 17:31:36 -08001588</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08001589<p>
Robert Griesemer434c6052008-11-07 13:34:37 -08001590Two type names denote identical types if they originate in the same
1591type declaration. Two type literals specify identical types if they have the
1592same literal structure and corresponding components have identical types.
1593More precisely:
Rob Pike4501d342009-02-19 17:31:36 -08001594</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08001595<ul>
1596 <li>Two array types are identical if they have identical element types and if
Robert Griesemera3294712009-01-05 11:17:26 -08001597 they have the same array length.
Robert Griesemer434c6052008-11-07 13:34:37 -08001598
Robert Griesemerc2d55862009-02-19 16:49:10 -08001599 <li>Two struct types are identical if they have the same number of fields in
Robert Griesemer434c6052008-11-07 13:34:37 -08001600 the same order, corresponding fields either have both the same name or
1601 are both anonymous, and corresponding field types are identical.
1602
Robert Griesemerc2d55862009-02-19 16:49:10 -08001603 <li>Two pointer types are identical if they have identical base types.
Robert Griesemer434c6052008-11-07 13:34:37 -08001604
Robert Griesemerc2d55862009-02-19 16:49:10 -08001605 <li>Two function types are identical if they have the same number of
Robert Griesemer434c6052008-11-07 13:34:37 -08001606 parameters and result values both with the same (or absent) names, and
1607 if corresponding parameter and result types are identical (a "..."
1608 parameter is identical to another "..." parameter with the same name).
1609
Robert Griesemerc2d55862009-02-19 16:49:10 -08001610 <li>Two slice types are identical if they have identical element types.
Robert Griesemera3294712009-01-05 11:17:26 -08001611
Robert Griesemerc2d55862009-02-19 16:49:10 -08001612 <li>Two channel types are identical if they have identical value types and
Robert Griesemer434c6052008-11-07 13:34:37 -08001613 the same direction.
1614
Robert Griesemerc2d55862009-02-19 16:49:10 -08001615 <li>Two map types are identical if they have identical key and value types.
Robert Griesemer434c6052008-11-07 13:34:37 -08001616
Robert Griesemerc2d55862009-02-19 16:49:10 -08001617 <li>Two interface types are identical if they have the same set of methods
Robert Griesemer434c6052008-11-07 13:34:37 -08001618 with the same names and identical function types. Note that the order
1619 of the methods in the respective type declarations is irrelevant.
Robert Griesemerc2d55862009-02-19 16:49:10 -08001620</ul>
Robert Griesemer434c6052008-11-07 13:34:37 -08001621
1622Note that the type denoted by a type name is identical only to the type literal
1623in the type name's declaration.
Robert Griesemerc2d55862009-02-19 16:49:10 -08001624<p>
Robert Griesemer434c6052008-11-07 13:34:37 -08001625Finally, two types are different if they are not structurally equal.
1626(By definition, they cannot be identical, either).
1627
1628For instance, given the declarations
1629
Robert Griesemerc2d55862009-02-19 16:49:10 -08001630<pre>
1631type (
1632 T0 []string;
1633 T1 []string
1634 T2 struct { a, b int };
1635 T3 struct { a, c int };
1636 T4 func (int, float) *T0
1637 T5 func (x int, y float) *[]string
1638)
1639</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -08001640
1641these are some types that are equal
1642
Robert Griesemerc2d55862009-02-19 16:49:10 -08001643<pre>
1644T0 and T0
1645T0 and []string
1646T2 and T3
1647T4 and T5
1648T3 and struct { a int; int }
1649</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -08001650
1651and these are some types that are identical
1652
Robert Griesemerc2d55862009-02-19 16:49:10 -08001653<pre>
1654T0 and T0
1655[]int and []int
1656struct { a, b *T5 } and struct { a, b *T5 }
1657</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -08001658
1659As an example, "T0" and "T1" are equal but not identical because they have
1660different declarations.
1661
Robert Griesemerc2d55862009-02-19 16:49:10 -08001662<hr>
Robert Griesemer434c6052008-11-07 13:34:37 -08001663
Robert Griesemerc2d55862009-02-19 16:49:10 -08001664<h2>Expressions</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001665
Robert Griesemerad711102008-09-11 17:48:20 -07001666An expression specifies the computation of a value via the application of
1667operators and function invocations on operands. An expression has a value and
1668a type.
Robert Griesemerc2d55862009-02-19 16:49:10 -08001669<p>
Robert Griesemerc8e18762008-09-12 12:26:22 -07001670The type of a constant expression may be an ideal number. The type of such expressions
Robert Griesemer71696ac2008-10-16 15:03:22 -07001671is implicitly converted into the 'expected numeric type' required for the expression.
Robert Griesemerad711102008-09-11 17:48:20 -07001672The conversion is legal if the (ideal) expression value is a member of the
Robert Griesemer71696ac2008-10-16 15:03:22 -07001673set represented by the expected numeric type. In all other cases, and specifically
1674if the expected type is not a numeric type, the expression is erroneous.
Robert Griesemerc2d55862009-02-19 16:49:10 -08001675<p>
Robert Griesemer71696ac2008-10-16 15:03:22 -07001676For instance, if the expected numeric type is a uint32, any ideal number
1677which fits into a uint32 without loss of precision can be legally converted.
1678Thus, the values 991, 42.0, and 1e9 are ok, but -1, 3.14, or 1e100 are not.
Robert Griesemerad711102008-09-11 17:48:20 -07001679
Robert Griesemer71696ac2008-10-16 15:03:22 -07001680<!--
Robert Griesemerc8e18762008-09-12 12:26:22 -07001681TODO(gri) This may be overly constraining. What about "len(a) + c" where
1682c is an ideal number? Is len(a) of type int, or of an ideal number? Probably
Robert Griesemera3294712009-01-05 11:17:26 -08001683should be ideal number, because for arrays, it is a constant.
Robert Griesemer71696ac2008-10-16 15:03:22 -07001684-->
Robert Griesemerc8e18762008-09-12 12:26:22 -07001685
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001686
Robert Griesemerc2d55862009-02-19 16:49:10 -08001687<h3>Operands</h3>
Robert Griesemerad711102008-09-11 17:48:20 -07001688
1689Operands denote the elementary values in an expression.
1690
Robert Griesemerc2d55862009-02-19 16:49:10 -08001691<pre>
1692Operand = Literal | QualifiedIdent | "(" Expression ")" .
1693Literal = BasicLit | CompositeLit | FunctionLit .
1694BasicLit = int_lit | float_lit | char_lit | StringLit .
1695StringLit = string_lit { string_lit } .
1696</pre>
Robert Griesemerad711102008-09-11 17:48:20 -07001697
1698
Robert Griesemerc2d55862009-02-19 16:49:10 -08001699<h3>Constants</h3>
Robert Griesemerb90b2132008-09-19 15:49:55 -07001700
1701An operand is called ``constant'' if it is a literal of a basic type
Robert Griesemerc59b2a32008-09-30 10:57:59 -07001702(including the predeclared constants "true" and "false", and the values
1703denoted by "iota"), the predeclared constant "nil", or a parenthesized
1704constant expression (§Constant expressions). Constants have values that
1705are known at compile-time.
Robert Griesemerb90b2132008-09-19 15:49:55 -07001706
1707
Robert Griesemerc2d55862009-02-19 16:49:10 -08001708<h3>Qualified identifiers</h3>
Robert Griesemerad711102008-09-11 17:48:20 -07001709
Robert Griesemer337af312008-11-17 18:11:36 -08001710A qualified identifier is an identifier qualified by a package name.
Robert Griesemerc2d55862009-02-19 16:49:10 -08001711<p>
1712<font color=red>
Robert Griesemer337af312008-11-17 18:11:36 -08001713TODO(gri) expand this section.
Robert Griesemerc2d55862009-02-19 16:49:10 -08001714</font>
Robert Griesemer337af312008-11-17 18:11:36 -08001715
Robert Griesemerc2d55862009-02-19 16:49:10 -08001716<pre>
1717QualifiedIdent = { PackageName "." } identifier .
1718PackageName = identifier .
1719</pre>
Robert Griesemerad711102008-09-11 17:48:20 -07001720
1721
Robert Griesemerc2d55862009-02-19 16:49:10 -08001722<h3>Composite literals</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001723
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001724Literals for composite data structures consist of the type of the value
Robert Griesemer91bbd642009-01-07 09:31:35 -08001725followed by a braced expression list for array, slice, and structure literals,
Robert Griesemer0976e342008-09-03 13:37:44 -07001726or a list of expression pairs for map literals.
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001727
Robert Griesemerc2d55862009-02-19 16:49:10 -08001728<pre>
1729CompositeLit = LiteralType "(" [ ( ExpressionList | ExprPairList ) [ "," ] ] ")" .
1730LiteralType = Type | "[" "..." "]" ElementType .
1731ExprPairList = ExprPair { "," ExprPair } .
1732ExprPair = Expression ":" Expression .
1733</pre>
Robert Griesemer0976e342008-09-03 13:37:44 -07001734
Robert Griesemer91bbd642009-01-07 09:31:35 -08001735The LiteralType must be an struct, array, slice, or map type.
1736The types of the expressions must match the respective field, element, and
1737key types of the LiteralType; there is no automatic type conversion.
Robert Griesemer47121652008-10-23 17:19:56 -07001738Composite literals are values of the type specified by LiteralType; that is
1739a new value is created every time the literal is evaluated. To get
Robert Griesemerc2d55862009-02-19 16:49:10 -08001740a pointer to the literal, the address operator "&amp;" must be used.
1741<p>
Robert Griesemer0976e342008-09-03 13:37:44 -07001742Given
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001743
Robert Griesemerc2d55862009-02-19 16:49:10 -08001744<pre>
1745type Rat struct { num, den int }
1746type Num struct { r Rat; f float; s string }
1747</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001748
Robert Griesemer434c6052008-11-07 13:34:37 -08001749one can write
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001750
Robert Griesemerc2d55862009-02-19 16:49:10 -08001751<pre>
1752pi := Num(Rat(22, 7), 3.14159, "pi");
1753</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001754
Robert Griesemera3294712009-01-05 11:17:26 -08001755The length of an array literal is the length specified in the LiteralType.
1756If fewer elements than the length are provided in the literal, the missing
1757elements are set to the appropriate zero value for the array element type.
Robert Griesemer91bbd642009-01-07 09:31:35 -08001758It is an error to provide more elements than specified in LiteralType. The
1759notation "..." may be used in place of the length expression to denote a
1760length equal to the number of elements in the literal.
Robert Griesemerb90b2132008-09-19 15:49:55 -07001761
Robert Griesemerc2d55862009-02-19 16:49:10 -08001762<pre>
1763buffer := [10]string(); // len(buffer) == 10
1764primes := [6]int(2, 3, 5, 7, 9, 11); // len(primes) == 6
1765days := [...]string("sat", "sun"); // len(days) == 2
1766</pre>
Robert Griesemer91bbd642009-01-07 09:31:35 -08001767
1768A slice literal is a slice describing the entire underlying array literal.
1769Thus, the length and capacity of a slice literal is the number of elements
1770provided in the literal. A slice literal of the form
1771
Robert Griesemerc2d55862009-02-19 16:49:10 -08001772<pre>
1773[]T(x1, x2, ... xn)
1774</pre>
Robert Griesemer91bbd642009-01-07 09:31:35 -08001775
1776is essentially a shortcut for a slice operation applied to an array literal:
1777
Robert Griesemerc2d55862009-02-19 16:49:10 -08001778<pre>
1779[n]T(x1, x2, ... xn)[0 : n]
1780</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001781
1782Map literals are similar except the elements of the expression list are
1783key-value pairs separated by a colon:
1784
Robert Griesemerc2d55862009-02-19 16:49:10 -08001785<pre>
1786m := map[string]int("good": 0, "bad": 1, "indifferent": 7);
1787</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001788
Robert Griesemerc2d55862009-02-19 16:49:10 -08001789<font color=red>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07001790TODO: Consider adding helper syntax for nested composites
1791(avoids repeating types but complicates the spec needlessly.)
Robert Griesemerc2d55862009-02-19 16:49:10 -08001792</font>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001793
1794
Robert Griesemerc2d55862009-02-19 16:49:10 -08001795<h3>Function literals</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001796
Robert Griesemer7abfcd92008-10-07 17:14:30 -07001797A function literal represents an anonymous function. It consists of a
1798specification of the function type and the function body. The parameter
1799and result types of the function type must all be complete types (§Types).
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001800
Robert Griesemerc2d55862009-02-19 16:49:10 -08001801<pre>
1802FunctionLit = "func" Signature Block .
1803Block = "{" [ StatementList ] "}" .
1804</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001805
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08001806The type of a function literal is the function type specified.
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001807
Robert Griesemerc2d55862009-02-19 16:49:10 -08001808<pre>
1809func (a, b int, z float) bool { return a*b &lt; int(z); }
1810</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001811
Robert Griesemer7231ceb2008-09-08 15:01:04 -07001812A function literal can be assigned to a variable of the
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08001813corresponding function type, or invoked directly.
Robert Griesemer7231ceb2008-09-08 15:01:04 -07001814
Robert Griesemerc2d55862009-02-19 16:49:10 -08001815<pre>
1816f := func(x, y int) int { return x + y; }
1817func(ch chan int) { ch &lt;- ACK; } (reply_chan)
1818</pre>
Robert Griesemer7231ceb2008-09-08 15:01:04 -07001819
Robert Griesemerd8a764c2009-02-06 17:01:10 -08001820Function literals are "closures": they may refer to variables
1821defined in a surrounding function. Those variables are then shared between
1822the surrounding function and the function literal, and they survive as long
1823as they are accessible in any way.
Robert Griesemer7231ceb2008-09-08 15:01:04 -07001824
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001825
Robert Griesemerc2d55862009-02-19 16:49:10 -08001826<h3>Primary expressions</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001827
Robert Griesemerc2d55862009-02-19 16:49:10 -08001828<pre>
1829PrimaryExpr =
1830 Operand |
1831 PrimaryExpr Selector |
1832 PrimaryExpr Index |
1833 PrimaryExpr Slice |
1834 PrimaryExpr TypeGuard |
1835 PrimaryExpr Call .
Robert Griesemer57b34612008-10-10 12:45:44 -07001836
Robert Griesemerc2d55862009-02-19 16:49:10 -08001837Selector = "." identifier .
1838Index = "[" Expression "]" .
1839Slice = "[" Expression ":" Expression "]" .
1840TypeGuard = "." "(" Type ")" .
1841Call = "(" [ ExpressionList ] ")" .
1842</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001843
1844
Robert Griesemerc2d55862009-02-19 16:49:10 -08001845<pre>
1846x
18472
1848(s + ".txt")
1849f(3.1415, true)
1850Point(1, 2)
1851m["foo"]
1852s[i : j + 1]
1853obj.color
1854Math.sin
1855f.p[i].x()
1856</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001857
1858
Robert Griesemerc2d55862009-02-19 16:49:10 -08001859<h3>Selectors</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001860
Robert Griesemerbbfe3122008-10-09 17:12:09 -07001861A primary expression of the form
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001862
Robert Griesemerc2d55862009-02-19 16:49:10 -08001863<pre>
1864x.f
1865</pre>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07001866
1867denotes the field or method f of the value denoted by x (or of *x if
Robert Griesemer071c91b2008-10-23 12:04:45 -07001868x is of pointer type). The identifier f is called the (field or method)
1869``selector''.
Robert Griesemerc2d55862009-02-19 16:49:10 -08001870<p>
Robert Griesemer071c91b2008-10-23 12:04:45 -07001871A selector f may denote a field f declared in a type T, or it may refer
1872to a field f declared in a nested anonymous field of T. Analogously,
1873f may denote a method f of T, or it may refer to a method f of the type
1874of a nested anonymous field of T. The number of anonymous fields traversed
1875to get to the field or method is called its ``depth'' in T.
Robert Griesemerc2d55862009-02-19 16:49:10 -08001876<p>
Robert Griesemer071c91b2008-10-23 12:04:45 -07001877More precisely, the depth of a field or method f declared in T is zero.
1878The depth of a field or method f declared anywhere inside
1879an anonymous field A declared in T is the depth of f in A plus one.
Robert Griesemerc2d55862009-02-19 16:49:10 -08001880<p>
Robert Griesemer071c91b2008-10-23 12:04:45 -07001881The following rules apply to selectors:
Robert Griesemerc2d55862009-02-19 16:49:10 -08001882<p>
Robert Griesemer071c91b2008-10-23 12:04:45 -070018831) For a value x of type T or *T where T is not an interface type,
1884x.f denotes the field or method at the shallowest depth in T where there
1885is such an f. The type of x.f is the type of the field or method f.
1886If there is not exactly one f with shallowest depth, the selector
1887expression is illegal.
Robert Griesemerc2d55862009-02-19 16:49:10 -08001888<p>
Robert Griesemer071c91b2008-10-23 12:04:45 -070018892) For a variable x of type I or *I where I is an interface type,
1890x.f denotes the actual method with name f of the value assigned
1891to x if there is such a method. The type of x.f is the type
1892of the method f. If no value or nil was assigned to x, x.f is illegal.
Robert Griesemerc2d55862009-02-19 16:49:10 -08001893<p>
Robert Griesemer071c91b2008-10-23 12:04:45 -070018943) In all other cases, x.f is illegal.
Robert Griesemerc2d55862009-02-19 16:49:10 -08001895<p>
Robert Griesemer071c91b2008-10-23 12:04:45 -07001896Thus, selectors automatically dereference pointers as necessary. For instance,
1897for an x of type *T where T declares an f, x.f is a shortcut for (*x).f.
1898Furthermore, for an x of type T containing an anonymous field A declared as *A
1899inside T, and where A contains a field f, x.f is a shortcut for (*x.A).f
1900(assuming that the selector is legal in the first place).
Robert Griesemerc2d55862009-02-19 16:49:10 -08001901<p>
Robert Griesemer071c91b2008-10-23 12:04:45 -07001902The following examples illustrate selector use in more detail. Given the
1903declarations:
1904
Robert Griesemerc2d55862009-02-19 16:49:10 -08001905<pre>
1906type T0 struct {
1907 x int;
1908}
Robert Griesemer071c91b2008-10-23 12:04:45 -07001909
Robert Griesemerc2d55862009-02-19 16:49:10 -08001910func (recv *T0) M0()
Robert Griesemer071c91b2008-10-23 12:04:45 -07001911
Robert Griesemerc2d55862009-02-19 16:49:10 -08001912type T1 struct {
1913 y int;
1914}
Robert Griesemer071c91b2008-10-23 12:04:45 -07001915
Robert Griesemerc2d55862009-02-19 16:49:10 -08001916func (recv T1) M1()
Robert Griesemer071c91b2008-10-23 12:04:45 -07001917
Robert Griesemerc2d55862009-02-19 16:49:10 -08001918type T2 struct {
1919 z int;
1920 T1;
1921 *T0;
1922}
Robert Griesemer071c91b2008-10-23 12:04:45 -07001923
Robert Griesemerc2d55862009-02-19 16:49:10 -08001924func (recv *T2) M2()
Robert Griesemer071c91b2008-10-23 12:04:45 -07001925
Robert Griesemerc2d55862009-02-19 16:49:10 -08001926var p *T2; // with p != nil and p.T1 != nil
1927</pre>
Robert Griesemer071c91b2008-10-23 12:04:45 -07001928
Robert Griesemer434c6052008-11-07 13:34:37 -08001929one can write:
Robert Griesemer071c91b2008-10-23 12:04:45 -07001930
Robert Griesemerc2d55862009-02-19 16:49:10 -08001931<pre>
1932p.z // (*p).z
1933p.y // ((*p).T1).y
1934p.x // (*(*p).T0).x
Robert Griesemer071c91b2008-10-23 12:04:45 -07001935
Robert Griesemerc2d55862009-02-19 16:49:10 -08001936p.M2 // (*p).M2
1937p.M1 // ((*p).T1).M1
1938p.M0 // ((*p).T0).M0
1939</pre>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07001940
1941
Robert Griesemerc2d55862009-02-19 16:49:10 -08001942<font color=red>
Robert Griesemer071c91b2008-10-23 12:04:45 -07001943TODO: Specify what happens to receivers.
Robert Griesemerc2d55862009-02-19 16:49:10 -08001944</font>
Robert Griesemer7abfcd92008-10-07 17:14:30 -07001945
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001946
Robert Griesemerc2d55862009-02-19 16:49:10 -08001947<h3>Indexes</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001948
Robert Griesemerbbfe3122008-10-09 17:12:09 -07001949A primary expression of the form
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001950
Robert Griesemerc2d55862009-02-19 16:49:10 -08001951<pre>
1952a[x]
1953</pre>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07001954
Rob Pike4501d342009-02-19 17:31:36 -08001955<p>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07001956denotes the array or map element x. The value x is called the
1957``array index'' or ``map key'', respectively. The following
1958rules apply:
Rob Pike4501d342009-02-19 17:31:36 -08001959</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08001960<p>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07001961For a of type A or *A where A is an array type (§Array types):
Rob Pike4501d342009-02-19 17:31:36 -08001962</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08001963<ul>
1964 <li>x must be an integer value and 0 &lt;= x &lt; len(a)
1965 <li>a[x] is the array element at index x and the type of a[x]
Robert Griesemerbbfe3122008-10-09 17:12:09 -07001966 is the element type of A
Robert Griesemerc2d55862009-02-19 16:49:10 -08001967</ul>
1968<p>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07001969For a of type *M, where M is a map type (§Map types):
Rob Pike4501d342009-02-19 17:31:36 -08001970</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08001971<ul>
1972 <li>x must be of the same type as the key type of M
Robert Griesemerbbfe3122008-10-09 17:12:09 -07001973 and the map must contain an entry with key x
Robert Griesemerc2d55862009-02-19 16:49:10 -08001974 <li>a[x] is the map value with key x and the type of a[x]
Robert Griesemerbbfe3122008-10-09 17:12:09 -07001975 is the value type of M
Robert Griesemerc2d55862009-02-19 16:49:10 -08001976</ul>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07001977
1978Otherwise a[x] is illegal.
Robert Griesemerc2d55862009-02-19 16:49:10 -08001979<p>
1980<font color=red>
Robert Griesemerbbfe3122008-10-09 17:12:09 -07001981TODO: Need to expand map rules for assignments of the form v, ok = m[k].
Robert Griesemerc2d55862009-02-19 16:49:10 -08001982</font>
Robert Griesemer7abfcd92008-10-07 17:14:30 -07001983
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001984
Robert Griesemerc2d55862009-02-19 16:49:10 -08001985<h3>Slices</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001986
Robert Griesemer633957b2009-01-06 13:23:20 -08001987Strings, arrays, and slices can be ``sliced'' to construct substrings or descriptors
1988of subarrays. The index expressions in the slice select which elements appear
1989in the result. The result has indexes starting at 0 and length equal to the
1990difference in the index values in the slice. After slicing the array "a"
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001991
Robert Griesemerc2d55862009-02-19 16:49:10 -08001992<pre>
1993a := [4]int(1, 2, 3, 4);
1994s := a[1:3];
1995</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001996
Robert Griesemer633957b2009-01-06 13:23:20 -08001997the slice "s" has type "[]int", length 2, and elements
Robert Griesemerdf49fb32008-08-28 17:47:53 -07001998
Robert Griesemerc2d55862009-02-19 16:49:10 -08001999<pre>
2000s[0] == 2
2001s[1] == 3
2002</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002003
2004The index values in the slice must be in bounds for the original
2005array (or string) and the slice length must be non-negative.
Robert Griesemerc2d55862009-02-19 16:49:10 -08002006<p>
Robert Griesemer633957b2009-01-06 13:23:20 -08002007If the sliced operand is a string, the result of the slice operation is another
2008string (§String types). If the sliced operand is an array or slice, the result
2009of the slice operation is a slice (§Slice types).
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002010
2011
Robert Griesemerc2d55862009-02-19 16:49:10 -08002012<h3>Type guards</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002013
Robert Griesemer434c6052008-11-07 13:34:37 -08002014For an expression "x" and a type "T", the primary expression
2015
Robert Griesemerc2d55862009-02-19 16:49:10 -08002016<pre>
2017x.(T)
2018</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -08002019
2020asserts that the value stored in "x" is an element of type "T" (§Types).
2021The notation ".(T)" is called a ``type guard'', and "x.(T)" is called
2022a ``guarded expression''. The type of "x" must be an interface type.
Robert Griesemerc2d55862009-02-19 16:49:10 -08002023<p>
Robert Griesemer434c6052008-11-07 13:34:37 -08002024More precisely, if "T" is not an interface type, the expression asserts
2025that the dynamic type of "x" is identical to the type "T" (§Types).
2026If "T" is an interface type, the expression asserts that the dynamic type
2027of T implements the interface "T" (§Interface types). Because it can be
2028verified statically, a type guard in which the static type of "x" implements
2029the interface "T" is illegal. The type guard is said to succeed if the
2030assertion holds.
Robert Griesemerc2d55862009-02-19 16:49:10 -08002031<p>
Robert Griesemer434c6052008-11-07 13:34:37 -08002032If the type guard succeeds, the value of the guarded expression is the value
2033stored in "x" and its type is "T". If the type guard fails, a run-time
2034exception occurs. In other words, even though the dynamic type of "x"
2035is only known at run-time, the type of the guarded expression "x.(T)" is
2036known to be "T" in a correct program.
Robert Griesemerc2d55862009-02-19 16:49:10 -08002037<p>
Robert Griesemer434c6052008-11-07 13:34:37 -08002038As a special form, if a guarded expression is used in an assignment
2039
Robert Griesemerc2d55862009-02-19 16:49:10 -08002040<pre>
2041v, ok = x.(T)
2042v, ok := x.(T)
2043</pre>
Robert Griesemer434c6052008-11-07 13:34:37 -08002044
2045the result of the guarded expression is a pair of values with types "(T, bool)".
2046If the type guard succeeds, the expression returns the pair "(x.(T), true)";
2047that is, the value stored in "x" (of type "T") is assigned to "v", and "ok"
2048is set to true. If the type guard fails, the value in "v" is set to the initial
2049value for the type of "v" (§Program initialization and execution), and "ok" is
2050set to false. No run-time exception occurs in this case.
Robert Griesemerc2d55862009-02-19 16:49:10 -08002051<p>
2052<font color=red>
Robert Griesemer434c6052008-11-07 13:34:37 -08002053TODO add examples
Robert Griesemerc2d55862009-02-19 16:49:10 -08002054</font>
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -07002055
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002056
Robert Griesemerc2d55862009-02-19 16:49:10 -08002057<h3>Calls</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002058
Robert Griesemerc2d55862009-02-19 16:49:10 -08002059<font color=red>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002060TODO: This needs to be expanded and cleaned up.
Robert Griesemerc2d55862009-02-19 16:49:10 -08002061</font>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002062
2063Given a function or a function variable p, one writes
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002064
Robert Griesemerc2d55862009-02-19 16:49:10 -08002065<pre>
2066p()
2067</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002068
2069to call the function.
Robert Griesemerc2d55862009-02-19 16:49:10 -08002070<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002071A method is called using the notation
2072
Robert Griesemerc2d55862009-02-19 16:49:10 -08002073<pre>
2074receiver.method()
2075</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002076
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002077where receiver is a value of the receiver type of the method.
Robert Griesemerc2d55862009-02-19 16:49:10 -08002078<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002079For instance, given a *Point variable pt, one may call
2080
Robert Griesemerc2d55862009-02-19 16:49:10 -08002081<pre>
2082pt.Scale(3.5)
2083</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002084
2085The type of a method is the type of a function with the receiver as first
2086argument. For instance, the method "Scale" has type
2087
Robert Griesemerc2d55862009-02-19 16:49:10 -08002088<pre>
2089(p *Point, factor float)
2090</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002091
2092However, a function declared this way is not a method.
Robert Griesemerc2d55862009-02-19 16:49:10 -08002093<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002094There is no distinct method type and there are no method literals.
2095
2096
Robert Griesemerc2d55862009-02-19 16:49:10 -08002097<h3>Parameter passing</h3>
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002098
Robert Griesemerc2d55862009-02-19 16:49:10 -08002099<font color=red>
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002100TODO expand this section (right now only "..." parameters are covered).
Robert Griesemerc2d55862009-02-19 16:49:10 -08002101</font>
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002102
2103Inside a function, the type of the "..." parameter is the empty interface
2104"interface {}". The dynamic type of the parameter - that is, the type of
Robert Griesemer434c6052008-11-07 13:34:37 -08002105the value stored in the parameter - is of the form (in pseudo-
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002106notation)
2107
Robert Griesemerc2d55862009-02-19 16:49:10 -08002108<pre>
2109*struct {
2110 arg(0) typeof(arg(0));
2111 arg(1) typeof(arg(1));
2112 arg(2) typeof(arg(2));
2113 ...
2114 arg(n-1) typeof(arg(n-1));
2115}
2116</pre>
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002117
2118where the "arg(i)"'s correspond to the actual arguments passed in place
2119of the "..." parameter (the parameter and type names are for illustration
2120only). Reflection code may be used to access the struct value and its fields.
2121Thus, arguments provided in place of a "..." parameter are wrapped into
2122a corresponding struct, and a pointer to the struct is passed to the
2123function instead of the actual arguments.
2124
Robert Griesemer434c6052008-11-07 13:34:37 -08002125For instance, consider the function
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002126
Robert Griesemerc2d55862009-02-19 16:49:10 -08002127<pre>
2128func f(x int, s string, f_extra ...)
2129</pre>
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002130
2131and the call
2132
Robert Griesemerc2d55862009-02-19 16:49:10 -08002133<pre>
2134f(42, "foo", 3.14, true, []int(1, 2, 3))
2135</pre>
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002136
Robert Griesemer6f8df7a2009-02-11 21:57:15 -08002137Upon invocation, the parameters "3.14", "true", and "[]int(1, 2, 3)"
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002138are wrapped into a struct and the pointer to the struct is passed to f.
2139In f the type of parameter "f_extra" is "interface{}".
Robert Griesemer434c6052008-11-07 13:34:37 -08002140The dynamic type of "f_extra" is the type of the value assigned
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002141to it upon invocation (the field names "arg0", "arg1", "arg2" are made
2142up for illustration only, they are not accessible via reflection):
2143
Robert Griesemerc2d55862009-02-19 16:49:10 -08002144<pre>
2145*struct {
2146 arg0 float;
2147 arg1 bool;
2148 arg2 []int;
2149}
2150</pre>
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002151
2152The values of the fields "arg0", "arg1", and "arg2" are "3.14", "true",
Robert Griesemer6f8df7a2009-02-11 21:57:15 -08002153and "[]int(1, 2, 3)".
Robert Griesemerc2d55862009-02-19 16:49:10 -08002154<p>
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002155As a special case, if a function passes a "..." parameter as the argument
2156for a "..." parameter of a function, the parameter is not wrapped again into
2157a struct. Instead it is passed along unchanged. For instance, the function
2158f may call a function g with declaration
2159
Robert Griesemerc2d55862009-02-19 16:49:10 -08002160<pre>
2161func g(x int, g_extra ...)
2162</pre>
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002163
2164as
2165
Robert Griesemerc2d55862009-02-19 16:49:10 -08002166<pre>
2167g(x, f_extra);
2168</pre>
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002169
Robert Griesemer434c6052008-11-07 13:34:37 -08002170Inside g, the value stored in g_extra is the same as the value stored
Robert Griesemer69e26bf2008-11-04 16:46:45 -08002171in f_extra.
2172
2173
Robert Griesemerc2d55862009-02-19 16:49:10 -08002174<h3>Operators</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002175
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002176Operators combine operands into expressions.
2177
Robert Griesemerc2d55862009-02-19 16:49:10 -08002178<pre>
2179Expression = UnaryExpr | Expression binaryOp UnaryExpr .
2180UnaryExpr = PrimaryExpr | unary_op UnaryExpr .
Robert Griesemer57b34612008-10-10 12:45:44 -07002181
Robert Griesemerc2d55862009-02-19 16:49:10 -08002182binary_op = log_op | com_op | rel_op | add_op | mul_op .
2183log_op = "||" | "&amp;&amp;" .
2184com_op = "&lt;-" .
2185rel_op = "==" | "!=" | "&lt;" | "&lt;=" | ">" | ">=" .
2186add_op = "+" | "-" | "|" | "^" .
2187mul_op = "*" | "/" | "%" | "&lt;&lt;" | ">>" | "&amp;" .
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002188
Robert Griesemerc2d55862009-02-19 16:49:10 -08002189unary_op = "+" | "-" | "!" | "^" | "*" | "&amp;" | "&lt;-" .
2190</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002191
Robert Griesemerc2d55862009-02-19 16:49:10 -08002192<p>
Rob Pike4501d342009-02-19 17:31:36 -08002193The operand types in binary operations must be equal, with the following exceptions:
2194</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002195<ul>
2196 <li>If one operand has numeric type and the other operand is
Robert Griesemera6b546f2008-10-20 11:46:40 -07002197 an ideal number, the ideal number is converted to match the type of
2198 the other operand (§Expression).
Robert Griesemerc8e18762008-09-12 12:26:22 -07002199
Robert Griesemerc2d55862009-02-19 16:49:10 -08002200 <li>If both operands are ideal numbers, the conversion is to ideal floats
Robert Griesemere28cceb2008-09-11 18:23:28 -07002201 if one of the operands is an ideal float (relevant for "/" and "%").
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002202
Robert Griesemerc2d55862009-02-19 16:49:10 -08002203 <li>The right operand in a shift operation must be always be an unsigned int
Robert Griesemera6b546f2008-10-20 11:46:40 -07002204 (or an ideal number that can be safely converted into an unsigned int)
2205 (§Arithmetic operators).
2206
Robert Griesemerc2d55862009-02-19 16:49:10 -08002207 <li>When comparing two operands of channel type, the channel value types
Robert Griesemer18b05c12009-01-26 09:34:19 -08002208 must be equal but the channel direction is ignored.
Robert Griesemerc2d55862009-02-19 16:49:10 -08002209</ul>
Robert Griesemer18b05c12009-01-26 09:34:19 -08002210
Robert Griesemer57b34612008-10-10 12:45:44 -07002211Unary operators have the highest precedence. They are evaluated from
Robert Griesemera6b546f2008-10-20 11:46:40 -07002212right to left. Note that "++" and "--" are outside the unary operator
2213hierachy (they are statements) and they apply to the operand on the left.
2214Specifically, "*p++" means "(*p)++" in Go (as opposed to "*(p++)" in C).
Robert Griesemerc2d55862009-02-19 16:49:10 -08002215<p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002216There are six precedence levels for binary operators:
2217multiplication operators bind strongest, followed by addition
Robert Griesemerad711102008-09-11 17:48:20 -07002218operators, comparison operators, communication operators,
Robert Griesemerc2d55862009-02-19 16:49:10 -08002219"&amp;&amp;" (logical and), and finally "||" (logical or) with the
Robert Griesemerad711102008-09-11 17:48:20 -07002220lowest precedence:
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002221
Robert Griesemerc2d55862009-02-19 16:49:10 -08002222<pre>
2223Precedence Operator
2224 6 * / % &lt;&lt; >> &amp;
2225 5 + - | ^
2226 4 == != &lt; &lt;= > >=
2227 3 &lt;-
2228 2 &amp;&amp;
2229 1 ||
2230</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002231
Robert Griesemer57b34612008-10-10 12:45:44 -07002232Binary operators of the same precedence associate from left to right.
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002233For instance, "x / y / z" stands for "(x / y) / z".
Robert Griesemerc2d55862009-02-19 16:49:10 -08002234<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002235Examples
2236
Robert Griesemerc2d55862009-02-19 16:49:10 -08002237<pre>
2238+x
223923 + 3*x[i]
2240x &lt;= f()
2241^a >> b
2242f() || g()
2243x == y + 1 &amp;&amp; &lt;-chan_ptr > 0
2244</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002245
2246
Robert Griesemerc2d55862009-02-19 16:49:10 -08002247<h3>Arithmetic operators</h3>
2248<p>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002249Arithmetic operators apply to numeric types and yield a result of the same
2250type as the first operand. The four standard arithmetic operators ("+", "-",
2251"*", "/") apply to both integer and floating point types, while "+" also applies
2252to strings and arrays; all other arithmetic operators apply to integer types only.
2253
Robert Griesemerc2d55862009-02-19 16:49:10 -08002254<pre>
2255+ sum integers, floats, strings, arrays
2256- difference integers, floats
2257* product integers, floats
2258/ quotient integers, floats
2259% remainder integers
2260
2261&amp; bitwise and integers
2262| bitwise or integers
2263^ bitwise xor integers
2264
2265<< left shift integer << unsigned integer
2266>> right shift integer >> unsigned integer
2267</pre>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002268
Robert Griesemer6f8df7a2009-02-11 21:57:15 -08002269Strings can be concatenated using the "+" operator (or the "+=" assignment):
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002270
Robert Griesemerc2d55862009-02-19 16:49:10 -08002271<pre>
2272s := "hi" + string(c)
2273</pre>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002274
Robert Griesemer6f8df7a2009-02-11 21:57:15 -08002275String addition creates a new string by copying the elements.
Robert Griesemerc2d55862009-02-19 16:49:10 -08002276<p>
Robert Griesemer7231ceb2008-09-08 15:01:04 -07002277For integer values, "/" and "%" satisfy the following relationship:
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002278
Robert Griesemerc2d55862009-02-19 16:49:10 -08002279<pre>
2280(a / b) * b + a % b == a
2281</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002282
2283and
2284
Robert Griesemerc2d55862009-02-19 16:49:10 -08002285<pre>
2286(a / b) is "truncated towards zero".
2287</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002288
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002289Examples:
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002290
Robert Griesemerc2d55862009-02-19 16:49:10 -08002291<pre>
2292 x y x / y x % y
2293 5 3 1 2
2294-5 3 -1 -2
2295 5 -3 -1 2
2296-5 -3 1 -2
2297</pre>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002298
2299Note that if the dividend is positive and the divisor is a constant power of 2,
2300the division may be replaced by a left shift, and computing the remainder may
2301be replaced by a bitwise "and" operation:
2302
Robert Griesemerc2d55862009-02-19 16:49:10 -08002303<pre>
2304 x x / 4 x % 4 x >> 2 x &amp; 3
2305 11 2 3 2 3
2306-11 -2 -3 -3 1
2307</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002308
2309The shift operators shift the left operand by the shift count specified by the
2310right operand. They implement arithmetic shifts if the left operand is a signed
2311integer, and logical shifts if it is an unsigned integer. The shift count must
2312be an unsigned integer. There is no upper limit on the shift count. It is
2313as if the left operand is shifted "n" times by 1 for a shift count of "n".
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002314Specifically, "x << 1" is the same as "x*2"; and "x >> 1" is the same as
2315"x/2 truncated towards negative infinity".
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002316
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002317For integer operands, the unary operators "+", "-", and "^" are defined as
2318follows:
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002319
Robert Griesemerc2d55862009-02-19 16:49:10 -08002320<pre>
2321+x is 0 + x
2322-x negation is 0 - x
2323^x bitwise complement is m ^ x with m = "all bits set to 1"
2324</pre>
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002325
2326
Robert Griesemerc2d55862009-02-19 16:49:10 -08002327<h3>Integer overflow</h3>
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002328
Robert Griesemerc2d55862009-02-19 16:49:10 -08002329For unsigned integer values, the operations "+", "-", "*", and "&lt;&lt;" are
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002330computed modulo 2^n, where n is the bit width of the unsigned integer type
2331(§Arithmetic types). Loosely speaking, these unsigned integer operations
2332discard high bits upon overflow, and programs may rely on ``wrap around''.
Robert Griesemerc2d55862009-02-19 16:49:10 -08002333<p>
2334For signed integers, the operations "+", "-", "*", and "&lt;&lt;" may legally
Robert Griesemer9dfb2ea2008-12-12 10:30:10 -08002335overflow and the resulting value exists and is deterministically defined
2336by the signed integer representation, the operation, and its operands.
2337No exception is raised as a result of overflow. As a consequence, a
2338compiler may not optimize code under the assumption that overflow does
Robert Griesemerc2d55862009-02-19 16:49:10 -08002339not occur. For instance, it may not assume that "x &lt; x + 1" is always true.
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002340
2341
Robert Griesemerc2d55862009-02-19 16:49:10 -08002342<h3>Comparison operators</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002343
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002344Comparison operators yield a boolean result. All comparison operators apply
2345to strings and numeric types. The operators "==" and "!=" also apply to
Robert Griesemer7471eab2009-01-27 14:51:24 -08002346boolean values, pointer, interface, and channel types. Slice and
2347map types only support testing for equality against the predeclared value
2348"nil".
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002349
Robert Griesemerc2d55862009-02-19 16:49:10 -08002350<pre>
2351== equal
2352!= not equal
2353< less
2354<= less or equal
2355> greater
2356>= greater or equal
2357</pre>
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002358
Robert Griesemer52a54802008-09-30 13:02:50 -07002359Strings are compared byte-wise (lexically).
Robert Griesemerc2d55862009-02-19 16:49:10 -08002360<p>
Robert Griesemer18b05c12009-01-26 09:34:19 -08002361Booleans are equal if they are either both "true" or both "false".
Robert Griesemerc2d55862009-02-19 16:49:10 -08002362<p>
Robert Griesemerb5e0cc72008-10-10 16:34:44 -07002363Pointers are equal if they point to the same value.
Robert Griesemerc2d55862009-02-19 16:49:10 -08002364<p>
Robert Griesemer18b05c12009-01-26 09:34:19 -08002365Interface, slice, map, and channel types can be compared for equality according
2366to the rules specified in the section on §Interface types, §Slice types, §Map types,
2367and §Channel types, respectively.
Robert Griesemera3294712009-01-05 11:17:26 -08002368
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002369
Robert Griesemerc2d55862009-02-19 16:49:10 -08002370<h3>Logical operators</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002371
Robert Griesemer41d65ac2008-09-04 15:17:27 -07002372Logical operators apply to boolean operands and yield a boolean result.
2373The right operand is evaluated conditionally.
2374
Robert Griesemerc2d55862009-02-19 16:49:10 -08002375<pre>
2376&amp;&amp; conditional and p &amp;&amp; q is "if p then q else false"
2377|| conditional or p || q is "if p then true else q"
2378! not !p is "not p"
2379</pre>
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -07002380
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002381
Robert Griesemerc2d55862009-02-19 16:49:10 -08002382<h3>Address operators</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002383
Robert Griesemerc2d55862009-02-19 16:49:10 -08002384<font color=red>TODO: Need to talk about unary "*", clean up section below.</font>
2385<p>
2386<font color=red>TODO: This text needs to be cleaned up and go elsewhere, there are no address
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002387operators involved.
Robert Griesemerc2d55862009-02-19 16:49:10 -08002388</font>
2389<p>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002390Methods are a form of function, and a method ``value'' has a function type.
2391Consider the type T with method M:
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002392
Robert Griesemerc2d55862009-02-19 16:49:10 -08002393<pre>
2394type T struct {
2395 a int;
2396}
2397func (tp *T) M(a int) int;
2398var t *T;
2399</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002400
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002401To construct the value of method M, one writes
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002402
Robert Griesemerc2d55862009-02-19 16:49:10 -08002403<pre>
2404t.M
2405</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002406
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002407using the variable t (not the type T).
Robert Griesemerc2d55862009-02-19 16:49:10 -08002408<font color=red>TODO: It makes perfect sense to be able to say T.M (in fact, it makes more
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002409sense then t.M, since only the type T is needed to find the method M, i.e.,
2410its address). TBD.
Robert Griesemerc2d55862009-02-19 16:49:10 -08002411</font>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002412
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002413The expression t.M is a function value with type
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002414
Robert Griesemerc2d55862009-02-19 16:49:10 -08002415<pre>
2416func (t *T, a int) int
2417</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002418
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002419and may be invoked only as a function, not as a method:
2420
Robert Griesemerc2d55862009-02-19 16:49:10 -08002421<pre>
2422var f func (t *T, a int) int;
2423f = t.M;
2424x := f(t, 7);
2425</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002426
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002427Note that one does not write t.f(7); taking the value of a method demotes
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002428it to a function.
2429
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002430In general, given type T with method M and variable t of type T,
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002431the method invocation
2432
Robert Griesemerc2d55862009-02-19 16:49:10 -08002433<pre>
2434t.M(args)
2435</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002436
2437is equivalent to the function call
2438
Robert Griesemerc2d55862009-02-19 16:49:10 -08002439<pre>
2440(t.M)(t, args)
2441</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002442
Robert Griesemerc2d55862009-02-19 16:49:10 -08002443<font color=red>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002444TODO: should probably describe the effect of (t.m) under §Expressions if t.m
2445denotes a method: Effect is as described above, converts into function.
Robert Griesemerc2d55862009-02-19 16:49:10 -08002446</font>
2447<p>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002448If T is an interface type, the expression t.M does not determine which
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002449underlying type's M is called until the point of the call itself. Thus given
Robert Griesemerd8a764c2009-02-06 17:01:10 -08002450T1 and T2, both implementing interface I with method M, the sequence
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002451
Robert Griesemerc2d55862009-02-19 16:49:10 -08002452<pre>
2453var t1 *T1;
2454var t2 *T2;
2455var i I = t1;
2456m := i.M;
2457m(t2, 7);
2458</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002459
2460will invoke t2.M() even though m was constructed with an expression involving
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002461t1. Effectively, the value of m is a function literal
2462
Robert Griesemerc2d55862009-02-19 16:49:10 -08002463<pre>
2464func (recv I, a int) {
2465 recv.M(a);
2466}
2467</pre>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002468
2469that is automatically created.
Robert Griesemerc2d55862009-02-19 16:49:10 -08002470<p>
2471<font color=red>
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002472TODO: Document implementation restriction: It is illegal to take the address
Robert Griesemerc2d55862009-02-19 16:49:10 -08002473of a result parameter (e.g.: func f() (x int, p *int) { return 2, &amp;x }).
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002474(TBD: is it an implementation restriction or fact?)
Robert Griesemerc2d55862009-02-19 16:49:10 -08002475</font>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002476
Robert Griesemerc2d55862009-02-19 16:49:10 -08002477<h3>Communication operators</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002478
2479The syntax presented above covers communication operations. This
2480section describes their form and function.
Robert Griesemerc2d55862009-02-19 16:49:10 -08002481<p>
Robert Griesemera3294712009-01-05 11:17:26 -08002482Here the term "channel" means "variable of type chan".
Robert Griesemerc2d55862009-02-19 16:49:10 -08002483<p>
Robert Griesemer633957b2009-01-06 13:23:20 -08002484The built-in function "make" makes a new channel value:
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002485
Robert Griesemerc2d55862009-02-19 16:49:10 -08002486<pre>
2487ch := make(chan int)
2488</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002489
Robert Griesemer633957b2009-01-06 13:23:20 -08002490An optional argument to "make()" specifies a buffer size for an
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002491asynchronous channel; if absent or zero, the channel is synchronous:
2492
Robert Griesemerc2d55862009-02-19 16:49:10 -08002493<pre>
2494sync_chan := make(chan int)
2495buffered_chan := make(chan int, 10)
2496</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002497
Robert Griesemerc2d55862009-02-19 16:49:10 -08002498The send operation uses the binary operator "&lt;-", which operates on
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002499a channel and a value (expression):
2500
Robert Griesemerc2d55862009-02-19 16:49:10 -08002501<pre>
2502ch <- 3
2503</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002504
2505In this form, the send operation is an (expression) statement that
Rob Pike569a1072008-10-03 11:18:45 -07002506sends the value on the channel. Both the channel and the expression
2507are evaluated before communication begins. Communication blocks
2508until the send can proceed, at which point the value is transmitted
2509on the channel.
Robert Griesemerc2d55862009-02-19 16:49:10 -08002510<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002511If the send operation appears in an expression context, the value
2512of the expression is a boolean and the operation is non-blocking.
2513The value of the boolean reports true if the communication succeeded,
2514false if it did not. These two examples are equivalent:
2515
Robert Griesemerc2d55862009-02-19 16:49:10 -08002516<pre>
2517ok := ch <- 3;
2518if ok { print("sent") } else { print("not sent") }
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002519
Robert Griesemerc2d55862009-02-19 16:49:10 -08002520if ch <- 3 { print("sent") } else { print("not sent") }
2521</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002522
2523In other words, if the program tests the value of a send operation,
2524the send is non-blocking and the value of the expression is the
2525success of the operation. If the program does not test the value,
2526the operation blocks until it succeeds.
Robert Griesemerc2d55862009-02-19 16:49:10 -08002527<p>
2528<font color=red>
Robert Griesemer2902a822008-09-17 13:57:11 -07002529TODO: Adjust the above depending on how we rule on the ok semantics.
Rob Pike569a1072008-10-03 11:18:45 -07002530For instance, does the sent expression get evaluated if ok is false?
Robert Griesemerc2d55862009-02-19 16:49:10 -08002531</font>
2532<p>
2533The receive operation uses the prefix unary operator "&lt;-".
Robert Griesemer2902a822008-09-17 13:57:11 -07002534The value of the expression is the value received:
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002535
Robert Griesemerc2d55862009-02-19 16:49:10 -08002536<pre>
2537<-ch
2538</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002539
2540The expression blocks until a value is available, which then can
2541be assigned to a variable or used like any other expression:
2542
Robert Griesemerc2d55862009-02-19 16:49:10 -08002543<pre>
2544v1 := <-ch
2545v2 = <-ch
2546f(<-ch)
2547</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002548
2549If the receive expression does not save the value, the value is
2550discarded:
2551
Robert Griesemerc2d55862009-02-19 16:49:10 -08002552<pre>
2553<-strobe // wait until clock pulse
2554</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002555
Robert Griesemer2902a822008-09-17 13:57:11 -07002556If a receive expression is used in a tuple assignment of the form
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002557
Robert Griesemerc2d55862009-02-19 16:49:10 -08002558<pre>
2559x, ok = <-ch; // or: x, ok := <-ch
2560</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002561
Robert Griesemer2902a822008-09-17 13:57:11 -07002562the receive operation becomes non-blocking, and the boolean variable
2563"ok" will be set to "true" if the receive operation succeeded, and set
2564to "false" otherwise.
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002565
2566
Robert Griesemerc2d55862009-02-19 16:49:10 -08002567<h3>Constant expressions</h3>
Robert Griesemerb90b2132008-09-19 15:49:55 -07002568
2569A constant expression is an expression whose operands are all constants
2570(§Constants). Additionally, the result of the predeclared functions
2571below (with appropriate arguments) is also constant:
2572
Robert Griesemerc2d55862009-02-19 16:49:10 -08002573<pre>
2574len(a) if a is an array (as opposed to an array slice)
2575</pre>
Robert Griesemerb90b2132008-09-19 15:49:55 -07002576
Robert Griesemerc2d55862009-02-19 16:49:10 -08002577<font color=red>
Robert Griesemerb90b2132008-09-19 15:49:55 -07002578TODO: Complete this list as needed.
Robert Griesemerc2d55862009-02-19 16:49:10 -08002579</font>
2580<p>
Robert Griesemerb90b2132008-09-19 15:49:55 -07002581Constant expressions can be evaluated at compile time.
2582
Robert Griesemerc2d55862009-02-19 16:49:10 -08002583<hr>
Robert Griesemerb90b2132008-09-19 15:49:55 -07002584
Robert Griesemerc2d55862009-02-19 16:49:10 -08002585<h2>Statements</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002586
2587Statements control execution.
2588
Robert Griesemerc2d55862009-02-19 16:49:10 -08002589<pre>
2590Statement =
2591 Declaration | LabelDecl | EmptyStat |
2592 SimpleStat | GoStat | ReturnStat | BreakStat | ContinueStat | GotoStat |
2593 FallthroughStat | Block | IfStat | SwitchStat | SelectStat | ForStat |
2594 DeferStat .
Robert Griesemer7abfcd92008-10-07 17:14:30 -07002595
Robert Griesemerc2d55862009-02-19 16:49:10 -08002596SimpleStat =
2597 ExpressionStat | IncDecStat | Assignment | SimpleVarDecl .
2598</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002599
Robert Griesemer7271e042008-10-09 20:05:24 -07002600
Robert Griesemeraed247f2008-10-08 17:05:30 -07002601Statements in a statement list are separated by semicolons, which can be
2602omitted in some cases as expressed by the OptSemicolon production.
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002603
Robert Griesemerc2d55862009-02-19 16:49:10 -08002604<pre>
2605StatementList = Statement { OptSemicolon Statement } .
2606</pre>
Robert Griesemer7271e042008-10-09 20:05:24 -07002607
Robert Griesemerc2d55862009-02-19 16:49:10 -08002608<p>
Rob Pike4501d342009-02-19 17:31:36 -08002609A semicolon may be omitted immediately following:
2610</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08002611<ul>
2612 <li>a closing parenthesis ")" ending a list of declarations (§Declarations and scope rules)
2613 <li>a closing brace "}" ending a type declaration (§Type declarations)
2614 <li>a closing brace "}" ending a block (including switch and select statements)
2615 <li>a label declaration (§Label declarations)
2616</ul>
Robert Griesemer7271e042008-10-09 20:05:24 -07002617
2618In all other cases a semicolon is required to separate two statements. Since there
2619is an empty statement, a statement list can always be ``terminated'' with a semicolon.
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002620
2621
Robert Griesemerc2d55862009-02-19 16:49:10 -08002622<h3>Empty statements</h3>
Robert Griesemeraed247f2008-10-08 17:05:30 -07002623
2624The empty statement does nothing.
2625
Robert Griesemerc2d55862009-02-19 16:49:10 -08002626<pre>
2627EmptyStat = .
2628</pre>
Robert Griesemeraed247f2008-10-08 17:05:30 -07002629
2630
Robert Griesemerc2d55862009-02-19 16:49:10 -08002631<h3>Expression statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002632
Robert Griesemerc2d55862009-02-19 16:49:10 -08002633<pre>
2634ExpressionStat = Expression .
2635</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002636
Robert Griesemerc2d55862009-02-19 16:49:10 -08002637<pre>
2638f(x+y)
2639</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002640
Robert Griesemerc2d55862009-02-19 16:49:10 -08002641<font color=red>
Robert Griesemeraed247f2008-10-08 17:05:30 -07002642TODO: specify restrictions. 6g only appears to allow calls here.
Robert Griesemerc2d55862009-02-19 16:49:10 -08002643</font>
Robert Griesemeraed247f2008-10-08 17:05:30 -07002644
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002645
Robert Griesemerc2d55862009-02-19 16:49:10 -08002646<h3>IncDec statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002647
Robert Griesemer52a54802008-09-30 13:02:50 -07002648The "++" and "--" statements increment or decrement their operands
2649by the (ideal) constant value 1.
2650
Robert Griesemerc2d55862009-02-19 16:49:10 -08002651<pre>
2652IncDecStat = Expression ( "++" | "--" ) .
2653</pre>
Robert Griesemer52a54802008-09-30 13:02:50 -07002654
2655The following assignment statements (§Assignments) are semantically
2656equivalent:
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002657
Robert Griesemerc2d55862009-02-19 16:49:10 -08002658<pre>
2659IncDec statement Assignment
2660x++ x += 1
2661x-- x -= 1
2662</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002663
Robert Griesemer52a54802008-09-30 13:02:50 -07002664Both operators apply to integer and floating point types only.
Robert Griesemerc2d55862009-02-19 16:49:10 -08002665<p>
Robert Griesemer52a54802008-09-30 13:02:50 -07002666Note that increment and decrement are statements, not expressions.
2667For instance, "x++" cannot be used as an operand in an expression.
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002668
2669
Robert Griesemerc2d55862009-02-19 16:49:10 -08002670<h3>Assignments</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002671
Robert Griesemerc2d55862009-02-19 16:49:10 -08002672<pre>
2673Assignment = ExpressionList assign_op ExpressionList .
2674</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002675
Robert Griesemerc2d55862009-02-19 16:49:10 -08002676<pre>
2677assign_op = [ add_op | mul_op ] "=" .
2678</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002679
2680The left-hand side must be an l-value such as a variable, pointer indirection,
2681or an array index.
2682
Robert Griesemerc2d55862009-02-19 16:49:10 -08002683<pre>
2684x = 1
2685*p = f()
2686a[i] = 23
2687k = <-ch
2688</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002689
2690As in C, arithmetic binary operators can be combined with assignments:
2691
Robert Griesemerc2d55862009-02-19 16:49:10 -08002692<pre>
2693j <<= 2
2694</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002695
2696A tuple assignment assigns the individual elements of a multi-valued operation,
2697such as function evaluation or some channel and map operations, into individual
2698variables. For instance, a tuple assignment such as
2699
Robert Griesemerc2d55862009-02-19 16:49:10 -08002700<pre>
2701v1, v2, v3 = e1, e2, e3
2702</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002703
2704assigns the expressions e1, e2, e3 to temporaries and then assigns the temporaries
2705to the variables v1, v2, v3. Thus
2706
Robert Griesemerc2d55862009-02-19 16:49:10 -08002707<pre>
2708a, b = b, a
2709</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002710
2711exchanges the values of a and b. The tuple assignment
2712
Robert Griesemerc2d55862009-02-19 16:49:10 -08002713<pre>
2714x, y = f()
2715</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002716
2717calls the function f, which must return two values, and assigns them to x and y.
2718As a special case, retrieving a value from a map, when written as a two-element
2719tuple assignment, assign a value and a boolean. If the value is present in the map,
2720the value is assigned and the second, boolean variable is set to true. Otherwise,
2721the variable is unchanged, and the boolean value is set to false.
2722
Robert Griesemerc2d55862009-02-19 16:49:10 -08002723<pre>
2724value, present = map_var[key]
2725</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002726
2727To delete a value from a map, use a tuple assignment with the map on the left
2728and a false boolean expression as the second expression on the right, such
2729as:
2730
Robert Griesemerc2d55862009-02-19 16:49:10 -08002731<pre>
2732map_var[key] = value, false
2733</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002734
2735In assignments, the type of the expression must match the type of the left-hand side.
2736
2737
Robert Griesemerc2d55862009-02-19 16:49:10 -08002738<h3>If statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002739
Robert Griesemerac055792008-09-26 11:15:14 -07002740If statements specify the conditional execution of two branches; the "if"
2741and the "else" branch. If Expression evaluates to true,
2742the "if" branch is executed. Otherwise the "else" branch is executed if present.
2743If Condition is omitted, it is equivalent to true.
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002744
Robert Griesemerc2d55862009-02-19 16:49:10 -08002745<pre>
2746IfStat = "if" [ [ SimpleStat ] ";" ] [ Expression ] Block [ "else" Statement ] .
2747</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002748
Robert Griesemerc2d55862009-02-19 16:49:10 -08002749<pre>
2750if x > 0 {
2751 return true;
2752}
2753</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002754
2755An "if" statement may include the declaration of a single temporary variable.
2756The scope of the declared variable extends to the end of the if statement, and
2757the variable is initialized once before the statement is entered.
2758
Robert Griesemerc2d55862009-02-19 16:49:10 -08002759<pre>
2760if x := f(); x < y {
2761 return x;
2762} else if x > z {
2763 return z;
2764} else {
2765 return y;
2766}
2767</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002768
2769
Robert Griesemerac055792008-09-26 11:15:14 -07002770<!--
2771TODO: gri thinks that Statement needs to be changed as follows:
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002772
2773 IfStat =
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08002774 "if" [ [ SimpleStat ] ";" ] [ Expression ] Block
Robert Griesemerac055792008-09-26 11:15:14 -07002775 [ "else" ( IfStat | Block ) ] .
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002776
Robert Griesemerac055792008-09-26 11:15:14 -07002777To facilitate the "if else if" code pattern, if the "else" branch is
2778simply another "if" statement, that "if" statement may be written
2779without the surrounding Block:
2780
2781 if x > 0 {
2782 return 0;
2783 } else if x > 10 {
2784 return 1;
2785 } else {
2786 return 2;
2787 }
2788
2789-->
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002790
Robert Griesemerc2d55862009-02-19 16:49:10 -08002791<h3>Switch statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002792
2793Switches provide multi-way execution.
2794
Robert Griesemerc2d55862009-02-19 16:49:10 -08002795<pre>
2796SwitchStat = "switch" [ [ SimpleStat ] ";" ] [ Expression ] "{" { CaseClause } "}" .
2797CaseClause = SwitchCase ":" [ StatementList ] .
2798SwitchCase = "case" ExpressionList | "default" .
2799</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002800
Robert Griesemeraed247f2008-10-08 17:05:30 -07002801There can be at most one default case in a switch statement. In a case clause,
2802the last statement only may be a fallthrough statement ($Fallthrough statement).
2803It indicates that the control should flow from the end of this case clause to
2804the first statement of the next clause.
Robert Griesemerc2d55862009-02-19 16:49:10 -08002805<p>
Robert Griesemer347cf672008-10-03 14:04:28 -07002806Each case clause effectively acts as a block for scoping purposes
2807($Declarations and scope rules).
Robert Griesemerc2d55862009-02-19 16:49:10 -08002808<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002809The expressions do not need to be constants. They will
2810be evaluated top to bottom until the first successful non-default case is reached.
2811If none matches and there is a default case, the statements of the default
2812case are executed.
2813
Robert Griesemerc2d55862009-02-19 16:49:10 -08002814<pre>
2815switch tag {
2816default: s3()
2817case 0, 1: s1()
2818case 2: s2()
2819}
2820</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002821
2822A switch statement may include the declaration of a single temporary variable.
2823The scope of the declared variable extends to the end of the switch statement, and
2824the variable is initialized once before the switch is entered.
2825
Robert Griesemerc2d55862009-02-19 16:49:10 -08002826<pre>
2827switch x := f(); true {
2828case x &lt; 0: return -x
2829default: return x
2830}
2831</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002832
2833Cases do not fall through unless explicitly marked with a "fallthrough" statement.
2834
Robert Griesemerc2d55862009-02-19 16:49:10 -08002835<pre>
2836switch a {
2837case 1:
2838 b();
2839 fallthrough
2840case 2:
2841 c();
2842}
2843</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002844
2845If the expression is omitted, it is equivalent to "true".
2846
Robert Griesemerc2d55862009-02-19 16:49:10 -08002847<pre>
2848switch {
2849case x < y: f1();
2850case x < z: f2();
2851case x == 4: f3();
2852}
2853</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002854
2855
Robert Griesemerc2d55862009-02-19 16:49:10 -08002856<h3>For statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002857
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08002858A for statement specifies repeated execution of a block. The iteration is
2859controlled by a condition, a for clause, or a range clause.
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002860
Robert Griesemerc2d55862009-02-19 16:49:10 -08002861<pre>
2862ForStat = "for" [ Condition | ForClause | RangeClause ] Block .
2863Condition = Expression .
2864</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002865
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08002866In its simplest form, a for statement specifies the repeated execution of
2867a block as long as a condition evaluates to true. The condition is evaluated
2868before each iteration. The type of the condition expression must be boolean.
2869If the condition is absent, it is equivalent to "true".
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002870
Robert Griesemerc2d55862009-02-19 16:49:10 -08002871<pre>
2872for a &lt; b {
2873 a *= 2
2874}
2875</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002876
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08002877A for statement with a for clause is also controlled by its condition, but
2878additionally it may specify an init and post statement, such as an assignment,
2879an increment or decrement statement. The init statement may also be a (simple)
2880variable declaration; no variables can be declared in the post statement.
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002881
Robert Griesemerc2d55862009-02-19 16:49:10 -08002882<pre>
2883ForClause = [ InitStat ] ";" [ Condition ] ";" [ PostStat ] .
2884InitStat = SimpleStat .
2885PostStat = SimpleStat .
2886</pre>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08002887
2888For instance, one may declare an iteration variable in the init statement:
2889
Robert Griesemerc2d55862009-02-19 16:49:10 -08002890<pre>
2891for i := 0; i < 10; i++ {
2892 f(i)
2893}
2894</pre>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08002895
2896If present, the init statement is executed once before commencing the iteration;
2897the post statement is executed after each execution of the statement block (and
2898only if the block was executed). The scope of any variable declared in the init
2899statement ends with the end of the for statement block ($Declarations and scope
2900rules, Rule 3).
Robert Griesemerc2d55862009-02-19 16:49:10 -08002901<p>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08002902The init and post statement as well as the condition may be omitted; however
2903if either the init or post statement are present, the separating semicolons
2904must be present. If the condition is absent, it is equivalent to "true".
2905The following statements are equivalent:
2906
Robert Griesemerc2d55862009-02-19 16:49:10 -08002907<pre>
2908for ; cond ; { S() } is the same as for cond { S() }
2909for true { S() } is the same as for { S() }
2910</pre>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08002911
2912Alternatively, a for statement may be controlled by a range clause. A
2913range clause specifies iteration through all entries of an array or map.
2914For each entry it first assigns the current index or key to an iteration
2915variable - or the current (index, element) or (key, value) pair to a pair
2916of iteration variables - and then executes the block. Iteration terminates
2917when all entries have been processed, or if the for statement is terminated
2918early, for instance by a break or return statement.
2919
Robert Griesemerc2d55862009-02-19 16:49:10 -08002920<pre>
2921RangeClause = IdentifierList ( "=" | ":=" ) "range" Expression .
2922</pre>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08002923
2924The type of the right-hand expression in the range clause must be an array or
2925map, or a pointer to an array or map. If it is a pointer, it must not be nil.
2926The left-hand identifier list must contain one or two identifiers denoting the
2927iteration variables. The first variable is set to the current array index or
2928map key, and the second variable, if present, is set to the corresponding
2929array element or map value. The types of the array index (int) and element,
2930or of the map key and value respectively, must be assignment-compatible to
2931the iteration variables.
Robert Griesemerc2d55862009-02-19 16:49:10 -08002932<p>
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08002933The iteration variables may be declared by the range clause (":="), in which
2934case their scope ends at the end of the for statement block ($Declarations and
2935scope rules, Rule 3). In this case their types are the array index and element,
2936or the map key and value types, respectively.
2937
Robert Griesemerc2d55862009-02-19 16:49:10 -08002938<pre>
2939var a [10]string;
2940m := map[string]int("mon":0, "tue":1, "wed":2, "thu":3, "fri":4, "sat":5, "sun":6);
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002941
Robert Griesemerc2d55862009-02-19 16:49:10 -08002942for i, s := range a {
2943 // type of i is int
2944 // type of s is string
2945 // s == a[i]
2946 g(i, s)
2947}
2948
2949var key string;
2950var val interface {}; // value type of m is assignment-compatible to val
2951for key, value = range m {
2952 h(key, value)
2953}
2954// key == last map key encountered in iteration
2955// val == map[key]
2956</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002957
Robert Griesemer30a1a8c2008-12-16 11:38:56 -08002958If map entries that have not yet been processed are deleted during iteration,
2959they will not be processed. If map entries are inserted during iteration, the
2960behavior is implementation-dependent. Likewise, if the range expression is a
2961pointer variable, the behavior of assigning to that variable is implementation-
2962dependent. Assigning to the iteration variables during iteration simply changes
2963the values of those variables for the current iteration; it does not affect any
2964subsequent iterations.
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002965
2966
Robert Griesemerc2d55862009-02-19 16:49:10 -08002967<h3>Go statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002968
2969A go statement starts the execution of a function as an independent
Robert Griesemer57b34612008-10-10 12:45:44 -07002970concurrent thread of control within the same address space. The expression
Robert Griesemer7471eab2009-01-27 14:51:24 -08002971must be a function or method call.
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002972
Robert Griesemerc2d55862009-02-19 16:49:10 -08002973<pre>
2974GoStat = "go" Expression .
2975</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002976
Robert Griesemerb9f8b9c2008-09-26 13:38:38 -07002977Unlike with a regular function call, program execution does not wait
2978for the invoked function to complete.
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002979
Robert Griesemerc2d55862009-02-19 16:49:10 -08002980<pre>
2981go Server()
2982go func(ch chan <- bool) { for { sleep(10); ch <- true; }} (c)
2983</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002984
2985
Robert Griesemerc2d55862009-02-19 16:49:10 -08002986<h3>Select statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002987
2988A select statement chooses which of a set of possible communications
2989will proceed. It looks similar to a switch statement but with the
2990cases all referring to communication operations.
2991
Robert Griesemerc2d55862009-02-19 16:49:10 -08002992<pre>
2993SelectStat = "select" "{" { CommClause } "}" .
2994CommClause = CommCase ":" [ StatementList ] .
2995CommCase = "case" ( SendExpr | RecvExpr) | "default" .
2996SendExpr = Expression "&lt;-" Expression .
2997RecvExpr = [ Expression ( "=" | ":=" ) ] "&lt;-" Expression .
2998</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07002999
Robert Griesemer347cf672008-10-03 14:04:28 -07003000Each communication clause acts as a block for the purpose of scoping
3001(§Declarations and scope rules).
Robert Griesemerc2d55862009-02-19 16:49:10 -08003002<p>
Rob Pike569a1072008-10-03 11:18:45 -07003003For all the send and receive expressions in the select
3004statement, the channel expression is evaluated. Any values
3005that appear on the right hand side of send expressions are also
3006evaluated. If any of the resulting channels can proceed, one is
3007chosen and the corresponding communication and statements are
3008evaluated. Otherwise, if there is a default case, that executes;
Rob Pikecd368a22008-10-02 10:37:12 -07003009if not, the statement blocks until one of the communications can
Rob Pike569a1072008-10-03 11:18:45 -07003010complete. The channels and send expressions are not re-evaluated.
3011A channel pointer may be nil, which is equivalent to that case not
3012being present in the select statement.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003013<p>
Rob Pike569a1072008-10-03 11:18:45 -07003014Since all the channels and send expressions are evaluated, any side
3015effects in that evaluation will occur for all the communications
3016in the select.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003017<p>
Robert Griesemer2902a822008-09-17 13:57:11 -07003018If the channel sends or receives an interface type, its
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003019communication can proceed only if the type of the communication
3020clause matches that of the dynamic value to be exchanged.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003021<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003022If multiple cases can proceed, a uniform fair choice is made regarding
3023which single communication will execute.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003024<p>
Robert Griesemer2902a822008-09-17 13:57:11 -07003025The receive case may declare a new variable (via a ":=" assignment). The
3026scope of such variables begins immediately after the variable identifier
3027and ends at the end of the respective "select" case (that is, before the
3028next "case", "default", or closing brace).
3029
Robert Griesemerc2d55862009-02-19 16:49:10 -08003030<pre>
3031var c, c1, c2 chan int;
3032var i1, i2 int;
3033select {
3034case i1 = &lt;-c1:
3035 print("received ", i1, " from c1\n");
3036case c2 &lt;- i2:
3037 print("sent ", i2, " to c2\n");
3038default:
3039 print("no communication\n");
3040}
3041
3042for { // send random sequence of bits to c
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003043 select {
Robert Griesemerc2d55862009-02-19 16:49:10 -08003044 case c &lt;- 0: // note: no statement, no fallthrough, no folding of cases
3045 case c &lt;- 1:
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003046 }
Robert Griesemerc2d55862009-02-19 16:49:10 -08003047}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003048
Robert Griesemerc2d55862009-02-19 16:49:10 -08003049var ca chan interface {};
3050var i int;
3051var f float;
3052select {
3053case i = &lt;-ca:
3054 print("received int ", i, " from ca\n");
3055case f = &lt;-ca:
3056 print("received float ", f, " from ca\n");
3057}
3058</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003059
Robert Griesemerc2d55862009-02-19 16:49:10 -08003060<font color=red>
Robert Griesemer2902a822008-09-17 13:57:11 -07003061TODO: Make semantics more precise.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003062</font>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003063
3064
Robert Griesemerc2d55862009-02-19 16:49:10 -08003065<h3>Return statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003066
3067A return statement terminates execution of the containing function
3068and optionally provides a result value or values to the caller.
3069
Robert Griesemerc2d55862009-02-19 16:49:10 -08003070<pre>
3071ReturnStat = "return" [ ExpressionList ] .
3072</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003073
3074
3075There are two ways to return values from a function. The first is to
3076explicitly list the return value or values in the return statement:
3077
Robert Griesemerc2d55862009-02-19 16:49:10 -08003078<pre>
3079func simple_f() int {
3080 return 2;
3081}
3082</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003083
3084A function may return multiple values.
3085The syntax of the return clause in that case is the same as
3086that of a parameter list; in particular, names must be provided for
3087the elements of the return value.
3088
Robert Griesemerc2d55862009-02-19 16:49:10 -08003089<pre>
3090func complex_f1() (re float, im float) {
3091 return -7.0, -4.0;
3092}
3093</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003094
Robert Griesemer7abfcd92008-10-07 17:14:30 -07003095A second method to return values
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003096is to use those names within the function as variables
3097to be assigned explicitly; the return statement will then provide no
3098values:
3099
Robert Griesemerc2d55862009-02-19 16:49:10 -08003100<pre>
3101func complex_f2() (re float, im float) {
3102 re = 7.0;
3103 im = 4.0;
3104 return;
3105}
3106</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003107
3108
Robert Griesemerc2d55862009-02-19 16:49:10 -08003109<h3>Break statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003110
Robert Griesemer434c6052008-11-07 13:34:37 -08003111Within a for, switch, or select statement, a break statement terminates
3112execution of the innermost such statement.
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003113
Robert Griesemerc2d55862009-02-19 16:49:10 -08003114<pre>
3115BreakStat = "break" [ identifier ].
3116</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003117
Robert Griesemer434c6052008-11-07 13:34:37 -08003118If there is an identifier, it must be a label marking an enclosing
3119for, switch, or select statement, and that is the one whose execution
3120terminates.
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003121
Robert Griesemerc2d55862009-02-19 16:49:10 -08003122<pre>
3123L: for i < n {
3124 switch i {
3125 case 5: break L
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003126 }
Robert Griesemerc2d55862009-02-19 16:49:10 -08003127}
3128</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003129
3130
Robert Griesemerc2d55862009-02-19 16:49:10 -08003131<h3>Continue statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003132
3133Within a for loop a continue statement begins the next iteration of the
3134loop at the post statement.
3135
Robert Griesemerc2d55862009-02-19 16:49:10 -08003136<pre>
3137ContinueStat = "continue" [ identifier ].
3138</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003139
3140The optional identifier is analogous to that of a break statement.
3141
3142
Robert Griesemerc2d55862009-02-19 16:49:10 -08003143<h3>Label declarations</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003144
3145A label declaration serves as the target of a goto, break or continue statement.
3146
Robert Griesemerc2d55862009-02-19 16:49:10 -08003147<pre>
3148LabelDecl = identifier ":" .
3149</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003150
Robert Griesemer434c6052008-11-07 13:34:37 -08003151Example:
3152
Robert Griesemerc2d55862009-02-19 16:49:10 -08003153<pre>
3154Error:
3155</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003156
3157
Robert Griesemerc2d55862009-02-19 16:49:10 -08003158<h3>Goto statements</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003159
3160A goto statement transfers control to the corresponding label statement.
3161
Robert Griesemerc2d55862009-02-19 16:49:10 -08003162<pre>
3163GotoStat = "goto" identifier .
3164</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003165
Robert Griesemerc2d55862009-02-19 16:49:10 -08003166<pre>
3167goto Error
3168</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003169
3170Executing the goto statement must not cause any variables to come into
3171scope that were not already in scope at the point of the goto. For
3172instance, this example:
3173
Robert Griesemerc2d55862009-02-19 16:49:10 -08003174<pre>
3175goto L; // BAD
3176v := 3;
3177L:
3178</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003179
3180is erroneous because the jump to label L skips the creation of v.
3181
3182
Robert Griesemerc2d55862009-02-19 16:49:10 -08003183<h3>Fallthrough statements</h3>
Robert Griesemeraed247f2008-10-08 17:05:30 -07003184
3185A fallthrough statement transfers control to the first statement of the
3186next case clause in a switch statement (§Switch statements). It may only
3187be used in a switch statement, and only as the last statement in a case
3188clause of the switch statement.
3189
Robert Griesemerc2d55862009-02-19 16:49:10 -08003190<pre>
3191FallthroughStat = "fallthrough" .
3192</pre>
Robert Griesemeraed247f2008-10-08 17:05:30 -07003193
3194
Robert Griesemerc2d55862009-02-19 16:49:10 -08003195<h3>Defer statements</h3>
Robert Griesemer4a903e02009-01-27 09:29:40 -08003196
3197A defer statement invokes a function whose execution is deferred to the moment
3198when the surrounding function returns.
3199
Robert Griesemerc2d55862009-02-19 16:49:10 -08003200<pre>
3201DeferStat = "defer" Expression .
3202</pre>
Robert Griesemer4a903e02009-01-27 09:29:40 -08003203
Robert Griesemer7471eab2009-01-27 14:51:24 -08003204The expression must be a function or method call. Each time the defer statement
3205executes, the parameters to the function call are evaluated and saved anew but the
Robert Griesemer4a903e02009-01-27 09:29:40 -08003206function is not invoked. Immediately before the innermost function surrounding
3207the defer statement returns, but after its return value (if any) is evaluated,
3208each deferred function is executed with its saved parameters. Deferred functions
3209are executed in LIFO order.
3210
Robert Griesemerc2d55862009-02-19 16:49:10 -08003211<pre>
3212lock(l);
3213defer unlock(l); // unlocking happens before surrounding function returns
Robert Griesemer4a903e02009-01-27 09:29:40 -08003214
Robert Griesemerc2d55862009-02-19 16:49:10 -08003215// prints 3 2 1 0 before surrounding function returns
3216for i := 0; i &lt;= 3; i++ {
3217 defer fmt.Print(i);
3218}
3219</pre>
Robert Griesemer4a903e02009-01-27 09:29:40 -08003220
Robert Griesemerc2d55862009-02-19 16:49:10 -08003221<hr>
Robert Griesemer4a903e02009-01-27 09:29:40 -08003222
Robert Griesemerc2d55862009-02-19 16:49:10 -08003223<h2>Function declarations</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003224
Robert Griesemer7abfcd92008-10-07 17:14:30 -07003225A function declaration binds an identifier to a function.
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003226Functions contain declarations and statements. They may be
Robert Griesemer7abfcd92008-10-07 17:14:30 -07003227recursive. Except for forward declarations (see below), the parameter
Robert Griesemer2b9fe0e2009-01-30 14:48:29 -08003228and result types of the signature must all be complete types (§Type declarations).
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003229
Robert Griesemerc2d55862009-02-19 16:49:10 -08003230<pre>
3231FunctionDecl = "func" identifier Signature [ Block ] .
3232</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003233
Robert Griesemerc2d55862009-02-19 16:49:10 -08003234<pre>
3235func min(x int, y int) int {
3236 if x &lt; y {
3237 return x;
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003238 }
Robert Griesemerc2d55862009-02-19 16:49:10 -08003239 return y;
3240}
3241</pre>
Robert Griesemer667ef6c2008-09-10 13:00:32 -07003242
Robert Griesemerad711102008-09-11 17:48:20 -07003243A function declaration without a block serves as a forward declaration:
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003244
Robert Griesemerc2d55862009-02-19 16:49:10 -08003245<pre>
3246func MakeNode(left, right *Node) *Node
3247</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003248
3249
Robert Griesemer667ef6c2008-09-10 13:00:32 -07003250Implementation restrictions: Functions can only be declared at the global level.
3251A function must be declared or forward-declared before it can be invoked.
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003252
3253
Robert Griesemerc2d55862009-02-19 16:49:10 -08003254<h3>Method declarations</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003255
Robert Griesemer1f3e8422008-09-29 18:41:30 -07003256A method declaration is a function declaration with a receiver. The receiver
3257is the first parameter of the method, and the receiver type must be specified
3258as a type name, or as a pointer to a type name. The type specified by the
3259type name is called ``receiver base type''. The receiver base type must be a
Robert Griesemera1065fa2008-09-29 20:37:46 -07003260type declared in the current file, and it must not be a pointer type.
3261The method is said to be ``bound'' to the receiver base type; specifically
Robert Griesemer6ccca612008-12-18 13:29:11 -08003262it is declared within the scope of that type (§Type declarations). If the
3263receiver value is not needed inside the method, its identifier may be omitted
3264in the declaration.
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003265
Robert Griesemerc2d55862009-02-19 16:49:10 -08003266<pre>
3267MethodDecl = "func" Receiver identifier Signature [ Block ] .
3268Receiver = "(" [ identifier ] [ "*" ] TypeName ")" .
3269</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003270
Robert Griesemer1f3e8422008-09-29 18:41:30 -07003271All methods bound to a receiver base type must have the same receiver type:
3272Either all receiver types are pointers to the base type or they are the base
Robert Griesemerc2d55862009-02-19 16:49:10 -08003273type. <font color=red>
3274(TODO: This restriction can be relaxed at the cost of more complicated
Robert Griesemer1f3e8422008-09-29 18:41:30 -07003275assignment rules to interface types).
Robert Griesemerc2d55862009-02-19 16:49:10 -08003276</font>
Robert Griesemer1f3e8422008-09-29 18:41:30 -07003277
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003278For instance, given type Point, the declarations
3279
Robert Griesemerc2d55862009-02-19 16:49:10 -08003280<pre>
3281func (p *Point) Length() float {
3282 return Math.sqrt(p.x * p.x + p.y * p.y);
3283}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003284
Robert Griesemerc2d55862009-02-19 16:49:10 -08003285func (p *Point) Scale(factor float) {
3286 p.x = p.x * factor;
3287 p.y = p.y * factor;
3288}
3289</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003290
Robert Griesemer1f3e8422008-09-29 18:41:30 -07003291bind the methods "Length" and "Scale" to the receiver base type "Point".
3292
3293Method declarations may appear anywhere after the declaration of the receiver
3294base type and may be forward-declared.
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003295
3296
Robert Griesemerc2d55862009-02-19 16:49:10 -08003297<h3>Predeclared functions</h3>
Robert Griesemerc2d55862009-02-19 16:49:10 -08003298<ul>
3299 <li>cap
3300 <li>convert
3301 <li>len
3302 <li>make
3303 <li>new
3304 <li>panic
3305 <li>panicln
3306 <li>print
3307 <li>println
3308 <li>typeof
3309</ul>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003310
Robert Griesemerc2d55862009-02-19 16:49:10 -08003311<h3>Length and capacity</h3>
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -07003312
Robert Griesemerc2d55862009-02-19 16:49:10 -08003313<pre>
3314Call Argument type Result
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -07003315
Robert Griesemerc2d55862009-02-19 16:49:10 -08003316len(s) string, *string string length (in bytes)
3317 [n]T, *[n]T array length (== n)
3318 []T, *[]T slice length
3319 map[K]T, *map[K]T map length
3320 chan T number of elements in channel buffer
Robert Griesemer4dc25282008-09-09 10:37:19 -07003321
Robert Griesemerc2d55862009-02-19 16:49:10 -08003322cap(s) []T, *[]T capacity of s
3323 map[K]T, *map[K]T capacity of s
3324 chan T channel buffer capacity
3325</pre>
Robert Griesemer4dc25282008-09-09 10:37:19 -07003326
Robert Griesemerc2d55862009-02-19 16:49:10 -08003327<font color=red>
Robert Griesemer633957b2009-01-06 13:23:20 -08003328TODO: confirm len() and cap() for channels
Robert Griesemerc2d55862009-02-19 16:49:10 -08003329</font>
Robert Griesemer633957b2009-01-06 13:23:20 -08003330
Robert Griesemerc2d55862009-02-19 16:49:10 -08003331<p>
Robert Griesemer633957b2009-01-06 13:23:20 -08003332The type of the result is always "int" and the implementation guarantees that
3333the result always fits into an "int".
Robert Griesemerc2d55862009-02-19 16:49:10 -08003334<p>
Robert Griesemer633957b2009-01-06 13:23:20 -08003335The capacity of a slice or map is the number of elements for which there is
3336space allocated in the underlying array (for a slice) or map. For a slice "s",
3337at any time the following relationship holds:
3338
Robert Griesemerc2d55862009-02-19 16:49:10 -08003339<pre>
33400 <= len(s) <= cap(s)
3341</pre>
Robert Griesemer667ef6c2008-09-10 13:00:32 -07003342
Robert Griesemer4dc25282008-09-09 10:37:19 -07003343
Robert Griesemerc2d55862009-02-19 16:49:10 -08003344<h3>Conversions</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003345
Robert Griesemer133c68e2008-09-26 14:04:21 -07003346Conversions syntactically look like function calls of the form
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003347
Robert Griesemerc2d55862009-02-19 16:49:10 -08003348<pre>
3349T(value)
3350</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003351
Robert Griesemer133c68e2008-09-26 14:04:21 -07003352where "T" is the type name of an arithmetic type or string (§Basic types),
3353and "value" is the value of an expression which can be converted to a value
3354of result type "T".
Robert Griesemerc2d55862009-02-19 16:49:10 -08003355<p>
Robert Griesemer133c68e2008-09-26 14:04:21 -07003356The following conversion rules apply:
Robert Griesemerc2d55862009-02-19 16:49:10 -08003357<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -070033581) Between integer types. If the value is a signed quantity, it is
3359sign extended to implicit infinite precision; otherwise it is zero
3360extended. It is then truncated to fit in the result type size.
3361For example, uint32(int8(0xFF)) is 0xFFFFFFFF. The conversion always
3362yields a valid value; there is no signal for overflow.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003363<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -070033642) Between integer and floating point types, or between floating point
3365types. To avoid overdefining the properties of the conversion, for
Robert Griesemer434c6052008-11-07 13:34:37 -08003366now it is defined as a ``best effort'' conversion. The conversion
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003367always succeeds but the value may be a NaN or other problematic
Robert Griesemerc2d55862009-02-19 16:49:10 -08003368result. <font color=red>TODO: clarify?</font>
3369<p>
Robert Griesemer133c68e2008-09-26 14:04:21 -070033703) Strings permit two special conversions.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003371<p>
Robert Griesemer133c68e2008-09-26 14:04:21 -070033723a) Converting an integer value yields a string containing the UTF-8
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003373representation of the integer.
3374
Robert Griesemerc2d55862009-02-19 16:49:10 -08003375<pre>
3376string(0x65e5) // "\u65e5"
3377</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003378
Robert Griesemer133c68e2008-09-26 14:04:21 -070033793b) Converting an array of uint8s yields a string whose successive
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003380bytes are those of the array. (Recall byte is a synonym for uint8.)
3381
Robert Griesemerc2d55862009-02-19 16:49:10 -08003382<pre>
3383string([]byte('h', 'e', 'l', 'l', 'o')) // "hello"
3384</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003385
Robert Griesemer133c68e2008-09-26 14:04:21 -07003386There is no linguistic mechanism to convert between pointers
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003387and integers. A library may be provided under restricted circumstances
Robert Griesemer133c68e2008-09-26 14:04:21 -07003388to acccess this conversion in low-level code.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003389<p>
3390<font color=red>
Robert Griesemer133c68e2008-09-26 14:04:21 -07003391TODO: Do we allow interface/ptr conversions in this form or do they
3392have to be written as type guards? (§Type guards)
Robert Griesemerc2d55862009-02-19 16:49:10 -08003393</font>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003394
3395
Robert Griesemerc2d55862009-02-19 16:49:10 -08003396<h3>Allocation</h3>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003397
Robert Griesemer633957b2009-01-06 13:23:20 -08003398The built-in function "new" takes a type "T" and returns a value of type "*T".
Robert Griesemera3294712009-01-05 11:17:26 -08003399The memory is initialized as described in the section on initial values
Robert Griesemer434c6052008-11-07 13:34:37 -08003400(§Program initialization and execution).
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003401
Robert Griesemerc2d55862009-02-19 16:49:10 -08003402<pre>
3403new(T)
3404</pre>
Robert Griesemer4dc25282008-09-09 10:37:19 -07003405
3406For instance
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003407
Robert Griesemerc2d55862009-02-19 16:49:10 -08003408<pre>
3409type S struct { a int; b float }
3410new(S)
3411</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003412
Robert Griesemer4dc25282008-09-09 10:37:19 -07003413dynamically allocates memory for a variable of type S, initializes it
3414(a=0, b=0.0), and returns a value of type *S pointing to that variable.
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003415
Robert Griesemerc2d55862009-02-19 16:49:10 -08003416<p>
3417<font color=red>
Robert Griesemer633957b2009-01-06 13:23:20 -08003418TODO Once this has become clearer, connect new() and make() (new() may be
3419explained by make() and vice versa).
Robert Griesemerc2d55862009-02-19 16:49:10 -08003420</font>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003421
Robert Griesemerc2d55862009-02-19 16:49:10 -08003422<h3>Making slices, maps, and channels</h3>
Robert Griesemer633957b2009-01-06 13:23:20 -08003423
3424The built-in function "make" takes a type "T", optionally followed by a
3425type-specific list of expressions. It returns a value of type "T". "T"
3426must be a slice, map, or channel type.
3427The memory is initialized as described in the section on initial values
3428(§Program initialization and execution).
3429
Robert Griesemerc2d55862009-02-19 16:49:10 -08003430<pre>
3431make(T [, optional list of expressions])
3432</pre>
Robert Griesemer633957b2009-01-06 13:23:20 -08003433
3434For instance
3435
Robert Griesemerc2d55862009-02-19 16:49:10 -08003436<pre>
3437make(map[string] int)
3438</pre>
Robert Griesemer633957b2009-01-06 13:23:20 -08003439
3440creates a new map value and initializes it to an empty map.
3441
3442The only defined parameters affect sizes for allocating slices, maps, and
3443buffered channels:
3444
Robert Griesemerc2d55862009-02-19 16:49:10 -08003445<pre>
3446s := make([]int, 10, 100); # slice with len(s) == 10, cap(s) == 100
3447c := make(chan int, 10); # channel with a buffer size of 10
3448m := make(map[string] int, 100); # map with initial space for 100 elements
3449</pre>
Robert Griesemer633957b2009-01-06 13:23:20 -08003450
Robert Griesemerc2d55862009-02-19 16:49:10 -08003451<font color=red>
Robert Griesemer633957b2009-01-06 13:23:20 -08003452TODO Once this has become clearer, connect new() and make() (new() may be
3453explained by make() and vice versa).
Robert Griesemerc2d55862009-02-19 16:49:10 -08003454</font>
Robert Griesemerebf14c62008-10-30 14:50:23 -07003455
Robert Griesemerc2d55862009-02-19 16:49:10 -08003456<hr>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003457
Robert Griesemerc2d55862009-02-19 16:49:10 -08003458<h2>Packages</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003459
3460A package is a package clause, optionally followed by import declarations,
3461followed by a series of declarations.
3462
Robert Griesemerc2d55862009-02-19 16:49:10 -08003463<pre>
3464Package = PackageClause { ImportDecl [ ";" ] } { Declaration [ ";" ] } .
3465</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003466
Robert Griesemer347cf672008-10-03 14:04:28 -07003467The source text following the package clause acts like a block for scoping
3468purposes ($Declarations and scope rules).
Robert Griesemerc2d55862009-02-19 16:49:10 -08003469<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003470Every source file identifies the package to which it belongs.
3471The file must begin with a package clause.
3472
Robert Griesemerc2d55862009-02-19 16:49:10 -08003473<pre>
3474PackageClause = "package" PackageName .
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003475
Robert Griesemerc2d55862009-02-19 16:49:10 -08003476package Math
3477</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003478
3479
Robert Griesemer83c17602009-01-16 14:12:50 -08003480A package can gain access to exported identifiers from another package
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003481through an import declaration:
3482
Robert Griesemerc2d55862009-02-19 16:49:10 -08003483<pre>
3484ImportDecl = "import" ( ImportSpec | "(" [ ImportSpecList ] ")" ) .
3485ImportSpecList = ImportSpec { ";" ImportSpec } [ ";" ] .
3486ImportSpec = [ "." | PackageName ] PackageFileName .
3487PackageFileName = StringLit .
3488</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003489
Robert Griesemer83c17602009-01-16 14:12:50 -08003490An import statement makes the exported top-level identifiers of the named
3491package file accessible to this package.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003492<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003493In the following discussion, assume we have a package in the
Robert Griesemer83c17602009-01-16 14:12:50 -08003494file "/lib/math", called package "math", which exports the identifiers
3495"Sin" and "Cos" denoting the respective trigonometric functions.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003496<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003497In the general form, with an explicit package name, the import
3498statement declares that package name as an identifier whose
3499contents are the exported elements of the imported package.
3500For instance, after
3501
Robert Griesemerc2d55862009-02-19 16:49:10 -08003502<pre>
3503import M "/lib/math"
3504</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003505
3506the contents of the package /lib/math can be accessed by
Robert Griesemer83c17602009-01-16 14:12:50 -08003507"M.Sin", "M.Cos", etc.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003508<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003509In its simplest form, with no package name, the import statement
3510implicitly uses the imported package name itself as the local
3511package name. After
3512
Robert Griesemerc2d55862009-02-19 16:49:10 -08003513<pre>
3514import "/lib/math"
3515</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003516
Robert Griesemer83c17602009-01-16 14:12:50 -08003517the contents are accessible by "math.Sin", "math.Cos".
Robert Griesemerc2d55862009-02-19 16:49:10 -08003518<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003519Finally, if instead of a package name the import statement uses
3520an explicit period, the contents of the imported package are added
3521to the current package. After
3522
Robert Griesemerc2d55862009-02-19 16:49:10 -08003523<pre>
3524import . "/lib/math"
3525</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003526
Robert Griesemer83c17602009-01-16 14:12:50 -08003527the contents are accessible by "Sin" and "Cos". In this instance, it is
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003528an error if the import introduces name conflicts.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003529<p>
Robert Griesemer7a4ed4f2008-09-03 15:15:51 -07003530Here is a complete example Go package that implements a concurrent prime sieve:
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003531
Robert Griesemerc2d55862009-02-19 16:49:10 -08003532<pre>
3533package main
3534
3535// Send the sequence 2, 3, 4, ... to channel 'ch'.
3536func generate(ch chan <- int) {
3537 for i := 2; ; i++ {
3538 ch <- i // Send 'i' to channel 'ch'.
3539 }
3540}
3541
3542// Copy the values from channel 'in' to channel 'out',
3543// removing those divisible by 'prime'.
3544func filter(in chan <- int, out *<-chan int, prime int) {
3545 for {
3546 i := <-in; // Receive value of new variable 'i' from 'in'.
3547 if i % prime != 0 {
3548 out <- i // Send 'i' to channel 'out'.
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003549 }
3550 }
Robert Griesemerc2d55862009-02-19 16:49:10 -08003551}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003552
Robert Griesemerc2d55862009-02-19 16:49:10 -08003553// The prime sieve: Daisy-chain filter processes together.
3554func sieve() {
3555 ch := make(chan int); // Create a new channel.
3556 go generate(ch); // Start generate() as a subprocess.
3557 for {
3558 prime := <-ch;
3559 print(prime, "\n");
3560 ch1 := make(chan int);
3561 go filter(ch, ch1, prime);
3562 ch = ch1
3563 }
3564}
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003565
Robert Griesemerc2d55862009-02-19 16:49:10 -08003566func main() {
3567 sieve()
3568}
3569</pre>
Robert Griesemer67153582008-12-16 14:45:09 -08003570
Robert Griesemerc2d55862009-02-19 16:49:10 -08003571<hr>
3572
3573<h2>Program initialization and execution</h2>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003574
3575When memory is allocated to store a value, either through a declaration
Robert Griesemer52a54802008-09-30 13:02:50 -07003576or "new()", and no explicit initialization is provided, the memory is
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003577given a default initialization. Each element of such a value is
3578set to the ``zero'' for that type: "false" for booleans, "0" for integers,
Robert Griesemer52a54802008-09-30 13:02:50 -07003579"0.0" for floats, '''' for strings, and "nil" for pointers and interfaces.
3580This intialization is done recursively, so for instance each element of an
3581array of integers will be set to 0 if no other value is specified.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003582<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003583These two simple declarations are equivalent:
3584
Robert Griesemerc2d55862009-02-19 16:49:10 -08003585<pre>
3586var i int;
3587var i int = 0;
3588</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003589
3590After
3591
Robert Griesemerc2d55862009-02-19 16:49:10 -08003592<pre>
3593type T struct { i int; f float; next *T };
3594t := new(T);
3595</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003596
3597the following holds:
3598
Robert Griesemerc2d55862009-02-19 16:49:10 -08003599<pre>
3600t.i == 0
3601t.f == 0.0
3602t.next == nil
3603</pre>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003604
3605
3606A package with no imports is initialized by assigning initial values to
3607all its global variables in declaration order and then calling any init()
3608functions defined in its source. Since a package may contain more
3609than one source file, there may be more than one init() function, but
3610only one per source file.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003611<p>
Robert Griesemer566e3b22008-09-26 16:41:50 -07003612Initialization code may contain "go" statements, but the functions
Robert Griesemerd8a764c2009-02-06 17:01:10 -08003613they invoke do not begin execution until initialization of the entire
3614program is complete. Therefore, all initialization code is run in a single
3615thread of execution.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003616<p>
Robert Griesemer566e3b22008-09-26 16:41:50 -07003617Furthermore, an "init()" function cannot be referred to from anywhere
3618in a program. In particular, "init()" cannot be called explicitly, nor
3619can a pointer to "init" be assigned to a function variable).
Robert Griesemerc2d55862009-02-19 16:49:10 -08003620<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003621If a package has imports, the imported packages are initialized
Robert Griesemer566e3b22008-09-26 16:41:50 -07003622before initializing the package itself. If multiple packages import
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003623a package P, P will be initialized only once.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003624<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003625The importing of packages, by construction, guarantees that there can
3626be no cyclic dependencies in initialization.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003627<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003628A complete program, possibly created by linking multiple packages,
3629must have one package called main, with a function
Robert Griesemer4dc25282008-09-09 10:37:19 -07003630
Robert Griesemerc2d55862009-02-19 16:49:10 -08003631<pre>
3632func main() { ... }
3633</pre>
Robert Griesemer4dc25282008-09-09 10:37:19 -07003634
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003635defined. The function main.main() takes no arguments and returns no
3636value.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003637<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003638Program execution begins by initializing the main package and then
3639invoking main.main().
Robert Griesemerc2d55862009-02-19 16:49:10 -08003640<p>
Robert Griesemerdf49fb32008-08-28 17:47:53 -07003641When main.main() returns, the program exits.
Robert Griesemer52c02c22009-02-11 13:46:30 -08003642
Robert Griesemerc2d55862009-02-19 16:49:10 -08003643<hr>
Robert Griesemer52c02c22009-02-11 13:46:30 -08003644
Robert Griesemerc2d55862009-02-19 16:49:10 -08003645<h2>Systems considerations</h2>
Robert Griesemer52c02c22009-02-11 13:46:30 -08003646
Robert Griesemerc2d55862009-02-19 16:49:10 -08003647<h3>Package unsafe</h3>
Robert Griesemer52c02c22009-02-11 13:46:30 -08003648
Robert Griesemer6f8df7a2009-02-11 21:57:15 -08003649The built-in package "unsafe", known to the compiler, provides facilities
Robert Griesemer52c02c22009-02-11 13:46:30 -08003650for low-level programming including operations that violate the Go type
3651system. A package using "unsafe" must be vetted manually for type safety.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003652<p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08003653The package "unsafe" provides (at least) the following package interface:
3654
Robert Griesemerc2d55862009-02-19 16:49:10 -08003655<pre>
3656package unsafe
Robert Griesemer52c02c22009-02-11 13:46:30 -08003657
Robert Griesemerc2d55862009-02-19 16:49:10 -08003658const Maxalign int
Robert Griesemer52c02c22009-02-11 13:46:30 -08003659
Robert Griesemerc2d55862009-02-19 16:49:10 -08003660type Pointer *any
Robert Griesemer52c02c22009-02-11 13:46:30 -08003661
Robert Griesemerc2d55862009-02-19 16:49:10 -08003662func Alignof(variable any) int
3663func Offsetof(selector any) int
3664func Sizeof(variable any) int
3665</pre>
Robert Griesemer52c02c22009-02-11 13:46:30 -08003666
3667The pseudo type "any" stands for any Go type; "any" is not a type generally
3668available in Go programs.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003669<p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08003670Any pointer type as well as values of type "uintptr" can be converted into
3671an "unsafe.Pointer" and vice versa.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003672<p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08003673The function "Sizeof" takes an expression denoting a variable of any type
3674and returns the size of the variable in bytes.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003675<p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08003676The function "Offsetof" takes a selector (§Selectors) denoting a struct
3677field of any type and returns the field offset in bytes relative to the
3678struct address. Specifically, the following condition is satisfied for
3679a struct "s" with field "f":
3680
Robert Griesemerc2d55862009-02-19 16:49:10 -08003681<pre>
3682uintptr(unsafe.Pointer(&amp;s)) + uintptr(unsafe.Offsetof(s.f)) ==
3683uintptr(unsafe.Pointer(&amp;s.f))
3684</pre>
Robert Griesemer52c02c22009-02-11 13:46:30 -08003685
3686Computer architectures may impose restrictions on the memory addresses accessed
3687directly by machine instructions. A common such restriction is the requirement
3688for such addresses to be ``aligned''; that is, addresses must be a multiple
3689of a factor, the ``alignment''. The alignment depends on the type of datum
3690accessed.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003691<p>
Robert Griesemer52c02c22009-02-11 13:46:30 -08003692The function "Alignof" takes an expression denoting a variable of any type
3693and returns the alignment of the variable in bytes. The following alignment
3694condition is satisfied for a variable "x":
3695
Robert Griesemerc2d55862009-02-19 16:49:10 -08003696<pre>
3697uintptr(unsafe.Pointer(&amp;x)) % uintptr(unsafe.Alignof(x)) == 0
3698</pre>
Robert Griesemer52c02c22009-02-11 13:46:30 -08003699
3700The maximum alignment is given by the constant "unsafe.Maxalign".
3701It usually corresponds to the value of "unsafe.Sizeof(x)" for
3702a variable "x" of the largest arithmetic type (8 for a float64), but may
3703be smaller on systems that have less stringent alignment restrictions
3704or are space constrained.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003705<p>
Robert Griesemer6f8df7a2009-02-11 21:57:15 -08003706The results of calls to "unsafe.Alignof", "unsafe.Offsetof", and
3707"unsafe.Sizeof" are compile-time constants.
3708
Robert Griesemer52c02c22009-02-11 13:46:30 -08003709
Robert Griesemerc2d55862009-02-19 16:49:10 -08003710<h3>Size and alignment guarantees</h3>
Robert Griesemer52c02c22009-02-11 13:46:30 -08003711
3712For the arithmetic types (§Arithmetic types), a Go compiler guarantees the
3713following sizes:
3714
Robert Griesemerc2d55862009-02-19 16:49:10 -08003715<pre>
3716type size in bytes
Robert Griesemer52c02c22009-02-11 13:46:30 -08003717
Robert Griesemerc2d55862009-02-19 16:49:10 -08003718byte, uint8, int8 1
3719uint16, int16 2
3720uint32, int32, float32 4
3721uint64, int64, float64 8
3722</pre>
Robert Griesemer52c02c22009-02-11 13:46:30 -08003723
Robert Griesemerc2d55862009-02-19 16:49:10 -08003724<p>
Rob Pike4501d342009-02-19 17:31:36 -08003725A Go compiler guarantees the following minimal alignment properties:
3726</p>
Robert Griesemerc2d55862009-02-19 16:49:10 -08003727<ol>
3728<li>For a variable "x" of any type: "1 <= unsafe.Alignof(x) <= unsafe.Maxalign".
Robert Griesemer52c02c22009-02-11 13:46:30 -08003729
Robert Griesemerc2d55862009-02-19 16:49:10 -08003730<li>For a variable "x" of arithmetic type: "unsafe.Alignof(x)" is the smaller
Robert Griesemer52c02c22009-02-11 13:46:30 -08003731 of "unsafe.Sizeof(x)" and "unsafe.Maxalign", but at least 1.
3732
Robert Griesemerc2d55862009-02-19 16:49:10 -08003733<li>For a variable "x" of struct type: "unsafe.Alignof(x)" is the largest of
Robert Griesemer52c02c22009-02-11 13:46:30 -08003734 all the values "unsafe.Alignof(x.f)" for each field "f" of x, but at least 1.
3735
Robert Griesemerc2d55862009-02-19 16:49:10 -08003736<li>For a variable "x" of array type: "unsafe.Alignof(x)" is the same as
Robert Griesemer52c02c22009-02-11 13:46:30 -08003737 unsafe.Alignof(x[0]), but at least 1.
Robert Griesemerc2d55862009-02-19 16:49:10 -08003738</ol>
Robert Griesemer52c02c22009-02-11 13:46:30 -08003739
Robert Griesemerc2d55862009-02-19 16:49:10 -08003740<hr>
Robert Griesemer52c02c22009-02-11 13:46:30 -08003741
Robert Griesemerc2d55862009-02-19 16:49:10 -08003742</div>
3743</body>
3744</html>