title: Goroutines sharing an array channel : trying to solve data race
body: <p>I try to write a complex program with parallel goroutines. It is my first program with channels ;)  Each goroutine returns an array and, unfortunately, the result is "random". If I run 10 times the program, I have 10 different results :(</p>
  
  <p>This is an over simplification of my program, the results is good (maybe because it is too simple) but when I run it with -race argument, there is 4 data races.</p>
  
  <p>I tried to had a close() function but it did not worked.</p>
  
  <p>May you help me to find the mistake ? Thank you very much in advance !</p>
  
  <pre><code>package main
  
  import "fmt"
  import "sync"
  import "strconv"
  
  
  func cat_strings(a int, b string) []string{
      var y []string
  
      j := strconv.Itoa(a)
      y = append(y, j)
      y = append(y, b)
      return y
  }
  
  func main() {
      var slice []string
      var wg sync.WaitGroup
      var x []string
  
      queue := make(chan []string, 10)
  
      wg.Add(10)
      for i := 0; i &lt; 10; i++ {
          go func(i int) {
              defer wg.Done()
              x = cat_strings(i, "var")
              queue &lt;- x
          }(i)
  
      }
      //close(queue)
  
      go func() {
          defer wg.Done()
          for t := range queue {
              slice = append(slice, t...)
          }
      }()
  
      wg.Wait()
      fmt.Println(slice)
  }
  </code></pre>