So I've started messing around with Go, and I'm fascinated by go routines. I now wrote a simple test to see if I can change the value of a variable while I continuously print it out.
I now have the following code:
package main
import (
"fmt"
"time"
)
func change(c chan float64) float64 {
time.Sleep(2 * time.Second)
return 2.5
}
func main() {
s := 1.1
c := make(chan float64)
go change(c)
s = <-c
for {
fmt.Println(s)
time.Sleep(100 * time.Millisecond)
}
}
Unfortunately it ends in an error:
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan receive]:
main.main()
/home/kramer65/repos/go/src/messing_around/main.go:19 +0x7d
exit status 2
I searched around and found that this deadlock happens when the main function ends while goroutines didn't end yet. But since I have an endless loop I wouldn't know what else is wrong with my code.
Could anybody enlighten me as to what is wrong with this code, and how I can change the value of a variable while I continuously print it out? All tips are welcome!