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.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/golang/go/llms.txt
Use this file to discover all available pages before exploring further.
Boolean Constants
true
The boolean true value.Untyped boolean constant with value true.
false
The boolean false value.Untyped boolean constant with value false.
Zero Value Constant
nil
The zero value for pointers, channels, functions, interfaces, maps, and slices.Predeclared identifier representing the zero value for reference types.Can only be used with: pointer, channel, func, interface, map, or slice types.
- Pointers
- Slices
- Maps
- Channels
- Interfaces
- 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.Untyped integer constant starting at 0 in each const block.Increments by 1 for each const specification in the same parenthesized group.
How iota Works
iota represents the index within a const declaration:
- Resets to 0 at the start of each
constblock - Increments by 1 for each const specification (line)
- Can be used in expressions
- Repeats the expression for subsequent lines if not specified
Best practices
Best practices
- Use
iotafor related constant groups that need sequential values - Start with
_ = iotaif 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
iotawith bit shifts for flag constants
Common patterns
Common patterns
Enumerations:Bit flags:Powers of 10:
Common mistakes
Common mistakes
- Forgetting that
iotaresets in each new const block - Assuming
iotapersists across multiple const declarations - Not understanding that blank lines don’t affect
iotaincrement - Mixing
iotaand explicit values without understanding the behavior
Zero Values
While not predeclared constants, Go’s zero values are important to understand:| Type | Zero Value |
|---|---|
bool | false |
int, int8, int16, int32, int64 | 0 |
uint, uint8, uint16, uint32, uint64, uintptr | 0 |
float32, float64 | 0.0 |
complex64, complex128 | 0+0i |
string | "" (empty string) |
pointer, function, interface, slice, channel, map | nil |
array, struct | All fields/elements set to their zero values |
Go automatically initializes all variables to their zero value. This ensures variables are always in a defined state.