Assignment operator in Go language

Nenad picture Nenad · May 13, 2013 · Viewed 25.7k times · Source

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"

Answer

Fabien picture Fabien · May 13, 2013

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