英文:
no new variables on left side of :=
问题
这里发生了什么?
package main
import "fmt"
func main() {
myArray := [...]int{12,14,26};
fmt.Println(myArray)
myArray := [...]int{11,12,14} //在这一行上出现错误
fmt.Println(myArray);
}
它抛出一个错误,错误信息是
:= 左边没有新的变量
我所做的是给已经声明的变量重新赋值。
英文:
What's happening here?
package main
import "fmt"
func main() {
myArray :=[...]int{12,14,26} ;
fmt.Println(myArray)
myArray :=[...]int{11,12,14} //error pointing on this line
fmt.Println(myArray) ;
}
It throws an error that says
no new variables on left side of :=
What I was doing was re-assigning values to an already declared variable.
答案1
得分: 170
从第二个语句中删除冒号 :
,因为你正在给现有变量赋新值。
myArray = [...]int{11,12,14}
冒号 :
在你第一条语句中执行短声明和赋值时使用,即 myArray :=[...]int{12,14,26}
。
英文:
Remove the colon :
from the second statement as you are assigning a new value to existing variable.
myArray = [...]int{11,12,14}
colon :
is used when you perform the short declaration and assignment for the first time as you are doing in your first statement i.e. myArray :=[...]int{12,14,26}
.
答案2
得分: 31
在Go语言中有两种赋值操作符:=
和=
。它们在语义上是等价的(就赋值而言),但第一个操作符也是一种“短变量声明”(http://golang.org/ref/spec#Short_variable_declarations),这意味着在左边至少需要有一个新的变量声明才能正确使用。
你可以将第二个操作符改为简单的赋值语句:=
-> =
,或者如果你的算法允许的话,可以使用一个新的变量。
英文:
There are two types of assignment operators in go :=
and =
. They are semantically equivalent (with respect to assignment) but the first one is also a "short variable declaration" ( http://golang.org/ref/spec#Short_variable_declarations ) which means that in the left we need to have at least a new variable declaration for it to be correct.
You can change the second to a simple assignment statement :=
-> =
or you can use a new variable if that's ok with your algorithm.
答案3
得分: 11
作为一个附注,重新声明只能出现在多变量的短声明中
引用自语言规范:
> 与常规变量声明不同,短变量声明可以重新声明变量,前提是它们在同一块中以相同的类型进行了原始声明,并且至少有一个非空白变量是新的。因此,重新声明只能出现在多变量的短声明中。重新声明不会引入新变量;它只是给原始变量赋予新值。
package main
import "fmt"
func main() {
a, b := 1, 2
c, b := 3, 4
fmt.Println(a, b, c)
}
这是关于在golang中重新声明变量的一个非常好的例子:
https://stackoverflow.com/a/27919847/4418897
英文:
As a side note, redeclaration can only appear in a multi-variable short declaration
Quoting from the Language specification:
> Unlike regular variable declarations, a short variable declaration may
> redeclare variables provided they were originally declared earlier in
> the same block with the same type, and at least one of the non-blank
> variables is new. As a consequence, redeclaration can only appear in a
> multi-variable short declaration. Redeclaration does not introduce a
> new variable; it just assigns a new value to the original.
package main
import "fmt"
func main() {
a, b := 1, 2
c, b := 3, 4
fmt.Println(a, b, c)
}
Here is a very good example about redeclaration of variables in golang:
https://stackoverflow.com/a/27919847/4418897
答案4
得分: 8
myArray :=[...]int{12,14,26}
根据之前的评论者所说,:=
是一种简写形式,也是变量声明的简写形式。
所以在上面的语句中,你正在做两件事。
- 你正在声明你的变量为myArray。
- 你正在将一个整数数组赋值给myArray变量。
你代码的第二部分失败了,因为你在这里做的是:
myArray :=[...]int{11,12,14} //错误指向这一行
重新声明了已存在的变量myArray,该变量已经包含整数值。
这个是可以工作的:
myArray = [...]int{11,12,14} //这一行不会产生错误
因为它是将整数数组赋值给已存在(预先声明/初始化)的变量。
英文:
myArray :=[...]int{12,14,26}
As stated by the previous commenters, :=
is a type of short-hand and/or the short-form of variable declaration.
So in the statment above you are doing two things.
- You are declaring your variable to be myArray.
- You are assigning an array of integers to the myArray variable.
The second part of your code fails, because what you are doing here:
myArray :=[...]int{11,12,14} //error pointing on this line
Is RE-declaring the existing variable myArray, which already contains integer values.
This works:
myArray = [...]int{11,12,14} // NO error will be produced by this line
Because, it is assigning the integer array to the existing ( pre-declared / initialized ) variable.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论