difference between declaring variables with var vs without var in go

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

difference between declaring variables with var vs without var in go

问题

我在Go之旅的第35步练习中遇到了一些问题。

这是我的代码:

  1. package main
  2. import "code.google.com/p/go-tour/pic"
  3. func Pic(dx, dy int) [][]uint8 {
  4. var pic = make([][]uint8, dy)
  5. for y := 0; y < dy; y++ {
  6. pic[y] = make([]uint8, dx)
  7. for x := 0; y < dx; x++ {
  8. pic[y][x] = uint8(x*y)
  9. }
  10. }
  11. return pic
  12. }

在寻找解决方案时,我找到了PeterSO的代码,它完美地工作了

  1. func main() {
  2. pic.Show(Pic)
  3. }
  4. func Pic(dx, dy int) [][]uint8 {
  5. pixels := make([][]uint8, dy)
  6. for y := 0; y < dy; y++ {
  7. pixels[y] = make([]uint8, dx)
  8. for x := 0; x < dx; x++ {
  9. pixels[y][x] = uint8(x * y)
  10. }
  11. }
  12. return pixels
  13. }

我唯一能看到的区别是,我使用var关键字定义了pic变量,而他的代码使用了:=赋值。现在,为什么我的代码不起作用?

英文:

I was having some trouble with step 35 in the tour of Go exercise.

Here's what my code looks like:

  1. package main
  2. import &quot;code.google.com/p/go-tour/pic&quot;
  3. func Pic(dx, dy int) [][]uint8 {
  4. var pic = make([][]uint8, dy)
  5. for y := 0; y &lt; dy; y++ {
  6. pic[y] = make([]uint8, dx)
  7. for x := 0; y &lt; dx; x++ {
  8. pic[y][x] = uint8(x*y)
  9. }
  10. }
  11. return pic
  12. }

When looking for a solution, I found PeterSO's code which works perfectly

  1. func main() {
  2. pic.Show(Pic)
  3. }
  4. func Pic(dx, dy int) [][]uint8 {
  5. pixels := make([][]uint8, dy)
  6. for y := 0; y &lt; dy; y++ {
  7. pixels[y] = make([]uint8, dx)
  8. for x := 0; x &lt; dx; x++ {
  9. pixels[y][x] = uint8(x * y)
  10. }
  11. }
  12. return pixels
  13. }

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

你写的代码是:

  1. for x := 0; y &lt; dx; x++ {
  2. pic[y][x] = uint8(x * y)
  3. }

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

我写的代码是:

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

特别是:x &lt; dx。因此,请将你的y改为x

  1. package main
  2. import "code.google.com/p/go-tour/pic"
  3. func Pic(dx, dy int) [][]uint8 {
  4. var pic = make([][]uint8, dy)
  5. for y :=0; y &lt; dy; y++ {
  6. pic[y] = make([]uint8, dx)
  7. for x :=0; x&lt;dx; x++ {
  8. pic[y][x] = uint8(x*y)
  9. }
  10. }
  11. return pic
  12. }
  13. func main() {
  14. pic.Show(Pic)
  15. }

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

difference between declaring variables with var vs without var in go

变量声明

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

  1. VarDecl = "var" ( VarSpec | "(" { VarSpec ";" } ")" ) .
  2. VarSpec = IdentifierList ( Type [ "=" ExpressionList ] | "=" ExpressionList ) .

短变量声明

短变量声明使用以下语法:

ShortVarDecl = IdentifierList ":=" ExpressionList .

它是一个带有初始化表达式但没有类型的常规变量声明的简写形式:

  1. "var" IdentifierList = ExpressionList .

与常规变量声明不同,短变量声明可以重新声明变量,前提是它们最初在同一块中以相同的类型进行了声明,并且至少有一个非空白变量是新的。

在你的代码中,var pic = make([][]uint8, dy) 和短形式 pic := make([][]uint8, dy) 都可以工作。

英文:

You wrote

  1. for x := 0; y &lt; dx; x++ {
  2. pic[y][x] = uint8(x * y)
  3. }

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

  1. panic: runtime error: index out of range

I wrote

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

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

  1. package main
  2. import &quot;code.google.com/p/go-tour/pic&quot;
  3. func Pic(dx, dy int) [][]uint8 {
  4. var pic = make([][]uint8, dy)
  5. for y :=0; y &lt; dy; y++ {
  6. pic[y] = make([]uint8, dx)
  7. for x :=0; x&lt;dx; x++ {
  8. pic[y][x] = uint8(x*y)
  9. }
  10. }
  11. return pic
  12. }
  13. func main() {
  14. pic.Show(Pic)
  15. }

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

如果你使用:=,变量的类型将从等号右边的表达式中推断出来。如果你使用=,则不会做任何假设,你需要自己指定类型。

在这种情况下,你应该这样写:

  1. var pic [][]uint8 = make([][]uint8, dy)

但这样写更好,因为更简短和清晰:

  1. 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:

  1. var pic [][]uint8 = make([][]uint8, dy)

but this is indeed better because shorter and as clear:

  1. 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:

确定