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

HTTP Server

This program is a simple HTTP server written in Go. It defines two HTTP handlers: /hello and /headers. When the server receives a request to either of these paths, it will respond accordingly.

Here's a breakdown of the code:

package main

import (
	"fmt"
	"net/http"
)

// hello is an HTTP handler function that responds with "hello\n".
func hello(w http.ResponseWriter, req *http.Request) {
	fmt.Fprintf(w, "hello\n")
}

// headers is an HTTP handler function that responds with the request headers.
func headers(w http.ResponseWriter, req *http.Request) {
	// Iterate over request headers and write them to the response.
	for name, headers := range req.Header {
		for _, h := range headers {
			fmt.Fprintf(w, "%v: %v\n", name, h)
		}
	}
}

func main() {
	// Register the /hello and /headers handlers with the default ServeMux.
	http.HandleFunc("/hello", hello)
	http.HandleFunc("/headers", headers)

	// Start the HTTP server on port 8090.
	http.ListenAndServe(":8090", nil)
}

Explanation:

  1. hello is an HTTP handler function that writes the string "hello\n" to the response writer (w).

  2. headers is an HTTP handler function that iterates over the request headers and writes them to the response writer.

  3. In the main function:

    • The /hello and /headers paths are registered with their corresponding handler functions using http.HandleFunc.

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

To test the server, you can make requests to http://localhost:8090/hello and http://localhost:8090/headers. The /hello path will respond with "hello\n", and the /headers path will respond with the headers of the incoming request.

PreviousHTTP ClientNextContext

Last updated 1 year ago

Was this helpful?