Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

The select Statement in Go

The select Statement in Go

Go Language Conditional Statements Go Language Conditional Statements

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

The select statement can only be used with channel operations; each case must be a channel operation, either a send or a receive.

The select statement listens to operations on all specified channels, and as soon as one channel is ready, it executes the corresponding code block.

If multiple channels are ready, the select statement randomly chooses one to execute. If no channel is ready, the code in the default block is executed.

Syntax

The syntax of 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 executed when no channel is ready

}

The following describes the syntax of the select statement:

  • If there is a default clause, that statement is executed.
  • If there is no default clause, select blocks until a channel can run; Go does not re-evaluate channels or values.
  • 1. If there is a default clause, that statement is executed. 2. If there is no default clause, select blocks until a channel can run; Go does not re-evaluate channels or values.

    Example

    Demonstration of select statement usage:

    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 execution result of the code above is:

    
    
    received one
    
    received two
    
    

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

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

    In the following example, we define two channels and start two goroutines to fetch data from them. In the main function, we use a select statement to make a non-blocking choice between the two channels; if neither channel has available data, the statement in the default clause is executed.

    When executed, the following example continuously fetches data from the two 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 fetch data from the two channels respectively

     

    go

    func

    ()

    {

       

    for

    {

          ch1 <

    "from 1"

       

    }

     

    }()

     

    go

    func

    ()

    {

       

    for

    {

          ch2 <

    "from 2"

       

    }

     

    }()

     

    // use a select statement to fetch data from the two channels without blocking

     

    for

    {

       

    select

    {

       

    case

    msg1

    :=

    <

    ch1

    :

          fmt

    .

    Println

    (

    msg1

    )

       

    case

    msg2

    :=

    <

    ch2

    :

          fmt

    .

    Println

    (

    msg2

    )

       

    default

    :

         

    // if neither channel has available data, execute the statement here

          fmt

    .

    Println

    (

    "no message received"

    )

       

    }

     

    }

    }

    Go Language Conditional Statements Go Language Conditional Statements