Standard library / net/http/httptest

httptest.ResponseRecorder.Result()

Builds a response snapshot after the handler completes; inspect fields individually rather than DeepEqual on the entire response. Expected stdout: '200 ok\n'.

Formal Syntax & Structure

go syntaxgo
func (rw *ResponseRecorder) Result() *http.Response

Return Value

*http.Response

Operation result; check errors and validity indicators when returned.

Working Example

Example Snippetgo
package main
import (  "fmt"  "io"  "net/http/httptest")
func main() {  w := httptest.NewRecorder()  fmt.Fprint(w, "ok")  r := w.Result()  defer r.Body.Close()  fmt.Println(r.StatusCode, string(must(io.ReadAll(r.Body))))}
func must[T any](v T, e error) T {  if e != nil {    panic(e)  }  return v}func check(e error) {  if e != nil {    panic(e)  }}
Go Reference