close chanel
The sender can send one close(ch)
, and the receiver knows that the sender will not send any more information after receiving it.
The receiver side can use an additional variable to detect whether the channel has been closed. Similar to this: v, ok := <- ch
Here ok is false, which means that we get data from a closed channel, and this data is the zero value corresponding to the channel type.
E.g:
package main
import (
"fmt"
"time"
)
func main() {
// 子协程:写10个数据,每写一次,就阻塞一次,主协程读取一次,阻塞解除一次
// 主协程:读取一次数据,阻塞一次,子协程写一次,阻塞解除一次
ch1 := make(chan int)
go sendData(ch1)
//读取chanel中的数据,不过由于我们可能不知道发送方发送了多少数据, 这里用死循环
for {
time.Sleep(1 * time.Second)
v, ok := <-ch1
if !ok {
fmt.Println("all the data has bean read ", ok)
break
}
fmt.Println("the data read is: ", v, ok)
}
}
func sendData(ch1 chan int) {
// 发送方: 发送10条数据
for i := 0; i < 10; i++ {
//把i写入chanel
ch1 <- i
}
//关闭ch1通道
close(ch1)
}
The result of running is:
the data read is: 0 true
the data read is: 1 true
the data read is: 2 true
the data read is: 3 true
the data read is: 4 true
the data read is: 5 true
the data read is: 6 true
the data read is: 7 true
the data read is: 8 true
the data read is: 9 true
all the data has bean read false
chanel's range loop
Compared with the above for, using range can save the judgment of the ok value, and operate the channel more concisely and conveniently.
In the following example, the meaning of range is to read data from channel ch1 and assign it to v:
package main
import (
"fmt"
"time"
)
func main() {
// 子协程:写10个数据,每写一次,就阻塞一次,主协程读取一次,阻塞解除一次
// 主协程:读取一次数据,阻塞一次,子协程写一次,阻塞解除一次
ch1 := make(chan int)
go sendData(ch1)
//读取chanel中的数据,不过由于我们可能不知道发送方发送了多少数据, 这里用死循环
for v := range ch1 { //这里range的含义就是从通道ch1中读取数据并且赋值给v [v<- ch1]
fmt.Println("reading data: ", v)
}
fmt.Println("main-goroutine completed")
}
func sendData(ch1 chan int) {
// 发送方: 发送10条数据
for i := 0; i < 10; i++ {
time.Sleep(1 * time.Second)
//把i写入chanel
ch1 <- i
}
//关闭ch1通道
close(ch1)
}
The result of running is:
reading data: 0
reading data: 1
reading data: 2
reading data: 3
reading data: 4
reading data: 5
reading data: 6
reading data: 7
reading data: 8
reading data: 9
main-goroutine completed
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。