Skip to content

Commit 2a45572

Browse files
Use slices.Backward in go (#342)
1 parent 362ecf4 commit 2a45572

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

go/patterns-use-cases/src/sagas/bookingworkflow.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package main
22

33
import (
44
"context"
5+
"log"
6+
"slices"
7+
58
restate "github.com/restatedev/sdk-go"
69
"github.com/restatedev/sdk-go/server"
7-
"log"
810
)
911

1012
type BookingRequest struct {
@@ -51,7 +53,7 @@ func (BookingWorkflow) Run(ctx restate.Context, req BookingRequest) (err error)
5153
// Run compensations at the end if err != nil
5254
defer func() {
5355
if err != nil {
54-
for _, compensation := range compensations {
56+
for _, compensation := range slices.Backward(compensations) {
5557
if _, compErr := compensation(); compErr != nil {
5658
err = compErr
5759
}

go/tutorials/tour-of-orchestration-go/examples/sagas.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package examples
22

33
import (
44
"fmt"
5+
"slices"
56

67
"github.com/restatedev/sdk-go"
78
)
@@ -14,8 +15,8 @@ func (SubscriptionSaga) Add(ctx restate.Context, req SubscriptionRequest) (err e
1415
// Run compensations at the end if err != nil
1516
defer func() {
1617
if err != nil {
17-
for i := len(compensations) - 1; i >= 0; i-- {
18-
if compErr := compensations[i](); compErr != nil {
18+
for _, compensation := range slices.Backward(compensations) {
19+
if compErr := compensation(); compErr != nil {
1920
err = compErr
2021
}
2122
}

go/tutorials/tour-of-workflows-go/examples/sagas.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package examples
22

33
import (
4+
"slices"
5+
46
restate "github.com/restatedev/sdk-go"
57
)
68

@@ -14,8 +16,8 @@ func (SagasWorkflow) Run(ctx restate.WorkflowContext, user User) (res bool, err
1416
// All errors that end up here are terminal errors, so run compensations
1517
// (Retry-able errors got returned by the SDK without ending up here)
1618
if err != nil {
17-
for i := len(compensations) - 1; i >= 0; i-- {
18-
if compErr := compensations[i](); compErr != nil {
19+
for _, compensation := range slices.Backward(compensations) {
20+
if compErr := compensation(); compErr != nil {
1921
err = compErr
2022
}
2123
}

0 commit comments

Comments
 (0)