[13th][Day11] errgroup

既然已经有了 waiting group
为什麽还需要 error group 呢?

Using errgroup
As you may have surmised from the code comments, we have a problem—no error propagation. Any of these individual network calls might fail, time out, or otherwise produce an error. In the real world, returning partial results is often better than failing completely, but for our example, let’s assume that if any part of the process fails, we’d like to immediately exit the entire operation and return the error for whatever piece failed.

https://www.fullstory.com/blog/why-errgroup-withcontext-in-golang-server-handlers/

昨天我的 goroutine 中并没有任何的错物回传,在 goroutine 的每一次执行当中都是有『机会』发生错误的,如果要捞出错误就必须另外存一个 chan 并塞入 error

在现实世界当中,『部分成功』通常比『完全失败』来得好(当然这要看情境,有时候部分成功但程序继续往下执行的话造成的麻烦可能後续要补的动作变得更加繁复,这个先不讨论)

errgroup 的概念是如果有任何一个 goroutine 失败的话就要『停止全部任务』并『全部取消』

Package errgroup provides synchronization, error propagation, and Context cancelation for groups of goroutines working on subtasks of a common task.

https://pkg.go.dev/golang.org/x/sync/errgroup

使用到 WithContext 方法

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

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

https://pkg.go.dev/golang.org/x/sync/errgroup#WithContext
WithContext 返回一个新的 Group 并与往前 往後的 ctx 产生关联性

相关范例:https://pkg.go.dev/golang.org/x/sync/errgroup#example-Group-JustErrors


<<:  Day06 Flutter 启动流程

>>:  Episode 6 - BMI 计算机之我会瘦下来的!

[DAY24] Azure Machine Learning SDK 的 ScriptRunConfig

DAY24 Azure Machine Learning SDK 的 ScriptRunConfig...

PHP 一些概念

汇入 require: 可汇入多次, 如果出现错误, 会出现提醒, 并停止代码运行 include:...

Day 6 - 目前(传统)的机器学习三步骤(1)-收集数据

如前所言,假设 y是结果(如股票价格) , x是变数(如进料成本,薪资成本......等) , 以机...

Youtube Analytics API 教学 - OAuth2.0 开放授权 (1)

「鲑鱼均,因为一场鲑鱼之乱被主管称为鲑鱼世代,广义来说以年龄和脸蛋分类的话这应该算是一种 KNN 的...

Day#28 上传照片(2)+Debug

前言 接着呢,我们继续把上传照片的功能做出来。首先是希望能在注册的时候,能够取得相簿的照片,然後把他...