围棋语言中的赋值算子

最近我在玩 google 的新编程语言 去吧,我想知道为什么赋值操作符 :=在等号 =前面有一个 结肠

是否有一个特殊的原因,为什么该语言的作者想要使用 name := "John"而不是 name = "John"

31663 次浏览

:= 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".

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' ?
name := "John"

is just syntactic sugar for

var name string
name = "John"

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

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

var name = "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

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.

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.

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.