https://github.com/daanv2/go-tasks
A package that allows you to easily parallelize tasks and chain them together. As well as allowing you to easily create a task that can be reused.
https://github.com/daanv2/go-tasks
concurrency go parallel-computing parallel-programming tasks
Last synced: about 1 year ago
JSON representation
A package that allows you to easily parallelize tasks and chain them together. As well as allowing you to easily create a task that can be reused.
- Host: GitHub
- URL: https://github.com/daanv2/go-tasks
- Owner: DaanV2
- License: mit
- Created: 2023-01-15T22:17:42.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-01-15T22:35:07.000Z (about 3 years ago)
- Last Synced: 2025-01-08T18:54:33.664Z (about 1 year ago)
- Topics: concurrency, go, parallel-computing, parallel-programming, tasks
- Language: Go
- Homepage:
- Size: 13.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Go Tasks


[](https://github.com/DaanV2/go-tasks/actions/workflows/go-checks.yml)
A package that allows you to easily parallelize tasks and chain them together.
As well as allowing you to easily create a task that can be reused.
## Examples
```golang
//State object
type User struct {
Email string
Name string
Age int
Scopes []string
}
//Example 1
//Create a new task around the state object
task := tasks.New[User]()
task.
//Add a tasks to the chain that will execute in order
Do(RetrieveFromCache).
Do(RetrieveFromDatabase).
//What to do if the tasks is successful
Then(UpdateCache).
// What to do when the task is done
Finally(func(user *User, ctx context.Context) error {
zap.S().Infof("User: %+v", user)
return nil
}).
//What to do if the tasks fails
OnError(func(user *User, ctx context.Context, err error) {
zap.S().Errorf("Error: %+v", err)
})
err := task.Run(context.Background())
//Example 2
task := NewWith[User](&User{Email: "foo@bar.com"})
task.
// If the first function returns an error, run the second function.
Do(IfElse(RetrieveFromCache, RetrieveFromDatabase)).
Then(UpdateCache)
//Example 3
task := tasks.New[User]()
task.
Do(RetrieveFromCache).
Do(RetrieveFromDatabase).
Then(UpdateCache).
Finally(func(user *User, ctx context.Context) error {
zap.S().Infof("User: %+v", user)
return nil
}).
OnError(func(user *User, ctx context.Context, err error) {
zap.S().Errorf("Error: %+v", err)
})
newTask := task.CopyFor(&User{Email: "foo@bar.com"})
//Example 4
next := task.Chain(New[User]())
next.
Do(ValidateUser).
Finally(func(user *User, ctx context.Context) error {
err := ctx.Value("error").(error)
if err != nil {
zap.S().Errorf("Error: %+v", err)
}
return nil
})
```