Home [golang 비동기 패턴] Pipeline 패턴
Post
Cancel

[golang 비동기 패턴] Pipeline 패턴

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main

import "fmt"

func worker(in chan int, do func(int) int) chan int {
	out := make(chan int)
	go func() {
		defer close(out)
		for i := range in {
			out <- do(i)
		}
	}()
	return out
}

func run(in chan int) chan int {
	for i := 0; i < 5; i++ {
		in = worker(in, func(num int) int {
			fmt.Println("Data process", num)
			return num + 1
		})
	}

	return in
}

func main() {
	in := make(chan int)
	out := run(in)

	go func() {
		for o := range out {
			fmt.Println("Data out", o)
		}
	}()

	// for i := 0; i < 5; i++ {
	// 	fmt.Println("Data in", i)
	// 	in <- i
	// }

	// for {

	// }
}
This post is licensed under CC BY 4.0 by the author.

[함수형 프로그래밍] 2. 액션에서 계산 분리하기

[번역] Go Style Guide

Comments powered by Disqus.