上一篇: https://hacpai.com/article/1438938175118
ToC
- Go 边看边练 -《Go 学习笔记》系列(一)- 变量、常量
- Go 边看边练 -《Go 学习笔记》系列(二)- 类型、字符串
- Go 边看边练 -《Go 学习笔记》系列(三)- 指针
- Go 边看边练 -《Go 学习笔记》系列(四)- 控制流 1
- Go 边看边练 -《Go 学习笔记》系列(五)- 控制流 2
- Go 边看边练 -《Go 学习笔记》系列(六)- 函数
- Go 边看边练 -《Go 学习笔记》系列(七)- 错误处理
- Go 边看边练 -《Go 学习笔记》系列(八)- 数组、切片
- Go 边看边练 -《Go 学习笔记》系列(九)- Map、结构体
- Go 边看边练 -《Go 学习笔记》系列(十)- 方法
- Go 边看边练 -《Go 学习笔记》系列(十一)- 表达式
- Go 边看边练 -《Go 学习笔记》系列(十二)- 接口
- Go 边看边练 -《Go 学习笔记》系列(十三)- Goroutine
- Go 边看边练 -《Go 学习笔记》系列(十四)- Channel
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) }
全系列完
- 本系列是基于雨痕的《Go 学习笔记》(第四版)整理汇编而成,非常感谢雨痕的辛勤付出与分享!
- 转载请注明:文章转载自:黑客与画家的社区 [https://hacpai.com]
- 如果你觉得本章节做得不错,请在下面打赏一下吧~
社区小贴士
- 关注标签 [golang] 可以方便查看 Go 相关帖子
- 关注作者后如有新帖将会收到通知
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于