Execute tasks concurrently in golang.
go get -u github.com/nbys/asyncwork/worker
The task is a function with following signature:
type TaskFunction func() interface{}
It can be declared like this:
task1 := func() interface{} {
time.Sleep(time.Second * 4)
fmt.Println("very slow function")
return "I'm ready"
}
All task have to be collected into the slice:
tasks := []worker.TaskFunction{task1, task2, task3}
Use context.Context to stop running goroutines
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Start task execution:
resultChannel := worker.PerformTasks(ctx, tasks)
Loop over the channel with results.
for result := range resultChannel {
switch result.(type) {
case error:
fmt.Println("Received error")
cancel()
return
case string:
fmt.Println("Here is a string:", result.(string))
case int:
fmt.Println("Here is an integer:", result.(int))
default:
fmt.Println("Some unknown type ")
}
}