Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

goroutines done channel unbuffered channel

In unbuffered channel by default sends and receives block until both the sender and receiver are ready.

package main
    
    import (
        "fmt"
        "time"
    )
    
    func worker(done chan bool) {
        fmt.Print("working...")
        fmt.Println("done")
    
        done <- true
    }
    
    func main() {
    
        done := make(chan bool)
        go worker(done)
    
        time.Sleep(5 * time.Second)
        <-done
    }

But the above code is executed instantly. It should wait for 5 seconds right ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If you are running the above on playground then I guess you are hitting the same case as this question Why does time.Sleep not work if there are no further statements to execute?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading