1. 对于无缓冲信道,在接收者未准备好之前,发送操作是阻塞的.
package mainimport "fmt"func main() {pipline := make(chan string)pipline <- "hello world"fmt.Println(<-pipline)}
因此,对于解决此问题有两种方法:
- 使接收者代码在发送者之前执行
- 使用缓冲信道,而不使用无缓冲信道
package mainfunc hello(pipline chan string) {<-pipline}func main() {pipline := make(chan string)go hello(pipline)pipline <- "hello world"}
