Standard library / net/http

http.Response.Write()

Serializes an HTTP response in HTTP/1 wire format, reading and closing its body. Expected stdout: 'true true\n'.

Formal Syntax & Structure

go syntaxgo
func (r *Response) Write(w io.Writer) error

Parameters & Attributes

NameTypeDescription
wio.WriterDestination writer.

Return Value

error

Operation result; check errors and validity indicators when returned.

Working Example

Example Snippetgo
package main
import (  "bufio"  "bytes"  "fmt"  "net/http"  "strings")
func main() {  r := must(http.ReadResponse(bufio.NewReader(strings.NewReader("HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nok")), nil))  var b bytes.Buffer  check(r.Write(&b))  fmt.Println(strings.HasPrefix(b.String(), "HTTP/1.1 200 OK"), strings.HasSuffix(b.String(), "ok"))}
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