如何为HTTP.Request添加额外的标头以跟踪问题[重复]

This question already has an answer here:

In golang, it seems no hook methods exist for http.client, so I'm wondering how can I add extra headers like trace-id: xxx in framework.

Ideal code is like below:

// this func add a hook method to http-client to rewrite header
client := buildWithRewriteHeaderHook()
//customRequest has no `trace-id`
client.Do(customRequest)
// remote server get trace-id from http headers
...

PS: I know how to add headers to a request by 'req.setHeader()', and in my opinion it's not a duplicate question to this one. How to set headers in http get request?

</div>

You should use headers.

You can do:

r.Header.Add("trace-id", "yourid")

You can also use open source package such as the opentracing one, which follows open tracing conventions and has method to add and read tracing information from requests: https://github.com/opentracing/opentracing-go