difference between declaring variables with var vs without var in go

huangapple go评论86阅读模式
英文:

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 &quot;code.google.com/p/go-tour/pic&quot;

func Pic(dx, dy int) [][]uint8 {
    var pic = make([][]uint8, dy)
    for y := 0; y &lt; dy; y++ {
        pic[y] = make([]uint8, dx)
        for x := 0; y &lt; 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 &lt; dy; y++ {
        pixels[y] = make([]uint8, dx)
        for x := 0; x &lt; 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 &lt; dx; x++ {
	pic[y][x] = uint8(x * y)
}

特别是:y &lt; dx,导致了运行时错误:索引超出范围。

我写的代码是:

for x := 0; x &lt; dx; x++ {
	pixels[y][x] = uint8(x * y)
}

特别是:x &lt; 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 &lt; dy; y++ {
		pic[y] = make([]uint8, dx)
		for x :=0; x&lt;dx; x++ {
			pic[y][x] = uint8(x*y)
		} 
	}
	return pic
}

func main() {
	pic.Show(Pic)
}

http://play.golang.org/p/UvGgszFhl-

difference between declaring variables with var vs without var in go

变量声明

变量声明创建一个变量,将标识符绑定到它,并给它一个类型和可选的初始值。

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 &lt; dx; x++ {
	pic[y][x] = uint8(x * y)
}

in particular: y &lt; dx, which causes,

panic: runtime error: index out of range

I wrote

    for x := 0; x &lt; dx; x++ {
        pixels[y][x] = uint8(x * y)
    }

in particular: x &lt; dx. Therefore, change your y to x.

package main

import &quot;code.google.com/p/go-tour/pic&quot;

func Pic(dx, dy int) [][]uint8 {
    var pic = make([][]uint8, dy)
    for y :=0; y &lt; dy; y++ {
        pic[y] = make([]uint8, dx)
        for x :=0; x&lt;dx; x++ {
            pic[y][x] = uint8(x*y)
        } 
    }
    return pic
}

func main() {
    pic.Show(Pic)
}

http://play.golang.org/p/UvGgszFhl-

difference between declaring variables with var vs without var in go

> 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 &quot;:=&quot; 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)

huangapple
  • 本文由 发表于 2013年6月5日 11:36:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/16931126.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定