英文:
Why does go have := short assignments inside functions?
问题
我不太理解短赋值的具体目的,为什么要这样做:
x:= 10
而这样也是可以的:
var x = 10
是否有特定的使用场景,短赋值更方便一些呢?谢谢。
英文:
I don't quite understand the specific purpose of short assignments,
why do this:
x:= 10
when this is also possible:
var x = 10
Is there any specific use case where short assignments are more convenient
Thanks
答案1
得分: 15
如果 x, err := fn(); err != nil {
// 做一些事情
}
在上面的例子中,变量被限制在 if 语句内部。如果你试图在 if 语句外部访问 err
,它将不可用。x
也是一样。在许多情况下,保持这样的作用域可能很有用,但我认为使用 :=
的情况是给定的样式,比如上面的 if
、switch
、for
。
对于一些额外的背景,var
也允许分组,就像使用 import
一样。
var (
y = 1
z = 2
)
这进一步区分了 var
和 :=
的用例。
英文:
if x, err := fn(); err != nil {
// do something
}
In the above case, the variables are confined within the if statement. If you try to access err
outside of the if statement, it won't be available. Likewise for x
. There's various cases where maintaining scope like this might be useful, but I'd say the use of :=
is for given styles like the above with if
, switch
, for
.
For some additional background, var
also allows grouping, much like using import
.
var (
y = 1
z = 2
)
which pushes the use-cases for var
vs :=
further apart.
答案2
得分: 3
Example 1:
var age int = 30
Example 2:
var age = 30
Example 3:
age := 30
以上的例子都是相同的。Example 2 和 Example 3 只是简单地“推断”类型。这也是一种简写方式。下面是来自Caleb Doxsey的公共领域 - 创作共用pdf《An Introduction To Programming In GO》的摘录:
“由于创建一个带有初始值的新变量非常常见,Go还支持一种更短的语句:
x := "Hello World"
注意在**=
之前的:
,并且没有指定类型。类型是不必要的,因为Go编译器能够根据您分配给变量的字面值**来推断类型。(由于您分配了一个字符串字面值,x被赋予了字符串类型)
编译器也可以对var语句进行推断:
var x = "Hello World"
对于其他类型也是一样的:
x := 5
fmt.Println(x)
通常情况下,您应该尽可能使用这种更短的形式。”
英文:
Example 1:
var age int = 30
Example 2:
var age = 30
Example 3:
age := 30
All of the examples above are the same. Example 2 and Example 3 simply 'infer' the type. It's also a type of shorthand. Below is an excerpt from a public domain - creative commons pdf, "An Introduction To Programming In GO", by Caleb Doxsey
'Since creating a new variable with a starting value is so common Go also supports a shorter statement:
x := "Hello World"
Notice the :
before the =
and that no type was specified. The type is not necessary because the Go compiler is able to infer the type based on the literal value you assign the variable. (Since you are assigning a string
literal, x is given the type string)
The compiler can also do inference with the var statement:
var x = "Hello World"
The same thing works for other types:
x := 5
fmt.Println(x)
Generally you should use this shorter form whenever possible.'
答案3
得分: 2
我相信 :=
主要是作为一个方便的语法存在,用于接收函数调用的结果,其中一个常常希望重用现有变量,同时声明一个新变量:
x, err := func1()
if err != nil {
fmt.Fatal(err)
}
y, err := func2()
if err != nil {
fmt.Fatal(err)
}
上述代码可以编译,因为 :=
允许列出现有变量,只要至少有一个新变量被创建。尝试将 y, err :=
替换为 var y, err =
,你会发现它无法编译。
英文:
I believe that :=
mainly exists as a convenient syntax for receiving the results of function calls, where one often desires to reuse an existing variable, while declaring a new one:
x, err := func1()
if err != nil {
fmt.Fatal(err)
}
y, err := func2()
if err != nil {
fmt.Fatal(err)
}
The above code compiles because :=
allows existing variables to be listed, as long as at least one new variable is created. Try replacing y, err :=
with var y, err =
and you will find it does not compile.
答案4
得分: 1
在这种情况下没有理由,它们是等价的。
当你有这样的情况时是有意义的
var i int
i = 0
所以你可以更简洁地推断类型
i := 0
但除此之外它们完全相同。
英文:
There is no reason in that case, they are equivalent.
It makes sense when you have this
var i int
i = 0
So you can be more concise and infer the type with
i := 0
But otherwise they're exactly the same.
答案5
得分: 0
x := fn()
这将使x
的类型与函数fn
的返回类型相同。如果您重构代码并且fn
的返回类型发生更改,x
的类型将免费更改。
...而且打字更少。
英文:
x := fn()
that will make x
the same type as the return type of the function fn
. If you refactor your code and the return type of fn
changes, x
's type will be changed for free.
...and it's less typing.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论