skip to content
Alvin Lucillo

Constants in Go

/ 1 min read

💻 Tech

Constants in Go have 256 bits of precision and have parallel type system. This means a constant can hold values greater than standard types like float64 or int64 (64 bits) without needing to speify a specific type initially.

A constant can either have a kind (or untyped constants) or type (typed constants). Untyped constants are not bound to specific type until the context requires one.

const a = 100 // kind int
const b int = 100 // type int
const c int32 = 100 // type int32

const x = 100 // kind int
const y uint8 = 1000 // error: overflows; uint8 can only hold up to 1 byte of data only (i.e., 8 bits)

A kind can be promoted to another kind or type

const j = 1 // kind int
const k = j * 1.2 // j promoted to kind float
const l float64 = j // j promoted to type float64