Skip to main content
Go provides several predeclared constants that are available in all packages without import. These constants are fundamental to Go’s type system and control flow.

Boolean Constants

true

The boolean true value.
true
bool
required
Untyped boolean constant with value true.
var isValid bool = true

if true {
    fmt.Println("This always executes")
}

// Used in comparisons
result := (5 == 5) // true

false

The boolean false value.
false
bool
required
Untyped boolean constant with value false.
var isComplete bool = false

if false {
    fmt.Println("This never executes")
}

// Used in comparisons
result := (3 > 5) // false
isValid := true
isComplete := false

if isValid && !isComplete {
    fmt.Println("Valid but not complete")
}

Zero Value Constant

nil

The zero value for pointers, channels, functions, interfaces, maps, and slices.
nil
Type
required
Predeclared identifier representing the zero value for reference types.Can only be used with: pointer, channel, func, interface, map, or slice types.
var p *int = nil

if p == nil {
    fmt.Println("Pointer is nil")
}

// Dereferencing nil pointer panics
// fmt.Println(*p) // panic: runtime error

// Check before dereferencing
if p != nil {
    fmt.Println(*p)
}
Attempting to use nil values incorrectly can cause runtime panics. Always check for nil before dereferencing pointers, writing to maps, or calling functions.
Starting in Go 1.21, calling panic(nil) causes a run-time error. The GODEBUG setting panicnil=1 can disable this check.

Enumeration Constant

iota

Predeclared identifier representing the index of the current const specification.
iota
int
required
Untyped integer constant starting at 0 in each const block.Increments by 1 for each const specification in the same parenthesized group.
const (
    Sunday    = iota // 0
    Monday           // 1
    Tuesday          // 2
    Wednesday        // 3
    Thursday         // 4
    Friday           // 5
    Saturday         // 6
)
Use iota for defining sequential constants, especially enumerations and bit flags. It makes code more maintainable and less error-prone.

How iota Works

iota represents the index within a const declaration:
  1. Resets to 0 at the start of each const block
  2. Increments by 1 for each const specification (line)
  3. Can be used in expressions
  4. Repeats the expression for subsequent lines if not specified
const (
    a = iota         // 0
    b = iota * 2     // 1 * 2 = 2
    c                 // 2 * 2 = 4 (expression repeated)
    d                 // 3 * 2 = 6 (expression repeated)
)

// New const block resets iota
const (
    e = iota         // 0 (reset)
    f                 // 1
)
  • Use iota for related constant groups that need sequential values
  • Start with _ = iota if you want the first constant to be 1 instead of 0
  • Use meaningful type aliases for better type safety with enumerations
  • Document what each constant represents
  • Consider using iota with bit shifts for flag constants
Enumerations:
type Status int
const (
    Pending Status = iota
    Approved
    Rejected
)
Bit flags:
type Flag uint
const (
    FlagA Flag = 1 << iota
    FlagB
    FlagC
)
Powers of 10:
const (
    One     = 1
    Ten     = One * 10
    Hundred = Ten * 10
    Thousand = Hundred * 10
)
  • Forgetting that iota resets in each new const block
  • Assuming iota persists across multiple const declarations
  • Not understanding that blank lines don’t affect iota increment
  • Mixing iota and explicit values without understanding the behavior
const (
    a = iota  // 0
    b = 42    // 42 (explicit, iota is 1 but unused)
    c = iota  // 2 (continues from where it was)
)

Zero Values

While not predeclared constants, Go’s zero values are important to understand:
TypeZero Value
boolfalse
int, int8, int16, int32, int640
uint, uint8, uint16, uint32, uint64, uintptr0
float32, float640.0
complex64, complex1280+0i
string"" (empty string)
pointer, function, interface, slice, channel, mapnil
array, structAll fields/elements set to their zero values
var b bool        // false
var i int         // 0
var f float64     // 0.0
var s string      // ""
var p *int        // nil
var slice []int   // nil
var m map[string]int // nil

type Point struct {
    X, Y int
}
var pt Point      // {0, 0}
Go automatically initializes all variables to their zero value. This ensures variables are always in a defined state.

Build docs developers (and LLMs) love