103

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

2
  • 2
    Great question and the answer is that using = for assignment is a mistake. You learned early on that x + 2 = 4 is an equation not an assignment. However, since assignment is far more frequent than checks for equality, some dominant languages started out using "=" for assignment early on... and the convention stuck. May 31, 2020 at 18:48
  • 5
    @RickO'Shea - but Go also uses = for assignment.
    – Davor
    Sep 6, 2021 at 19:46

7 Answers 7

140

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

is just syntactic sugar for

var name string
name = "John"

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

8
  • 11
    name := "John" is shorthand for var name = "John".
    – peterSO
    May 13, 2013 at 13:41
  • 20
    @peterSO but var name = "John" is short for var name string = "John" which is short for the two lines given.
    – Dave C
    May 13, 2013 at 14:24
  • 3
    @Dave C: The Go Programming Language Specification is explicit: "A short variable declaration ... is a shorthand for a regular variable declaration with initializer expressions but no types: "var" IdentifierList = ExpressionList .".
    – peterSO
    May 13, 2013 at 14:31
  • 8
    @peterSO I know, I read that too. That doesn't change what I said. This answer is correct.
    – Dave C
    May 13, 2013 at 19:07
  • 1
    @KennethWorden How does it hinder readability? It minimizes unnecessary information. Languages with complete type inference are a good example of why this is a good thing.
    – Joonazan
    Jun 20, 2017 at 14:29
24

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

18

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.

10

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

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.