From ef45e64afc7b6b49083dd3aa82d8363044e6aaaf Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Fri, 21 Aug 2009 11:25:00 -0700 Subject: [PATCH] - unifying rules for var decls, short var decls, and assignments DELTA=39 (4 added, 15 deleted, 20 changed) OCL=33639 CL=33649 --- doc/go_spec.html | 53 +++++++++++++++++++----------------------------- 1 file changed, 21 insertions(+), 32 deletions(-) diff --git a/doc/go_spec.html b/doc/go_spec.html index 2190dca3fc8..861546c9475 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -1547,41 +1547,41 @@ var (

-If there are expressions, their number must be equal -to the number of identifiers, and the nth variable -is initialized to the value of the nth expression. -Otherwise, each variable is initialized to the zero -of the type (§The zero value). -The expressions can be general expressions; they need not be constants. +If a list of expressions is given, the variables are initialized +by assigning those expressions to the variables (§Assignments). +Otherwise, each variable is initialized to its zero value +(§The zero value).

+

-Either the type or the expression list must be present. If the -type is present, it sets the type of each variable and the expressions -(if any) must be assignment-compatible to that type. If the type -is absent, the variables take the types of the corresponding -expressions. +If the type is present, each variable is given that type. +Otherwise, the types are deduced from the assignment +of the expression list.

+

If the type is absent and the corresponding expression is a constant -expression of ideal integer or ideal float type, the type of the -declared variable is int or float -respectively: +expression of ideal integer, float, or string type, the type of the +declared variable is int, float, +or string respectively:

 var i = 0       // i has type int
 var f = 3.1415  // f has type float
+var s = "OMDB"  // s has type string
 

Short variable declarations

-A short variable declaration uses the syntax +A short variable declaration uses the syntax:
 ShortVarDecl = IdentifierList ":=" ExpressionList .
 
-and is shorthand for the declaration syntax +It is a shorthand for a regular variable declaration with +initializer expressions but no types:
 "var" IdentifierList = ExpressionList .
@@ -1591,24 +1591,11 @@ and is shorthand for the declaration syntax
 i, j := 0, 10;
 f := func() int { return 7; }
 ch := make(chan int);
-
- -

-Unlike regular variable declarations, short variable declarations -can be used, by analogy with tuple assignment (§Assignments), to -receive the individual elements of a multi-valued expression such -as a call to a multi-valued function. In this form, the ExpressionList -must be a single such multi-valued expression, the number of -identifiers must equal the number of values, and the declared -variables will be assigned the corresponding values. -

- -
 r, w := os.Pipe(fd);  // os.Pipe() returns two values
 

-A short variable declaration may redeclare variables provided they +Unlike regular variable declarations, a short variable declaration may redeclare variables provided they were originally declared in the same block with the same type, and at least one of the variables is new. As a consequence, redeclaration can only appear in a multi-variable short declaration. @@ -3133,7 +3120,9 @@ assigns the first value to x and the second to y.

In the second form, the number of operands on the left must equal the number -of expressions on the right, each of which must be single-valued. +of expressions on the right, each of which must be single-valued, and the +nth expression on the right is assigned to the nth +operand on the left. The expressions on the right are evaluated before assigning to any of the operands on the left, but otherwise the evaluation order is unspecified. @@ -4141,7 +4130,7 @@ func main() { When memory is allocated to store a value, either through a declaration or new(), and no explicit initialization is provided, the memory is given a default initialization. Each element of such a value is -set to the zero value for its type: false for booleans, +set to the zero value for its type: false for booleans, 0 for integers, 0.0 for floats, "" for strings, and nil for pointers and interfaces. This initialization is done recursively, so for instance each element of an