Skip to content

Instantly share code, notes, and snippets.

@reiki4040
Created April 2, 2021 14:59
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 reiki4040/b7e634e105b8bfba54515b3d51280c52 to your computer and use it in GitHub Desktop.
Save reiki4040/b7e634e105b8bfba54515b3d51280c52 to your computer and use it in GitHub Desktop.
closed channel is not also send message to receivers only once but send to channel until release all receivers or program finished.
package main
import (
"fmt"
"time"
)
/*
closed channel is not also send message to receivers only once
but send to channel until release all receivers or program finished.
*/
func main() {
ch := make(chan struct{})
go func() {
for {
select {
case _, ok := <-ch:
if ok {
fmt.Printf("chan value\n")
} else {
fmt.Printf("closed\n")
}
}
}
}()
time.Sleep(500 * time.Millisecond)
close(ch)
fmt.Println("closed channel")
time.Sleep(200 * time.Millisecond)
fmt.Println("end")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment