Standard library / net/http/httputil

httputil.ClientConn.Write()

Write a request, recording it as pending until the response is read. ErrPersistEOF signals the end of persistent HTTP use. Legacy-only: use net/http Client or Server for new code. Expected stdout: '1\n'.

Formal Syntax & Structure

go syntaxgo
func (cc *ClientConn) Write(req *http.Request) error

Parameters & Attributes

NameTypeDescription
req*http.RequestHTTP request to inspect or exchange. Use only synthetic data in dumps.

Return Value

error

Nil on success, otherwise the operation error.

Working Example

Example Snippetgo
package main
import (  "bufio"  "fmt"  "net"  "net/http"  "net/http/httputil"  "time")
func main() {  c, s := net.Pipe()  defer c.Close()  defer s.Close()  check(c.SetDeadline(time.Now().Add(3 * time.Second)))  check(s.SetDeadline(time.Now().Add(3 * time.Second)))  cc := httputil.NewClientConn(c, nil)  req := must(http.NewRequest("GET", "http://demo.invalid/a", nil))  done := make(chan error, 1)  go func() { _, e := http.ReadRequest(bufio.NewReader(s)); done <- e }()  check(cc.Write(req))  check(<-done)  fmt.Println(cc.Pending())  check(cc.Close())}
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