Assignment operator in Go language

SyntaxGo

Syntax Problem Overview


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"?

Syntax Solutions


Solution 1 - Syntax

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' ?

Solution 2 - Syntax

name := "John"

is just syntactic sugar for

var name string
name = "John"

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

Solution 3 - Syntax

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

Solution 4 - Syntax

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.

Solution 5 - Syntax

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.

Solution 6 - Syntax

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

Solution 7 - Syntax

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNenadView Question on Stackoverflow
Solution 1 - SyntaxFabienView Answer on Stackoverflow
Solution 2 - SyntaxJoonazanView Answer on Stackoverflow
Solution 3 - SyntaxpeterSOView Answer on Stackoverflow
Solution 4 - Syntaxuser2418306View Answer on Stackoverflow
Solution 5 - Syntaxmaria sView Answer on Stackoverflow
Solution 6 - SyntaxPravin MishraView Answer on Stackoverflow
Solution 7 - SyntaxWebengView Answer on Stackoverflow