box-o-sand/oldstuff/gotime/gotour-artifacts/channels/main.go
2015-06-22 13:15:42 -05:00

32 lines
496 B
Go

package main
import (
"fmt"
"math"
"time"
)
func sum(a []int, c chan int) {
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
}
func main() {
a := []int{7, 2, 8, -9, 4, 0}
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)
}