Skip to content

Instantly share code, notes, and snippets.

@agonzalezro
Last active April 23, 2016 21:08
Show Gist options
  • Select an option

  • Save agonzalezro/ccd3c29a24149f7787ea to your computer and use it in GitHub Desktop.

Select an option

Save agonzalezro/ccd3c29a24149f7787ea to your computer and use it in GitHub Desktop.
"Better" way to handle Go errors - blog gist
package main
import (
"errors"
"math/rand"
"time"
)
func main() {
rand.Seed(time.Now().UTC().UnixNano())
err := func() error {
if rand.Intn(100) > 50 {
return errors.New("I've failed miserably on the first step")
}
return errors.New("Now I did it on the second")
}()
if err != nil {
println(err.Error())
return
}
println("Everything is alright!")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment