代码拉取完成,页面将自动刷新
package main
// Closing a channel indicates that no more values will be sent on
// it. This can be useful to communicate completion to the channel's
// receivers.
import "fmt"
func main() {
// use a jobs channel to communicate work to be done from
// goroutine to a worker goroutine.
// When we have no more jobs for the worker we'll close the jobs channel
jobs := make(chan int, 5)
done := make(chan bool)
// Here's the worker goroutine.
// It repeatedly receives from jobs with j, more := <- jobs.
// In this special 2-value form of receive, the more value will
// be false if jobs has been closed and all values in the channel
// have already been received. We use this to notify on done when
// we've worked all our jobs.
go func() {
for {
j, more := <-jobs
if more {
fmt.Println("received job", j)
} else {
fmt.Println("received all jobs")
done <- true
return
}
}
}()
// This sends 3 jobs to the worker over the jobs channel, then
// close it.
for j := 1; j <= 3; j++ {
jobs <- j
fmt.Println("sent job", j)
}
close(jobs)
fmt.Println("sent all jobs")
// await the worker using the synchronization approach
<-done
// Reading from a closed channel succeeds immediately, returning
// zero value of the underlying type.
// The optional second return value is true if the value received
// was delivered by a successful send operation to the channel, or
// false if it was a zero value generated because the channel is
// closed and empty.
_, ok := <-jobs
fmt.Println("received more jobs:", ok)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。