import (
"fmt"
)
var j int = 0
func Count(ch chan int) {
j++
ch <- j
fmt.Printf("Counting:%d\n", j)
}
func main() {
chs := make(chan int)
for i := 0; i < 10; i++ {
go Count(chs)
}
for i := 0; i < 10; i++ {
fmt.Println(<-chs)
}
}
为什么输出的结果为:
Counting:1
1
2
3
4
5
6
7
8
9
10
为什么只有一次Counting的输出?有谁能给解释一下吗
package main
import (
"fmt"
)
var j int = 0
func Count(ch chan int) {
j++
fmt.Printf("Counting:%d\n", j)
ch <- j
}
func main() {
chs := make(chan int)
for i := 0; i < 10; i++ {
go Count(chs)
}
for i := 0; i < 10; i++ {
fmt.Println(<-chs)
}
}
#2
更多评论
package main
import (
"fmt"
)
var j int = 0
func Count(ch chan int) {
j++
ch <- j
fmt.Printf("Counting:%d\n", j)
}
func main() {
chs := make(chan int)
for i := 0; i < 10; i++ {
go Count(chs)
}
for i := 0; i < 10; i++ {
fmt.Println(<-chs)
}
}
#1