Go routine not receiving all data sent through channel — toy example program

Issue

I’m just playing around with Go, taking it for a test drive so to speak. I’m having a problem where a go routine that is mean to receive 3 integers only seems to receive one.

type simpleFunction func() int

func run(fChan chan simpleFunction, result chan int) {
  for{
    select {
    case fn := <-fChan:
      fmt.Printf("sending: %d down result chan\n", fn())
      result <- fn()
    case <-time.After(time.Second * 2):
      close(fChan)
    }
  }
}

func recieve(result chan int){
  for {
    select {
    case x := <-result:
      fmt.Printf("recieved: %d from result chan\n", x)
    case <-time.After(time.Second * 2):
      close(result)
    }
  }
}

So, as you can see the run routine receives functions, evaluates them, and then sends the result down the result channel.

Here’s my main/test:

func main() {
  fns := []simpleFunction{
    func() int {return 1},
    func() int {return 2},
    func() int {return 3},
  }

  fChan := make(chan simpleFunction)
  result := make(chan int)

  go run(fChan, result)
  go recieve(result)
  for _, fn := range fns {
    fmt.Printf("sending a function that returns: %d down function chan\n", fn())
    fChan <- fn
  }
} 

And here’s my output:

sending a function that returns: 1 down function chan
sending: 1 down result chan
recieved: 1 from result chan
sending a function that returns: 2 down function chan
sending a function that returns: 3 down function chan
sending: 2 down result chan
sending: 3 down result chan

So, as you can see, everything seems to go swimmingly for the first function, but it’s not so hot afterwards. Any tips or suggestions?

Solution

There are a couple of issues with this code:

  • The program terminates when main returns. It does not wait for the run and receive goroutines to complete.
  • There’s a race on closing the channels. There’s no guarantee that the sender will top sending before the timeout.
  • If main does not exit, then the for { select { } } loops will spin forever printing zero values. Receive on a closed channel returns the zero value.

Answered By – Bayta Darell

Answer Checked By – Dawn Plyler (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.