gobyexample/httpserver/httpserver.go

26 lines
471 B
Go
Raw Permalink Normal View History

2024-05-04 16:59:15 +00:00
package httpserver
import (
"fmt"
"net/http"
"strings"
)
func hello(rw http.ResponseWriter, req *http.Request) {
fmt.Fprintln(rw, "Hello, world!")
}
func headers(rw http.ResponseWriter, req *http.Request) {
for k, v := range req.Header {
fmt.Fprintf(rw, "%s: %v\n", k, strings.Join(v, " "))
}
}
func HttpServer() {
http.HandleFunc("/hello", hello)
http.HandleFunc("/headers", headers)
fmt.Println("Listening on 8002")
http.ListenAndServe(":8002", nil)
}