https://github.com/mhmtszr/pipeline
Golang pipeline solution with generics.
https://github.com/mhmtszr/pipeline
chainofresponsibility go golang pipeline
Last synced: 3 months ago
JSON representation
Golang pipeline solution with generics.
- Host: GitHub
- URL: https://github.com/mhmtszr/pipeline
- Owner: mhmtszr
- License: mit
- Created: 2023-03-17T05:30:03.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2026-03-19T10:58:23.000Z (4 months ago)
- Last Synced: 2026-03-20T03:58:57.389Z (4 months ago)
- Topics: chainofresponsibility, go, golang, pipeline
- Language: Go
- Homepage:
- Size: 28.3 KB
- Stars: 33
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Pipeline [![GoDoc][doc-img]][doc] [![Build Status][ci-img]][ci] [![Coverage Status][cov-img]][cov] [![Go Report Card][go-report-img]][go-report]

Go pipeline solution that can be used in many different combinations for chaining pipeline steps.
Inspired by [@bilal-kilic](https://github.com/bilal-kilic)'s Kotlin implementation [boru](https://github.com/Trendyol/boru).
### Usage
Supports 1.22+ Go versions because of Go Generics
```
go get github.com/mhmtszr/pipeline
```
### Examples
#### Basic Pipeline
``` go
package main
import (
"fmt"
"github.com/mhmtszr/pipeline"
)
func square(ctx *int, next func(*int) error) error {
*ctx = (*ctx) * (*ctx)
fmt.Printf("After first step: %d\n", *ctx)
return next(ctx)
}
func add(ctx *int, next func(*int) error) error {
*ctx = (*ctx) + (*ctx)
fmt.Printf("After second step: %d\n", *ctx)
return next(ctx)
}
func main() {
p := pipeline.NewBuilder[*int]().Use(square).Use(add).Build()
nm := 3
_ = p.Execute(&nm)
}
// After first step: 9
// After second step: 18
```
#### Concurrent Pipeline
``` go
p := pipeline.NewBuilder[*atomic.Uint64]().
UseConcurrent(
func(ctx *atomic.Uint64) error {
ctx.Add(ctx.Load())
return nil
},
func(ctx *atomic.Uint64) error {
ctx.Add(ctx.Load())
return nil
},
).Build()
var nmb atomic.Uint64
nmb.Add(5)
_ = p.Execute(&nmb)
```
#### Conditional Pipeline
``` go
p := pipeline.NewBuilder[*int]().
UseConditional(
func(ctx *int) bool { return *ctx == 3 },
[]pipeline.StepFunc[*int]{square},
[]pipeline.StepFunc[*int]{add},
).Use(add).Build()
nm := 3
_ = p.Execute(&nm)
// nm == 18
nm = 4
_ = p.Execute(&nm)
// nm == 16
```
[doc-img]: https://godoc.org/github.com/mhmtszr/pipeline?status.svg
[doc]: https://godoc.org/github.com/mhmtszr/pipeline
[ci-img]: https://github.com/mhmtszr/pipeline/actions/workflows/build-test.yml/badge.svg
[ci]: https://github.com/mhmtszr/pipeline/actions/workflows/build-test.yml
[cov-img]: https://codecov.io/gh/mhmtszr/pipeline/branch/master/graph/badge.svg
[cov]: https://codecov.io/gh/mhmtszr/pipeline
[go-report-img]: https://goreportcard.com/badge/github.com/mhmtszr/pipeline
[go-report]: https://goreportcard.com/report/github.com/mhmtszr/pipeline