Last active
September 15, 2019 18:57
-
-
Save parvezmrobin/51d56ab77d255998b50b0ff36dc49f45 to your computer and use it in GitHub Desktop.
A Go program that will panic
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" | |
| "sync" | |
| ) | |
| func main() { | |
| channel := make(chan string, 20) | |
| wg := sync.WaitGroup{} | |
| wg.Add(3) | |
| go writeA(channel, &wg) | |
| go writeB(channel, &wg) | |
| go read(channel, &wg) | |
| wg.Wait() | |
| } | |
| func writeA(channel chan<- string, wg *sync.WaitGroup) { | |
| for i := 0; i < 10; i++ { | |
| channel <- strconv.Itoa(i) + " sent from A" | |
| } | |
| close(channel) | |
| wg.Done() | |
| } | |
| func writeB(channel chan<- string, wg *sync.WaitGroup) { | |
| for i := 0; i < 10; i++ { | |
| channel <- strconv.Itoa(i) + " sent from B" | |
| } | |
| close(channel) | |
| wg.Done() | |
| } | |
| func read(channel <-chan string, wg *sync.WaitGroup) { | |
| for str := range channel { | |
| print(str, "\n") | |
| } | |
| wg.Done() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment