Created
June 14, 2019 11:45
-
-
Save igolaizola/c8b4c861a034c01d82e8a5bcdb562e07 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Test it on The Go Playground: https://play.golang.org/p/in60aXGnagU