patterngoCritical
Assignment operator in Go language
Viewed 0 times
operatorlanguageassignment
Problem
Lately I was playing with google's new programming language Go and was wondering why the assignment operator
Is there a particular reason why the authors of the language wanted to use
:= 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
is equivalent to
Why not using only
:= 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.