1.7 A Web Server
Go’s libraries makes it easy to write a web server that responds to client
requests like those made by fetch
.
In this section, we’ll show a minimal server that
returns the path component of the URL used to access the server.
That is, if the request is for http://localhost:8000/hello
, the
response will be URL.Path = "/hello"
.
// Server1 is a minimal "echo" server. package main import ( "fmt" "log" "net/http" ) func main() { http.HandleFunc("/", handler) // each request calls handler log.Fatal(http.ListenAndServe("localhost:8000", nil)) } // handler echoes the Path component of the requested URL. func handler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "URL.Path = ...
Get The Go Programming Language now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.