Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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")
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement