Skip to content

Instantly share code, notes, and snippets.

@ronaldpetty
Created October 29, 2021 08:10
Show Gist options
  • Save ronaldpetty/6471b92f720622483c100f6a1ce9e029 to your computer and use it in GitHub Desktop.
Save ronaldpetty/6471b92f720622483c100f6a1ce9e029 to your computer and use it in GitHub Desktop.
Demo - channel is blocked with tuple form if its open and unbuffered
package main
import (
"strconv"
"time"
)
func main() {
c := make(chan string)
w := make(chan struct{})
f := func() {
print("top")
_, more := <-c
print("bottom")
print(strconv.FormatBool(more))
if more {
print(" channel is open\n")
} else {
print(" channel is closed\n")
}
w <- struct{}{}
}
go f()
print("func should be blocked\n")
time.Sleep(time.Second)
c <- "not ok"
close(c)
go f()
<-w
<-w
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment