Created
March 6, 2016 15:08
-
-
Save mertenvg/756afb259239485e84ea to your computer and use it in GitHub Desktop.
An example of handling errors using a task list
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