- 代码片段一:输出结果有序
```
package main
import (
"fmt"
"time"
)
func main() {
ch := make(chan struct{})
for i := 0; i < 10; i++ {
go func(num int) {
for {
<-ch
fmt.Println(num)
}
}(i)
time.Sleep(time.Millisecond)
}
time.Sleep(time.Second)
for j := 0; j < 10; j++ {
ch <- struct{}{}
time.Sleep(time.Millisecond)
}
ch <- struct{}{}
time.Sleep(time.Minute)
}
```
- 代码片段二:输出结果无序
```
package main
import (
"fmt"
"time"
)
func main() {
ch := make(chan struct{})
for i := 0; i < 10; i++ {
go func(num int) {
for {
<-ch
fmt.Println(num)
}
}(i)
time.Sleep(time.Millisecond)
}
time.Sleep(time.Second)
for j := 0; j < 10; j++ {
ch <- struct{}{}
}
ch <- struct{}{}
time.Sleep(time.Minute)
}
```