Last active
April 23, 2016 21:08
-
-
Save agonzalezro/ccd3c29a24149f7787ea to your computer and use it in GitHub Desktop.
"Better" way to handle Go errors - blog gist
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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