Skip to content

Instantly share code, notes, and snippets.

@mertenvg
Last active March 6, 2016 18:36
Show Gist options
  • Save mertenvg/6d9718029d2f4df4f237 to your computer and use it in GitHub Desktop.
Save mertenvg/6d9718029d2f4df4f237 to your computer and use it in GitHub Desktop.
An example of handling errors using a task list
package main
import (
"errors"
"fmt"
)
// Task represents a callable task to be executed
type Task func() error
// 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 {
err = t()
if err != nil {
return
}
}
return
}
func main() {
work := Work{
// task A
func() (err error) {
fmt.Println("A")
return
},
// task B
func() (err error) {
fmt.Println("B")
return
},
// task C
func() (err error) {
fmt.Println("C")
err = errors.New("I don't know why, but Fail!")
return
},
// task D
func() (err error) {
fmt.Println("D")
return
},
}
err := work.Do()
if err != nil {
fmt.Println(err.Error())
}
}
// http://play.golang.org/p/TdABljnoPt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment