Consider the following code
package main
import (
"fmt"
)
var global int = 40
func main() {
var global int = 30
fmt.Println(::global) // In C++ this would print 40
}
How can I print the global variable value 40? There are posts on SO about accessing global variables from different packages but I could not find anything about accessing globals within the same file/package
How can I print the global variable value 40?
By not shadowing it. Or if you absolutely must shadow it (but why would you?), re-assign it to another variable first.
func main() {
originalGlobal := global
var global int = 30
fmt.Println(originalGlobal)
}