Go pipeline solution that can be used in many different combinations for chaining pipeline steps.
Inspired by @bilal-kilic's Kotlin implementation boru.
Supports 1.18+ Go versions because of Go Generics
go get github.com/mhmtszr/pipeline
package main
import (
"fmt"
"github.com/mhmtszr/pipeline"
)
type Square struct{}
type Add struct{}
func (s Square) Execute(context int, next func(context int)) error {
context = context * context
println(fmt.Sprintf("After first chain context: %d", context))
return next(context)
}
func (a Add) Execute(context int, next func(context int)) {
context = context + context
println(fmt.Sprintf("After second chain context: %d", context))
return next(context)
}
func main() {
p, _ := pipeline.Builder[int]{}.UsePipelineStep(Square{}).UsePipelineStep(Add{}).Build()
p.Execute(3)
}
// After first chain context: 9
// After second chain context: 18
p := pipeline.Builder[*int]{}.
UseConditionalStepBuilder(
pipeline.NewConditionalStepBuilder[*int]().
Condition(func(context *int) bool {
return *context == 3
}).
IfTrue(Square{}).
IfFalse(Add{}),
).UsePipelineStep(Add{}).Build()
nm := 3
_ = p.Execute(&nm)
// nm 18
nm = 4
_ = p.Execute(&nm)
// nm 16