程序如下,当num为奇数的时候,输出就会少了最后一个。
本人小白,刚接触go,希望能够有人解惑,非常感谢!
```
func main() {
var ch = make(chan int)
num := 11
go func() {
for i := 1; i <= num; i++ {
ch <- i
//fmt.Println("++", i)
}
close(ch)
}()
for ii := range ch {
val := ii
go func() {
fmt.Println("--", val)
}()
}
fmt.Println("程序运行结束")
}
```
输出结果:
```
-- 2
-- 1
-- 4
-- 3
-- 6
-- 5
-- 8
-- 7
-- 10
-- 9
程序运行结束
```
```
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/Iverson/Project/Go"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.7.3/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.7.3/libexec/pkg/tool/darwin_amd64"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/9x/fl5jm4yn53b4lwrnxlbsjvrm0000gn/T/go-build013247892=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
```
有疑问加站长微信联系(非本文作者)