I am learning go concurrency and I want two go routines to keep talking to each other while passing along updated values via channels. One add 2 to the number while the other subtracts 1. The code and the output are given below:
What's wrong with this code?
package main
import (
"fmt"
"sync"
"time"
)
var wg sync.WaitGroup
func addTwo(r chan int, e chan int) {
val := <-r
fmt.Println("AT Recieved: ", val)
time.Sleep(1 * time.Second)
e <- val + 2
}
func subtractOne(r chan int, e chan int) {
val := <-r
fmt.Println("SO Recieved: ", val)
time.Sleep(1 * time.Second)
e <- val - 1
}
func main() {
ch := make(chan int)
ch2 := make(chan int)
wg.Add(1)
go addTwo(ch, ch2)
wg.Add(1)
go subtractOne(ch2, ch)
ch <- 1
wg.Wait()
}
Output:
AT Recieved: 1
SO Recieved: 3
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [semacquire]:
sync.runtime_Semacquire(0x4b2de8?)
/usr/lib/go-1.18/src/runtime/sema.go:56 +0x25
sync.(*WaitGroup).Wait(0x0?)
/usr/lib/go-1.18/src/sync/waitgroup.go:136 +0x52
main.main()
And then it just exits.
Why aren't the goroutines exchanging values forever, even when I am not calling wg.Done() in the goroutines?