package main import "fmt" func sum(s []int, c chan int) { sum := 0 for _, v := range s { sum += v } c <- sum // send sum to c fmt.Println("c len: ", len(c)) } func testWithCap(capacity int) { s := []int{7, 2, 8, -9, 4, 0} c := make(chan int, capacity) go sum(s[:len(s)/2], c) go sum(s[len(s)/2:], c) x, y := <-c, <-c // receive from c assert(x + y == 12) fmt.Println(x, y, x+y) go func() {c <- 888}() nn, ok := <- c assert(nn == 888) assert(ok) close(c) assert(<-c == 0) fmt.Println(<-c) m := <- c assert(m == 0) n, ok := <- c assert(n == 0) assert(!ok) fmt.Println(m, n, nn) assert(cap(c) == capacity) } func main() { testWithCap(0) testWithCap(1) testWithCap(2) testWithCap(3) testWithCap(999) }