Golang select statement

Golang | select statement: Learn about the select statement with syntax, flow chart, and examples.
Submitted by IncludeHelp, on December 25, 2021

The select statement

The select statement in Golang is also a multiway conditional statement like a switch. But it offers a special function, each case of the switch statement is a communication. Each case either contains a send operation or receive operation on the channel which is selected based on the case value. The select statement is capable of selecting any one operation to be performed on the channel.

Flow chart:

Go select statement

Syntax:

select { 
    case send_Rec_Op1:
    // code 
    case send_Rec_Op2:
    // code
    ...
    ...
    ...
    default:
    // code
}

Each case value is either a send or receive operation on the channel and the statement works on the communication.

Important Details:

  1. The number of cases in select's body has no upper limit.
  2. The values of each case need to be an operation from the communication channel.
  3. There are no break statements in the select statement but fallthrough can be implemented.
  4. For the statement to execute, a communication channel needs to be prepared i.e. case statements will execute after send or receive operations are ready to work.
  5. The condition goes into a deadlock state and waits forever when no case statement is present.
  6. In case when all the condition cases are ready for communication, a random case is selected randomly.

Example: Golang program to illustrate the working of the select statement

package main

import (
	"fmt"
	"time"
)

func port1(channel1 chan string) {
	time.Sleep(3 * time.Second)
	channel1 <- "Communicating through 1ST channel"
}

func port2(channel2 chan string) {
	channel2 <- "Communicating through 2ND channel"
}

func main() {
	chan1 := make(chan string)
	chan2 := make(chan string)

	go port1(chan1)
	go port2(chan2)

	select {
	case op1 := <-chan1:
		fmt.Println(op1)
	case op2 := <-chan2:
		fmt.Println(op2)
	}
}

Output:

Communicating through 2ND channel

The select statement with default

Example: Golang program to illustrate the working of the select statement with default

package main

import (
	"fmt"
	"time"
)

func port1(channel1 chan string) {
	time.Sleep(3 * time.Second)
	channel1 <- "Communicating through 1ST channel"
}

func port2(channel2 chan string) {
	channel2 <- "Communicating through 2ND channel"
}

func main() {
	chan1 := make(chan string)
	chan2 := make(chan string)

	select {
	case op1 := <-chan1:
		fmt.Println(op1)
	case op2 := <-chan2:
		fmt.Println(op2)
	default:
		fmt.Println("No Working Channel Found")
	}
}

Output:

No Working Channel Found

In the above code, there is no call to the channel creating function that is not defined. So, both the channels could not be ready which might lead to deadlock which is avoided by using the default statement. But the default is executed even if there might be a ready in the future, which leads to wrong output.

Let's see how the above code will react if there is no default statement.

Example: Golang program to illustrate the working of the select statement

package main

import (
	"fmt"
	"time"
)

func port1(channel1 chan string) {
	time.Sleep(3 * time.Second)
	channel1 <- "Communicating through 1ST channel"
}

func port2(channel2 chan string) {
	channel2 <- "Communicating through 2ND channel"
}

func main() {
	chan1 := make(chan string)
	chan2 := make(chan string)

	// go port1(chan1)
	// go port2(chan2)

	select {
	case op1 := <-chan1:
		fmt.Println(op1)
	case op2 := <-chan2:
		fmt.Println(op2)
	}
}

Output:

fatal error: all goroutines are asleep - deadlock!

goroutine 1 [select]:
main.main()
	/tmp/sandbox3748578454/prog.go:24 +0xbb




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.