Created
March 6, 2016 18:37
-
-
Save mertenvg/57d9da9acb0010840fa0 to your computer and use it in GitHub Desktop.
Another example using a task list to handle errors
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" | |
"fmt" | |
) | |
// Task represents a callable task to be executed | |
type Task func() | |
// Work slice of consecutive tasks that each may or | |
// may not have a single point of failure | |
type Work []Task | |
// Do the work | |
func (w Work) Do(err *error) { | |
for _, t := range w { | |
t() | |
if *err != nil { | |
return | |
} | |
} | |
return | |
} | |
func main() { | |
var err error | |
work := Work{ | |
// task A | |
func() { | |
fmt.Println("A") | |
}, | |
// task B | |
func() { | |
fmt.Println("B") | |
}, | |
// task C | |
func() { | |
fmt.Println("C") | |
err = errors.New("I don't know why, but Fail!") | |
}, | |
// task D | |
func() { | |
fmt.Println("D") | |
}, | |
} | |
work.Do(&err) | |
if err != nil { | |
fmt.Println(err.Error()) | |
} | |
} | |
// http://play.golang.org/p/RgJ5PipuG0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment