Standard library / net/http/httputil

httputil.ClientConn.Hijack()

Detach the connection and buffered reader. Caller assumes ownership; do not call during a read or write. Legacy-only: use net/http Client or Server for new code. Expected stdout: 'true true\n'.

Formal Syntax & Structure

go syntaxgo
func (cc *ClientConn) Hijack() (c net.Conn, r *bufio.Reader)

Return Value

(c net.Conn, r *bufio.Reader)

Detached connection and buffered reader; caller owns both.

Working Example

Example Snippetgo
package main
import (  "fmt"  "net"  "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)  conn, r := cc.Hijack()  fmt.Println(conn == c, r != nil)  check(conn.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