-
Notifications
You must be signed in to change notification settings - Fork 0
/
routine_demo.go
39 lines (33 loc) · 924 Bytes
/
routine_demo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package main
import (
"fmt"
"runtime"
"time"
)
var result chan int
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
go func() {
result = make(chan int)
defer close(result)
sum := 0
println("This is child process !!!")
for i := 0; i < 1000; i++ {
sum += i
}
// time.Sleep(2 * time.Second)
result <- sum
}()
//time.Sleep(2 * time.Second)
// println("This is parent process !!!")
for {
time.Sleep(time.Second)
if r, ok := <-result; ok == true {
fmt.Printf("The result is %d\n", r)
break
} else {
println("Not ready")
}
}
runtime.Goexit()
}