Standard library / net/http

http.Request.Clone()

Copy request metadata deeply except Body, and replace its context. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func (r *Request) Clone(ctx context.Context) *Request

Parameters & Attributes

NameTypeDescription
ctxcontext.ContextContext controlling cancellation.

Return Value

*Request

Operation result; check errors and validity indicators when returned.

Working Example

Example Snippetgo
package main
import (  "context"  "fmt"  "net/http")
func main() {  r := must(http.NewRequest("GET", "https://example.invalid/", nil))  c := r.Clone(context.Background())  c.Header.Set("X-Demo", "a")  fmt.Println(r.Header.Get("X-Demo") == "")}
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