Let me use an example:
func WaitForStringOrTimeout() (string, error) {
my_channel := make(chan string)
go WaitForString(my_channel)
select {
case found_string := <-my_channel:
return found_string, nil
case <-time.After(15 * time.Minute):
return nil, errors.New("Timed out waiting for string")
}
}
In this simple example, I have some function WaitForString which blocks for awhile and eventually may return a string. I want to wrap WaitForString with this code which either returns the same string or times out with an error.
If a string is found quickly, is there still a goroutine running with a 15 minute sleep statement somewhere or is this garbage collected somehow?
If the timeout occurs and a string is never found, is there still a goroutine running WaitForString, even though there are no other routines that could observe it's output? What if WaitForString allocates a lot of memory but never returns?
Is there some way I can make WaitForString() become aware of the timeout occurring and give up?