Idiomatic way of handling a goroutine with one "return" value? : <p>Imagine I want to write a function that returns a random number divided by 2:</p>

<pre><code>func randomIntInHalf() int {
    return rand.Int() / 2
}
</code></pre>

<p>But then I decide I want to build this function for concurrency so I end up with:</p>

<pre><code>func randomIntInHalf(c chan&lt;- int) {
    c &lt;- rand.Int() / 2
}

func main() {
    // Generate 10 random ints in parallel
    amount := 10
    c := make(chan int, amount)

    for i := 0; i &lt; amount; i++ {
        go randomIntInHalf(c)
    }

    for i := 0; i &lt; amount; i++ {
        fmt.Println(&lt;-c)
    }

    close(c)
}
</code></pre>

<p>I see some issues with this approach. For example, I require the caller to close the channel when it's done generating, making it possible that someone might call the function and leave the channel open indefinitely. It was sort of my understanding that you always want the sender to close the channel. A) is that true and b) is it even possible in this case? Is there a better way to write this code or approach this problem?</p>

<p>And, generally, is there a better, idiomatic approach for running a function in a parallel that only ever writes 1 (or a known N) value to the channel?</p>

<p>Thanks.</p>
