What Are Typed vs Untyped Constants in Go

Typed constants have a fixed type at declaration, while untyped constants infer their type based on usage context, offering greater flexibility.

Typed constants in Go have an explicit type (like int or string), while untyped constants have no type until used in a context that requires one. Untyped constants are more flexible because the compiler infers the most specific type needed at the point of use, whereas typed constants are fixed immediately upon declaration.

// Untyped constant: type inferred as int64 when assigned to int64
const Pi = 3.14159
var radius int64 = Pi // Works: Pi becomes int64

// Typed constant: fixed as float64 immediately
const PiTyped float64 = 3.14159
// var radius int64 = PiTyped // Error: cannot use float64 as int64