Skip to content

Instantly share code, notes, and snippets.

@nstogner
Last active May 24, 2017 00:39
Show Gist options
  • Save nstogner/57012ef4811a2ff749a45f7c5491dcc8 to your computer and use it in GitHub Desktop.
Save nstogner/57012ef4811a2ff749a45f7c5491dcc8 to your computer and use it in GitHub Desktop.
Go: Simple retry function (usage)
// 1. Prints: A
retry(3, time.Second, func() error {
fmt.Print("A")
return nil
})
// 2. Prints: BB
var i int
retry(3, time.Second, func() error {
i++
fmt.Print("B")
if i == 2 {
return stop{errors.New("stop it")}
}
return errors.New("keep going")
})
// 3. Prints: CCC
retry(3, time.Second, func() error {
fmt.Print("C")
return errors.New("not today")
})
@nstogner
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment