Standard library / net/http

http.ErrContentLength

Returned when a response write exceeds the declared Content-Length. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
var ErrContentLength error

Working Example

Example Snippetgo
package main
import (  "errors"  "fmt"  "net/http"  "net/http/httptest")
func main() {  ch := make(chan bool, 1)  s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {    w.Header().Set("Content-Length", "1")    _, e := w.Write([]byte("xx"))    ch <- errors.Is(e, http.ErrContentLength)    fmt.Fprint(w, "x")  }))  defer s.Close()  res := must(s.Client().Get(s.URL))  check(res.Body.Close())  fmt.Println(<-ch)}
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