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

Timeouts

This Go code demonstrates the use of the select statement with time.After to handle timeouts when waiting for channel communication. Let's go through it with inline comments:

package main

import (
	"fmt"
	"time"
)

func main() {
	// Creating a buffered channel 'c1' with a capacity of 1
	c1 := make(chan string, 1)

	// Launching a goroutine to send "result 1" to 'c1' after a 2-second delay
	go func() {
		time.Sleep(2 * time.Second)
		c1 <- "result 1"
	}()

	// The 'select' statement is used to either receive from 'c1' or timeout after 1 second
	select {
	case res := <-c1:
		fmt.Println(res)
	case <-time.After(1 * time.Second):
		fmt.Println("timeout 1")
	}

	// Creating another buffered channel 'c2' with a capacity of 1
	c2 := make(chan string, 1)

	// Launching a goroutine to send "result 2" to 'c2' after a 2-second delay
	go func() {
		time.Sleep(2 * time.Second)
		c2 <- "result 2"
	}()

	// The 'select' statement is used to either receive from 'c2' or timeout after 3 seconds
	select {
	case res := <-c2:
		fmt.Println(res)
	case <-time.After(3 * time.Second):
		fmt.Println("timeout 2")
	}
}

Output

timeout 1
result 2

Explanation:

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

  2. import (...): Imports necessary packages, including "fmt" for formatting and printing, and "time" for handling time-related operations.

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

  4. c1 := make(chan string, 1): Creates a buffered channel named 'c1' with a capacity of 1.

  5. A goroutine is launched to send "result 1" to 'c1' after a 2-second delay.

  6. The select statement is used to either receive a message from 'c1' or timeout after 1 second. If a message is received, it prints the result; otherwise, it prints a timeout message.

  7. c2 := make(chan string, 1): Creates another buffered channel named 'c2' with a capacity of 1.

  8. Another goroutine is launched to send "result 2" to 'c2' after a 2-second delay.

  9. The second select statement is used to either receive a message from 'c2' or timeout after 3 seconds. Similarly, it prints the result or a timeout message.

This code demonstrates how the select statement can be combined with time.After to handle timeouts when waiting for channel communication. The output will depend on which goroutine completes its work first, and the timeouts are used to handle scenarios where communication takes longer than expected.

PreviousSelectNextNon-Blocking Channel Operations

Last updated 1 year ago

Was this helpful?