From 61af4f905bd0de1eb907da336bb48f9f48d7d2b3 Mon Sep 17 00:00:00 2001
From: Andrew Gerrand
-Any value may be assigned to the blank identifier.
-T
.
-Blocks
@@ -1516,6 +1512,11 @@ No identifier may be declared twice in the same block, and
no identifier may be declared in both the file and package block.
+The blank identifier may be used like any other identifier +in a declaration, but it does not introduce a binding and thus is not declared. +
+Declaration = ConstDecl | TypeDecl | VarDecl . TopLevelDecl = Declaration | FunctionDecl | MethodDecl . @@ -1585,8 +1586,10 @@ the body of any nested function.Blank identifier
-The blank identifier, represented by the underscore character
@@ -2077,8 +2080,8 @@ operators and functions to operands._
, may be used in a declaration like -any other identifier but the declaration does not introduce a new binding. +The blank identifier is represented by the underscore character_
. +It serves as an anonymous placeholder instead of a regular (non-blank) +identifier and has special meaning in declarations, +as an operand, and in assignments.Operands denote the elementary values in an expression. An operand may be a -literal, a (possibly qualified) identifier -denoting a +literal, a (possibly qualified) +non-blank identifier denoting a constant, variable, or function, @@ -2086,6 +2089,11 @@ a method expression yielding a function, or a parenthesized expression.
++The blank identifier may appear as an +operand only on the left-hand side of an assignment. +
+Operand = Literal | OperandName | MethodExpr | "(" Expression ")" . Literal = BasicLit | CompositeLit | FunctionLit . @@ -4255,7 +4263,8 @@ assign_op = [ add_op | mul_op ] "=" .Each left-hand side operand must be addressable, -a map index expression, or the blank identifier. +a map index expression, or (for
@@ -4268,12 +4277,13 @@ a[i] = 23=
assignments only) the +blank identifier. Operands may be parenthesized.An assignment operation
x
op=
-y
where op is a binary arithmetic operation is equivalent +y
where op is a binary arithmetic operation equivalent tox
=
x
opy
but evaluatesx
only once. The op=
construct is a single token. In assignment operations, both the left- and right-hand expression lists -must contain exactly one single-valued expression. +must contain exactly one single-valued expression, and the left-hand +expression must not be the blank identifier.@@ -4298,21 +4308,26 @@ x, y = f()assigns the first value to
- -x
and the second toy
. -The blank identifier provides a -way to ignore values returned by a multi-valued expression: --x, _ = f() // ignore second value returned by f() -- -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, and the nth expression on the right is assigned to the nth -operand on the left. +operand on the left:
++one, two, three = '一', '二', '三' ++ ++The blank identifier provides a way to +ignore right-hand side values in an assignment: +
+ ++_ = x // evaluate x but ignore it +x, _ = f() // evaluate f() but ignore second result value ++The assignment proceeds in two phases. First, the operands of index expressions @@ -4350,16 +4365,29 @@ for i, x[i] = range x { // set i, x[2] = 0, x[0]
-In assignments, each value must be -assignable to the type of the -operand to which it is assigned. If an untyped constant -is assigned to a variable of interface type, the constant is converted -to type
+bool
,rune
,int
,float64
, -complex128
orstring
-respectively, depending on whether the value is a -boolean, rune, integer, floating-point, complex, or string constant. +In assignments, each value must be assignable +to the type of the operand to which it is assigned, with the following special cases:+
- + +
+ If an untyped constant + is assigned to a variable of interface type or the blank identifier, + the constant is first converted to type +
bool
,rune
,int
,float64
, +complex128
orstring
respectively, depending on + whether the value is a boolean, rune, integer, floating-point, complex, or + string constant. +- +
+ + If a left-hand side is the blank identifier, any typed or non-constant + value except for the predeclared identifier +
nil
+ may be assigned to it. +If statements