Skip to content

Instantly share code, notes, and snippets.

@parvezmrobin
Last active September 15, 2019 18:57
Show Gist options
  • Save parvezmrobin/51d56ab77d255998b50b0ff36dc49f45 to your computer and use it in GitHub Desktop.
Save parvezmrobin/51d56ab77d255998b50b0ff36dc49f45 to your computer and use it in GitHub Desktop.
A Go program that will panic
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