News for hackers. Stuff that matters.
Re:Down the Golang Nil Rabbit Hole by pishpash on Tuesday March 30, 2021 @02:51AM (#26631813)
Sure, with = (in this case the shorthand :=), what happens before can change how it works.

The second := in case 2 should have raised a visual alarm. The fact that := even assigned for err a second time means inference could have happened, like

  err = (*MyErr)(nil)
To catch this, you could have written the nil check like:

  fmt.Printf("err is nil: %v\n", (*MyErr)(err) == nil)
and the compiler will croak. Or, to prevent this, replicate the inference explicitly:

  fmt.Printf("err is nil: %v\n", err == (*MyErr)(nil))

There are no comments on this comment yet.