Golang var keyword
last modified May 7, 2025
This tutorial explains how to use the var keyword in Go. We'll
cover variable declaration basics with practical examples of different usage.
The var keyword declares variables in Go, optionally initializing them with values. It's used for explicit variable declaration with type.
In Go, var can declare single variables, multiple variables, and
package-level variables. It provides clear type specification and initialization
control.
Basic variable declaration
The simplest use of var declares a single variable with its type.
This example shows basic variable declaration.
package main
import "fmt"
func main() {
var age int
age = 30
fmt.Println("Age:", age)
}
The variable age is declared as type int and later
assigned a value. The zero value for integers (0) is assigned initially.
Variable with initialization
var can declare and initialize variables in one statement. This
example shows declaration with immediate initialization.
package main
import "fmt"
func main() {
var name string = "Alice"
var height float64 = 1.75
fmt.Println("Name:", name)
fmt.Println("Height:", height)
}
Both variables are declared with their types and initial values. The type can be omitted when the initializer provides enough type information.
Type inference with var
Go can infer variable types from initializers. This example demonstrates type
inference with the var keyword.
package main
import "fmt"
func main() {
var active = true
var count = 42
var price = 19.99
fmt.Printf("active: %T, %v\n", active, active)
fmt.Printf("count: %T, %v\n", count, count)
fmt.Printf("price: %T, %v\n", price, price)
}
The variables get their types from the assigned values: bool,
int, and float64. The %T verb shows
the inferred types.
Multiple variable declaration
var can declare multiple variables in one statement. This example
shows different ways to declare multiple variables.
package main
import "fmt"
func main() {
var x, y int = 10, 20
var a, b = "hello", true
var (
name = "Bob"
age = 25
)
fmt.Println(x, y)
fmt.Println(a, b)
fmt.Println(name, age)
}
Variables can be declared with the same type, different types, or in a block. Parentheses create a declaration block for better readability.
Package-level variables
var can declare variables at package scope. These are accessible
throughout the package. This example shows package-level variables.
package main
import "fmt"
var globalCount = 0
func increment() {
globalCount++
}
func main() {
fmt.Println("Initial count:", globalCount)
increment()
increment()
fmt.Println("Final count:", globalCount)
}
globalCount is accessible in all functions within the package.
Package variables exist for the lifetime of the program.
Zero values with var
Variables declared with var receive zero values when not
initialized. This example demonstrates default zero values.
package main
import "fmt"
func main() {
var i int
var f float64
var b bool
var s string
fmt.Printf("int: %v\n", i)
fmt.Printf("float64: %v\n", f)
fmt.Printf("bool: %v\n", b)
fmt.Printf("string: %q\n", s)
}
The zero values are 0 for numbers, false for booleans, and "" for strings.
The %q verb shows empty string as "".
var vs short declaration
This example contrasts var with short declaration syntax to
highlight their differences.
package main
import "fmt"
var packageVar = "I'm a package variable"
func main() {
// Using var
var functionVar = "I'm a function variable"
// Short declaration
shortVar := "I'm a short declaration"
fmt.Println(packageVar)
fmt.Println(functionVar)
fmt.Println(shortVar)
// Re-declaration with short syntax
shortVar, anotherVar := "changed", "new"
fmt.Println(shortVar, anotherVar)
}
var works at package and function level, while short declaration
is function-only. Short syntax allows redeclaration when declaring new variables.
Source
This tutorial covered the var keyword in Go with practical
examples of variable declaration in various scenarios.
Author
List all Golang tutorials.