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.

Last updated