百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
A

async

> 编程语言
开源

用于 Golang 的异步工具。

100 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

用于 Golang 的异步工具。

async

async is a utility library for manipulating synchronous and asynchronous flows.

Install

bash
$ go get github.com/rafaeldias/async

Concurrent

go
async.Concurrent(tasks taskier) (Results, error)

Concurrent will execute all the functions concurrently using goroutines.

  • tasks is an internal interface which accept two async public types:
    • Tasks is a list of functions to be executed concurrently.
    • MapTasks is a map of string of functions to be executed concurrently.

The type of Results value depends on the type of tasks passed to the function. See type Results

All errors ocurred in the functions will be returned. See returning error and type Errors.

Parallel

go
async.Parallel(tasks taskier) (Results, error)

Parallel will execute all the functions in parallel. It creates multiple goroutines and distributes the functions execution among them. The number of goroutines defaults to GOMAXPROCS. If the number of active goroutines is equal to GOMAXPROCS and there're more functions to execute, these functions will wait until one of functions being executed finishes its job.

  • tasks is an internal interface which accept two async public types:
    • async.Tasks is a list of functions to be executed in parallel.
    • async.MapTasks is a map of string of functions to be executed in parallel.

The type of Results value depends on the type of tasks passed to the function. See type Results.

All errors ocurred in the functions will be returned. See returning error and type Errors.

Waterfall

go
async.Waterfall(tasks Tasks, args ...interface{}) ([]interface[}, error)

Waterfall will execute all the functions in sequence, each returning their results to the next. If the last returning value of the function is of type error, then this value will not be passed to the next function.

  • tasks is a list of functions that will be executed in series.
  • args are optional parameters that will be passed to the first task.

Waterfall returns the results of the last task as []interface{} and error.

If an error occur in any of the functions to be executed, the next function will not be executed, and the error will be returned to the caller. See returning error.

Type Results

The Results is the type that is returned by Parallel and Concurrent:

go
type Results interface {
      Index(int) []interface{}  // Gets values by index
      Key(string) []interface{} // Gets values by key
      Len() int                 // Gets the length of the results
      Keys() []string           // Gets the keys of the results
}

The underlying type of Results will be different depending on the type of tasks passed to either Parallel or Concurrent. There are two async public types that can be passed to both functions:

  • Tasks is a list of functions that will be executed.
  • MapTasks is a map of string of functions to be executed.

When using Tasks, the underlying Results will be [][]interface{}:

go
res, err : = async.Parallel(async.Tasks{
        func1,
        func2,
        ...funcN,
})

We can get the results of the the second function:

go
res.Index(1)

Or we can iterate over the results using the Len() method:

go
for i := 0; i  0 {
                    // This line will interrupt the execution flow
                    return "", errors.New("Error occurred")
                }
                return "Ok", nil
        },
        // This function will not be executed.
        func (s string) {
            return
        }
});

if err != nil {
      fmt.Println(err.Error()); // "Error occurred"
}

Examples

Waterfall

go
import (
        "fmt"

        "github.com/rafaeldias/async"
)

func fib(p, c int) (int, int) {
  return c, p + c
}

func main() {

        // execution in series.
        res, e := async.Waterfall(async.Tasks{
                fib,
                fib,
                fib,
                func(p, c int) (int, error) {
                        return c, nil
                },
        }, 0, 1)

        if e != nil {
              fmt.Printf("Error executing a Waterfall (%s)\n", e.Error())
        }

        fmt.Println(res[0].(int)) // Prints 3
}

Parallel

go
import (
        "fmt"
        "time"

        "github.com/rafaeldias/async"
)

func main() {

        res, e := async.Parallel(async.MapTasks{
                "one": func() int {
                        for i := 'a'; i = 'a'; i-- {
                                fmt.Printf("%c ", i)
                        }
                        
                        return 3
                },
        })

        if e != nil {
                fmt.Printf("Errors [%s]\n", e.Error())
        }
        
        fmt.Println("Results from task 'two': %v", res.Key("two"))
}

Concurrent

…

License

Distributed under MIT License. See LICENSE file for more details.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Goasyncconcurrencygoroutineparallel

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言