Declaring Variables with var
In this page:
var name type
var name type = value
var name = value
Basic var Declaration
The var keyword declares a variable with an explicit name and type: 'var age int'. Until you assign it a value, it holds the zero value for that type -- 0 for numbers, "" for strings, false for booleans.
This is different from many languages where an uninitialized variable is undefined or an error.
Example: Basic var Declaration
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// main is where the program starts running
func main() {
// Declare age with the zero value of its type
var age int
// Declare name with the zero value of its type
var name string
// Print the values followed by a newline
fmt.Println("age:", age)
// Print the values followed by a newline
fmt.Println("name:", name)
}
Login to try C/C++/Java/PHP code in the editor
Declaring and Initializing Together
You can supply an initial value in the same line as a var declaration: 'var age int = 30'. If an initializer is present, Go can often infer the type, letting you drop the explicit type entirely and write 'var age = 30'.
Example: Declaring and Initializing Together
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// main is where the program starts running
func main() {
// Declare int and set it to 30
var age int = 30
var city = "Bengaluru"
// Print the values followed by a newline
fmt.Println(age, city)
}
Login to try C/C++/Java/PHP code in the editor
Declaring Multiple Variables
A single var block can declare several variables at once using parentheses, which keeps related declarations grouped and readable, especially at the top of a file for package-level variables.
Example: Declaring Multiple Variables
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
var (
appName = "Inventory"
appVersion = "1.0"
maxItems = 100
)
// main is where the program starts running
func main() {
// Print the values followed by a newline
fmt.Println(appName, appVersion, maxItems)
}
Login to try C/C++/Java/PHP code in the editor
Package-Level vs Local Variables
Variables declared with var inside a function are local to that function, while var declarations outside any function, at the top level of a file, become package-level variables visible throughout the package.
Package-level var is one of the few places Go allows a variable to exist without being immediately used.
Example: Package-Level vs Local Variables
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// Declare counter with the zero value of its type
var counter int
// Define the function increment
func increment() {
counter++
}
// main is where the program starts running
func main() {
increment()
increment()
// Print the values followed by a newline
fmt.Println("counter:", counter)
}
Login to try C/C++/Java/PHP code in the editor
- Declaring a var and never using it -- Go refuses to compile a function with an unused local variable.
- Forgetting that var declared without an initial value gets its type's zero value, not nil/undefined like in other languages.
- Redundantly writing the type when the initializer already makes it obvious, when := or type inference would be cleaner.
- 'var name type' declares a variable with an explicit type.
- 'var name type = value' declares and initializes in one step.
- Variables declared without a value get that type's zero value automatically.
- var can declare multiple variables at once inside a parenthesized block.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: