2012-10-23 22:45:23 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2012-11-06 02:00:35 +00:00
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"time"
|
2012-10-23 22:45:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func sum(a []int, c chan int) {
|
2012-11-06 02:00:35 +00:00
|
|
|
sum := 0
|
|
|
|
for _, v := range a {
|
|
|
|
sum += v
|
|
|
|
}
|
|
|
|
to_sleep := time.Duration(100*math.Abs(float64(sum))) * time.Millisecond
|
|
|
|
fmt.Printf("Sleeping %s...\n", to_sleep)
|
|
|
|
time.Sleep(to_sleep)
|
|
|
|
c <- sum
|
2012-10-23 22:45:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2012-11-06 02:00:35 +00:00
|
|
|
a := []int{7, 2, 8, -9, 4, 0}
|
2012-10-23 22:45:23 +00:00
|
|
|
|
2012-11-06 02:00:35 +00:00
|
|
|
c := make(chan int)
|
|
|
|
go sum(a[:len(a)/2], c)
|
|
|
|
go sum(a[len(a)/2:], c)
|
|
|
|
x := <-c
|
|
|
|
fmt.Printf("x = %d\n", x)
|
|
|
|
y := <-c
|
|
|
|
fmt.Printf("y = %d\n", y)
|
|
|
|
fmt.Println(x, y, x+y)
|
2012-10-23 22:45:23 +00:00
|
|
|
}
|