41

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)
1
  • 15
    ineffectual assignment to "cfg" means that the assignment to cfg has no effect, as you unconditionaly overwrite it later.
    – Volker
    Commented Jun 23, 2021 at 6:00

1 Answer 1

48

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
...
2
  • 4
    Alright, var cfg *utils.Config worked. Thanks. It was needed a declaration only.
    – Ishmeet
    Commented Jun 23, 2021 at 5:25
  • 2
    @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
    Commented Jun 23, 2021 at 9:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.