iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Constants & iota

Go constants: const, iota, typed vs untyped, and the idioms that keep them clear at any scale.

Go — constants

EXAMPLE
// ===== Basic const =====
const Pi = 3.14159
const Greeting = "hello"
const MaxRetries = 3

// const NAME = ...     UPPER_SNAKE is NOT the Go convention; use UpperCamel
// for exported constants and lowerCamel for unexported.

// ===== Typed vs untyped =====
const A = 10              // untyped: takes the default type when used (int)
const B int = 10          // typed: explicitly int
const C = 10.0            // untyped: default float64
const D float32 = 10.0    // typed

var x int32 = A           // ok: untyped const converts to int32 implicitly
var y int32 = B           // ERROR: B is int, not int32, needs explicit conversion

// Lesson: untyped constants are flexible at use site; typed lock the type.

// ===== Const blocks =====
const (
    StatusNew      = "new"
    StatusPaid     = "paid"
    StatusShipped  = "shipped"
    StatusCanceled = "canceled"
)

// ===== iota: auto-increment within const () =====
const (
    Sunday = iota    // 0
    Monday           // 1
    Tuesday          // 2
    // ...
    Saturday         // 6
)

// Expressions involving iota:
const (
    _  = iota        // skip 0
    KB = 1 << (10 * iota)   // 1 << 10 = 1024
    MB                       // 1 << 20
    GB                       // 1 << 30
    TB
)

// ===== Bitmask flags =====
type Permission int
const (
    Read    Permission = 1 << iota   // 1
    Write                              // 2
    Execute                            // 4
)

var p = Read | Write
if p & Read != 0 { /* readable */ }

// ===== Typed enums =====
type Color int
const (
    Red   Color = iota   // 0
    Green                 // 1
    Blue                  // 2
)

func (c Color) String() string {
    return [...]string{"Red", "Green", "Blue"}[c]
}

// ===== Const expressions must be compile-time =====
const Pi2 = 2 * 3.14159       // ok
const N = 3
const Arr = N + 1             // ok
// const T = time.Now()       // ERROR — runtime value

// ===== Patterns to internalise =====
// - Group related constants in a single const ( ... )
// - Untyped constants when you want flexibility at use site
// - iota for enums; underscore-skip the zero if it has no meaning
// - Bitmask + iota for flag enums

// ===== Pitfalls =====
// - Using const for things that should be variables (config loaded at startup)
// - Surprising int / float / string default types from untyped consts
// - Forgetting to handle the zero value when iota starts at 0
// - Exporting bitmask values without a String() method (debug pain)

Why it matters

Constants are compile-time values, untyped by default, with iota for enums. Group them in const blocks, use String() methods on typed enums, and lean on the typed/untyped distinction. The patterns are small and pay back forever in readable code.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
const (
    Sunday = iota
    Monday
    Tuesday
)
// Sunday=0, Monday=1, Tuesday=2
Try it Yourself »

Discussion

Loading…