package main
import "fmt"
const name = "Yosua"
// or var name string = "James"
func main() {
name := "Jobs"
fmt.Println(name)
}
How to refer to the constant and not the the function level variable?
package main
import "fmt"
const name = "Yosua"
// or var name string = "James"
func main() {
name := "Jobs"
fmt.Println(name)
}
How to refer to the constant and not the the function level variable?
You can't. While the local variable
name
is in scope, the namename
denotes the local variable. And there is no "qualifier" to refer to top-level identifiers.Spec: Declarations and scope:
If you need to access both the top-level constant/variable and the local variable at the same time, use different names.
If for some reason you can't or don't want to, you may save the value of the top-level constant or variable first:
Or you may provide other means to access it, e.g. a function:
Output in both cases (try them on the Go Playground):