Tokens form the vocabulary of the Go language.
There are four classes: identifiers, keywords, operators
and delimiters, and literals. <i>White space</i>, formed from
-blanks, tabs, and newlines, is ignored except as it separates tokens
+spaces (U+0020), horizontal tabs (U+0009),
+carriage returns (U+000D), and newlines (U+000A),
+is ignored except as it separates tokens
that would otherwise combine into a single token. Comments
behave as white space. While breaking the input into tokens,
the next token is the longest sequence of characters that form a
\" U+0022 double quote (valid escape only within string literals)
</pre>
<p>
-All other sequences are illegal inside character literals.
+All other sequences starting with a backslash are illegal inside character literals.
</p>
<pre class="ebnf">
char_lit = "'" ( unicode_value | byte_value ) "'" .
</p>
<p>
Interpreted string literals are character sequences between double
-quotes <code>""</code>. The text between the quotes forms the
+quotes <code>""</code>. The text between the quotes,
+which may not span multiple lines, forms the
value of the literal, with backslash escapes interpreted as they
are in character literals (except that <code>\'</code> is illegal and
<code>\"</code> is legal). The three-digit octal (<code>\000</code>)
It is an error if the constant value
cannot be accurately represented as a value of the respective type.
For instance, <code>3.0</code> can be given any integer type but also any
-floating-point type, while <code>-1e12</code> can be given the types
-<code>float32</code>, <code>float64</code>, or even <code>int64</code> but
-not <code>uint64</code> or <code>string</code>.
+floating-point type, while <code>2147483648.0</code> (equal to <code>1<<31</code>)
+can be given the types <code>float32</code>, <code>float64</code>, or <code>uint32</code> but
+not <code>int32</code> or <code>string</code>.
</p>
<p>
stands for one item (parameter or result) of the specified type; if absent, each
type stands for one item of that type. Parameter and result
lists are always parenthesized except that if there is exactly
-one unnamed result that is not a function type it may writen as an unparenthesized type.
+one unnamed result that is not a function type it may written as an unparenthesized type.
</p>
<p>
For the last parameter only, instead of a type one may write
<p>
A value <code>v</code> of static type <code>V</code> is <i>assignment compatible</i>
-with a type <code>T</code> if one of the following conditions applies:
+with a type <code>T</code> if one or more of the following conditions applies:
</p>
<ul>
been assigned another slice value equal to <code>nil</code>·
</li>
<li>
-Similarly, an interface value is equal to <code>nil</code> if it has
+An interface value is equal to <code>nil</code> if it has
been assigned the explicit value <code>nil</code>, if it is uninitialized,
or if it has been assigned another interface value equal to <code>nil</code>.
</li>
// NewMutex has the same composition as Mutex but its method set is empty.
type NewMutex Mutex
-// PrintableMutex has no methods bound to it, but the method set contains
-// the methods Lock and Unlock bound to its anonymous field Mutex.
+// PrintableMutex's method set contains the methods
+// Lock and Unlock bound to its anonymous field Mutex.
type PrintableMutex struct {
Mutex;
}
If a list of expressions is given, the variables are initialized
by assigning the expressions to the variables (§<a href="#Assignments">Assignments</a>)
in order; all expressions must be consumed and all variables initialized from them.
-Otherwise, each variable is initialized to its <a href="#The_zero_value"><i>zero value</i></a>.
+Otherwise, each variable is initialized to its <a href="#The_zero_value">zero value</a>.
</p>
<p>
which is a function with a <i>receiver</i>.
</p>
<pre class="ebnf">
-MethodDecl = "func" Receiver MethodName Signature [ Body ] .
-Receiver = "(" [ identifier ] [ "*" ] BaseTypeName ")" .
+MethodDecl = "func" Receiver MethodName Signature [ Body ] .
+Receiver = "(" [ identifier ] [ "*" ] BaseTypeName ")" .
BaseTypeName = identifier .
</pre>
<p>
calls <code>f</code> with arguments <code>a1, a2, ... an</code>.
-The arguments must be single-valued expressions
+Except for one special case, arguments must be single-valued expressions
<a href="#Assignment_compatibility">assignment compatible</a> with the parameter types of
<code>F</code> and are evaluated before the function is called.
The type of the expression is the result type
pt.Scale(3.5) // method call with receiver pt
</pre>
+<p>
+As a special case, if the return parameters of a function or method
+<code>g</code> are equal in number and individually assignment
+compatible with the parameters of another function or method
+<code>f</code>, then the call <code>f(g(<i>parameters_of_g</i>))</code>
+will invoke <code>f</code> after binding the return values of
+<code>g</code> to the parameters of <code>f</code> in order. The call
+of <code>f</code> must contain no parameters other than the call of <code>g</code>.
+If <code>f</code> has a final <code>...</code> parameter, it is
+assigned the return values of <code>g</code> that remain after
+assignment of regular parameters.
+</p>
+
+<pre>
+func Split(s string, pos int) (string, string) {
+ return s[0:pos], s[pos:len(s)]
+}
+
+func Join(s, t string) string {
+ return s + t
+}
+
+if Join(Split(value, len(value)/2)) != value {
+ log.Fatal("test fails")
+}
+</pre>
+
<p>
A method call <code>x.m()</code> is valid if the method set of
(the type of) <code>x</code> contains <code>m</code> and the
order.
</p>
-<p>
-Floating-point operations within a single expression are evaluated according to
-the associativity of the operators. Explicit parentheses affect the evaluation
-by overriding the default associativity.
-In the expression <code>x + (y + z)</code> the addition <code>y + z</code>
-is performed before adding <code>x</code>.
-</p>
-
<p>
For example, in the assignment
</p>
of <code>y</code> is not specified.
</p>
+<p>
+Floating-point operations within a single expression are evaluated according to
+the associativity of the operators. Explicit parentheses affect the evaluation
+by overriding the default associativity.
+In the expression <code>x + (y + z)</code> the addition <code>y + z</code>
+is performed before adding <code>x</code>.
+</p>
+
<h2 id="Statements">Statements</h2>
<p>
<p>
Each left-hand side operand must be <a href="#Address_operators">addressable</a>,
-a map index expresssion,
+a map index expression,
or the <a href="#Blank_identifier">blank identifier</a>.
</p>
An <i>assignment operation</i> <code>x</code> <i>op</i><code>=</code>
<code>y</code> where <i>op</i> is a binary arithmetic operation is equivalent
to <code>x</code> <code>=</code> <code>x</code> <i>op</i>
-<code>y</code> but evalutates <code>x</code>
+<code>y</code> but evaluates <code>x</code>
only once. The <i>op</i><code>=</code> construct is a single token.
In assignment operations, both the left- and right-hand expression lists
must contain exactly one single-valued expression.
}
var key string;
-var val interface {}; // value type of m is assignment compatible to val
+var val interface {}; // value type of m is assignment compatible with val
for key, val = range m {
h(key, val)
}
func init()
</pre>
<p>
-defined in its source. Since a package may contain more
-than one source file, there may be more than one
-<code>init()</code> function in a package, but
-only one per source file.
+defined in its source.
+A package may contain multiple
+<code>init()</code> functions, even
+within a single source file; they execute
+in unspecified order.
</p>
<p>
Within a package, package-level variables are initialized,
invoking <code>main.main()</code>.
</p>
<p>
-When <code>main.main()</code> returns, the program exits.
+When <code>main.main()</code> returns, the program exits. It does not wait for
+other (non-<code>main</code>) goroutines to complete.
</p>
<p>
Implementation restriction: The compiler assumes package <code>main</code>
<ul>
<li><span class="alert">Implementation does not honor the restriction on goto statements and targets (no intervening declarations).</span></li>
<li><span class="alert">Method expressions are not implemented.</span></li>
+ <li><span class="alert">Gccgo allows only one init() function per source file.</span></li>
</ul>