HTTP Server
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)
}Last updated