go get -u github.com/JigneshSatam/parallel
Example code snippet: https://play.golang.org/p/uHBw49pwFwt
import (
"github.com/JigneshSatam/parallel"
)
- Let
employee
be a user-defined type - Suppose fetching
employee details
is an independent task and needs to be done be in parallel - Create a method
Execute()
foremployee
type - Signature of Execute method
Execute() interface{}
- Add the code to fetch the
employee details
in thisExecute()
method
// employee -> Let `employee` be a user-defined type
type employee struct {
name string
}
// Execute() -> Create `Execute() interface{}` method for employee type
func (e employee) Execute() interface{} {
return employeeDetails(e)
}
func employeeDetails(e employee) string {
return "Employee details Name: " + e.name
}
- Call
parallel.Run
by passing list of employees who's details to be fetched.
func Example() {
employees := []employee{employee{"foo"}, employee{"bar"}, employee{"baz"}}
// Call `parallel.Run()` to start parallel execution
outputChannel := parallel.Run(employees)
for op := range outputChannel {
// Cast `interface{}` to desired output type
output := op.(string)
fmt.Println(output)
}
}
// Unordered output:
// "Employee details Name: bar"
// "Employee details Name: foo"
// "Employee details Name: baz"
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update the tests as appropriate.
This project is licensed under the MIT License - see the LICENSE.md file for details