package main

// Channels 可以用于多个goroutine之间的数据交换
func main() {
	natural := make(chan int)
	square := make(chan int)

	// counter
	go func() {
		for x := 0; x <= 100; x++ {
			natural <- x
		}
		close(natural) // 关闭channel
	}()

	// squarer
	go func() {
		for {
			x, ok := <-natural
			if !ok {
				break // channel关闭，跳出循环
			}
			square <- x * x
		}
		close(square)
	}()

	// printer (in main goroutine)
	for {
		x, ok := <-square
		if !ok {
			break
		}
		println(x)
	}
}
