Adarsh Shahi
Adarsh Shahi

Reputation: 53

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 ?

Upvotes: 1

Views: 26

Answers (1)

Georgios Kampitakis
Georgios Kampitakis

Reputation: 475

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?

Upvotes: 1

Related Questions