</>
Skip to content
Go lessons (6/34)

Go — Variables

Variable declaration

package main

import "fmt"

func main() {
    // var declaration
    var name string = "Alice"
    var age int = 30

    // Type inference
    var city = "NYC" // inferred as string

    // Short declaration (inside functions only)
    score := 95

    fmt.Println(name, age, city, score)
}

Zero values

When declared without initialization, variables get zero values:

var i int       // 0
var f float64   // 0.0
var b bool      // false
var s string    // ""
var p *int      // nil

Multiple declarations

var (
    x int    = 10
    y string = "hello"
    z bool   = true
)

// Short declaration multiple
a, b, c := 1, 2, 3

Scope

package main

import "fmt"

var globalVar = "I am global"

func main() {
    localVar := "I am local"

    {
        blockVar := "I am block-scoped"
        fmt.Println(blockVar)
    }
    // blockVar is not accessible here

    fmt.Println(globalVar)
    fmt.Println(localVar)
}

iota

Generate a sequence of constants:

const (
    Sunday = iota // 0
    Monday        // 1
    Tuesday       // 2
    Wednesday     // 3
    Thursday      // 4
    Friday        // 5
    Saturday      // 6
)

// Bit shifting with iota
const (
    _  = iota
    KB = 1 << (10 * iota) // 1024
    MB                    // 1048576
    GB                    // 1073741824
)

Type inference

Go infers types automatically:

x := 42          // int
y := 3.14        // float64
z := "hello"     // string
b := true        // bool
nums := []int{1, 2, 3} // []int

// Explicit type when needed
var pi float32 = 3.14
var count int64 = 100

Pointer variables

package main

import "fmt"

func main() {
    x := 10
    p := &x    // p is *int, points to x

    fmt.Println(*p)  // 10 (dereference)
    *p = 20           // modify x through pointer
    fmt.Println(x)    // 20
}

Unused variables

Go does not allow unused variables:

// This will NOT compile:
func main() {
    x := 10 // Error: x declared but not used
}

// Use blank identifier to ignore:
func main() {
    _ = 10 // OK
}

Best practices

  • Use := for short, clear declarations
  • Use var for package-level or zero-value declarations
  • Keep variables close to where they're used
  • Prefer value types over pointers when possible
  • Use meaningful names, avoid single-letter except in loops

Mini Practice

Write Go code that:

  1. Declares variables with var and :=
  2. Demonstrates zero values
  3. Uses iota for a sequence of constants
  4. Shows pointer variable usage

Up Next

In the next lesson, you'll learn about Constants — defining fixed values in Go.

Related Topics

Frequently Asked Questions about Variables

What is Variables in Go?

Variables is a fundamental concept in Go. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Variables?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Variables.

Why is Variables important in Go?

Variables is essential for Go development. Understanding this concept will help you write better code and solve real-world problems more effectively.