Go 边看边练 -《Go 学习笔记》系列(十四)(已完结)

本贴最后更新于 3426 天前,其中的信息可能已经时移世易

上一篇: https://hacpai.com/article/1438938175118


ToC


7.2.1 单向

可以将 channel 隐式转换为单向队列,只收或只发。

c := make(chan int, 3) var send chan<- int = c // send-only var recv <-chan int = c // receive-only send <- 1 // <-send // Error: receive from send-only type chan<- int <-recv // recv <- 2 // Error: send to receive-only type <-chan int

不能将单向 channel 转换为普通 channel

d := (chan int)(send) // Error: cannot convert type chan<- int to type chan int d := (chan int)(recv) // Error: cannot convert type <-chan int to type chan int

7.2.2 选择

如果需要同时处理多个 channel,可使用 select 语句。它随机选择一个可用 channel 做收发操作,或执行 default case

在循环中使用 select default case 需要小心,避免形成洪水。

7.2.3 模式

用简单工厂模式打包并发任务和 channel

func NewTest() chan int { c := make(chan int) rand.Seed(time.Now().UnixNano()) go func() { time.Sleep(time.Second) c <- rand.Int() }() return c } func main() { t := NewTest() println(<-t) // 等待 goroutine 结束返回。 }

channel 实现信号量 (semaphore)。

closed channel 发出退出通知。

select 实现超时 (timeout)。

func main() { w := make(chan bool) c := make(chan int, 2) go func() { select { case v := <-c: fmt.Println(v) case <-time.After(time.Second * 3): fmt.Println("timeout.") } w <- true }() // c <- 1 // 注释掉,引发 timeout。 <-w }

channel 是第一类对象,可传参 (内部实现为指针) 或者作为结构成员。

type Request struct { data []int ret chan int } func NewRequest(data ...int) *Request { return &Request{ data, make(chan int, 1) } } func Process(req *Request) { x := 0 for _, i := range req.data { x += i } req.ret <- x } func main() { req := NewRequest(10, 20, 30) Process(req) fmt.Println(<-req.ret) }

全系列完



社区小贴士

  • 关注标签 [golang] 可以方便查看 Go 相关帖子
  • 关注作者后如有新帖将会收到通知
打赏 50 积分后可见
50 积分 • 1 打赏
  • golang

    Go 语言是 Google 推出的一种全新的编程语言,可以在不损失应用程序性能的情况下降低代码的复杂性。谷歌首席软件工程师罗布派克(Rob Pike)说:我们之所以开发 Go,是因为过去 10 多年间软件开发的难度令人沮丧。Go 是谷歌 2009 发布的第二款编程语言。

    499 引用 • 1395 回帖 • 245 关注
  • 教程
    144 引用 • 627 回帖 • 8 关注
  • 雨痕
    14 引用 • 68 回帖 • 4 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...