Programming Code Center(PCC)
[GO]

(PCC)::[How-to-use-Closing-a-channel-indicates-that-no-more-values-will-be-sent-on-it-in-go-language]::[go]

File Name : closing-channels.go

package main

import "fmt"

func main() {
    jobs := make(chan int, 5)
    done := make(chan bool)

    go func() {
        for {
            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
}

Output :

closing-channels.jpg