/*
按顺序打印数字
/
import (
"fmt"
"sync"
"time"
)
var Number struct {
num int
lock sync.RWMutex
}
var ch = make(chan int)
func main() {
// fun 1
for i := 0; i < 10; i++ {
go PirntNum()
}
time.Sleep(2 * time.Second)
fmt.Println("------------------------")
// fun2
for i := 0; i < 10; i++ {
go print2(i)
ch <- i
}
}
func PirntNum() {
Number.lock.Lock()
fmt.Println(Number.num)
Number.num++
Number.lock.Unlock()
}
func print2(i int) {
<-ch
fmt.Println(i)
}
有疑问加站长微信联系(非本文作者)