You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Simply retry a function to execute for max 10 times with interval of 1 second
package main
import (
"fmt""time""github.com/thedevsaddam/retry"
)
funcmain() {
i:=1// lets assume we expect i to be a value of 8err:=retry.DoFunc(10, 1*time.Second, func() error {
fmt.Printf("trying for: %dth time\n", i)
i++ifi>7 {
returnnil
}
returnfmt.Errorf("i = %d is still low value", i)
})
iferr!=nil {
panic(err)
}
fmt.Println("Got our expected result: ", i)
}
We can execute function from other package with arguments and return values
package main
import (
"errors""log""time""github.com/thedevsaddam/retry"
)
funcdiv(a, bfloat64) (float64, error) {
ifb==0 {
return0, errors.New("Can not divide by zero")
}
returna/b, nil
}
funcmain() {
a:=20.6b:=3.7// if we assign 0.0 to b, it will cause an error and will retry for 3 timesres, err:=retry.Do(3, 5*time.Second, div, a, b)
iferr!=nil {
panic(err)
}
log.Println(res)
}
Contribution
If you are interested to make the package better please send pull requests or create an issue so that others can fix. Read the contribution guide here.
License
The retry is an open-source software licensed under the MIT License.