Menu Close

Go – Select statement in go golang

In this article, We are going to learn about select statement in go golang. What is select statement in go golang. How to implement or use of select statement with example and code analysis.

Select statement allows to wait for multiple send or receive operations simul­taneously in golang.

  • Only one case statement will be execute at a time.
  • Select statement is a blocking call.
  • We can have any number of case statements inside select statement. 
  • Each case statement must have channel operation. 
  • Unlike C,  break statement is not needed in select statement.
  • Selection of case statement is random.
  • Send and receive operations on a nil channel block forever. You will get panic error from gocompiler.

Syntax:

select {

case communication_clause:
statement(s);

case communication_clause:
statement(s);

case communication_clause:
statement(s);

default : /* It is Optional */
statement(s);
}

Example with code:

package main

import (
    "fmt"
    "time"
)

func go_server1(ch chan string) {

    time.Sleep(5 * time.Second)

    ch <- "from go server1"

}

func go_server2(ch chan string) {

    time.Sleep(2 * time.Second)

    ch <- "from go server2"

}

func main() {

    // Make two channal for go_server1 and go_server2
    channel_1 := make(chan string)

    channel_2 := make(chan string)

    // goroutine for go_server 1
    go go_server1(channel_1)

    // goroutine for go_server 2
    go go_server2(channel_1)

    select {

        case rec_ch_1 := <- channel_1:

            fmt.Println(rec_ch_1)

        case rec_ch_2 := <- channel_2:

            fmt.Println(rec_ch_2)

    }

}

Output:

from go server2

Code analysis:

In the above program, go_server1 function in line no 8 sleeps for 5 seconds then write the text i.e “from go server1” to the channel ch and go_server2 function in line no 16 sleeps for 2 seconds then write the text i.e “from go server2” to the channel ch.
The main function calls these two goroutines i.e go_server1 and go_server2 at line no 32 and 35 respectively so when control reaches at line number 37, It starts executing select statement. Here, select statement blocks until one of its case is ready to execute.
go_server1 goroutine starts writing into channel i.e “ch” after 5 seconds at line number 12. go_server2 goroutine starts writing into channel i.e ch after 2 seconds at line no 20.
So go_server2 goroutine takes less time i.e 2 seconds to write into channel. That’s why output is “from go server2”. Then control exit from select statement.

Default case in select statement:

Default case is executed when all case statements are blocked.

Example:

// never blocks

select {

    case x := <-ch:

        fmt.Println("Received", x)
    
    default:

        fmt.Println("Nothing available")
}

Use of nil channel:

nil channel can be used to disable the channel in select statement.

Example of nil channel:

ch1 = nil // disables this channel

select {

    case <-ch1:

        fmt.Println("Received from ch1")
    
    default:

        fmt.Println("Received from ch2")
}

In the above example, channel ch1 is nil. So default case will get executed. if we do not use default case then this will be blocked and will return deadlock error like “fatal error: all goroutines are asleep – deadlock!” .

Empty select statement:

Empty select statement blocks grouting forever because select statement blocks until at least one of it’s cases can proceed .

Syntax for empty select statement:

Select {}

Reference link: https://golangbot.com/select/, https://yourbasic.org/golang/select-explained/

To get more details about golang please follow given below link.
https://techieindoor.com/go-lang/
Posted in golang

Leave a Reply

Your email address will not be published. Required fields are marked *