Standard library / reflect

reflect.Select()

Waits for a ready case or selects a default case; returns the chosen index, received value and receive success flag. Multiple ready cases need not be selected in a predictable order. Expected stdout: '0 9 true\n'.

Formal Syntax & Structure

go syntaxgo
func Select(cases []SelectCase) (chosen int, recv Value, recvOK bool)

Parameters & Attributes

NameTypeDescription
cases[]SelectCaseOrdered list of select cases.

Return Value

(chosen int, recv Value, recvOK bool)

Waits for a ready case or selects a default case; returns the chosen index, received value and receive success flag.

Working Example

Example Snippetgo
package main
import (  "fmt"  "reflect")
func main() {  ch := make(chan int, 1)  v := reflect.ValueOf(ch)  ch <- 9  i, x, ok := reflect.Select([]reflect.SelectCase{{Dir: reflect.SelectRecv, Chan: v}, {Dir: reflect.SelectDefault}})  fmt.Println(i, x.Int(), ok)}
Go Reference