6

golang 使用 errgroup 停止其他 goroutine

 3 years ago
source link: https://www.v2ex.com/t/799216
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  Go

golang 使用 errgroup 停止其他 goroutine

  Zach369 · 19 小时 14 分钟前 · 685 次点击

目前的需求是这样的

func main() {
	errg := new(errgroup.Group)
	errg.Go(func() error {
		return test1()
	})
	errg.Go(func() error {
		return test2()
	})
	if err := errg.Wait(); err != nil {
		log.Error(err)
	}
}

func test1() error {
	return nil
}

func test2() error {
	return errors.New("test2 err")
}

我如何控制 go 中的协程 有一个错误,其他 go 协程就不会执行? 同时大家如何控制超时的那? 希望能给个范例


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK