SO golang example with infinite for / channel workers I whipped this up as an example of how infinite loops can often be used to great effect in golang, and also to discuss closing channels and worker communications.  It was strictly off the cuff, but I was curious if anybody would like to offer a critique.  In particular, I feel as though inflight is a crude implementation detail.  I've done it many times, and looking at wait group implementation, seems like it is a synchronized version of the same thing.
package main

import (
  &quot;log&quot;
)

func doStuff(datachan &lt;-chan map[string]string, reschan chan&lt;- int) {
  for {
    data, ok := &lt;-datachan
    if !ok {
      log.Print(&quot;Channel closed.&quot;)
      break
    }
    log.Printf(&quot;Data had %d length: %+v&quot;, len(data), data)
    reschan&lt;-len(data)
  }
  return
}

const workers = 3

func main() {
  var datachan = make(chan map[string]string)
  var reschan = make(chan int)
  var inflight = 0
  var inputs = []map[string]string {
    map[string]string{ &quot;hi&quot;: &quot;world&quot; },
    map[string]string{ &quot;bye&quot;: &quot;space&quot;, &quot;including&quot;: &quot;moon&quot; },
    map[string]string{ &quot;bye&quot;: &quot;space&quot;, &quot;including&quot;: &quot;moon&quot; },
    map[string]string{ },
    map[string]string{ },
  }
  // an inline funciton definition can change inflight within main()'s scope
  processResults := func (res int) {
    log.Printf(&quot;Main function got result %d&quot;, res)
    inflight--
  }
  // start some workers
  for i := 0; i &lt; workers; i++{
    go doStuff(datachan, reschan)
  }
  for _, data := range inputs {
      //Select allows reading from reschan if datachan is not available for
      // writing, thus freeing up a worker to read from datachan next loop
      written := false
      for written  != true {
        select {
          case res := &lt;-reschan:
            processResults(res)
          case datachan &lt;- data:
            inflight++
            written = true
        }
      }
  }
  close(datachan)
  for inflight &gt; 0 {
    processResults(&lt;-reschan)
  }
}
You wrote:
func doStuff(datachan &lt;-chan map[string]string, reschan chan&lt;- int) {
    for {
        data, ok := &lt;-datachan
        if !ok {
            log.Print(&quot;Channel closed.&quot;)
            break
        }
        log.Printf(&quot;Data had %d length: %+v&quot;, len(data), data)
        reschan &lt;- len(data)
    }
    return
}

I don't understand why you didn't write:
func doStuff(datachan &lt;-chan map[string]string, reschan chan&lt;- int) {
    for data := range datachan {
        log.Printf(&quot;Data had %d length: %+v&quot;, len(data), data)
        reschan &lt;- len(data)
    }
    log.Print(&quot;Channel closed.&quot;)
}