Files
bulma/make_channel.go

21 lines
551 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package main
import "fmt"
func main() {
// 示例1:带缓冲通道(容量2
ch := make(chan int, 2)
ch <- 100
ch <- 200
fmt.Printf("ch: 长度=%d, 容量=%d\n", len(ch), cap(ch)) // ch: 长度=2, 容量=2
fmt.Println(<-ch) // 100
fmt.Println(<-ch) // 200
// 示例2:无缓冲通道(容量0
ch2 := make(chan string)
go func() {
ch2 <- "hello make" // 发送阻塞,直到接收方准备好
}()
fmt.Println(<-ch2) // 接收阻塞,直到发送方完成
}