For exmaple
package main
import "fmt"
const s string = "constant"
func main() {
const s = 0
fmt.Println(s)
}
actually prints
0
Yet I declared it as "constant" before main.
I thought you were unable to change a constant. If this is not the case, why not use other types?
It's a new constant in the scope of main
. It doesn't change the one in the outer scope. Look up shadowing.
This program demonstrates it well:
package main
import "fmt"
func main() {
const a = 0
fmt.Println(a)
{
const a = 1
fmt.Println(a)
}
fmt.Println(a)
}
The output is as follows:
0
1
0