3 ms·
I would love it if there were a keyword to check if an error return value received was non-nil, and then return it and default values for other return types if
by cjslep 10y ago
I would love it if there were a keyword to check if an error return value received was non-nil, and then return it and default values for other return types if so.
So:
if x, err := foo(); err != nil {
return 0, nil, err
}
becomes:
x := check foo()
with "check" being the keyword here.
- barsonme 10y agoSame. I've some spots in my code where I return three or four values (there's no logical reason to put them inside a struct...) and so I end up with a handful of if err != nil { return "", 0, err } _all_ _over_ _the_ _place_. What I also don't like (perhaps I'm just being too uptight) is return myStructType{}, nil The alternative is to use a named return parameter (which usually is good form), but it's still kinda gross, especially if it's a longer package + struct name.
- richard_todd 10y agoYou are not alone! I want things like this so much I've considered adding an m4/go-generate step into my projects to get them.