title: How to implement a counter when using golang's goroutine?
body: <p>I'm trying to make a queue struct that have push and pop functions.</p>
  
  <p>I need to use 10 threads push and another 10 threads pop data, just like I did in the code below.</p>
  
  <p>Questions:</p>
  
  <ol>
  <li>I need to print out how much I have pushed/popped, but I don't know how to do that.</li>
  <li>Is there anyway to speed up my code? The code is too slow for me.</li>
  </ol>
  
  <pre class="lang-golang prettyprint-override"><code>package main
  
  import (
      "runtime"
      "time"
  )
  
  const (
      DATA_SIZE_PER_THREAD = 10000000
  )
  
  type Queue struct {
      records string
  }
  
  
  func (self Queue) push(record chan interface{}) {
      // need push counter
      record &lt;- time.Now()
  }
  
  func (self Queue) pop(record chan interface{}) {
      // need pop counter
      &lt;- record
  }
  
  func main() {
      runtime.GOMAXPROCS(runtime.NumCPU())
      //record chan
      record := make(chan interface{},1000000)
      //finish flag chan
      finish := make(chan bool)
      queue := new(Queue)
      for i:=0; i&lt;10; i++ {
          go func() {
              for j:=0; j&lt;DATA_SIZE_PER_THREAD; j++ {
                  queue.push(record)
              }
              finish&lt;-true
          }()
      }
      for i:=0; i&lt;10; i++ {
          go func() {
              for j:=0; j&lt;DATA_SIZE_PER_THREAD; j++ {
                  queue.pop(record)
              }
              finish&lt;-true
          }()
      }
      for i:=0; i&lt;20; i++ {
          &lt;-finish
      }
  }
  </code></pre>