title: context.WithDeadline while passing context to go routine?
body: <p>context.WithDeadline while passing context to go routine? </p>
  
  <p>I have put together some sample code that will start a new goroutine for every item in my slice.
  At the moment, this will wait for the done channel to be called len(slice) times. </p>
  
  <p>However, I would also like to implement a timeout in the goroutines to event leaking.
  It seems that context.WithDeadline (or maybe WithTimeout?)is the appropriate function to use. </p>
  
  <p>For example, lets say I want to pass in a 23 second deadline for all goroutines that are initialized from main().
  However, its not clear to me how I should do this.</p>
  
  <p>I have read godoc along with <a href="https://blog.golang.org/context" rel="nofollow noreferrer">Go Concurrency Patterns: Context (on the go blog)</a>  but as a new gopher, 
  I am none the wiser. Many of the examples that I have found use http.handler(or similar as examples and so they are a source of some confusion for me. </p>
  
  <p>What is an appropriate way to pass in context with deadline / timeout here. </p>
  
  <pre><code>package main
  
  import (
    "fmt"
    "time"
  )
  
  func sleepNow(i int, done chan bool) {
    time.Sleep(time.Duration(i) * time.Second)
    fmt.Println(i, "has just woken up from sleep and the time is", time.Now())
  
    done &lt;- true
  }
  
  func main() {
    done := make(chan bool)
    numbersSlice := []int{10, 20, 30, 12}
  
    for _, v := range(numbersSlice){
      go sleepNow(v, done)
    }
  
    for x := 0; x &lt; len(numbersSlice); x++ {
    &lt;-done
    }
    fmt.Println("Looks like we are all done here!")
  
  }
  </code></pre>