Untyped Constants Keep Arbitrary Precision
Untyped constants keep arbitrary precision until assignment, so one constant can initialize any integer type that can hold its value.
Code
package main
import "fmt"
// untyped constant: no type yet, arbitrary precision at compile time
const huge = 1 << 40
// typed constant: carries int and must fit it
const typedInt int = 1 << 20
func main() {
// an untyped constant assigns into any type that can hold its value
var n16 int16 = huge / (1 << 30) // 2^40 / 2^30 = 2^10 = 1024
fmt.Println("int16 sees:", n16)
var n64 int64 = huge
fmt.Println("int64 sees:", n64)
// a typed constant needs an explicit conversion for smaller types
var b byte = byte(typedInt / (1 << 13)) // 2^20 / 2^13 = 2^7 = 128
fmt.Println("byte from typed constant:", b)
// untyped arithmetic stays exact before the final assignment
fmt.Println("exact constant sum:", int64(huge+huge))
}What the code does
The short version: the work belongs to the function main, doing all the work.
What to watch for
- Only the runtime is needed — no packages to fetch and no installs.
Applying it
The run emits 4 labeled sections — int16 sees:, int64 sees:, byte from typed constant:, exact constant sum: — one per step.
Statements that carry the weight
var b byte = byte(typedInt / (1 << 13)) // 2^20 / 2^13 = 2^7 = 128
Interactive Tools for This Topic
cURL to Python & Go ConverterTranslate curl CLI commands with headers, auth, JSON bodies, and timeouts into ready-to-run Python requests and Go net/http client code snippets.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.
FAQ
- What does Untyped Constants Keep Arbitrary Precision print when run?
- Running the file directly is enough — expect labeled output lines such as `int16 sees:`, `int64 sees:`, `byte from typed constant:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.