reflect.Select() Function in Golang with Examples Last Updated : 03 May, 2020 Comments Improve Suggest changes Like Article Like Report Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package. The reflect.Select() Function in Golang is used to executes a select operation described by the list of cases. To access this function, one needs to imports the reflect package in the program. Syntax: func Select(cases []SelectCase) (chosen int, recv Value, recvOK bool) Parameters: This function takes the following parameters: cases : This parameter is the list of cases. Return Value: This function returns the index of the chosen case. Below examples illustrate the use of the above method in Golang: Example 1: C // Golang program to illustrate // reflect.Select() Function package main import ( "fmt" "reflect" ) func main() { c := make(chan int, 1) vc := reflect.ValueOf(c) succeeded := vc.TrySend(reflect.ValueOf(123)) fmt.Println(succeeded, vc.Len(), vc.Cap()) vSend, vZero := reflect.ValueOf(789), reflect.Value{} branches := []reflect.SelectCase{ {Dir: reflect.SelectDefault, Chan: vZero, Send: vZero}, {Dir: reflect.SelectRecv, Chan: vc, Send: vZero}, {Dir: reflect.SelectSend, Chan: vc, Send: vSend}, } // use of Select() method selIndex, vRecv, sentBeforeClosed := reflect.Select(branches) fmt.Println(selIndex) fmt.Println(sentBeforeClosed) fmt.Println(vRecv.Int()) vc.Close() // Remove the send case branch this time, // for it may cause panic. selIndex, _, sentBeforeClosed = reflect.Select(branches[:2]) fmt.Println(selIndex, sentBeforeClosed) } Output: true 1 1 1 true 123 1 false Example 2: C // Golang program to illustrate // reflect.Select() Function package main import ( "fmt" "reflect" ) func main() { var sendCh = make(chan int) var increaseInt = func(c chan int) { for i := 0; i < 8; i++ { c <- i } close(c) } go increaseInt(sendCh) var selectCase = make([]reflect.SelectCase, 1) selectCase[0].Dir = reflect.SelectRecv selectCase[0].Chan = reflect.ValueOf(sendCh) counter := 0 for counter < 1 { // use of Select() method chosen, recv, recvOk := reflect.Select(selectCase) if recvOk { fmt.Println(chosen, recv.Int(), recvOk) } else { counter++ } } } Output: 0 0 true 0 1 true 0 2 true 0 3 true 0 4 true 0 5 true 0 6 true 0 7 true Comment More infoAdvertise with us Next Article Interview Preparation For Software Developers S shubhamsingh10 Follow Improve Article Tags : Go Language Golang-reflect Similar Reads Interview PreparationInterview Preparation For Software DevelopersMust Coding Questions - Company-wise Must Do Coding Questions - Topic-wiseCompany-wise Practice ProblemsCompany PreparationCompetitive ProgrammingSoftware Design-PatternsCompany-wise Interview ExperienceExperienced - Interview ExperiencesInternship - Interview ExperiencesPractice @GeeksforgeeksProblem of the DayTopic-wise PracticeDifficulty Level - SchoolDifficulty Level - BasicDifficulty Level - EasyDifficulty Level - MediumDifficulty Level - HardLeaderboard !!Explore More...Data StructuresArraysLinked ListStackQueueBinary TreeBinary Search TreeHeapHashingGraphAdvance Data StructuresMatrixStringAll Data StructuresAlgorithmsAnalysis of AlgorithmsSearching AlgorithmsSorting AlgorithmsPattern SearchingGeometric AlgorithmsMathematical AlgorithmsRandomized AlgorithmsGreedy AlgorithmsDynamic ProgrammingDivide & ConquerBacktrackingBranch & BoundAll AlgorithmsProgramming LanguagesCC++JavaPythonC#Go LangSQLPHPScalaPerlKotlinWeb TechnologiesHTMLCSSJavaScriptBootstrapTailwind CSSAngularJSReactJSjQueryNodeJSPHPWeb DesignWeb BrowserFile FormatsComputer Science SubjectsOperating SystemsDBMSComputer NetworkComputer Organization & ArchitectureTOCCompiler DesignDigital Elec. & Logic DesignSoftware EngineeringEngineering MathematicsData Science & MLComplete Data Science CourseData Science TutorialMachine Learning TutorialDeep Learning TutorialNLP TutorialMachine Learning ProjectsData Analysis TutorialTutorial LibraryPython TutorialDjango TutorialPandas TutorialKivy TutorialTkinter TutorialOpenCV TutorialSelenium TutorialGATE CSGATE CS NotesGate CornerPrevious Year GATE PapersLast Minute Notes (LMNs)Important Topic For GATE CSGATE CoursePrevious Year Paper: CS exams Like