关于go:Go-关闭chanel-chanel的range循环

3次阅读

共计 1463 个字符,预计需要花费 4 分钟才能阅读完成。

敞开 chanel

发送者这边能够发送一个 close(ch), 接管方收到之后就晓得发送方之后都不会再发送信息了。
接收者这边能够通过一个额定的变量,检测通道是否曾经敞开。相似这样:v, ok := <- ch这里的 ok 为 false 就代表咱们从一个曾经敞开的 chanel 获取数据,这个数据是 chanel 类型对应的零值。

例如:

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 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 的 range 循环

绝对于下面的 for 用 range 可能省去对 ok 值的判断,更加简洁不便地操作 chanel。

上面这个例子中,range 的含意就是从通道 ch1 中读取数据并且赋值给 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)
}

运行后果是:

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
正文完
 0