HiveBrain v1.2.0
Get Started
← Back to all entries
patterngoCritical

Assignment operator in Go language

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
operatorlanguageassignment

Problem

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

Code Snippets

foo := "bar"
var foo = "bar"
foo = "bar"
fooo = "baz" + foo + "baz"   // Oops, is fooo a new variable or did I mean 'foo' ?

Context

Stack Overflow Q#16521472, score: 148

Revisions (0)

No revisions yet.