August 2019
Beginner to intermediate
798 pages
17h 2m
English
In this section, we are going to learn how to test HTTP handlers in Go. We will begin with the Go code for www.go and modify it where needed.
The new version of www.go is called testWWW.go, and it will be presented in three parts. The first code portion of testWWW.go is as follows:
package main
import (
"fmt"
"net/http"
"os"
)
func CheckStatusOK(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `Fine!`)
}
The second part of testWWW.go is contained in the following Go code:
func StatusNotFound(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNotFound) } func MyHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Serving: %s\n", r.URL.Path) fmt.Printf("Served: ...Read now
Unlock full access