title: How to create global counter in highly concurrent system
body: <p>I'm creating global counter, which can be shared between goroutines.
  Referring to this <a href="https://stackoverflow.com/questions/16783273/golang-best-way-to-implement-global-counters-for-highly-concurrent-applications">question</a>, following code may satisfy my needs.</p>
  <p>However if there ware lots of concurrent requests, could it happen that the same number is assigned to more than two goroutines?
  If so how can I avoid this?</p>
  <p>This question is different from the link I pasted, as what I want to know about is how I can avoid duplication using channel counter. if the only possible solution is other implementation like sync.Mutex or atomic, I'll use it. however, according to the link (again), channel seems to be the best option. Any comment or answer really helpful. thanks in advance.
  I'm new to multithread coding and also go, might be silly question. sorry for that.</p>
  <pre><code>package main
  
  import (
      &quot;fmt&quot;
      &quot;time&quot;
  )
  
  var counter int
  var counter_chan chan int
  
  func main() {
      counter_chan = make(chan int, 100)
  
      counter = 0
  
      go func() {
          for {
              select {
              case chanc := &lt;-counter_chan:
                  counter += chanc
                  fmt.Printf(&quot;%d \n&quot;, counter)
              }
          }
      }()
  
      for i := 0; i &lt; 10; i++ {
          go AddCounter(counter_chan)
      }
  
      time.Sleep(time.Second)
      fmt.Printf(&quot;Total Count is ... %d \n&quot;, GetCount())
  
  }
  
  func AddCounter(ch chan int) {
      ch &lt;- 1
  }
  
  func GetCount() int {
      return counter
  }
  
  func ResetCount() {
      if counter &gt; 8190 {
          counter = 0
      }
  }
  </code></pre>
  <p>-- Edit 05/14 2018</p>
  <p>Assume following code is thread-safe for getting and resetting value. Am I right?</p>
  <pre><code>package main
  
  import (
      &quot;fmt&quot;
      &quot;time&quot;
  )
  
  var counter int
  var addCounterChan chan int
  var readCounterChan chan int
  
  func main() {
      addCounterChan = make(chan int, 100)
      readCounterChan = make(chan int, 100)
  
      counter = 0
  
      go func() {
          for {
              select {
              case val := &lt;-addCounterChan:
                  counter += val
                  if counter &gt; 5 {
                      counter = 0
                  }
                  readCounterChan &lt;- counter
                  fmt.Printf(&quot;%d \n&quot;, counter)
              }
          }
      }()
  
      for i := 0; i &lt; 10; i++ {
          go AddCounter(addCounterChan)
      }
  
      time.Sleep(time.Second)
  
      for i := 0; i &lt; 10; i++ {
          fmt.Printf(&quot;Total Count #%d is ... %d \n&quot;, (i + 1), GetCount(readCounterChan))
      }
  
  }
  
  // Following two functions will be implemented in another package in real case.
  func AddCounter(ch chan int) {
      ch &lt;- 1
  }
  
  func GetCount(ch chan int) int {
      r := &lt;-ch
      return r
  }
  </code></pre>