Learn › Go Programming

Variables & Types

Understand Go's type system, variable declaration styles, zero values, type inference, and constants.

Variable Declaration

Go offers several ways to declare variables, each suited to different situations. The 'var' keyword is the most explicit form and can be used both inside and outside functions. Inside functions, the short declaration operator ':=' provides a concise way to declare and initialize variables simultaneously. Go is statically typed, so once a variable's type is set, it cannot hold values of a different type.

package main

import "fmt"

// Package-level variables use var
var globalName string = "Go"

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

    // Short declaration (type inferred)
    city := "New York"
    score := 95.5

    // Multiple declarations
    var x, y, z int = 1, 2, 3
    a, b := "hello", true

    fmt.Println(age, name, city, score, x, y, z, a, b)
}

Zero Values

In Go, every variable that is declared without an explicit initial value is automatically assigned its zero value. Numeric types default to 0, booleans default to false, and strings default to an empty string. Pointers, slices, maps, channels, interfaces, and function types all default to nil. This eliminates an entire class of uninitialized variable bugs that plague other languages.

package main

import "fmt"

func main() {
    var i int        // 0
    var f float64    // 0.0
    var b bool       // false
    var s string     // ""
    var p *int       // nil

    fmt.Printf("int: %d, float: %f, bool: %t, string: %q, pointer: %v\n",
        i, f, b, s, p)
}

Type Inference

Go's compiler can infer the type of a variable from the value assigned to it, which reduces verbosity while maintaining type safety. When using ':=' or 'var' without a type annotation, the compiler determines the type from the right-hand side expression. Integer literals default to 'int', floating-point literals default to 'float64', and string literals produce 'string'. You can check a variable's type at runtime using the fmt.Printf verb '%T'.

package main

import "fmt"

func main() {
    name := "Go"           // string
    version := 1           // int
    pi := 3.14159          // float64
    isActive := true       // bool
    complex := 1 + 2i      // complex128

    fmt.Printf("name: %T, version: %T, pi: %T, active: %T, complex: %T\n",
        name, version, pi, isActive, complex)
}

Constants

Constants in Go are declared with the 'const' keyword and must be assigned a value at compile time. Unlike variables, constants cannot be declared using the ':=' syntax. Go supports untyped constants, which have higher precision and can be used more flexibly across different numeric types. The 'iota' identifier is a powerful feature for creating enumerated constants, automatically incrementing within a const block.

package main

import "fmt"

const Pi = 3.14159

const (
    StatusPending  = iota // 0
    StatusActive          // 1
    StatusInactive        // 2
    StatusDeleted         // 3
)

const (
    KB = 1 << (10 * (iota + 1))
    MB
    GB
    TB
)

func main() {
    fmt.Println("Pi:", Pi)
    fmt.Println("Status Active:", StatusActive)
    fmt.Printf("KB: %d, MB: %d, GB: %d, TB: %d\n", KB, MB, GB, TB)
}

← Introduction to Go · Functions & Methods →