October 2015
Beginner to intermediate
400 pages
14h 44m
English
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 = ...