不知道为何,第一串代码不加关键字go的时候,函数至少是运行的,打印输出正常,第二串代码想用并发的方式,加了go,就感觉函数根本没调用。没有打印输出。
1:普通
```
package main
import (
"fmt"
)
func main() {
for i := 10; i < 20; i++ {
func(i int) {
fmt.Println("i:", i)
}(i)
}
}
```
2:协程
```
package main
import (
"fmt"
)
func main() {
for i := 10; i < 20; i++ {
go func(i int) {
fmt.Println("i:", i)
}(i)
}
}
```
A complete program is created by linking a single, unimported package called the main package with all the packages it imports, transitively. The main package must have package name main and declare a function main that takes no arguments and returns no value.
func main() { … }
Program execution begins by initializing the main package and then invoking the function main. When that function invocation returns, the program exits. It does not wait for other (non-main) goroutines to complete.
#3
更多评论