Programming Code Center(PCC)
[GO]

(PCC)::[How-to-select-with-a-default-clause-to-implement-non-blocking-sends-receives-multi-way-selects-in-go-language]::[go]

File Name : non-blocking-channel-operations.go

package main

import "fmt"

func main() {
    messages := make(chan string)
    signals := make(chan bool)

    select {
    case msg := <-messages:
        fmt.Println("received message", msg)
    default:
        fmt.Println("no message received")
    }

    msg := "hi"
    select {
    case messages <- msg:
        fmt.Println("sent message", msg)
    default:
        fmt.Println("no message sent")
    }

    select {
    case msg := <-messages:
        fmt.Println("received message", msg)
    case sig := <-signals:
        fmt.Println("received signal", sig)
    default:
        fmt.Println("no activity")
    }
}

Output :

non-blocking-channel-operations.jpg