英文:
difference between declaring variables with var vs without var in go
问题
我在Go之旅的第35步练习中遇到了一些问题。
这是我的代码:
package main
import "code.google.com/p/go-tour/pic"
func Pic(dx, dy int) [][]uint8 {
var pic = make([][]uint8, dy)
for y := 0; y < dy; y++ {
pic[y] = make([]uint8, dx)
for x := 0; y < dx; x++ {
pic[y][x] = uint8(x*y)
}
}
return pic
}
在寻找解决方案时,我找到了PeterSO的代码,它完美地工作了
func main() {
pic.Show(Pic)
}
func Pic(dx, dy int) [][]uint8 {
pixels := make([][]uint8, dy)
for y := 0; y < dy; y++ {
pixels[y] = make([]uint8, dx)
for x := 0; x < dx; x++ {
pixels[y][x] = uint8(x * y)
}
}
return pixels
}
我唯一能看到的区别是,我使用var
关键字定义了pic
变量,而他的代码使用了:=
赋值。现在,为什么我的代码不起作用?
英文:
I was having some trouble with step 35 in the tour of Go exercise.
Here's what my code looks like:
package main
import "code.google.com/p/go-tour/pic"
func Pic(dx, dy int) [][]uint8 {
var pic = make([][]uint8, dy)
for y := 0; y < dy; y++ {
pic[y] = make([]uint8, dx)
for x := 0; y < dx; x++ {
pic[y][x] = uint8(x*y)
}
}
return pic
}
When looking for a solution, I found PeterSO's code which works perfectly
func main() {
pic.Show(Pic)
}
func Pic(dx, dy int) [][]uint8 {
pixels := make([][]uint8, dy)
for y := 0; y < dy; y++ {
pixels[y] = make([]uint8, dx)
for x := 0; x < dx; x++ {
pixels[y][x] = uint8(x * y)
}
}
return pixels
}
The only difference I can see, is that I'm defining the pic
variable using the var
keyword whereas his code is using the :=
assignment. Now, why does my code not work?
答案1
得分: 5
你写的代码是:
for x := 0; y < dx; x++ {
pic[y][x] = uint8(x * y)
}
特别是:y < dx
,导致了运行时错误:索引超出范围。
我写的代码是:
for x := 0; x < dx; x++ {
pixels[y][x] = uint8(x * y)
}
特别是:x < dx
。因此,请将你的y
改为x
。
package main
import "code.google.com/p/go-tour/pic"
func Pic(dx, dy int) [][]uint8 {
var pic = make([][]uint8, dy)
for y :=0; y < dy; y++ {
pic[y] = make([]uint8, dx)
for x :=0; x<dx; x++ {
pic[y][x] = uint8(x*y)
}
}
return pic
}
func main() {
pic.Show(Pic)
}
http://play.golang.org/p/UvGgszFhl-
变量声明创建一个变量,将标识符绑定到它,并给它一个类型和可选的初始值。
VarDecl = "var" ( VarSpec | "(" { VarSpec ";" } ")" ) . VarSpec = IdentifierList ( Type [ "=" ExpressionList ] | "=" ExpressionList ) .
短变量声明使用以下语法:
ShortVarDecl = IdentifierList ":=" ExpressionList
.它是一个带有初始化表达式但没有类型的常规变量声明的简写形式:
"var" IdentifierList = ExpressionList .
与常规变量声明不同,短变量声明可以重新声明变量,前提是它们最初在同一块中以相同的类型进行了声明,并且至少有一个非空白变量是新的。
在你的代码中,var pic = make([][]uint8, dy)
和短形式 pic := make([][]uint8, dy)
都可以工作。
英文:
You wrote
for x := 0; y < dx; x++ {
pic[y][x] = uint8(x * y)
}
in particular: y < dx
, which causes,
panic: runtime error: index out of range
I wrote
for x := 0; x < dx; x++ {
pixels[y][x] = uint8(x * y)
}
in particular: x < dx
. Therefore, change your y
to x
.
package main
import "code.google.com/p/go-tour/pic"
func Pic(dx, dy int) [][]uint8 {
var pic = make([][]uint8, dy)
for y :=0; y < dy; y++ {
pic[y] = make([]uint8, dx)
for x :=0; x<dx; x++ {
pic[y][x] = uint8(x*y)
}
}
return pic
}
func main() {
pic.Show(Pic)
}
http://play.golang.org/p/UvGgszFhl-
> Variable declarations
>
> A variable declaration creates a variable, binds an identifier to it
> and gives it a type and optionally an initial value.
>
> VarDecl = "var" ( VarSpec | "(" { VarSpec ";" } ")" ) .
> VarSpec = IdentifierList ( Type [ "=" ExpressionList ] | "=" ExpressionList ) .
>
> Short variable declarations
>
> A short variable declaration uses the syntax:
>
> ShortVarDecl = IdentifierList ":=" ExpressionList
.
>
> It is a shorthand for a regular variable declaration with initializer
> expressions but no types:
>
> "var" IdentifierList = ExpressionList .
>
> 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.
In your code var pic = make([][]uint8, dy)
and the short form pic := make([][]uint8, dy)
will both work.
答案2
得分: 2
如果你使用:=
,变量的类型将从等号右边的表达式中推断出来。如果你使用=
,则不会做任何假设,你需要自己指定类型。
在这种情况下,你应该这样写:
var pic [][]uint8 = make([][]uint8, dy)
但这样写更好,因为更简短和清晰:
pic := make([][]uint8, dy)
英文:
If you use the :=
, the type of the variable is implied from the expression on the right of the sign. If you use =
, no assumption is made and you need to specify the type yourself.
In this case, you should write it like this:
var pic [][]uint8 = make([][]uint8, dy)
but this is indeed better because shorter and as clear:
pic := make([][]uint8, dy)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论