Rebuild for tabs

This commit is contained in:
Mark McGranaghan 2019-06-04 07:44:57 -07:00
parent fab09ef735
commit 573cb47955

View File

@ -3,8 +3,8 @@
package main package main
import ( import (
"fmt" "fmt"
"net/http" "net/http"
) )
// A fundamental concept in `net/http` servers is // A fundamental concept in `net/http` servers is
@ -14,37 +14,37 @@ import (
// on functions with the appropriate signature. // on functions with the appropriate signature.
func hello(w http.ResponseWriter, req *http.Request) { func hello(w http.ResponseWriter, req *http.Request) {
// Functions serving as handlers take a // Functions serving as handlers take a
// `http.ResponseWriter` and a `http.Request` as // `http.ResponseWriter` and a `http.Request` as
// arguments. The response writer is used to fill in the // arguments. The response writer is used to fill in the
// HTTP response. Here out simple response is just // HTTP response. Here out simple response is just
// "hello\n". // "hello\n".
fmt.Fprintf(w, "hello\n") fmt.Fprintf(w, "hello\n")
} }
func headers(w http.ResponseWriter, req *http.Request) { func headers(w http.ResponseWriter, req *http.Request) {
// This handler does something a little more // This handler does something a little more
// sophisticated by reading all the HTTP request // sophisticated by reading all the HTTP request
// headers and echoing them into the response body. // headers and echoing them into the response body.
for name, headers := range req.Header { for name, headers := range req.Header {
for _, h := range headers { for _, h := range headers {
fmt.Fprintf(w, "%v: %v\n", name, h) fmt.Fprintf(w, "%v: %v\n", name, h)
} }
} }
} }
func main() { func main() {
// We register our handlers on server routes using the // We register our handlers on server routes using the
// `http.HandleFunc` convenience function. It sets up // `http.HandleFunc` convenience function. It sets up
// the *default router* in the `net/http` package and // the *default router* in the `net/http` package and
// takes a function as an argument. // takes a function as an argument.
http.HandleFunc("/hello", hello) http.HandleFunc("/hello", hello)
http.HandleFunc("/headers", headers) http.HandleFunc("/headers", headers)
// Finally, we call the `ListenAndServe` with the port // Finally, we call the `ListenAndServe` with the port
// and a handler. `nil` tells it to use the default // and a handler. `nil` tells it to use the default
// router we've just set up. // router we've just set up.
http.ListenAndServe(":8090", nil) http.ListenAndServe(":8090", nil)
} }