go 语言管道
package main
import (
"fmt"
"time"
)
func main() {
c := make(chan struct{}, 3)
c <- struct{}{}
fmt.Printf(" len(c) = %d\n", len(c))
fmt.Printf(" cap(c) = %d\n", cap(c))
c <- struct{}{}
fmt.Printf(" len(c) = %d\n", len(c))
fmt.Printf(" cap(c) = %d\n", cap(c))
c <- struct{}{}
fmt.Printf(" len(c) = %d\n", len(c))
fmt.Printf(" cap(c) = %d\n", cap(c))
close(c)
_, ok := <-c
fmt.Printf("ok=%v\n", ok)
_, ok = <-c
fmt.Printf("ok=%v\n", ok)
_, ok = <-c
fmt.Printf("ok=%v\n", ok)
_, ok = <-c
fmt.Printf("ok=%v\n", ok)
noBuffChan := make(chan struct{})
go func() {
time.Sleep(6 * time.Second)
<-noBuffChan
}()
go func() {
time.Sleep(3 * time.Second)
noBuffChan <- struct{}{}
}()
<-noBuffChan
fmt.Println("after <-noBuffChan")
noBuffChan <- struct{}{}
fmt.Println("end")
}