Constants
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
}
fn rgb(r int, g int, b int) Color {
return Color{
r: r
g: g
b: b
}
}
const (
numbers = [1, 2, 3]
red = Color{
r: 255
g: 0
b: 0
}
// evaluate function call at compile-time
blue = rgb(0, 0, 255)
)
println(numbers)
println(red)
println(blue)
Global variables are not allowed, so this can be really useful.
println('Top cities: $top_cities.filter(.usa)')