Created
April 2, 2021 14:59
-
-
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.
This file contains 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 ( | |
"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