Select Keyword in Golang

Learn via video courses
Topics Covered

Overview

A channel acts as a medium that enables the goroutine to communicate amongst each other and the communication is lock-free. The channel is bidirectional that is, through the same, it can receive as well as send data.

What is the Select Keyword in Golang?

The golang select statement is very similar to the switch statement. The cases inside the select statement include operations that either receive or send data using channels. The select statement is used for operations that require multiple channels.

Syntax

Example

Let us consider an example, where we have two channels storing some valid integer data.

In the code above,

  • At first, we made two goroutines g1 and g2, which run concurrently along with the main function, under which the channels ch are sending some random values.
  • In the main function, we created two channels ch1 and ch2 of type int and we call the go g1() and go g2() on channels ch1 and ch2 respectively.
  • Under the select statement, we define certain cases x1 and x2 receiving the data from channels ch1 and ch2 respectively.

The Output of the above code is random. It selects any of the values if all the statements are ready for execution.

Why Do We Use the Select Keyword in Golang?

The versatility of the go-select statement enables the selection of data from multiple channels. The select statement comes into play when we have multiple goroutines sending data, then randomly data is chosen by the select statement. If all the cases are not ready, the default case is executed if provided earlier.

Code Implementation

Let us consider a code, where we have multiple goroutines.

In the above code,

  • At first we made two goroutines g1 and g2, which run concurrently along with the main function, under which the channels ch are sending some random values.
  • In the main function, we created two channels ch1 and ch2 of type int and we call the go g1() and go g2() on channels ch1 and ch2 respectively.
  • Under the select statement, we define two cases w1 and w2, and a default case. The two cases receive values from channels ch1 and ch2.

The Output of the above code is,

Go Select with One Channel Ready for Execution

When both the channels are ready then the select statement chooses the channels randomly and executes them.

If only one channel is ready for execution then that is chosen by the select statement.

Let's understand with an example, where we have two channels out of which one is made unavailable using the method time.sleep().

In the code above,

  • We created two goroutines channel_num and channel_text.
  • Under the goroutinechannel_text, at first, we used the method time.sleep(2 * time.Second) to make this channel unavailable for 2 seconds, and send data to the channel number.
  • Under the goroutine channel_num, we send data to the channel text.
  • In the main function, two channels are created text and num, and both the goroutines are called on the created channels.
  • Under the select statement, data was received in channel_1 and channel_2 by num and text respectively.
  • The select statement chose the case channel_1 since the data received by channel_2 is unavailable.

The Output of the above code is,

Since only one channel is ready for execution that is channel_1, therefore it is executed by the select statement.

Go Select Block Channels

The select statement blocks all the channels until they are available for execution. Let's understand the above example where if both channels are not available, then the select statement will block both channels for a certain amount of time and executes until one is available.

In the above code,

  • We made both the channels channel_num and channel_text unavailable by putting them to sleep for 2 seconds using the method time.sleep().

The Output of the above code is,

Since both channels are blocked for 2 seconds, we will not get any output for the first 2 seconds. When both channels are available, the select statement will randomly choose any of the channels and execute it.

Golang Select with the Default Case

When both the channels are blocked then we will have to wait until anyone is available, therefore we use a default case to display a message until any one channel is ready for execution.

In the above code,

  • In the select statement apart from two of the cases we also add a default case, which will be executed only if both cases are not executed.

The Output of the above code is,

Conclusion

Hello developer!! I am sure by now you must have learned about Select Keyword in Golang. Let us summarize what we have learned so far

  • Channels are a typed conduit via which we can transmit values using the channel operator.
  • A channel acts as a medium that enables the goroutine to communicate amongst each other and the communication is lock-free.
  • The select statement is used for operations that require multiple channels.
  • The cases inside the select statement include operations that are either receiving data or sending data using channels.