可以将通道设置为对接收该通道的任何人都是只读的,而发送方仍然可以使用双向通道进行写入。例如:
func F() <-chan int { // Create a regular, two-way channel. c := make(chan int) go func() { defer close(c) // Do stuff c <- 123 }() // Returning it, implicitely converts it to read-only, // as per the function return value. return c}呼叫的人
F()都会收到一个只能阅读的频道。这对于避免在编译时潜在地滥用通道很有用。因为只读/只写通道是不同的类型,所以编译器可以使用其现有的类型检查机制来确保调用者不会尝试将内容写入没有业务写入的通道。



