
- Go - Home
- Go - Overview
- Go - Environment Setup
- Go - Program Structure
- Go - Basic Syntax
- Go - Data Types
- Go - Variables
- Go - Constants
- Go - Identifiers
- Go - Keywords
- Go - Operators
- Go - Arithmetic Operators
- Go - Assignment Operators
- Go - Relational Operators
- Go - Logical Operators
- Go - Bitwise Operators
- Go - Miscellaneous Operators
- Go - Operators Precedence
- Go Decision Making
- Go - Decision Making
- Go - If Statement
- Go - If Else Statement
- Go - Nested If Statements
- Go - Switch Statement
- Go - Select Statement
- Go Control Flow Statements
- Go - For Loop
- Go - Nested for Loops
- Go - Break Statement
- Go - Continue Statement
- Go - Goto Statement
- Go Functions
- Go - Functions
- Go - Call by Value
- Go - Call by Reference
- Go - Functions as Values
- Go - Function Closure
- Go - Function Method
- Go - Anonymous function
- Go Strings
- Go - Strings
- Go - String Length
- Go - String Concatenation
- Go - Compare Strings
- Go - Split String
- Go - Substring Extraction
- Go - String Replacement
- Go - String Interpolation
- Go - Parse Date Strings
- Go Arrays
- Go - Arrays
- Go - Multidimensional Arrays
- Go - Multidimensional Arrays
- Go - Passing Arrays to Functions
- Go - Pointers
- Go - Pointers
- Go - Array of pointers
- Go - Pointer to pointer
- Go - Passing pointers to functions
- Go Advanced Control Structures
- Go - Scope Rules
- Go - Dereferencing Pointer
- Go - Structures
- Go - Slice
- Go - Slice of Slices
- Go - Range
- Go - Maps
- Go - Recursion
- Go - Type Casting
- Go - Interfaces
- Go - Type Assertion
- Go - Error Handling
- Go - Concurrency
- Go - Regular Expression
- Go - Inheritance
- Go - Packages
- Go - Templates
- Go - Reflection
- Go - Generics
- Go File Handling
- Go - Read File By Word
- Go - Read File By Line
- Go - Read CSV Files
- Go - Delete File
- Go - Rename & Move File
- Go - Truncate a File
- Go - File Read-Write Mode W/O Truncation
- Go Miscellaneous
- Go - defer Keyword
- Go - Fmt Package
- Go - Zero Value
- Go - Import
Go - The Select Statement
Go select statement is useful to handle multiple channel operations and selects the ready channel against multiple channels.
The Select Statement
When you are working with multiple channels in Go language, the select statement waits on multiple channel operations and executes the code block for the channel that is ready.
Syntax
The syntax for a select statement in Go programming language is as follows −
select { case communication clause : statement(s); case communication clause : statement(s); /* you can have any number of case statements */ default : /* Optional */ statement(s); }
Rules for Using a Select Statement in Go
The following rules apply to a select statement −
- You can have any number of case statements within a select. Each case is followed by the value to be compared to and a colon.
- The type for a case must be the a communication channel operation.
- When the channel operation occured the statements following that case will execute. No break is needed in the case statement.
- A select statement can have an optional default case, which must appear at the end of the select. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.
Example of Select Statement
The following example demonstrates how you can use the select statement to handle multiple channels:
package main import "fmt" func main() { var c1, c2, c3 chan int var i1, i2 int select { case i1 = <-c1: fmt.Printf("received ", i1, " from c1\n") case c2 <- i2: fmt.Printf("sent ", i2, " to c2\n") case i3, ok := (<-c3): // same as: i3, ok := <-c3 if ok { fmt.Printf("received ", i3, " from c3\n") } else { fmt.Printf("c3 is closed\n") } default: fmt.Printf("no communication\n") } }
When the above code is compiled and executed, it produces the following result −
no communication
Nested select Statement
A nested select statement allows you to use one or more select statements inside a select statement.
Example
The following is an example of a nested select statement in Go language:
package main import ( "fmt" "time" ) func main() { // Defineing channels ch1 := make(chan string) ch2 := make(chan int) // Goroutine to send data to channel1 go func() { time.Sleep(1 * time.Second) ch1 <- "I am in channel 1" }() // Goroutine to send data to channel2 go func() { time.Sleep(3 * time.Second) ch2 <- 10 }() // Nested select statement select { case ch_data1 := <-ch1: fmt.Println("Received from channel1:", ch_data1) select { case ch_data2 := <-ch2: fmt.Println("Received from channel2:", ch_data2) default: fmt.Println("No data in channel2 yet") } case ch_data2 := <-ch2: fmt.Println("Received from channel2:", ch_data2) default: fmt.Println("No messages received yet from any channel") } }
When the above code is compiled and executed, it produces the following result −
No messages received yet from any channel