有如下代码:
req, _ := http.NewRequest("POST", "http://example.com", nil)
req.Header.Set("Content-Type", "application/json")
追了一下 golang 源码 header 是 map[string][]string 类型,
set 方法的源码是:
func (h Header) Set(key, value string) {
textproto.MIMEHeader(h).Set(key, value)
}
看上去,在做 header set 的时候,golang 并没有做加锁解锁等操作。
我还去看了下 resty 请求库和 gorequest 请求库,发现实现方式和 go 原生的是一样的。
我的需求:要并发发起 http post 请求,每个请求需要设置不同的 auth header 头
我的疑问:
1 、在设置 header 的时候是否存在 map 并发读写的问题?
2 、在并发发起 http post 请求的时候,request 是共用一个,还是每次发起都 new 一个?
req, _ := http.NewRequest("POST", "http://example.com", nil)
req.Header.Set("Content-Type", "application/json")
追了一下 golang 源码 header 是 map[string][]string 类型,
set 方法的源码是:
func (h Header) Set(key, value string) {
textproto.MIMEHeader(h).Set(key, value)
}
看上去,在做 header set 的时候,golang 并没有做加锁解锁等操作。
我还去看了下 resty 请求库和 gorequest 请求库,发现实现方式和 go 原生的是一样的。
我的需求:要并发发起 http post 请求,每个请求需要设置不同的 auth header 头
我的疑问:
1 、在设置 header 的时候是否存在 map 并发读写的问题?
2 、在并发发起 http post 请求的时候,request 是共用一个,还是每次发起都 new 一个?