From the course: Go for Developers: Practical Techniques for Effective Coding

Unlock this course with a free trial

Join today to access over 24,900 courses taught by industry experts.

Iteration and select statements

Iteration and select statements

- Often you'll want to keep listening for messages on a channel until a channel is closed. This can be done with an infinite for loop, but the more appropriate way is to use a for range loop. When the channel is closed, the range loop will stop iterating and the listener function will return. We'll discuss closing channels in more detail shortly. When writing concurrent applications, it is often useful to listen to multiple channels at the same time. For example, an employee might need to listen to multiple channels at the same time, such as to receive work from their boss or to be told, it might be time to stop. The select statement lets a go routine wait on multiple channels and respond to the first channel that is ready. So consider the old telephone switchboards. The telephone operator waits for incoming calls into the switchboard. When a call comes in, the operator answers the call and redirects the call to the appropriate destination. The operator then goes back to waiting for…

Contents