-3

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
shadyabhi
  • 16,675
  • 26
  • 80
  • 131

1 Answers1

3

In your example, := does in fact introduce a new local variable called level, which is according to the specification since the global scope is distinct from any function scope:

a short variable declaration may redeclare variables provided they were originally declared earlier in the same block (or the parameter lists if the block is the function body) with the same type

The reason you seeing the behavior that you describe is that none of your usages of level actually reference the global variable. The usage in main is the new local variable from :=, and the usage in print is the function parameter. Delete the parameter (both in the declaration and in the callsite), and you will see that print prints an empty string after print:.

Aasmund Eldhuset
  • 37,289
  • 4
  • 68
  • 81
  • 1
    I'll just add that the root cause is the need to declare `err`, so the tuple return by the `getLevel` function will declare both variable as new. If you declare the err before assigning it and if you stop using `:=` to replace it with a simple `=` then you will not override the global scope variable with a new one. – vdolez Jun 16 '23 at 23:32
  • 1
    @vdolez: Indeed. This is a major stumbling block in Go, and IMO, the decision to have `:=` redeclare variables from outer scopes is very bad language design (in an otherwise mostly well-designed language). – Aasmund Eldhuset Jun 17 '23 at 12:00