Constants
Constants hold values that do not change. They are great for configuration, sizes, and enumerations. One common use for constants is declaring an API_KEY.
Key Points
- Constants are declared with
constand cannot be changed. - Numeric constants are high-precision until given a type.
- You can use constants in expressions; the compiler folds them.
package main
import "fmt"
import "math"
const s string = "constant"
func main() {
fmt.Println(s)
const pi = 3.14
const n = 500000000
const d = 3e20 / n
fmt.Println(math.Sqrt(pi))
fmt.Println(d)
fmt.Println(int64(d))
fmt.Println(math.Sin(n))
}constant
1.772004514666935
60000000000
-0.28470407323754404
Constants Reference
| Constant | Type | Value | Description |
|---|---|---|---|
s | string | "constant" | String constant with explicit type |
pi | untyped float | 3.14 | Mathematical constant for π |
n | untyped int | 500000000 | Large numeric constant |
d | untyped float | 3e20 / n | Computed constant expression |