I am a learner to GoLang and was trying to experiment and learn about Sync package and chan concept.
Follwoing is the code I am running and expecting to print Receiving Channel Value on console, but the value is not getting printed, it print the values sometimes but not always.
If I range through the chan without putting it into the go routine then its printing all the channel values but failing with an error "fatal error: all goroutines are asleep - deadlock!"
I tried synchronizing the channel reading using channel "done", but in that case it again started failing with same error. I also tried waitGroup API as well which you can see in my code (commented), but that also didn't worked for me.
Thanks for the help
Source Code:
package main
import (
"fmt"
"sync"
)
type safeOperation struct {
i int
sync.Mutex
}
var wg sync.WaitGroup
func main() {
so := new(safeOperation)
ch := make(chan int)
//done := make(chan bool)
for i := 0; i < 5; i++ {
go so.Increment(ch)
go so.Decrement(ch)
}
go func() {
//wg.Add(1)
for c := range ch {
fmt.Println("Receiving Channel Value: ", c)
}
//wg.Done()
//done <- true
}()
//wg.Wait()
//<-done
fmt.Println("Value: ", so.GetValue())
fmt.Println("Main method finished")
}
func (so *safeOperation) Increment(ch chan int) {
//so.Lock()
//defer wg.Done()
so.i++
ch <- so.i
//so.Unlock()
}
func (so *safeOperation) Decrement(ch chan int) {
//so.Lock()
//defer wg.Done()
so.i--
ch <- so.i
//so.Unlock()
}
func (so *safeOperation) GetValue() int {
so.Lock()
v := so.i
so.Unlock()
return v
}
Output: Value: 1 Main method finished