Go example--Close Channel

Source: Internet
Author: User

Tags: obs pack else PAC BOOL main return close int

package mainimport (    "fmt")func main() {    jobs := make(chan int, 5)    done := make(chan bool)    go func() {        for {            //读取通道方式, val,ok := <-chan 通道关闭后,ok是false            j, more := <-jobs            if more {                fmt.Println("received job", j)            } else {                fmt.Println("received all jobs")                done <- true                return            }        }    }()    for j := 1; j <= 3; j++ {        jobs <- j        fmt.Println("sent job", j)    }    //关闭通道    close(jobs)    fmt.Println("sent all jobs")    <-done}

Go example--Close Channel

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: