Created
October 29, 2021 08:10
-
-
Save ronaldpetty/6471b92f720622483c100f6a1ce9e029 to your computer and use it in GitHub Desktop.
Demo - channel is blocked with tuple form if its open and unbuffered
This file contains hidden or 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 ( | |
"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