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

Context

This program is a simple HTTP server written in Go that defines an HTTP handler function called hello. This handler uses the request's context to implement a timeout for processing the request.

Here's a breakdown of the code:

package main

import (
	"fmt"
	"net/http"
	"time"
)

// hello is an HTTP handler function that responds with "hello\n".
func hello(w http.ResponseWriter, req *http.Request) {
	// Get the request context.
	ctx := req.Context()

	fmt.Println("server: hello handler started")
	// Defer a statement to print a message when the handler ends.
	defer fmt.Println("server: hello handler ended")

	// Use a select statement to implement a timeout using time.After.
	select {
	case <-time.After(10 * time.Second):
		// Respond with "hello\n" if the operation completes within 10 seconds.
		fmt.Fprintf(w, "hello\n")
	case <-ctx.Done():
		// If the context is canceled (due to a timeout or cancellation),
		// handle the error and respond with an internal server error.
		err := ctx.Err()
		fmt.Println("server:", err)
		internalError := http.StatusInternalServerError
		http.Error(w, err.Error(), internalError)
	}
}

func main() {
	// Register the /hello path with the hello handler.
	http.HandleFunc("/hello", hello)
	// Start the HTTP server on port 8090.
	http.ListenAndServe(":8090", nil)
}

Explanation:

  1. hello is an HTTP handler function that uses the request's context (ctx). It prints messages when the handler starts and ends.

  2. Inside the select statement, there are two cases:

    • If the operation (responding with "hello\n") completes within 10 seconds, the response is sent.

    • If the context is canceled (due to a timeout or cancellation), an error message is printed, and an internal server error response is sent.

  3. In the main function:

    • The /hello path is registered with the hello handler using http.HandleFunc.

    • The server is started using http.ListenAndServe on port 8090.

This program demonstrates a simple way to handle timeouts in an HTTP server using the request context.

PreviousHTTP ServerNextSpawning Processes

Last updated 1 year ago

Was this helpful?