Skip to content

Instantly share code, notes, and snippets.

@igolaizola
Created June 14, 2019 11:45
Show Gist options
  • Save igolaizola/c8b4c861a034c01d82e8a5bcdb562e07 to your computer and use it in GitHub Desktop.
Save igolaizola/c8b4c861a034c01d82e8a5bcdb562e07 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"time"
)
func main() {
test(1)
test(2)
}
func test(n int) {
fmt.Println("TEST", n)
done := make(chan bool)
go func() {
<-time.After(100 * time.Millisecond)
select {
case <-done:
fmt.Println("channel closed")
default:
done <- true
fmt.Println("channel not closed, writing to channel")
}
fmt.Println("goroutine finished")
}()
switch n {
case 1:
fmt.Println("closing channel")
close(done)
case 2:
fmt.Println("reading from channel")
<-done
}
<-time.After(200 * time.Millisecond)
fmt.Println("test finished")
}
@igolaizola
Copy link
Author

Test it on The Go Playground: https://play.golang.org/p/in60aXGnagU

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment