# What Are Variables?
A variable is a named storage location in memory that holds a value. In Go, every variable has a specific type that determines what kind of data it can store and how much memory it uses.
Go is a statically typed language — the type of every variable is known at compile time. This catches many bugs before your program even runs.
More Details (Real-World Example)click to expand
Think of variables like labeled storage boxes in a warehouse. If a box is labeled “glass items only,” you won't put heavy tools inside it. The label (type) protects the system from misuse.
In software teams, this matters when many developers touch the same code. If a field is defined as int, everyone knows what belongs there. Go's type system is that shared labeling system, which prevents accidental misuse early.
Zero values are like getting empty but usable boxes by default. Even before you fill them, they are in a known safe state — that is why Go programs are often more predictable.
# Declaring Variables
Go gives you three ways to declare variables:
1. Full declaration with var
var name string = "Gopher"
var age int = 25
var isReady bool = trueThis is the most explicit form. You specify the keyword var, the variable name, its type, and its value.
2. Type inference with var
var name = "Gopher" // Go infers: string
var age = 25 // Go infers: int
var pi = 3.14159 // Go infers: float64When you provide an initial value, Go can figure out the type automatically. This is called type inference.
3. Short variable declaration (:=)
name := "Gopher" // string
age := 25 // int
isReady := true // bool
score := 99.5 // float64The := operator is the most common way to declare variables inside functions. It's short, clean, and idiomatic Go. It can only be used inside functions, not at package level.
# Zero Values
In Go, variables declared without an initial value are given a zero value. There is no "undefined" or "null" for basic types.
var i int // 0
var f float64 // 0.0
var b bool // false
var s string // "" (empty string)This is a powerful feature — you always know exactly what a new variable contains, even if you forget to initialize it.
# Basic Data Types
| Type | Description | Zero Value | Example |
|---|---|---|---|
| int | Integer (platform-dependent size) | 0 | 42 |
| float64 | 64-bit floating point | 0.0 | 3.14 |
| string | UTF-8 text | "" | "hello" |
| bool | Boolean (true/false) | false | true |
| byte | Alias for uint8 | 0 | 'A' |
| rune | Alias for int32 (Unicode) | 0 | '🚀' |
# Complete Example
package main
import "fmt"
func main() {
// Short declaration (most common)
name := "Alice"
age := 30
height := 5.7
isStudent := false
// Print all variables
fmt.Println("Name:", name)
fmt.Println("Age:", age)
fmt.Println("Height:", height)
fmt.Println("Student?", isStudent)
// Check types using Printf with %T
fmt.Printf("%s is %T\\n", "name", name)
fmt.Printf("%s is %T\\n", "age", age)
fmt.Printf("%s is %T\\n", "height", height)
}Output:
Name: Alice
Age: 30
Height: 5.7
Student? false
name is string
age is int
height is float64# Type Conversion
Go does not do implicit type conversion. You must convert explicitly:
package main
import "fmt"
func main() {
i := 42
f := float64(i) // int → float64
u := uint(f) // float64 → uint
fmt.Println(i, f, u) // 42 42 42
// This will NOT compile:
// var x float64 = i // cannot use i (int) as float64
}# Constants
Use const for values that never change:
const pi = 3.14159
const greeting = "Hello, World"
const maxRetries = 3
// Grouped constants
const (
StatusOK = 200
StatusError = 500
)Constants must be assigned at compile time — you cannot use a function result as a constant value. The := operator cannot be used with constants.
⚡ Key Takeaways
- Use
:=for short declarations inside functions (most common) - Use
varat package level or when you need the zero value - Go has zero values — no null/undefined for basic types
- Type conversion must be explicit (e.g.,
float64(x)) - Unused variables cause compile errors — Go keeps you disciplined