Channels, When the program flow exit from the infinite for { select }

  1. That’s right.
  2. Based on my understanding of your code, you don’t actually need a separate goroutine to wait for the ack. Here’s how I would improve the code:
		go func(cl *client) {
			// Send command...

			// Wait for ack or timeout
			begin := time.Now().UnixNano()			
			fmt.Printf(" Wait for ack or timeout  \n")
			var status string
			select {
			case ack := <-c.ackCh:
				fmt.Printf("  read channel after: %d  \n", time.Now().UnixNano()-begin)
				fmt.Printf("               cmdId: %s  \n", ack.CommandId)
				status = ack.Status
			case <-time.After(500 * time.Millisecond):
				fmt.Printf("  timeout after: %d  \n", time.Now().UnixNano()-begin)
				status = "timeout"
			}

			// Add the status to the results...
		}(c)
1 Like