Constants
Examples
Filter
const PI = 3.14;
console.log(PI);
Go supports constants of character, string, boolean, and numeric values.
package main
// Declare two individual constants. Yes,
// non-ASCII letters can be used in identifiers.
const π = 3.1416
const Pi = π // equivalent to: Pi == 3.1416
// Declare multiple constants in a group.
const (
No = !Yes
Yes = true
MaxDegrees = 360
Unit = "radian"
)
func main() {
// Declare multiple constants in one line.
const TwoPi, HalfPi, Unit2 = π * 2, π * 0.5, "degree"
}
const (
pi = 3.14
world = '世界'
)
println(pi)
println(world)
// Constants are declared with const. They can only be defined at the module level (outside of functions).
// Constant values can never be changed.
// V constants are more flexible than in most languages. You can assign more complex values:
struct Color {
r int
g int
b int
}
pub fn (c Color) str() string { return '{$c.r, $c.g, $c.b}' }
fn rgb(r, g, b int) Color { return Color{r: r, g: g, b: b} }
const (
numbers = [1, 2, 3]
red = Color{r: 255, g: 0, b: 0}
blue = rgb(0, 0, 255)
)
println(numbers)
println(red)
println(blue)