LearnAI ToolsCareerPractice BuildsPlayDevelopmentContact
Lesson 817 min read

Variables & Constants

Learn how to declare variables with var and short declaration, understand zero values, and work with constants and iota.

On this page 12 sections

Introduction

Variables and constants are how a program stores and names the data it works with. Go gives you two ways to declare variables — the explicit var keyword and the shorthand := operator — plus a distinctive feature called iota for cleanly generating related constant values.

This lesson covers both declaration styles, Go's "zero value" guarantee, and how constants (including iota) work.

Declaring Variables with var

The var keyword declares a variable with an explicit type, an inferred type from an initial value, or both.

package main
import "fmt"
func main() {
var age int = 25 // explicit type and value
var name = "Alice" // type inferred from the value (string)
var isActive bool // no value — gets the zero value (false)
fmt.Println(age, name, isActive)
}
Output

Click Run to see what this code prints.

You can also declare several variables in one var block, which is common at the top of a file for package-level variables.

var (
appName = "PrograMinds"
maxRetries = 3
debugMode = false
)

Short Declaration with :=

Inside functions, the := short variable declaration is the most common way to declare and initialize a variable — it infers the type automatically and needs no var keyword.

package main
import "fmt"
func main() {
name := "Bob"
score := 92
passed := score >= 60
fmt.Println(name, score, passed)
}
Output

Click Run to see what this code prints.

Important Restriction

The := operator can only be used inside functions, never at the package level. At the package level, you must use var.

Zero Values

Unlike many languages where an uninitialized variable is undefined or null, every Go variable declared without an explicit value is automatically set to its type's "zero value" — a well-defined default. This eliminates an entire category of "uninitialized variable" bugs.

TypeZero Value
int, float640
string"" (empty string)
boolfalse
pointer, slice, map, interfacenil
package main
import "fmt"
func main() {
var count int
var label string
var found bool
fmt.Println(count, label == "", found)
}
Output

Click Run to see what this code prints.

var vs := — Which to Use

  • Use := inside functions for local variables — it's shorter and the idiomatic default.
  • Use var when you want to declare a variable without immediately assigning a value (relying on the zero value).
  • Use var when you need to be explicit about a type that differs from what would be inferred (e.g. var x float64 = 10).
  • Use var for package-level (global) declarations, since := is not allowed there.

Constants with const

Constants are declared with const and must be assigned a value that can be computed at compile time — you cannot assign the result of a function call, for example. Once declared, a constant's value can never change.

package main
import "fmt"
const Pi = 3.14159
const AppName = "PrograMinds"
func main() {
const maxUsers = 100 // constants can also be local
fmt.Println(Pi, AppName, maxUsers)
}
Output

Click Run to see what this code prints.

Generating Constants with iota

iota is a special Go identifier used inside a const block that automatically generates a sequence of related integer constants, starting at 0 and incrementing by 1 for each new line. It's commonly used to build enum-like sets of named constants.

package main
import "fmt"
type Weekday int
const (
Sunday Weekday = iota // 0
Monday // 1
Tuesday // 2
Wednesday // 3
Thursday // 4
Friday // 5
Saturday // 6
)
func main() {
fmt.Println(Sunday, Wednesday, Saturday)
}
Output

Click Run to see what this code prints.

Each subsequent line in the const block automatically repeats the same expression (Weekday = iota) with iota incremented, so you only need to write it once on the first line.

Common Mistakes

Avoid These Mistakes
  • Trying to use := at the package level — it only works inside functions and will not compile outside one.
  • Declaring a variable with var or := and never using it — Go treats unused local variables as a compile error.
  • Assuming constants can hold values only known at runtime (like a function's return value) — const requires a compile-time-computable value.
  • Forgetting that iota resets to 0 at the start of each new const (...) block.

Best Practices

  • Prefer := for local variables unless you specifically need var's extra explicitness.
  • Group related constants together in a single const (...) block for readability.
  • Use iota for enum-style sets of related constants instead of manually numbering each one.
  • Give constants clear, descriptive, capitalized names when they need to be exported.

Frequently Asked Questions

Only partially — Go allows := if at least one variable on the left side is new; existing variables in the same statement are simply reassigned rather than redeclared.

Go's compiler raises a "declared and not used" error for local variables. This keeps code free of dead, forgotten variables. Package-level variables are exempt from this rule.

By default it starts at 0 on the first line of a const block, but you can skip values or apply expressions to change the effective starting point or step, for more advanced enum patterns.

Key Takeaways

  • var declares variables explicitly and works both inside and outside functions.
  • := is a shorthand for declaring and initializing local variables, valid only inside functions.
  • Every variable gets a predictable zero value if not explicitly initialized.
  • const declares compile-time constant values that can never change.
  • iota auto-generates sequential constant values within a const block, ideal for enum-like sets.

Summary

You now know both ways to declare variables in Go, what zero values guarantee, and how to define constants — including using iota to generate related values automatically. Next, you'll take a closer look at Go's built-in data types.

Next Lesson →

Data Types