Code with Go
About
  • Code With Go! 🚀
  • Hello World
  • Values
  • Variables
  • Constants
  • For Loop
  • If Else
  • Switch
  • Array
  • Slice
  • Map
  • Range
  • Functions
  • Multiple Return Values
  • Variadic Functions
  • Closure
  • Recursion
  • Pointers
  • Strings and Runes
  • Structs
  • Methods
  • Interfaces
  • Struct Embedding
  • Generics
  • Error
  • Goroutines
  • Channels
  • Channel Buffering
  • Channel Synchronization
  • Channel Directions
  • Select
  • Timeouts
  • Non-Blocking Channel Operations
  • Closing Channels
  • Range over Channels
  • Timers
  • Tickers
  • Worker Pools
  • Wait Groups
  • Rate Limiting
  • Atomic Counters
  • Mutexes
  • Stateful Goroutines
  • Sorting
  • Sorting by Functions
  • Panic
  • Defer
  • Recover
  • String Functions
  • String Formatting
  • Text Templates
  • Regular Expressions
  • JSON
  • XML
  • Time
  • Epoch
  • Time Formatting / Parsing
  • Random Numbers
  • Number Parsing
  • SHA256 Hashes
  • Base64 Encoding
  • Reading Files
  • Writing Files
  • Line Filters
  • File Paths
  • Directories
  • Temporary Files and Directories
  • Embed Directive
  • Testing and Benchmarking
  • Command-Line Arguments
  • Command-Line Flags
  • Command-Line Subcommands
  • Environment Variables
  • Logging
  • HTTP Client
  • HTTP Server
  • Context
  • Spawning Processes
  • Exec'ing Processes
  • Signals
  • Exit
  • Resize Image
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub

Non-Blocking Channel Operations

This Go code demonstrates the use of the select statement with default cases to handle multiple channel operations. Let's go through it with inline comments:

package main

import "fmt"

func main() {
    // Creating an unbuffered channel 'messages' for string communication
    messages := make(chan string)

    // Creating an unbuffered channel 'signals' for boolean communication
    signals := make(chan bool)

    // The first 'select' statement attempts to receive a message from 'messages'
    select {
    case msg := <-messages:
        fmt.Println("received message", msg)
    default:
        fmt.Println("no message received")
    }

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

    // The third 'select' statement tries to receive a message from 'messages'
    // or a signal from 'signals' with a 'default' case for handling no activity
    select {
    case msg := <-messages:
        fmt.Println("received message", msg)
    case sig := <-signals:
        fmt.Println("received signal", sig)
    default:
        fmt.Println("no activity")
    }
}

Explanation:

  1. package main: Indicates that this Go file belongs to the main executable package.

  2. import "fmt": Imports the "fmt" package for formatting and printing.

  3. func main() { ... }: The main function, where the execution of the program begins.

  4. messages := make(chan string): Creates an unbuffered channel named 'messages' for string communication.

  5. signals := make(chan bool): Creates an unbuffered channel named 'signals' for boolean communication.

  6. The first select statement attempts to receive a message from 'messages'. Since there is no message at this point, the default case is executed, and "no message received" is printed.

  7. msg := "hi": Defines a string variable 'msg' with the value "hi".

  8. The second select statement attempts to send the message "hi" to 'messages'. Since the channel is unbuffered and there is no receiver ready to receive the message, the default case is executed, and "no message sent" is printed.

  9. The third select statement tries to receive a message from 'messages' or a signal from 'signals'. However, neither message nor signal is present, so the default case is executed, and "no activity" is printed.

In summary, this code demonstrates the use of the select statement with default cases to handle channel operations. It shows how to check for activity on channels, handle cases where a channel operation cannot proceed immediately, and provide default actions when no activity occurs.

PreviousTimeoutsNextClosing Channels

Last updated 1 year ago

Was this helpful?