Chapter 10: Go Constants

1. What is a Constant in Go? (Core Idea)

  • A constant is a name bound to a compile-time fixed value.
  • Value must be known at compile time (literals, expressions of constants only).
  • Cannot be changed after declaration (no reassignment — compile error if you try).
  • Unlike variables: constants can be untyped (very flexible) and have arbitrary precision (no overflow during computation).
  • Types: boolean, rune, integer, floating-point, complex, string.

From the spec (go.dev/ref/spec#Constants — as of Go 1.26):

“There are boolean constants, rune constants, integer constants, floating-point constants, complex constants, and string constants. Rune, integer, floating-point, and complex constants are collectively called numeric constants.”

2. Declaration Styles (Similar to Variables — But No :=)

Three main ways — no short := for constants!

Style Syntax Example Where Allowed Type Required? Common Use
Single const const Pi = 3.14159 Anywhere Optional (inferred) Simple values
Typed single const MaxRetries int = 5 Anywhere Yes Force type
Grouped const block const ( … ) Anywhere Optional Related constants / enums
With iota const ( Sunday = iota … ) Inside block Usually inferred Enum-like sequences

Important: Value must be assigned at declaration — no “declare now, set later”.

3. Basic Examples (Run These!)

Go

Output (huge number prints exactly):

text

4. Typed vs Untyped Constants (The Magic Part)

  • Untyped constants (most common & powerful): no fixed type → compiler chooses best type when used.
  • Typed constants: forced to specific type (int32, float32, etc.).

Why untyped is awesome:

Go

From Rob Pike’s blog (go.dev/blog/constants — still relevant 2026):

“In Go, constants behave pretty much like regular numbers… Constants can be huge, with arbitrary precision.”

5. Grouped Constants + iota (Enum-like — Very Common)

iota resets to 0 in each const block, increments by 1 each line.

Go

Real code snippet (like time.Weekday in stdlib):

Go

6. Naming Conventions for Constants (2026 Style)

From Effective Go + stdlib:

  • Use MixedCaps / camelCase — NO ALL_CAPS or underscores (unlike C/Java)
  • Exported (public) → Uppercase first letter: MaxRetries, StatusOK
  • Unexported (package-private) → lowercase: defaultTimeout, internalKey
  • Short & descriptive — especially for local-ish use
  • Stdlib examples: http.StatusOK, os.O_RDONLY, math.Pi, time.Monday

Bad (non-idiomatic):

Go

Good:

Go

7. Common Use Cases & Patterns

  • Mathematical constants: math.Pi, math.E
  • HTTP / status codes: http.StatusNotFound
  • Configuration defaults: const DefaultTimeout = 30 * time.Second
  • Enums / states: iota for days, log levels, error codes
  • Bitmasks: flags for permissions, options

8. Quick Exercise (Try in Playground or VS Code)

Fix & improve this:

Go

Better version:

Go

Any part confusing yet?

  • Untyped vs typed deeper?
  • iota skipping values / custom increments?
  • Constants in expressions / build tags?
  • Or ready for functions, pointers, structs next?

You’re doing fantastic — constants are where Go starts showing its numeric elegance! 💪 Keep coding, paste any experiment results or questions. 🇮🇳🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *