Skip to content
 
 

Latest commit

 

History

5 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

asyncwork

Execute tasks concurrently in golang.

Prepare the tasks

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}

Initialize the channel which will orchestrate canceling of task execution.

done := make(chan struct{})
defer close(done)

Execute the tasks

Start task execution:

resultChannel := worker.PerformTasks(tasks, done)

Get result from completed tasks

Loop over the channel with results.

for result := range resultChannel {
		switch result.(type) {
		case string:
			fmt.Println("Here is a string:", result.(string))
		case int:
			fmt.Println("Here is an integer:", result.(int))

About

Run tasks concurrently in golang

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages