This code is for a fairly simple demo for my programming language class. I'm trying to display a few different techniques that Go allows, like interfaces and concurrency, but I can't seem to get the WaitGroups to work right, so it keeps deadlocking on me at the end. My biggest question is: how do I get the WaitGroups to synchronize correctly and not deadlock the system when the goroutines stop? I'm most likely missing something obvious.
package main
import (
"bufio"
"fmt"
"os"
"sync"
)
func Reader(wg *sync.WaitGroup, message chan string, done chan bool){
defer wg.Done()
reader := bufio.NewReader(os.Stdin)
for {
msg, _ := reader.ReadString('
')
if msg == "exit
" {
<-done
return
} else {
message <- msg
}
}
}
func main() {
message := make(chan string)
done := make(chan bool)
wg := &sync.WaitGroup{}
wg.Add(1)
go Reader(wg, message, done)
wg.Add(1)
go func(){
defer wg.Done()
for {
select {
case <-done:
return
case msg := <-message:
fmt.Println("main: "+msg)
}
}
}()
wg.Wait()
close(message)
close(done)
}