English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The short variable declaration operator (:=) in Golang is used to create variables with appropriate names and initial values. The main purpose of this operator is to declare and initialize local variables within functions and to narrow the scope of variables. The type of the variable is determined by the type of the expression. The var keyword is also used to create variables of specific types. So you can say that there are two ways to create variables in Golang, as shown below:
Use the var keyword
Use the short variable declaration operator (:=)
In this article, we will only discuss the short variable declaration operator. To understand the var keyword, you canIn GoReferencevar keyword.
Syntax for using the short variable declaration operator:
variable_name := expression or value
Here, variables must be initialized immediately after declaration without mentioning their type, and the expression or value on the right is used to evaluate the variable type. The use of the var keyword can avoid initialization at the time of declaration.
Example:}}Here, we declare variables using the short declaration operator and do not specify the variable type. The variable type is determined by:=The type of the expression on the right side of the operator determines.
package main import "fmt" func main() { //Variable declaration and initialization a := 30 //Accept a string variable Language := "Go Language Basic Tutorial" fmt.Println("a's value: ", a) fmt.Println("Language's value: ", Language) }
Output:
a's value: 30 Language's value: Go Language Basic Tutorial
The short declaration operator can also be used to declare multiple variables in a single declarationThe same type or different typesMultiple variables. The types of these variables are determined by:=Evaluate the expression on the right side of the operator.
package main import "fmt" func main() { //Multiple variables of the same type (int) w3codebox1, w3codebox2, w3codebox3 := 117, 7834, 5685 //Multiple variables of different types w3codebox4, w3codebox5, w3codebox6 := "GFG", 859.24, 1234 //Display the value and variable type fmt.Printf("w3codebox1 The value is : %d\n3codebox1)} fmt.Printf("w3codebox1 The type is : %T\n3codebox1)} fmt.Printf("\nw3codebox2 The value is : %d\n3codebox2)} fmt.Printf("w3codebox2 The type is : %T\n3codebox2)} fmt.Printf("\nw3codebox3 The value is : %d\n3codebox3)} fmt.Printf("w3codebox3 The type is : %T\n3codebox3)} fmt.Printf("\nw3codebox4 The value is : %s\n3codebox4)} fmt.Printf("w3codebox4 The type is : %T\n3codebox4)} fmt.Printf("\nw3codebox5 The value is : %f\n3codebox5)} fmt.Printf("w3codebox5 The type is : %T\n3codebox5)} fmt.Printf("\nw3codebox6 The value is : %d\n3codebox6)} fmt.Printf("w3codebox6 The type is : %T\n3codebox6)} }
Output:
w3codebox1 The value is : 117 w3codebox1 The type is : int w3codebox2 The value is : 7834 w3codebox2 The type is : int w3codebox3 The value is : 5685 w3codebox3 The type is : int w3codebox4 The value is : GFG w3codebox4 The type is : string w3codebox5 The value is : 859.240000 w3codebox5 The type is : float64 w3codebox6 The value is : 1234 w3codebox6 The type is : int
With the short variable declaration operator (:=),You can only declareonly have block scopelocal variables. Usually, local variables are declared within a functional block. If you try to declare a global variable using the short declaration operator (:=), an error message will be thrown.
package main import "fmt" //Declare using the var keyword //And initialize the variable //It is a package variable, or you can say it is a global scope var w3codebox1 := 900 //Use short variable declarations //It will throw an error w3codebox2 := 200 func main() { //Accessing w inside a function3codebox1 fmt.Println(w3codebox1)} // Accessing w inside a function3codebox2 fmt.Println(w3codebox2)} }
Output error:
.\test.go:12:1: syntax error: non-declaration statement outside function body #.\test.go:12:1: Syntax error: non-declaration statement outside function body
package main import "fmt" //Declare a global variable using var var w3codebox1 := 900 func main() { //Use short variable declarations //Inside the main function //It has a local scope, which means it cannot //Accessing outside the main function w3codebox2 := 200 //Accessing w inside a function3codebox1 fmt.Println(w3codebox1)} //Accessing w inside a function3codebox2 fmt.Println(w3codebox2)} }
Output:
900 200