What does := mean in Go?
As others have explained already, := is for both declaration, and assignment, whereas = is for assignment only.
For example, var abc int = 20 is the same as abc := 20.
It's useful when you don't want to fill up your code with type or struct declarations.
A short variable declaration uses the syntax:
ShortVarDecl = IdentifierList ":=" ExpressionList .
It is a shorthand for a regular variable declaration with initializer expressions but no types:
The :=
syntax is shorthand for declaring and initializing a variable, example f := "car"
is the short form of var f string = "car"
The short variable declaration operator(:=
) can only be used for declaring local variables. If you try to declare a global variable using the short declaration operator, you will get an error.
Refer official documentation for more details
Keep on going to page 12 of the tour!
A Tour of Go
Short variable declarations
Inside a function, the := short assignment statement can be used in place of a var declaration with implicit type.
(Outside a function, every construct begins with a keyword and the := construct is not available.)