concurrent

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2020 License: MIT Imports: 5 Imported by: 0

README

Package concurrent

run function concurrently.

Usage

package main

import (
	"errors"
	"log"
	"time"

	"github.com/QisFj/godry/concurrent"
)

func main() {
	log.Println("with error:")
	withErr()
	log.Println("no error:")
	noErr()
}

func withErr() {
	if err := concurrent.Do(
		func() error {
			time.Sleep(20 * time.Millisecond)
			return errors.New("error-1")
		},
		func() error {
			time.Sleep(20 * time.Millisecond)
			return errors.New("error-1")
		},
		func() error {
			time.Sleep(20 * time.Millisecond)
			return errors.New("error-2")
		},
	); err != nil {
		log.Printf("err: %s\n", err)
	} else {
		log.Println("no error")
	}
}

func noErr() {
	if err := concurrent.Do(
		func() error {
			time.Sleep(20 * time.Millisecond)
			return nil
		},
		func() error {
			time.Sleep(20 * time.Millisecond)
			return nil
		},
		func() error {
			time.Sleep(20 * time.Millisecond)
			return nil
		},
	); err != nil {
		log.Printf("err: %s\n", err)
	} else {
		log.Println("no error")
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Do

func Do(functions ...FuncMayError) error

func Foreach

func Foreach(slice interface{}, f func(i int, v interface{}) error) error

Types

type FuncMayError

type FuncMayError func() error

Jump to

Keyboard shortcuts

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