Skip to content

Instantly share code, notes, and snippets.

@doorbash
Created October 5, 2022 18:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doorbash/e1d0b827ea6f41016acc82a4e2ec294c to your computer and use it in GitHub Desktop.
Save doorbash/e1d0b827ea6f41016acc82a4e2ec294c to your computer and use it in GitHub Desktop.
package main
import (
"log"
"time"
)
func main() {
ch := make(chan string, 10)
done := make(chan struct{})
go func() {
for {
select {
case m, ok := <-ch:
if !ok {
log.Println("channel is closed")
log.Println()
return
}
log.Println("received:", m)
log.Println()
}
}
}()
go func() {
defer func() {
x := recover()
if x != nil {
log.Println(x)
}
done <- struct{}{}
}()
time.Sleep(1 * time.Second)
log.Println("sending hello...")
ch <- "hello"
time.Sleep(1 * time.Second)
log.Println("sending world!...")
ch <- "world!"
time.Sleep(1 * time.Second)
log.Println("closing the channel...")
close(ch)
time.Sleep(1 * time.Second)
log.Println("sending 1...")
ch <- "1"
time.Sleep(1 * time.Second)
done <- struct{}{}
}()
<-done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment