Go语言中的赋值运算符

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

Assignment operator in Go language

问题

最近我在玩谷歌的新编程语言Go,并且想知道为什么赋值运算符:=在等号=前面有一个冒号

编程语言的作者为什么想要使用name := "John"而不是name = "John"

英文:

Lately I was playing with google's new programming language Go and was wondering why the assignment operator := has a colon in front of the equal sign =.

Is there a particular reason why the authors of the language wanted to use name := "John" instead of name = "John"?

答案1

得分: 139

The := notation serves both as a declaration and as initialization.

foo := "bar"

is equivalent to

var foo = "bar"

Why not using only foo = "bar" like in any scripting language, you may ask ? Well, that's to avoid typos.

foo = "bar"
fooo = "baz" + foo + "baz"   // Oops, is fooo a new variable or did I mean 'foo' ?
英文:

The := notation serves both as a declaration and as initialization.

foo := "bar"

is equivalent to

var foo = "bar"

Why not using only foo = "bar" like in any scripting language, you may ask ? Well, that's to avoid typos.

foo = "bar"
fooo = "baz" + foo + "baz"   // Oops, is fooo a new variable or did I mean 'foo' ?

答案2

得分: 51

name := "John"只是语法糖,实际上等同于

var name string
name = "John"

Go是静态类型语言,所以你必须声明变量。

英文:
name := "John"

is just syntactic sugar for

var name string
name = "John"

Go is statically typed, so you have to declare variables.

答案3

得分: 24

:= 不是赋值运算符,它是一个短变量声明。= 是赋值运算符。

在Go语言中,name := "John"var name = "John" 的简写形式。

英文:

:= is not the assignment operator. It's a short variable declaration. = is the assignment operator.

> 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 .
>
> Assignments
>
> Assignment = ExpressionList assign_op ExpressionList .
>
> assign_op = [ add_op | mul_op ] "=" .

In Go, name := "John" is shorthand for var name = "John".

答案4

得分: 18

Rob Pike在他的演讲“Go的起源”(2010)中解释了为什么Go有:=

:=是Pike另一种语言Newsquek(1989)中的伪操作符。
Newsquek具有类似Pascal的符号和推断类型的能力,用于声明和初始化习惯用法(第15页

// 变量: [类型] = 值
x: int = 1
x := 1

*边注:*Robert Griesemer在Google I/O 2013的问答环节中提到了:=操作符,回答了“你会从Go中删除什么东西?”这个问题。他将其称为“方便但有问题”。

英文:

Rob Pike explains why Go has := during his talk "Origins of Go" (2010).

:= was a pseudo operator in another language codesigned by Pike called Newsquek (1989).
Which had Pascal-ish notation and ability to infer type for declare and initialize idiom (page 15)

// variable: [type] = value
x: int = 1
x := 1

Marginal note: Robert Griesemer brings up := operator answering the question "What would be one thing you take out from Go?" during QA session at Google I/O 2013. Referring to it as convenient but problematic.

答案5

得分: 9

至少有一个微妙的区别:

name := "John"

var name = "John"

前者是一个非声明语句,在函数体外不允许使用,而后者是在包级别上有效的语句。

英文:

There is at least one subtle difference between

name := "John"

and

var name = "John"

The former is a non-declaration statement and not allowed outside of a function body, whereas the latter is a valid statement at the package level.

答案6

得分: 6

:=是一个用于初始化变量的简写操作符。在Go语言中,以下操作是等价的:

var myNumb String = "one"
myNumb := "one"

现在的问题是:“为什么Go语言设计了在:=之前有一个:的简写符号?”原因是为了防止常见的拼写错误。如果简写赋值操作符只是=,那么可能会出现以下情况:

var myNumb String = "one"
myNumb = "two"

现在,创建该代码的用户是想要重新将two赋值给myNumb,还是他在正确输入myNumbTwo时拼写错误了myNumb?通过在:=中包含冒号,程序员必须犯两个错误(忘记冒号和忘记var)才能产生一个bug,从而大大降低了这种可能性。

英文:

Important Context for the Answer:

:= is a shorthand operator for initializing a variable. In Go, the following operations are equivalent:

var myNumb String = "one"
myNumb := "one"

Answer:

The implied question now is: "Why did go design the shorthand notation := to have a : before the =?". The reason is to prevent prevalent typos. If the shorthand assignment operator was just =, then you could have the following situation:

var myNumb String = "one"
myNumb = "two"

Now did the user who created that code intend to reassign two to myNumb, or did he mistype myNumb instead of correctly typing myNumbTwo? By including the colon in :=, the programmer would have to commit two errors (forget the colon and forget the var) in order to have a bug, hence decreasing the probability of doing so drastically.

答案7

得分: 5

Both are the different technique of variable declaration in Go language.

var name string = "John" // is a variable declaration 

AND

name := "John"   // is a short variable declaration. 

A short variable declaration is a shorthand for a regular variable declaration with initializer expressions but no types.

Read below for detail:

Variable declarations

Short variable declarations

英文:

Both are the different technique of variable declaration in Go language.

var name string = "John" // is a variable declaration 

AND

name := "John"   // is a short variable declaration. 

A short variable declaration is a shorthand for a regular variable declaration with initializer expressions but no types.

Read below for detail:

Variable declarations

Short variable declarations

huangapple
  • 本文由 发表于 2013年5月13日 19:56:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/16521472.html
匿名

发表评论

匿名网友

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

确定