spec: prevent variable aliasing in any way (original) (raw)

the following code

func OpenLogFile(lo LogOptions) (io.WriteCloser, error) { var logw io.WriteCloser switch lo.LogPath { case "stderr": logw = os.Stderr default: logw, err := openLogFileInternal(lo) if err != nil { return logw, err } } log.SetOutput(logw) return logw, nil }

will alias the logw variable: since logw is declared outside, and in the default switch case another logw is created with :=, every default case will return a nil. Fun ensues.

Go should either prevent this behaviour with a panic/error/warning or should note that the variable is initialized in an external block and properly initialize it rather than creating a new one.