reflect.TrySend() Function in Golang with Examples Last Updated : 10 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.TrySend() Function in Golang attempts to send x on the channel v but will not block. To access this function, one needs to imports the reflect package in the program. Syntax: func (v Value) TrySend(x Value) bool Parameters: This function accept one parameter of Value type (x). Return Value: This function returns whether the value was sent. Below examples illustrate the use of the above method in Golang: Example 1: C // Golang program to illustrate // reflect.TrySend() Function package main import ( "fmt" "reflect" ) func main() { c := make(chan int, 1) vc := reflect.ValueOf(c) // use of TrySend() method succeeded := vc.TrySend(reflect.ValueOf(123)) fmt.Println(succeeded, vc.Len(), vc.Cap()) } Output: true 1 1 Example 2: C // Golang program to illustrate // reflect.TrySend() Function package main import ( "fmt" "reflect" ) func main() { c := make(chan int, 1) vc := reflect.ValueOf(c) // use of TrySend() method 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}, } 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 Comment More infoAdvertise with us S shubhamsingh10 Follow Improve Article Tags : Go Language Golang-reflect Explore Go Tutorial 3 min read OverviewGo Programming Language (Introduction) 7 min read How to Install Go on Windows? 3 min read How to Install Golang on MacOS? 4 min read Hello World in Golang 3 min read FundamentalsIdentifiers in Go Language 3 min read Go Keywords 2 min read Data Types in Go 7 min read Go Variables 9 min read Constants- Go Language 6 min read Go Operators 9 min read Control StatementsGo Decision Making (if, if-else, Nested-if, if-else-if) 5 min read Loops in Go Language 5 min read Switch Statement in Go 2 min read Functions & MethodsFunctions in Go Language 3 min read Variadic Functions in Go 3 min read Anonymous function in Go Language 2 min read main and init function in Golang 2 min read What is Blank Identifier(underscore) in Golang? 3 min read Defer Keyword in Golang 3 min read Methods in Golang 3 min read StructureStructures in Golang 7 min read Nested Structure in Golang 3 min read Anonymous Structure and Field in Golang 3 min read ArraysArrays in Go 7 min read How to Copy an Array into Another Array in Golang? 3 min read How to pass an Array to a Function in Golang? 2 min read SlicesSlices in Golang 14 min read Slice Composite Literal in Go 3 min read How to sort a slice of ints in Golang? 2 min read How to trim a slice of bytes in Golang? 3 min read How to split a slice of bytes in Golang? 3 min read StringsStrings in Golang 7 min read How to Trim a String in Golang? 2 min read How to Split a String in Golang? 3 min read Different ways to compare Strings in Golang 2 min read PointersPointers in Golang 8 min read Passing Pointers to a Function in Go 3 min read Pointer to a Struct in Golang 3 min read Go Pointer to Pointer (Double Pointer) 4 min read Comparing Pointers in Golang 3 min read Like