Header Clone Snapshots For Retries

Snapshot request headers with http.Header.Clone before mutating a retry copy, so late changes never leak back into the base header.

GoData Fetching

Code

gosnippet
22 lines
package main

import (
	"fmt"
	"net/http"
)

func main() {
	base := http.Header{}
	base.Set("X-Request-Id", "req-42")
	base.Set("Accept", "application/json")

	// Clone 深拷贝:改副本不影响原头
	retry := base.Clone()
	retry.Set("X-Retry-Count", "1")
	retry.Add("Accept", "text/plain")

	fmt.Println("base accept:", base.Values("Accept"))
	fmt.Println("retry accept:", retry.Values("Accept"))
	fmt.Println("retry has id:", retry.Get("X-Request-Id"))
	fmt.Println("base retry count:", base.Get("X-Retry-Count"))
}

How the snippet works

The whole behavior is carried by the function main.

Caveats

  • Nothing to install: the snippet stays inside the standard library.

Applying it

The main signature, verbatim, is func main()

The file’s steps are announced right in the output: base accept:, retry accept:, retry has id:.

Statements that carry the weight

base.Set("Accept", "application/json")

Interactive Tools for This Topic

FAQ

What does Header Clone Snapshots For Retries print when run?
Running the file directly is enough — expect labeled output lines such as `base accept:`, `retry accept:`, `retry has id:`.