diff --git a/go/patterns-use-cases/src/sagas/bookingworkflow.go b/go/patterns-use-cases/src/sagas/bookingworkflow.go index d0eb4b46..32ae1585 100644 --- a/go/patterns-use-cases/src/sagas/bookingworkflow.go +++ b/go/patterns-use-cases/src/sagas/bookingworkflow.go @@ -2,9 +2,11 @@ package main import ( "context" + "log" + "slices" + restate "github.com/restatedev/sdk-go" "github.com/restatedev/sdk-go/server" - "log" ) type BookingRequest struct { @@ -51,7 +53,7 @@ func (BookingWorkflow) Run(ctx restate.Context, req BookingRequest) (err error) // Run compensations at the end if err != nil defer func() { if err != nil { - for _, compensation := range compensations { + for _, compensation := range slices.Backward(compensations) { if _, compErr := compensation(); compErr != nil { err = compErr } diff --git a/go/tutorials/tour-of-orchestration-go/examples/sagas.go b/go/tutorials/tour-of-orchestration-go/examples/sagas.go index abf80a14..1a425d91 100644 --- a/go/tutorials/tour-of-orchestration-go/examples/sagas.go +++ b/go/tutorials/tour-of-orchestration-go/examples/sagas.go @@ -2,6 +2,7 @@ package examples import ( "fmt" + "slices" "github.com/restatedev/sdk-go" ) @@ -14,8 +15,8 @@ func (SubscriptionSaga) Add(ctx restate.Context, req SubscriptionRequest) (err e // Run compensations at the end if err != nil defer func() { if err != nil { - for i := len(compensations) - 1; i >= 0; i-- { - if compErr := compensations[i](); compErr != nil { + for _, compensation := range slices.Backward(compensations) { + if compErr := compensation(); compErr != nil { err = compErr } } diff --git a/go/tutorials/tour-of-workflows-go/examples/sagas.go b/go/tutorials/tour-of-workflows-go/examples/sagas.go index 38285908..5403fd47 100644 --- a/go/tutorials/tour-of-workflows-go/examples/sagas.go +++ b/go/tutorials/tour-of-workflows-go/examples/sagas.go @@ -1,6 +1,8 @@ package examples import ( + "slices" + restate "github.com/restatedev/sdk-go" ) @@ -14,8 +16,8 @@ func (SagasWorkflow) Run(ctx restate.WorkflowContext, user User) (res bool, err // All errors that end up here are terminal errors, so run compensations // (Retry-able errors got returned by the SDK without ending up here) if err != nil { - for i := len(compensations) - 1; i >= 0; i-- { - if compErr := compensations[i](); compErr != nil { + for _, compensation := range slices.Backward(compensations) { + if compErr := compensation(); compErr != nil { err = compErr } }