package main
import (
"fmt"
"time"
)
func main() {
for i := 1; i <= 10; i++ {
go func(i int) {
fmt.Println(2*i - 1)
}(i)
}
for i := 1; i <= 10; i++ {
go func(i int) {
fmt.Println(2 * i)
}(i)
}
time.Sleep(3 * time.Second)
}
修正
package main
import (
"fmt"
)
func main() {
a := make(chan int)
b := make(chan int)
exit := make(chan int)
go func() {
for i := 1; i <= 20; i++ {
fmt.Println(<-a)
i++
b <- i
}
}()
go func() {
for i := 0; i < 20; i++ {
i++
a <- i
fmt.Println(<-b)
}
close(exit)
}()
<-exit
}