Yield pattern in Go
1func YieldFunction() <-chan int {
2 ch := make(chan int)
3 go func() {
4 defer close(ch)
5 for i := 0; i < 10; i++ {
6 ch <- i // Yield data to the consumer
7 }
8 }()
9 return ch
10}
11
12func main() {
13 data := YieldFunction()
14 for val := range data {
15 // Process data concurrently
16 fmt.Println(val)
17 }
18}
References
#golang #computer_science #pattern #concurrency #yield #goroutine