How do you create and use constants in Go?

Beginner

Answer

Constants are declared using the const keyword and cannot be changed after declaration:

const Pi = 3.14159
const MaxUsers = 100

// Grouped constants
const (
    StatusOK     = 200
    StatusError  = 500
    StatusPending = 202
)

// iota for auto-incrementing constants
const (
    Sunday = iota  // 0
    Monday         // 1
    Tuesday        // 2
)