Short Variable Declaration Operator(:=) in Go
Short Variable Declaration Operator(:=) in Golang is used to create the variables having a proper name and initial value. The main purpose of using this operator to declare and initialize the local variables inside the functions and to narrowing the scope of the variables. The type of the variable is determined by the type of the expression. var keyword is also used to create the variables of a specific type. So you can say that there are two ways to create the variables in Golang as follows:
- Using the var keyword
- Using the short variable declaration operator(:=)
In this article, we will only discuss the short variable declaration operator. To know about var keyword you can refer var keyword in Go. You can also read the difference between var keyword and short variable declaration operator to get a proper idea of using both.
Syntax of using short variable declaration operator:
variable_name := expression or value
Here, you must initialize the variable just after declaration. But using var keyword you can avoid initialization at the time of declaration. There is no need to mention the type of the variable. Expression or value on the right-hand side is used to evaluate the type of the variable.
Example: Here, we are declaring the variables using short declaration operator and we are not specifying the type of the variable. The type of the variable is determined by the type of the expression on the right-hand side of := operator.
The Value of a is: 30 The Value of Language is: Go Programming
Declaring Multiple Variables Using Short Declaration Operator (:=)
Short Declaration operator can also be used to declare multiple variables of the same type or different types in the single declaration. The type of these variables is evaluated by the expression on the right-hand side of := operator.
The value of geek1 is : 117 The type of geek1 is : int The value of geek2 is : 7834 The type of geek2 is : int The value of geek3 is : 5685 The type of geek3 is : int The value of geek4 is : GFG The type of geek4 is : string The value of geek5 is : 859.240000 The type of geek5 is : float64 The value of geek6 is : 1234 The type of geek6 is : int
- Short declaration operator can be used when at least one of the variable in the left-hand side of := operator is newly declared. A short variable declaration operator behaves like an assignment for those variables which are already declared in the same lexical block. To get a better idea about this concept, let’s take an example.
Example 1: Below program will give an error as there are no new variables in the left-hand side of the := operator.
./prog.go:17:10: no new variables on left side of :=
Example 2: In the below program, you can see that the line of code geek3, geek2 := 456, 200 will work fine without any error as there is at least a new variable i.e. geek3 on the left-hand side of := operator.
The value of geek1 and geek2 is : 78 200 The value of geek3 and geek2 is : 456 200
- Go is a strongly typed language as you cannot assign a value of another type to the declared variable.
./prog.go:16:4: no new variables on left side of :=
./prog.go:16:7: cannot use “Golang” (type string) as type int in assignment
- In a short variable declaration, it is allowed to initializing a set of variables by the calling function which returns multiple values. Or you can say variables can also be assigned values that are evaluated during run time.
// Here, math.Max function return // the maximum number in i variable i := math.Max(x, y)
Local Variables or Global Variables?
With the help of short variable declaration operator(:=) you can only declare the local variable which has only block-level scope. Generally, local variables are declared inside the function block. If you will try to declare the global variables using the short declaration operator then you will get an error.
./prog.go:15:1: syntax error: non-declaration statement outside function body