英文:
Difference between := and = operators in Go
问题
=
and :=
are both assignment operators in programming languages, but they have different use cases.
The =
operator is used for simple assignment. It assigns the value on the right-hand side to the variable on the left-hand side. For example, x = 5
assigns the value 5 to the variable x
.
On the other hand, the :=
operator is used for variable declaration and assignment in some programming languages. It declares a new variable and assigns a value to it. For example, x := 5
declares a new variable x
and assigns the value 5 to it.
In some programming languages, the :=
operator is also used for assignment within expressions. It allows you to assign a value to a variable and use that value in the same expression. For example, x := x + 1
increments the value of x
by 1.
Overall, the main difference between =
and :=
is that =
is used for simple assignment, while :=
is used for variable declaration and assignment, and sometimes for assignment within expressions.
英文:
What is the difference between the =
and :=
operators, and what are the use cases for them? They both seem to be for an assignment?
答案1
得分: 481
在Go语言中,:=
用于声明和赋值,而=
仅用于赋值。
例如,var foo int = 10
与foo := 10
是相同的。
英文:
In Go, :=
is for declaration + assignment, whereas =
is for assignment only.
For example, var foo int = 10
is the same as foo := 10
.
答案2
得分: 302
TLDR:
- 声明变量,使用
:=
。 - 改变变量的值,使用
=
。
如其他人已经解释过的那样,:=
用于声明、赋值和重新声明变量;它会自动猜测(推断)变量的类型。
例如,foo := 32
是以下形式的简写:
var foo int
foo = 32
// 或者:
var foo int = 32
// 或者:
var foo = 32
/* 这里是规则: */
★ 第一条规则:
在func
之外不能使用:=
。这是因为,在func
之外,语句应该以关键字开头。
// 下面没有关键字,是非法的。
illegal := 42
// `var`关键字使得该语句合法。
var legal = 42
func foo() {
alsoLegal := 42
// 原因:它在函数作用域内。
}
★ 第二条规则:
在同一作用域内不能使用两次(同一作用域内):
legal := 42
legal := 42 // <-- 错误
因为:=
引入了__“一个新变量”__,因此使用两次不会重新声明第二个变量,所以是非法的。
★ 第三条规则:
可以用于多变量的声明和赋值:
foo, bar := 42, 314
jazz, bazz := 22, 7
★ 第四条规则(重新声明):
可以在__“多变量”__声明中使用两次,如果其中一个变量是新的:
foo, bar := someFunc()
foo, jazz := someFunc() // <-- jazz是新的
baz, foo := someFunc() // <-- baz是新的
这是合法的,因为你没有声明所有的变量,你只是给现有的变量重新赋值,并同时声明新的变量。这被称为_重新声明_。
★ 第五条规则:
即使变量已经以相同的名称声明过,也可以在较新的作用域中使用短声明来声明变量:
var foo int = 34
func some() {
// 因为这里的foo是作用域在some函数中
foo := 42 // <-- 合法
foo = 314 // <-- 合法
}
这里,foo := 42
是合法的,因为它在some()
函数的作用域中声明了foo
。foo = 314
是合法的,因为它只是给foo
赋予了一个新值。
★ 第六条规则:
可以在__if__、for、__switch__等短语句块中声明相同的名称:
foo := 42
if foo := someFunc(); foo == 314 {
// 这里的foo作用域是314
// ...
}
// 这里的foo仍然是42
因为if foo := ...
中的foo
只属于该if
子句,并且它在不同的作用域中。
参考资料:
英文:
TLDR:
- Declare a variable, use:
:=
. - Change a variable’s value, use:
=
.
There are some rules and the TLDR is not always true.
As others have explained already, :=
is for both declaration, assignment, and also for redeclaration; and it guesses (infers) the variable's type automatically.
For example, foo := 32
is a short-hand form of:
var foo int
foo = 32
// OR:
var foo int = 32
// OR:
var foo = 32
/ Here are the rules: /
★ 1st Rule:
You can't use :=
outside of funcs
. It's because, outside a func, a statement should start with a keyword.
// no keywords below, illegal.
illegal := 42
// `var` keyword makes this statement legal.
var legal = 42
func foo() {
alsoLegal := 42
// reason: it's in a func scope.
}
★ 2nd Rule:
You can't use them twice (in the same scope):
legal := 42
legal := 42 // <-- error
Because, :=
introduces "a new variable", hence using it twice does not redeclare a second variable, so it's illegal.
★ 3rd Rule:
You can use them for multi-variable declarations and assignments:
foo, bar := 42, 314
jazz, bazz := 22, 7
★ 4th Rule (Redeclaration):
You can use them twice in "multi-variable" declarations, if one of the variables is new:
foo, bar := someFunc()
foo, jazz := someFunc() // <-- jazz is new
baz, foo := someFunc() // <-- baz is new
This is legal, because, you're not declaring all the variables, you're just reassigning new values to the existing variables, and declaring new variables at the same time. This is called redeclaration.
★ 5th Rule:
You can use the short declaration to declare a variable in a newer scope even if that variable is already declared with the same name before:
var foo int = 34
func some() {
// because foo here is scoped to some func
foo := 42 // <-- legal
foo = 314 // <-- legal
}
Here, foo := 42
is legal, because, it declares foo
in some()
func's scope. foo = 314
is legal, because, it just assigns a new value to foo
.
★ 6th Rule:
You can declare the same name in short statement blocks like: if, for, switch:
foo := 42
if foo := someFunc(); foo == 314 {
// foo is scoped to 314 here
// ...
}
// foo is still 42 here
Because, foo
in if foo := ...
, only belongs to that if
clause and it's in a different scope.
References:
答案3
得分: 277
只有=
是赋值运算符。
:=
是短变量声明子句的一部分。
👉虽然有一些规则。更多细节请参见这个答案。
英文:
Only =
is the assignment operator.
:=
is a part of the syntax of the short variable declaration clause.
👉 There are some rules though. See this other answer for more details.
答案4
得分: 25
:=
是声明的简写形式。
a := 10
b := "gopher"
a
将被声明为 int
类型,并初始化为值 10
,而 b
将被声明为字符串类型,并初始化为值 gopher
。
使用 =
的等价形式为
var a = 10
var b = "gopher"
=
是赋值运算符。它的使用方式与其他语言中的使用方式相同。
当你声明变量并且有初始化器时,可以省略类型 (http://tour.golang.org/#11)。
英文:
:=
is a short-hand for declaration.
a := 10
b := "gopher"
a
will be declared as an int
and initialized with value 10
where as b
will be declared as a string and initialized with value gopher
.
Their equivalents using =
would be
var a = 10
var b = "gopher"
=
is assignment operator. It is used the same way you would use it in any other language.
You can omit the type when you declare the variable and an initializer is present (http://tour.golang.org/#11).
答案5
得分: 19
The := 表示声明并赋值,而 = 表示简单赋值。
英文:
The := means declare and assign while the = means to simply assign.
答案6
得分: 12
:=
声明并赋值,=
只是赋值。
当你不想在代码中填充类型或结构声明时,这是很有用的。
// 使用 =
var i int
var U, V, W float64
var k = 0
var x, y float32 = -1, -2
// 使用 :=
i, j := 0, 10
f := func() int { return 7 }
ch := make(chan int)
英文:
:=
declares and assigns, =
just assigns
It's useful when you don't want to fill up your code with type or struct declarations.
// Usage with =
var i int
var U, V, W float64
var k = 0
var x, y float32 = -1, -2
// Usage with :=
i, j := 0, 10
f := func() int { return 7 }
ch := make(chan int)
答案7
得分: 8
从参考文档中:(tour.golang.org)
在函数内部,可以使用:=
短赋值语句来代替具有隐式类型的变量声明。
在函数外部,每个构造都以关键字(var、func等)开头,而:=
构造不可用。
英文:
from the reference doc : (tour.golang.org)
Inside a function, the :=
short assignment statement can be used in place of a var declaration with implicit type.
Outside a function, every construct begins with a keyword (var, func, and so on) and the := construct is not available.
答案8
得分: 2
在Go语言中,:=
用于声明和赋值,而=
只用于赋值。
例如:
var s string = "Omkar"
s := "Omkar"
英文:
In Go
:=
is for declaration and assignment also
whereas =
is only for the Assignment
for example:
var s string = "Omkar"
s:= "Omkar"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论