</>
Skip to content
Go lessons (7/34)

Go — Constants

Basic constants

package main

import "fmt"

const Pi = 3.14159
const MaxRetries = 3
const AppName = "MyApp"

func main() {
    fmt.Println(Pi)
    fmt.Println(MaxRetries)
    fmt.Println(AppName)
}

Typed constants

const (
    StatusOK    int = 200
    StatusError int = 500
    PI          float64 = 3.14159
    MaxSize     int64   = 1024
)

Untyped constants

const Big = 1 << 100        // Very large number
const Small = Big >> 99      // 2

// Untyped constants have higher precision
const Pi = 3.14159265358979323846

iota

Generate a sequence of constants automatically:

const (
    Sunday = iota    // 0
    Monday           // 1
    Tuesday          // 2
    Wednesday        // 3
    Thursday         // 4
    Friday           // 5
    Saturday         // 6
)

iota with expressions

const (
    _  = iota
    KB = 1 << (10 * iota) // 1024
    MB                    // 1048576
    GB                    // 1073741824
    TB                    // 1099511627776
)

const (
    FlagNone = 1 << iota // 1
    FlagRead             // 2
    FlagWrite            // 4
    FlagExecute          // 8
)

iota with custom start

const (
    A = iota + 10  // 10
    B              // 11
    C              // 12
)

Blank identifier with iota

const (
    _  = iota // skip 0
    A         // 1
    B         // 2
    C         // 3
)

Enum pattern

type Color int

const (
    Red   Color = iota // 0
    Green              // 1
    Blue               // 2
)

func (c Color) String() string {
    switch c {
    case Red:
        return "Red"
    case Green:
        return "Green"
    case Blue:
        return "Blue"
    default:
        return "Unknown"
    }
}

Constant block

const (
    Width  = 100
    Height = 200
    Area   = Width * Height // Can use other constants
)

Best practices

  • Use const for values that never change
  • Use iota for enumerations and bit flags
  • Group related constants in a const block
  • Use descriptive names for constants
  • Avoid magic numbers — use named constants

Mini Practice

Write Go code that:

  1. Declares constants for HTTP status codes
  2. Uses iota to create a bit flag enum
  3. Creates file size constants (KB, MB, GB)
  4. Defines an enum type with a String() method

Up Next

In the next lesson, you'll learn about Data Types — the full set of types in Go.

Related Topics

Frequently Asked Questions about Constants

What is Constants in Go?

Constants is a fundamental concept in Go. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Constants?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Constants.

Why is Constants important in Go?

Constants is essential for Go development. Understanding this concept will help you write better code and solve real-world problems more effectively.