- A pipeline library for go.
中文 | English
go get -u github.com/deatil/go-pipeline
package main
import (
"fmt"
"github.com/deatil/go-pipeline/pipeline"
)
func main() {
// pipeline test
data := pipeline.NewPipeline().
Send("开始的数据").
Through(
func(data any, next pipeline.NextFunc) any {
old := data.(string)
old = old + ", 第1次数据1"
data2 := next(old)
data2 = data2.(string) + ", 第1次数据2"
return data2
},
func(data any, next pipeline.NextFunc) any {
old := data.(string)
old = old + ", 第2次数据1"
data2 := next(old)
data2 = data2.(string) + ", 第2次数据2"
return data2
},
&PipelineEx{},
).
ThenReturn()
fmt.Println(data)
// 输出: 开始的数据, 第1次数据1, 第2次数据1, struct 数据1, struct 数据2, 第2次数据2, 第1次数据2
// hub test
hub := pipeline.NewHub()
hub.Pipeline("hub", func(pipe pipeline.Pipeline, object any) any {
data := pipe.
Send(object).
Through(
func(data any, next pipeline.NextFunc) any {
old := data.(string)
old = old + ", 第1次数据1"
data2 := next(old)
data2 = data2.(string) + ", 第1次数据2"
return data2
},
).
ThenReturn()
return data
})
data2 := hub.Pipe("hub 测试", "hub")
fmt.Println(data2)
// 输出: hub 测试, 第1次数据1, 第1次数据2
}
/* ======================== */
// pipeline struct
type PipelineEx struct {}
func (this PipelineEx) Handle(data any, next pipeline.NextFunc) any {
old := data.(string)
old = old + ", struct 数据1"
data2 := next(old)
data2 = data2.(string) + ", struct 数据2"
return data2
}
- The library LICENSE is
Apache2
, using the library need keep the LICENSE.
- Copyright deatil(https://github.com/deatil).