Help us shape the future of Go! Take the 2026 Go Developer Survey

task

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 22, 2021 License: MIT Imports: 2 Imported by: 1

README

task

CI codecov Go Report Card

task is a simple library that helps to compose complex business logic from smaller steps in a maintainable way.

Quick Start

  1. Make sure Go is installed and import task package to your project via the below command:
$ go get -u github.com/bongnv/task
  1. Import it to your code:
import "github.com/bongnv/task"
  1. Use task.Exec to run multiple tasks in sequence:
	step1 := makeTask("Rinse the rice")
	step2 := makeTask("Use the right ratio of water")
	step3 := makeTask("Bring the water to a boil")
	step4 := makeTask("Maintain a simmer")
	step5 := makeTask("Cook without peeking or stirring")
	step6 := makeTask("Let the rice rest covered")

	err := task.Exec(
		ctx,
		step1,
		step2,
		step3,
		step4,
		step5,
		step6,
	)

Usages

There are two ways to compose tasks together:

  1. task.Sequence creates a new task by running multiple tasks in a sequence. It stops and returns error if any task returns error.
	composedTask := task.Sequence(
		step1,
		step2,
		step3,
	)
  1. task.Concurence creates a new task by running multiple tasks concurrently. It stops, cancels the provided context and returns error if any task return error.
	composedTask := task.Concurrence(
		step1,
		step2,
		step3,
	)

Documentation

Overview

Package task is a simple library that helps to compose complex business logic from smaller steps in a maintainable way.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Exec

func Exec(ctx context.Context, tasks ...Task) error

Exec executes a task or multiple tasks in a sequence. It returns error if there is any.

Types

type Task

type Task func(ctx context.Context) error

Task is a unit of work that is executable.

Example
package main

import (
	"context"
	"fmt"

	"github.com/bongnv/task"
)

func makeTask(text string) task.Task {
	return func(ctx context.Context) error {
		fmt.Println(text)
		return nil
	}
}

func main() {
	step1 := makeTask("Rinse the rice")
	step2 := makeTask("Use the right ratio of water")
	step3 := makeTask("Bring the water to a boil")
	step4 := makeTask("Maintain a simmer")
	step5 := makeTask("Cook without peeking or stirring")
	step6 := makeTask("Let the rice rest covered")

	err := task.Exec(
		context.Background(),
		step1,
		step2,
		step3,
		step4,
		step5,
		step6,
	)

	if err != nil {
		fmt.Println(err)
	}

}
Output:
Rinse the rice
Use the right ratio of water
Bring the water to a boil
Maintain a simmer
Cook without peeking or stirring
Let the rice rest covered

func Concurrence

func Concurrence(tasks ...Task) Task

Concurrence creates a new task by running multiple tasks concurrently.

func Sequence

func Sequence(tasks ...Task) Task

Sequence creates a new task by running multiple tasks in a sequence. It stops if any task returns error.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL