I have a function creating a channel with no buffer. This function goes on to create several other concurrent anonymous functions writing to said channel. The function then goes on to wait for input on the channel and then returns the value.
See example below
package main
import (
"time"
"fmt"
"strconv"
"math/rand"
)
func main() {
for{
text := foo()
fmt.Println(text)
time.Sleep(time.Second)
}
}
func foo() string {
ch := make(chan string)
for i := 0; i < 10; i++ {
// Create some threads
go func(i int) {
time.Sleep(time.Duration(rand.Intn(1000))*time.Millisecond)
ch <- strconv.Itoa(i)
}(i)
}
return <- ch
}
What happens with the anonymous functions that are still waiting on the channel, even though the entire function (foo in example) is "dead"?
Will they be collected as garbage, or will they forever wander the limbo of my computers memory (or until I kill the main thread) eating away at it in a desperate attempt to send their last message before passing on?