errgroup

package module
v0.0.0-...-1c3cb7f Latest Latest
Warning

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

Go to latest
Published: May 20, 2021 License: MIT Imports: 3 Imported by: 0

README

errgroup

Go Report Card GoDoc

Provide synchronization, error propagation, and Context cancelation for groups of goroutines working on subtasks of a common task. This package highly inspired by errgroup.

Requirement

Go 1.15

Installing

go get github.com/hlts2/errgroup

Example


package main

import (
	"fmt"
	"net/http"

	"github.com/hlts2/errgroup"
)

func main() {
	var urls = []string{
		"http://www.golang.org/",
		"http://www.google.com/",
		"http://www.somestupidname.com/",
	}

	eg := new(errgroup.Group)
	for _, url := range urls {
		url := url
		eg.Go(func() error {
			resp, err := http.Get(url)
			if err != nil {
				return err
			}
			defer resp.Body.Close()

			return nil
		})
	}

	err := eg.Wait()
	if err != nil {
		if err, ok := err.(errgroup.Error); ok {
			fmt.Println(err.Errors()) // slice of errors that occurred inside eg.Go
		}
	} else {
		fmt.Println("Successfully fetched all URLs.")
	}
}

Contribution

  1. Fork it ( https://github.com/hlts2/errgroup/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Author

hlts2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error struct {
	// contains filtered or unexported fields
}

Error is a custom error type to track multiple errors.

func (Error) Error

func (e Error) Error() string

Error returns a string concatenation of multiple errors.

func (Error) Errors

func (e Error) Errors() []error

Errors returns the error of the Go method as a slice.

type Group

type Group struct {
	// contains filtered or unexported fields
}

Group is a collection of goroutines working on subtasks that are part of the same overall task.

A zero Group is valid and does not cancel.

func WithContext

func WithContext(ctx context.Context) (*Group, context.Context)

WithContext returns a new Group and an associated Context derived from ctx.

func (*Group) Go

func (g *Group) Go(f func() error)

Go calls the given function in a new goroutine.

func (*Group) Wait

func (g *Group) Wait() error

Wait blocks until all function calls from the Go method have returned, then returns the Error object if they returns an error.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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