Assignment operator in Go language

Issue

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

Solution

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

Answered By – Fabien

Answer Checked By – Senaida (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.