c := make(chan int)
c := make(chan string, 10)
x, ok = <- c
var c chan string
c <- "Hello, World!" 向一个Channel发送一个值
var c chan string
fmt.Println(<-c) 从一个Channel中接收一个值
var c = make(chan string, 1)
c <- "Hello, World!"
close(c)
c <- "Hello, Panic!"
var c = make(chan int, 2)
c <- 1
c <- 2
close(c)
for i := 0; i < 3; i++ {
fmt.Printf("%d ", <-c)
}
for i := range ch { ch关闭时,for循环会自动结束
println(i)
}
select {
case <- time.After(time.Second*2):
println("read channel timeout")
case i := <- ch:
println(i)
}
select {
case <- time.After(time.Second *2):
println("write channel timeout")
case ch <- "hello":
println("write ok")
}
腾图小抄 SCWY.net v0.03 小抄561条 自2022-01-02访问366340次