5 ms·
But wouldn't that look like: var s Settings if s, err := load_settings(); err != nil { s = defaults() } Where is the unhandled error?
by Skunkleton 8y ago
But wouldn't that look like:
var s Settings
if s, err := load_settings(); err != nil {
s = defaults()
}
Where is the unhandled error?
- TheDong 8y agoNote that your code will not work; you've shadowed 's' inside the if block because you have 'if s, err := ...'. As a result, 's = defaults()' will set the shadowed 's' variable that only exists within the if block to defaults, but the 'var s Settings' you declared above will not be modified. Here's a playground showing the issue: https://play.golang.org/p/eE0HEZx1MJu https://play.golang.org/p/eE0HEZx1MJu Go is full of nice little foot-guns like that :) You would want to also have 'var err error' above the block and use '=' instead of ':=' to fix this sort of issue.
- Skunkleton 8y agoI am by no means an expert (or even a frequent user) of go. However, the most unfortunate foot cannon I have encountered is this: package main type HowOdd interface { Poke() string } type AConcreteThing struct { } func (t *AConcreteThing) Poke() string { return "hehe" } func DoAConcreteThing() *AConcreteThing { return nil } func DoAThing() HowOdd { return DoAConcreteThing() } func main() { lolWhat := DoAThing() if lolWhat != nil { panic("How could this happen?") } } I understand that the interface at the bottom is a non-null interface containing a null value, but...damn, that sucks.