Even though code used :=
in main function
scope, global level
was assigned the value from the return value of getLevel()
. Can someone explain with language spec, how this is a predictable and documented behavior. My idea is to better read the spec, which I'm clearly not doing.
Code: https://go.dev/play/p/4Pz0VL-2sNN
package main
import "fmt"
var level string
func main() {
level, err := getLevel()
fmt.Println(level, err)
print(level)
}
func getLevel() (string, error) {
return "info", nil
}
func print(level string) {
fmt.Printf("print: %s", level)
}
Output:-
info <nil>
print: info