Constants
1. const (
2. pi = 3.14
3. world = '世界'
4. )
6. println(pi)
7. 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:
1. struct Color {
2. r int
3. g int
4. b int
5. }
7. fn rgb(r int, g int, b int) Color {
8. return Color{
9. r: r
10. g: g
11. b: b
12. }
13. }
15. const (
16. numbers = [1, 2, 3]
17. red = Color{
18. r: 255
19. g: 0
20. b: 0
21. }
22. // evaluate function call at compile-time
23. blue = rgb(0, 0, 255)
24. )
26. println(numbers)
27. println(red)
28. println(blue)
Global variables are not allowed, so this can be really useful.
1. println('Top cities: $top_cities.filter(.usa)')
