Newer
Older
01_hello / goroutine / pipeline.go
yhornisse on 9 Oct 2021 285 bytes add pipeline sample
package main

import (
	"fmt"
)

func main() {
	done := make(chan int)
	p := make(chan int)
	go func() {
		for i := 0; i < 5; i++ {
			p <- i
		}
	}()
	go func() {
		for {
			x := <- p
			done <- x * x
		}

	}()
	for i := 0; i < 5; i++ {
		fmt.Println(<-done)
		// 0, 1, 4, 9, 16
	}
}