I have a GoLang function that tries to set some named return variables inside a FOR loop. It looks something like this:
func (dbs *myClient) DoStuff() (response []byte, e error) {
for i:=0;i<5;i+=1 {
response, e = dbs.doNetworkThing()
if e == nil {
break;
}
}
return response, e
}
Basically what I'd like to do is set the return variables response
and e
to first result from doNetworkThing() that does not fail.
The problem is that response
and e
get reset once I exit from the for loop. What's the correct idiomatic way of doing something like this in GoLang?
I really want to use named return variables since they make things easier to read (I would consider something like this to be an ugly solution).
// Seems to be an ugly solution - would prefer using named return vars
func (dbs *myClient) DoStuff() ([]byte, error) {
var response []byte
var e error
for i:=0;i<5;i+=1 {
response, e := dbs.doNetworkThing()
if e == nil {
break;
}
}
return response, e
}