4 ms·
I always fall back on this pattern when the return values need to be used further down: foo, err := doThing(bar) if err != nil { // handle error }
by jodoherty 6y ago
I always fall back on this pattern when the return values need to be used further down:
foo, err := doThing(bar)
if err != nil {
// handle error
}
foo2, err := doThing(bar)
if err != nil {
// handle error
}
Only one value in a multi-valued assignment has to be undeclared to use the short variable declaration operator (:=). If the variable already exists in the current scope and the type matches, it will get reused.
https://play.golang.org/p/qo141lT-Jjp https://play.golang.org/p/qo141lT-Jjp
- Ashanmaril 6y ago>If the variable already exists in the current scope and the type matches, it will get reused. That's good to know. I suppose that would probably be the cleanest way of doing it. Thank you!