package main import "fmt2" func sum(s []int, c chan int) { sum := 0 for _, v := range s { sum += v } c <- sum // send sum to c fmt2.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) fmt2.Println(x, y, x+y) go func() {c <- 888}() nn, ok := <- c assert(nn == 888) assert(ok) close(c) assert(<-c == 0) fmt2.Println(<-c) m := <- c assert(m == 0) n, ok := <- c assert(n == 0) assert(!ok) fmt2.Println(m, n, nn) assert(cap(c) == capacity) } func main() { testWithCap(0) testWithCap(1) testWithCap(2) testWithCap(3) testWithCap(999) }