diff options
| author | sangtran <tranquangsang12.7@gmail.com> | 2026-09-03 23:10:33 +0700 |
|---|---|---|
| committer | sangtran <tranquangsang12.7@gmail.com> | 2026-09-03 23:10:33 +0700 |
| commit | 02123df8cc15896f97c1580e49d8e2a991e37ddd (patch) | |
| tree | d0c00bc3f65e5f5493a9dff8034c80640cb12960 /pipeline/example_test.go | |
| signature | Verified sangtran <tranquangsang12.7@gmail.com> Key: 8DD79823D235E1BB ultimate trust | |
| download | quacker-main.tar.gz quacker-main.zip | |
Diffstat (limited to 'pipeline/example_test.go')
| -rw-r--r-- | pipeline/example_test.go | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/pipeline/example_test.go b/pipeline/example_test.go new file mode 100644 index 0000000..e322529 --- /dev/null +++ b/pipeline/example_test.go | |||
| @@ -0,0 +1,53 @@ | |||
| 1 | package pipeline_test | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "context" | ||
| 5 | "fmt" | ||
| 6 | "sort" | ||
| 7 | |||
| 8 | "git.sangtran.io.vn/quacker.git/pipeline" | ||
| 9 | ) | ||
| 10 | |||
| 11 | func ExampleRun() { | ||
| 12 | input := make(chan int, 5) | ||
| 13 | for i := 1; i <= 5; i++ { | ||
| 14 | input <- i | ||
| 15 | } | ||
| 16 | close(input) | ||
| 17 | |||
| 18 | out := pipeline.Run(context.Background(), input, | ||
| 19 | pipeline.Filter(func(v int) bool { return v%2 == 0 }), | ||
| 20 | pipeline.Map(1, func(_ context.Context, v int) (int, bool) { | ||
| 21 | return v * 10, true | ||
| 22 | }), | ||
| 23 | ) | ||
| 24 | |||
| 25 | var results []int | ||
| 26 | for v := range out { | ||
| 27 | results = append(results, v) | ||
| 28 | } | ||
| 29 | sort.Ints(results) | ||
| 30 | fmt.Println(results) | ||
| 31 | // Output: [20 40] | ||
| 32 | } | ||
| 33 | |||
| 34 | func ExampleSink() { | ||
| 35 | input := make(chan string, 3) | ||
| 36 | input <- "hello" | ||
| 37 | input <- "world" | ||
| 38 | input <- "!" | ||
| 39 | close(input) | ||
| 40 | |||
| 41 | var collected []string | ||
| 42 | out := pipeline.Run(context.Background(), input, | ||
| 43 | pipeline.Sink(1, func(_ context.Context, v string) { | ||
| 44 | collected = append(collected, v) | ||
| 45 | }), | ||
| 46 | ) | ||
| 47 | |||
| 48 | for range out { | ||
| 49 | } | ||
| 50 | |||
| 51 | fmt.Println(collected) | ||
| 52 | // Output: [hello world !] | ||
| 53 | } | ||
