var keyword in Go
var keyword in Golang is used to create the variables of a particular type having a proper name and initial value. Initialization is optional at the time of declaration of variables using var keyword that we will discuss later in this article.
Syntax:
var identifier type = expression
Example:
// here geek1 is the identifier // or variable name, int is the // type and 200 is assigned value var geek1 int = 200
As you know that Go is a statically typed language but it still provides a facility to remove the declaration of data type while declaring a variable as shown in below syntax. This is generally termed as the Type Inference.
Syntax:
var identifier = initialValue
Example:
var geek1 = 200
Multiple variable declarations using var Keyword
var keyword is also used to declare multiple variables in a single line. You can also provide the initial value to the variables as shown below:
- Declaring multiple variables using var keyword along with the type:
var geek1, geek2, geek3, geek4 int
- Declaring multiple variables using var keyword along with the type and initial values:
var geek1, geek2, geek3, geek4 int = 10, 20, 30, 40
Note:
- You can also use type inference(discussed above) that will let the compiler to know about the type i.e. there is an option to remove the type while declaring multiple variables.
Example:
var geek1, geek2, geek3, geek4 = 10, 20, 30.30, true
- You can also use multiple lines to declare and initialize the values of different types using a var keyword as follows:
Example:
var( geek1 = 100 geek2 = 200.57 geek3 bool geek4 string = "GeeksforGeeks" ) - While using type during declaration you are only allowed to declare multiple variables of the same type. But removing type during declarations you are allowed to declare multiple variables of different types.
Example:
// Go program to demonstrate the multiple// variable declarations using var keywordpackage mainimport"fmt"func main() {// Multiple variables of the same type// are declared and initialized// in the single line along with typevar geek1, geek2, geek3int= 232, 784, 854// Multiple variables of different type// are declared and initialized// in the single line without specifying// any typevar geek4, geek5, geek6 = 100,"GFG", 7896.46// Display the values of the variablesfmt.Printf("The value of geek1 is : %d\n", geek1)fmt.Printf("The value of geek2 is : %d\n", geek2)fmt.Printf("The value of geek3 is : %d\n", geek3)fmt.Printf("The value of geek4 is : %d\n", geek4)fmt.Printf("The value of geek5 is : %s\n", geek5)fmt.Printf("The value of geek6 is : %f", geek6)}chevron_rightfilter_noneOutput:
The value of geek1 is : 232 The value of geek2 is : 784 The value of geek3 is : 854 The value of geek4 is : 100 The value of geek5 is : GFG The value of geek6 is : 7896.460000
Important Points about var keyword:
- During the declaration of the variable using var keyword, you can either remove type or = expression but not both. If you will do, then the compiler will give an error.
- If you removed the expression then the variable will contain the zero-value for numbers and false for booleans “” for strings and nil for interface and reference type by default. So, there is no such concept of an uninitialized variable in Go language.
Example:
// Go program to illustrate// concept of var keywordpackage mainimport"fmt"func main() {// Variable declared but// no initializationvar geek1intvar geek2 stringvar geek3 float64var geek4bool// Display the zero-value of the variablesfmt.Printf("The value of geek1 is : %d\n", geek1)fmt.Printf("The value of geek2 is : %s\n", geek2)fmt.Printf("The value of geek3 is : %f\n", geek3)fmt.Printf("The value of geek4 is : %t", geek4)}chevron_rightfilter_noneOutput:
The value of geek1 is : 0 The value of geek2 is : The value of geek3 is : 0.000000 The value of geek4 is : false
Recommended Posts:
- Defer Keyword in Golang
- Difference between var keyword and short declaration operator in Golang
- Hello World Program : First program while learning Programming
- Golang Tutorial - Learn Go Programming Language
- Packages in Golang
- How to Install Golang on MacOS?
- Golang | Deadlock and Default Case in Select Statement
- Rune in Golang
- Encapsulation in Golang
- How to Map a Rune to the Specified Case in Golang?
- How to Map a Rune to Uppercase in Golang?
- How to Map a Rune to Title Case in Golang?
- How to Map a Rune to Lowercase in Golang?
- Check If the Rune is a Space Character or not in Golang
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : shubham_singh


