Typed vs untyped constants
A constant is a compile-time value declared with const. Untyped constants keep their kind (integer, float, rune, string, bool) and adapt to the type you assign them into. Typed constants are locked to one type.
Detailed Explanation
const MaxRetries = 3 is not a variable you forgot to change. The compiler substitutes the value. You cannot take its address, you cannot increment it, and it does not exist in memory as a location. Constants are how Go gives you named numbers without the "this int happened to be 3" problem.
Untyped is the default and the useful one. const Timeout = 5 * time.Second is typed, because time.Second is a time.Duration. const Limit = 1 << 40 is an untyped integer. You can pass Limit to a function that wants int, int64, or uint64, as long as the value fits. That is why math.Pi can be assigned to float32 or float64. The constant carries a kind and a precise value, not a machine type.
Typed constants refuse to move. const n int32 = 1 is an int32. Passing n to a function that wants int is a compile error. You convert explicitly. Type a constant only when you want that refusal — for example a public API that must stay time.Duration.
Arithmetic happens at compile time with arbitrary precision. Untyped integer constants are not limited to 64 bits while they stay constants. const Huge = 1 << 100 is legal. Using Huge as an int is not, because it does not fit. The overflow check happens at the moment you give the constant a type, not when you write the expression.
iota lives in const blocks. It is a typed-vs-untyped story too: const ( A = iota; B ) produces untyped integers 0 and 1 unless you attach a type to the first line.
There are no constant maps, slices, or structs with slice fields. Constant expressions are booleans, strings, numbers, and iota expressions. A []string{"a"} is a variable.
What it costs. Nothing at runtime for the constant itself. The cost is in API design: an untyped exported constant is easier for callers; a typed one documents the unit (time.Duration, byte) and prevents unit mistakes.
Code Examples
package main
import "fmt"
const (
MaxRetries = 3 // untyped integer
Name = "shop" // untyped string
typed = int32(3) // typed
)
func takesInt(n int) { fmt.Println("int", n) }
func takes64(n int64) { fmt.Println("i64", n) }
func main() {
takesInt(MaxRetries)
takes64(MaxRetries) // legal: untyped 3 fits both
// takesInt(typed) // compile error: int32, not int
}const (
bit0 = 1 << iota // 1
bit1 // 2
bit2 // 4
)
const huge = 1 << 100
func fit() int64 {
// return huge // does not fit in int64
return 1 << 60
}import "time"
const Idle = 5 * time.Second // typed: time.Duration
func sleepIdle() { time.Sleep(Idle) }Common Gotchas
A typed constant will not convert implicitly
`const n int = 1` cannot be passed where `int32` is required without a conversion.
The value must fit when it becomes typed
`const n int8 = 1000` does not compile. The same value as an untyped constant is fine until you assign it.
You cannot take the address of a constant
`&MaxRetries` is a compile error. Put the value in a variable if you need a pointer.