Standard library / reflect

reflect.Value.TryRecv()

Attempts a receive without blocking. An empty open channel produces an invalid Value; a closed drained channel produces a typed zero with false. Expected stdout: 'false false\ntrue false\n'.

Formal Syntax & Structure

go syntaxgo
func (v Value) TryRecv() (x Value, ok bool)

Return Value

(x Value, ok bool)

Attempts a receive without blocking.

Working Example

Example Snippetgo
package main
import (  "fmt"  "reflect")
func main() {  ch := make(chan int, 1)  v := reflect.ValueOf(ch)  x, ok := v.TryRecv()  fmt.Println(x.IsValid(), ok)  close(ch)  x, ok = v.TryRecv()  fmt.Println(x.IsValid(), ok)}
Go Reference