Standard library / net/http/httptest

httptest.ResponseRecorder.Write()

Records bytes and commits an implicit status if needed. Expected stdout: '2 ok\n'.

Formal Syntax & Structure

go syntaxgo
func (rw *ResponseRecorder) Write(buf []byte) (int, error)

Parameters & Attributes

NameTypeDescription
buf[]byteInput value of the declared type; see the usage example.

Return Value

(int, error)

Operation result; check errors and validity indicators when returned.

Working Example

Example Snippetgo
package main
import (  "fmt"  "net/http/httptest")
func main() {  w := httptest.NewRecorder()  n := must(w.Write([]byte("ok")))  fmt.Println(n, w.Body.String())}
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