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.