Last active
August 29, 2015 14:05
-
-
Save holys/e585d2993cc5ff99887f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
import "sync" | |
var wg sync.WaitGroup | |
func f1() { | |
f := func(i int) { | |
println(i) | |
wg.Done() | |
} | |
wg.Add(10) | |
for i := 0; i <10; i++ { | |
go f(i) | |
} | |
wg.Wait() | |
} | |
func f2() { | |
wg.Add(10) | |
for i := 0; i < 10; i++ { | |
//大哥 这里是异步的啊。协程还没开始, for loop已经结束了 | |
go func(){ | |
println(i) | |
wg.Done() | |
}() | |
} | |
wg.Wait() | |
} | |
func main() { | |
f1() | |
f2() | |
fmt.Println("Hello, 世界") | |
} | |
0 | |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
10 | |
10 | |
10 | |
10 | |
10 | |
10 | |
10 | |
10 | |
10 | |
Hello, 世界 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// You can edit this code! | |
// Click here and start typing. | |
package main | |
import "fmt" | |
import "sync" | |
var wg sync.WaitGroup | |
func f1() { | |
//pointer | |
f := func(i *int) { | |
println(*i) | |
wg.Done() | |
} | |
wg.Add(10) | |
for i := 0; i <10; i++ { | |
go f(&i) | |
} | |
wg.Wait() | |
} | |
func main() { | |
f1() | |
fmt.Println("Hello, 世界") | |
} | |
10 | |
10 | |
10 | |
10 | |
10 | |
10 | |
10 | |
10 | |
10 | |
10 | |
Hello, 世界 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment