Go Variables
A variable is a named storage location in memory. It holds a value that the program can read and change during execution. Go provides multiple ways to declare variables, each suited to different situations.
Ways to Declare a Variable
Variable Declaration in Go
├── var keyword (explicit type)
├── var keyword (type inferred)
├── Short declaration := (inside functions only)
└── Multiple variables at once
Method 1 – var with Explicit Type
This is the most descriptive way. The type is written directly after the variable name.
package main
import "fmt"
func main() {
var name string = "Alice"
var age int = 30
var price float64 = 49.99
fmt.Println(name) // Alice
fmt.Println(age) // 30
fmt.Println(price) // 49.99
}
Method 2 – var with Type Inference
Go can figure out the type automatically from the value assigned. Writing the type explicitly becomes optional.
package main
import "fmt"
func main() {
var name = "Alice" // Go infers: string
var age = 30 // Go infers: int
var price = 49.99 // Go infers: float64
fmt.Println(name)
fmt.Println(age)
fmt.Println(price)
}
Method 3 – Short Variable Declaration (:=)
The := operator is the most common way to declare variables inside functions. It declares and assigns in one step without the var keyword.
package main
import "fmt"
func main() {
name := "Alice"
age := 30
price := 49.99
fmt.Println(name) // Alice
fmt.Println(age) // 30
fmt.Println(price) // 49.99
}
Important: := works only inside functions. Package-level variables always need the var keyword.
Declaration Methods Comparison
| Method | Syntax | Where It Works |
|---|---|---|
| var with type | var x int = 5 | Anywhere |
| var inferred | var x = 5 | Anywhere |
| Short declaration | x := 5 | Inside functions only |
Declaring Multiple Variables
Using var block
package main
import "fmt"
var (
country string = "India"
language string = "Go"
version int = 1
)
func main() {
fmt.Println(country) // India
fmt.Println(language) // Go
fmt.Println(version) // 1
}
Multiple Short Declarations
package main
import "fmt"
func main() {
a, b, c := 10, 20, 30
fmt.Println(a, b, c) // 10 20 30
}
Reassigning Variables
Once declared, a variable's value can be changed using the = operator. The type stays the same — only the value changes.
package main
import "fmt"
func main() {
score := 100
fmt.Println(score) // 100
score = 250
fmt.Println(score) // 250
}
Variable Memory Diagram
name := "Alice"
Memory:
┌──────────────────────────────┐
│ Variable: name │
│ Type: string │
│ Value: "Alice" │
│ Address: 0xc0000b4010 │
└──────────────────────────────┘
The Blank Identifier
Go does not allow unused variables. When a value needs to be ignored, the blank identifier _ acts as a throwaway variable.
package main
import "fmt"
func getCoords() (int, int) {
return 10, 20
}
func main() {
x, _ := getCoords() // ignore the second value
fmt.Println(x) // 10
}
Common Variable Mistakes
| Mistake | Error Message | Fix |
|---|---|---|
| Declared but never used | x declared and not used | Use the variable or remove it |
| Using := outside a function | Syntax error | Use var at package level |
| Wrong type assigned | cannot use "hello" as int | Match the value type to the declared type |
| Redeclaring with := | no new variables on left side | Use = to reassign existing variables |
Key Points
- Variables store values in named memory locations
varworks anywhere;:=works only inside functions- Go infers the type automatically when no explicit type is given
- Every declared variable must be used — Go refuses to compile otherwise
- Use
_to intentionally discard unwanted values - Use
=to reassign; use:=only for the first declaration
