Go Variables – Declare, Initialize & Use Variables in Go (2025 Guide)
Introduction – What Are Variables in Go?
Variables are named storage locations used to hold values in memory. In Go, variables must be declared before use, and they can hold data of various types like int, string, bool, or even custom structs.
In this section, you’ll learn:
- How to declare variables using
varand:= - Static vs dynamic (inferred) typing in Go
- Zero values and best practices
- Examples of single and multiple variable declarations
Declaring Variables in Go
Method 1: Static Type Declaration
var name string
name = "Alice"
This tells the compiler: “name is a string, and I’ll assign a value later.”
You can also declare and assign in one line:
var age int = 25
Method 2: Type Inference with :=
Go supports shorthand declaration using := — the type is inferred from the value:
city := "New York"
This is equivalent to:
var city string = "New York"
:= can only be used inside functions.
Variable Initialization & Zero Values
If you declare a variable without assigning a value, Go assigns it a zero value:
| Type | Zero Value |
|---|---|
int | 0 |
float64 | 0.0 |
string | "" |
bool | false |
pointer, slice, map, interface{} | nil |
Example:
var status bool
fmt.Println(status) // Output: false
Multiple Variable Declarations
Go allows multiple variables to be declared at once:
var x, y, z int
x, y, z = 1, 2, 3
Or using shorthand:
a, b, c := "Go", 3.14, true
Each variable can hold a different type when using :=.
Real-World Example
package main
import "fmt"
func main() {
var language string = "Go"
version := 1.21
var isStable bool
fmt.Println("Language:", language)
fmt.Println("Version:", version)
fmt.Println("Is Stable?", isStable)
}
Output:
Language: Go
Version: 1.21
Is Stable? false
Common Mistakes & Rules
You cannot redeclare a variable in the same scope.
x := 10
x := 20 // Compilation Error: no new variables on left side of :=
If at least one variable is new, it works:
x := 10
x, y := 20, 30 // y is new, so it's valid
Unused variables cause compilation errors.
Every declared variable must be used, or Go will not compile the program.
Best Practices
Use := for cleaner function-local declarations
Use var when you need zero value initialization or outside of functions
Always name variables meaningfully (count, isValid)
Use camelCase for variable names (userAge, maxScore)
Avoid overusing global variables unless necessary
Summary – Recap & Next Steps
Go’s variable declaration system is simple yet powerful. With type inference, zero values, and strict compilation rules, it encourages safe and readable code.
Key Takeaways:
- Use
varfor explicit type declarations and zero values - Use
:=inside functions for quick inferred declarations - Go enforces declared-but-unused variable rules
- All variables have predictable zero values
Next: Explore Go Constants, how they differ from variables, and when to use them.
FAQs – Go Variables
What is the difference between var and :=?
var can be used outside or inside functions with or without initialization. := is shorthand and only works inside functions.
Can I declare multiple variables with different types in one line?
Yes, especially with :=:
name, age, isMember := "Bob", 30, true
What happens if I don’t assign a value to a variable?
It gets a zero value based on its type.
Can I use := outside of functions?
No. It can only be used within function bodies.
Why does Go give an error for unused variables?
To ensure clean and intentional code, Go enforces that all declared variables must be used.
Share Now :
