Code snippets with tests and testable examples for the Go programming language
package main import ( "fmt" "net/http" "net/http/httptest" ) func Example() { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) w.Write([]byte("hello world")) })) defer ts.Close() resp, err := http.Get(ts.URL) if err != nil { fmt.Println(err) } defer resp.Body.Close() fmt.Printf("%s\n", resp.Status) fmt.Printf("%d\n", resp.StatusCode) // Output: // 200 OK // 200 }
by psampaz - source code - comment below or here