Introduction
Go supports constants of character, string, boolean, and numeric values. Constants provide a way to give names to fixed values and ensure they cannot be changed during program execution.
The Code
// Go supports _constants_ of character, string, boolean,
// and numeric values.
package main
import (
"fmt"
"math"
)
// `const` declares a constant value.
const s string = "constant"
func main() {
fmt.Println(s)
// A `const` statement can also appear inside a
// function body.
const n = 500000000
// Constant expressions perform arithmetic with
// arbitrary precision.
const d = 3e20 / n
fmt.Println(d)
// A numeric constant has no type until it's given
// one, such as by an explicit conversion.
fmt.Println(int64(d))
// A number can be given a type by using it in a
// context that requires one, such as a variable
// assignment or function call. For example, here
// `math.Sin` expects a `float64`.
fmt.Println(math.Sin(n))
}
Understanding Constants
Package-Level Constants
const s string = "constant"
Constants can be declared at the package level, making them available throughout the package. You can optionally specify the type explicitly.
Function-Level Constants
Constants can also be declared inside functions. They follow the same rules but are scoped to that function.
Arbitrary Precision Arithmetic
const d = 3e20 / n
fmt.Println(d) // 600000000000
Constant expressions are computed with arbitrary precision at compile time. This allows for calculations that would overflow regular numeric types.
Constant arithmetic happens at compile time with unlimited precision. The result is only converted to a specific type when used in a context that requires it.
Untyped Constants
const n = 500000000 // untyped constant
Numeric constants don’t have a type until they’re used in a context that requires one. This provides flexibility in how they can be used.
Type Conversion
You can explicitly convert a constant to a specific type using type conversion syntax.
Implicit Typing
When you use a constant in a context that expects a specific type (like math.Sin expecting float64), Go automatically gives the constant that type.
Untyped constants can be used in more contexts than typed ones. For example, an untyped numeric constant can be used as either an int or a float64 depending on context.
Constant Types
Go supports constants of these types:
| Type | Example |
|---|
| String | const name = "Go" |
| Numeric | const pi = 3.14159 |
| Boolean | const debug = true |
| Character | const newline = '\n' |
Constants vs Variables
| Feature | Constants | Variables |
|---|
| Can change | No | Yes |
| Declaration | const | var or := |
| Evaluation | Compile time | Runtime |
| Arithmetic precision | Arbitrary | Type-limited |
Not all types can be constants. You can’t have constant slices, maps, or structs. Constants are limited to basic types.
Key Takeaways
- Use
const to declare values that won’t change
- Constants can be declared at package or function level
- Numeric constants support arbitrary-precision arithmetic
- Untyped constants are flexible and adopt types based on context
- Constant expressions are evaluated at compile time
- Constants can be strings, numbers, booleans, or characters