mirror of
https://github.com/samber/lo.git
synced 2026-04-22 15:37:14 +08:00
fa095e4b4f
* fix(doc): fix go playground demo URL * fix(doc): add more go playground demo URL
1.2 KiB
1.2 KiB
name, slug, sourceRef, category, subCategory, playUrl, variantHelpers, similarHelpers, position, signatures
| name | slug | sourceRef | category | subCategory | playUrl | variantHelpers | similarHelpers | position | signatures | |||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| NewTransaction | newtransaction | retry.go#L253 | core | concurrency | https://go.dev/play/p/7B2o52wEQbj |
|
|
30 |
|
Creates a new Saga transaction that chains steps with rollback functions.
Use Then to add steps with exec and rollback functions. Call Process with an initial state to execute the pipeline; if a step returns an error, previously executed steps are rolled back in reverse order using their rollback functions.
type Acc struct{ Sum int }
tx := lo.NewTransaction[Acc]().
Then(
func(a Acc) (Acc, error) {
a.Sum += 10
return a, nil
},
func(a Acc) Acc {
a.Sum -= 10
return a
},
).
Then(
func(a Acc) (Acc, error) {
a.Sum *= 3
return a, nil
},
func(a Acc) Acc {
a.Sum /= 3
return a
},
)
res, err := tx.Process(Acc{Sum: 1})
// res.Sum == 33, err == nil