Easy Tutorial
❮ Go Decision Making Go Interfaces ❯

Go Language Select Statement

Go Language Conditional Statements

The select statement in Go is a control structure similar to the switch statement.

The select statement is used exclusively for channel operations, where each case must be a channel operation, either a send or a receive.

The select statement listens for operations on all specified channels, executing the corresponding block of code once one of the channels is ready.

If multiple channels are ready, the select statement randomly selects one to execute. If none of the channels are ready, it executes the code in the default block.

Syntax

The syntax for the select statement in the Go programming language is as follows:

select {
  case <- channel1:
    // Code to execute
  case value := <- channel2:
    // Code to execute
  case channel3 <- value:
    // Code to execute

    // You can define any number of cases

  default:
    // Code to execute if none of the channels are ready
}

The following describes the syntax of the select statement:

Example

Demonstration of the select statement application:

Example

package main

import (
    "fmt"
    "time"
)

func main() {

    c1 := make(chan string)
    c2 := make(chan string)

    go func() {
        time.Sleep(1 * time.Second)
        c1 <- "one"
    }()
    go func() {
        time.Sleep(2 * time.Second)
        c2 <- "two"
    }()

    for i := 0; i < 2; i++ {
        select {
        case msg1 := <-c1:
            fmt.Println("received", msg1)
        case msg2 := <-c2:
            fmt.Println("received", msg2)
        }
    }
}

The above code results in:

received one
received two

In the above example, we created two channels, c1 and c2.

The select statement waits for data from both channels. If it receives data from c1, it prints "received one"; if it receives data from c2, it prints "received two".

In the following example, we define two channels and start two goroutines to receive data from these channels. In the main function, we use the select statement to make non-blocking selections between these two channels. If neither channel has available data, it executes the statement in the default clause.

The following example continuously receives data from both channels. When neither channel has available data, it outputs "no message received".

Example

package main

import "fmt"

func main() {
  // Define two channels
  ch1 := make(chan string)
  ch2 := make(chan string)

  // Start two goroutines to receive data from both channels
  go func() {
    for {
      ch1 <- "from 1"
    }
  }()
  go func() {
    for {
      ch2 <- "from 2"
    }
  }()

  // Use the select statement to non-blockingly receive data from both channels
  for {
    select {
    case msg1 := <-ch1:
      fmt.Println(msg1)
    case msg2 := <-ch2:
      fmt.Println(msg2)
    default:
      // Execute this statement if neither channel has available data
      fmt.Println("no message received")
    }
  }
}

Go Language Conditional Statements

❮ Go Decision Making Go Interfaces ❯