Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A constant in Go is an immutable, compile-time construct representing a fixed value. Declared using the const keyword, constants are evaluated by the compiler rather than at runtime. They are strictly limited to boolean, rune, integer, floating-point, complex, and string types.

Syntax

Constants can be declared individually or grouped within a block.
// Single declaration
const Pi float64 = 3.14159265359

// Grouped declaration
const (
    StatusOK       = 200
    StatusNotFound = 404
)

Typed vs. Untyped Constants

Go distinguishes between typed and untyped constants, a mechanism that provides flexibility within its strict type system. Untyped Constants An untyped constant possesses a value and a default type (e.g., int for 42, float64 for 3.14), but it is not strictly bound to that type. Numeric untyped constants exist in an arbitrary-precision mathematical space. They do not overflow and can be implicitly converted to any compatible type upon assignment.
const MaxUint64 = 18446744073709551615 // Untyped integer, arbitrary precision
const Huge = 1e1000                    // Valid at compile time, despite exceeding float64 bounds
Typed Constants A typed constant is explicitly bound to a specific Go type. Once typed, the constant loses its arbitrary precision and is subject to the memory bounds and overflow rules of its underlying type.
const TypedInt int8 = 127
// const Overflow int8 = 128 // Compiler error: constant 128 overflows int8

Constant Expressions

Expressions consisting entirely of constants are evaluated at compile time. The result of a constant expression is itself a constant. If the operands are untyped, the result remains untyped, maintaining arbitrary precision during intermediate calculations.
const A = 10
const B = 20
const C = A * B / 5 // Evaluated entirely at compile time

The iota Identifier

iota is a predeclared identifier used exclusively within const declarations to generate sequences of numbers. It represents successive untyped integer constants. iota is reset to 0 whenever the reserved word const appears in the source code and increments by 1 after each ConstSpec (constant specification) within a block.
const (
    Zero = iota // 0
    One         // 1 (Implicitly uses iota)
    Two         // 2 (Implicitly uses iota)
)
iota can be combined with constant expressions to generate complex compile-time sequences, such as bitwise shifts.
const (
    ShiftA = 1 << iota // 1 << 0 = 1
    ShiftB             // 1 << 1 = 2
    ShiftC             // 1 << 2 = 4
)

Implicit Repetition

In a grouped constant declaration, if the expression list is omitted, the compiler implicitly repeats the preceding expression and its type.
const (
    X = 10.5 // Untyped float
    Y        // 10.5
    Z        // 10.5
)
Master Go with Deep Grasping Methodology!Learn More