golang程序判断用户是否使用代理

golang program determine if user uses proxy

我希望我的 golang http 客户端仅在用户提供代理值时使用代理。

// Make HTTP GET/POST request
proxyUrl, err := url.Parse(proxy)
tr := &http.Transport{
      DisableKeepAlives: true,
      Proxy:             http.ProxyURL(proxyUrl),
}

即使代理变量为空,上述代码也始终尝试通过代理进行连接。

感谢您的建议。现在我可以让它工作了。下面是修改后的代码。

tr := &http.Transport{}
tr.DisableKeepAlives = true
if len(proxy) != 0 { // Set the proxy only if the proxy param is specified
    proxyUrl, err := url.Parse(proxy)
    if err == nil {
        tr.Proxy = http.ProxyURL(proxyUrl)
    }
 }