38

Getting a lint warning ineffectual assignment to "cfg" at line cfg := &utils.Config{}. Why is that ?

    cfg := &utils.Config{}
    env := os.Getenv("TEST")
    if strings.EqualFold(env, "INT") {
        cfg = utils.GetIntConfig()
    } else {
        cfg = utils.GetConfig()
    }

    cgw.Cgw(cfg)
Ishmeet
  • 1,540
  • 4
  • 17
  • 34
  • 11
    `ineffectual assignment to "cfg"` means that the assignment to cfg has no effect, as you unconditionaly overwrite it later. – Volker Jun 23 '21 at 06:00

1 Answers1

44

After the following if statement, cfg is written, thus the value assigned to cfg using cfg := &utils.Config{} is never used. You are using an assignment where a declaration would do.

var cfg *utils.Config
...
Burak Serdar
  • 46,455
  • 3
  • 40
  • 59
  • 3
    Alright, `var cfg *utils.Config` worked. Thanks. It was needed a declaration only. – Ishmeet Jun 23 '21 at 05:25
  • @Ishmeet, note that the linter wasn't forcing you to use a `var` declaration; instead it hinted at that you might have a bug in your code—because the first assignment could be way more involved like assigning the return value of some function call which would return a value inintialized in a complicated way, and possibly depending on some input data. – kostix Jun 23 '21 at 09:08