This question already has an answer here:
I've got a goroutine that's playing some audio infinitely play()
. In order to keep the play()
alive, I've got the calling function running an infinite for loop afterward.
The unexpected thing is that a barebones loop does not seem to let the function play infinitely and I'm at a loss as to why. However, if I add a simple time.Sleep(time.Second)
into the body of the for-loop, it runs seemingly infinitely. Any idea as to why?
To visualize:
func PlaysForAFewSeconds() {
go play()
for {
}
}
^plays for a few seconds but never breaks out
func PlaysForever() {
go play()
for {
time.Sleep(time.Second)
}
}
^ plays forever.
I'm guessing that this has something to do with how play()
is implemented but I'm hoping this is a common enough problem that someone recognizes this symptom.
Thanks.
</div>