title: How to receive data and send back signal using goroutines
body: <p>I want to make use of <strong>concurrency</strong> in Go to send data to a goroutine for processing and computation <strong>using channels</strong>. The data points come one after another in a function which can be main function or some <code>sendData</code> function.   <strong>If possible, I would like to have the sending of the data to be done from main function.</strong></p>
  <p>I want to send the data from the send function to a goroutine, where data is stored in a slice (lets call this goroutine <code>getData</code>). Certain computations are done on this slice. <strong>After a certain condition is reached (which depends on the slice), I want the goroutine to signal the <code>sendData</code> function that processing is complete for a certain batch of data points.</strong> And now, the <code>sendData</code> function keeps sending data points through the channel to <code>getData</code> goroutine where a new slice keeps getting built and when the condition is reached the signal is sent - that processing is complete and the entire sequence keeps getting repeated.</p>
  <p>As an example, lets imagine that data in the form of numbers is being sent from <code>sendData</code> to <code>getData</code>. The condition is that the running mean of numbers received by <code>getData</code> should be equal to 4. Lets take the following sequence of numbers as our data - <code>[]int{3, 2, 3, 8, 2, 1, 1, 1, 15}</code>. Here, the first batch of numbers would be <code>{3, 2, 3, 8}</code> because after these numbers are sent in this sequence to <code>getData</code>, it finds that the running mean of numbers is equal to 4 after the number 8 is received by <code>getData</code>. Then it sends a signal to <code>sendData</code>. And the process of sending data starts again in sequence of numbers with the next batch being <code>{2, 1, 1, 1, 15}</code>. Here, after the number <code>15</code> is received by <code>getData</code> it finds that the running mean is equal to 4 an sigal is sent again to <code>sendData</code>. <strong>( This is a very basic example - in my real use case, the input data and the condition are more complex. I have data which will be read live in <code>sendData</code>. Here each data point is read sequentially but each data point arrives at a few microseconds after the previous one. As such the arrival of data is fast paced here, and I don't want to do too much of processing and calculations in this function. Furthermore, I want keep concurrency intact, because in the function where data is being read, data is arriving at a fast rate. And, I don't want the reading of data here to be missed because of the processing of data in the goroutine where processing is done.)</strong></p>
  <p>Here is how I have tried to structure the code:</p>
  <pre><code>package main
  
  import (
      &quot;fmt&quot;
  )
  
  func main() {
      ch := make(chan int)
      go sendData(ch)
      go getData(ch)
  }
  
  func sendData(ch chan int) {
      syntheticData := []int{3, 2, 3, 8, 2, 1, 1, 1, 15}
      for _, data := range syntheticData {
              ch &lt;- data
          }
  }
  
  func getData(ch chan int) {
          dataArr := []int{}
          dataArr = append( dataArr, &lt;-ch )
          fmt.Println(dataArr)
  
          if mean(dataArr) == 4{
            close(ch)
          }
  
  }
  
  func sum(array []int) int {
      var result int = 0
      for _, v := range array {
          result += v
      }
  
      return result
  }
  
  
  func mean(array []int) float64 {
      sumArr := float64(sum(array)) / float64(len(array))
      return sumArr
  }
  </code></pre>
  <p>I didn't achieve the functionality that I wanted to with the above code. <strong>How can I achieve the desired functionality in Go?</strong></p>