按顺序打印自然数字 Print Natural Number
题目:按照顺序打印自然数字
package main
import (
"fmt"
"sync"
)
var (
wg sync.WaitGroup
)
func print(ch *chan int, i int) {
defer wg.Done()
*ch <- i
}
func main() {
maxInt := 7
wg.Add(maxInt)
ch := make(chan int)
for i := 0; i < maxInt; i++ {
go print(&ch, i)
fmt.Println(<-ch) // 这里卡住等待第i个goroutine运行,然后打印
}
wg.Wait()
fmt.Println("done")
}