Reputation: 3566
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"
?
Upvotes: 105
Views: 37735
Reputation: 8444
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:
Upvotes: 5
Reputation: 281
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.
Upvotes: 11
Reputation: 7143
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.
Upvotes: 6
Reputation: 2382
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.
Upvotes: 18
Reputation: 13446
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' ?
Upvotes: 146
Reputation: 166895
:=
is not the assignment operator. It's a short variable declaration. =
is the assignment operator.
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 .
Assignment = ExpressionList assign_op ExpressionList .
assign_op = [ add_op | mul_op ] "=" .
In Go, name := "John"
is shorthand for var name = "John"
.
Upvotes: 24
Reputation: 1416
name := "John"
is just syntactic sugar for
var name string
name = "John"
Go is statically typed, so you have to declare variables.
Upvotes: 51