For most Go programs, setting HTTP_PROXY and HTTPS_PROXY is enough: the default net/http transport reads proxy settings from the environment. Use a custom http.Transport with its Proxy field when your application needs fixed or per-request routing. The http.Client handles requests; its transport decides how connections are routed.
Choose environment settings or an explicit transport
| Approach | Best for | What to check |
|---|---|---|
| Default environment-aware transport | Proxy configuration managed by the deployment environment | Confirm the process receives the intended variables and that NO_PROXY covers destinations that should connect directly. The default transport uses ProxyFromEnvironment as documented by Go’s net/http package and transport source. |
Custom Transport.Proxy |
Fixed or application-specific proxy routing | Choose the proxy URL for each request. A newly constructed transport does not automatically inherit the default transport’s proxy function; set Proxy explicitly if you want proxy routing. |
With a custom transport, a nil Proxy field—or a proxy function that returns a nil URL—means the request uses no proxy. If your application wants environment-based routing on a custom transport, set Proxy: http.ProxyFromEnvironment.
As an Amazon Associate I earn from qualifying purchases.
Configure a proxy through environment variables
For an HTTP destination, Go consults HTTP_PROXY; for an HTTPS destination, it consults HTTPS_PROXY. NO_PROXY specifies destinations that should bypass proxying. A proxy value can be a URL or a host[:port] value; when the scheme is omitted, Go assumes http. Invalid values can cause proxy selection to return an error.
Free tools Windows power users keep installed
One-click scans. No signup required.
export HTTP_PROXY="http://proxy.example:8080"
export HTTPS_PROXY="http://proxy.example:8080"
export NO_PROXY="localhost,.internal.example"
These example values show syntax only; they are not a live proxy endpoint. Supply the actual values through your deployment’s configuration, and avoid putting production credentials in shell commands that may be saved in shell history.
#1 Best Overall
Check what NO_PROXY matches
Go’s vendored httpproxy implementation documents comma-separated domain names, IP prefixes, CIDR values, optional ports, the wildcard *, and special treatment for localhost and loopback addresses. The precise matching behavior is an implementation detail rather than a promise covered by Go’s compatibility guarantee; check the source for the Go version you deploy if a bypass rule is important.
Set a fixed proxy on an http.Client
For application-controlled configuration, parse the proxy URL and assign http.ProxyURL to a reusable transport:
Rank #2
package proxyclient
import (
"net/http"
"net/url"
)
func newClient() (*http.Client, error) {
proxyURL, err := url.Parse("http://proxy.example:8080")
if err != nil {
return nil, err
}
transport := &http.Transport{
Proxy: http.ProxyURL(proxyURL),
}
return &http.Client{Transport: transport}, nil
}
Replace the example URL with the proxy address for your application. Create the client and transport once and reuse them rather than constructing a new transport for every request.
Recommended Free Tools
Select a proxy dynamically
For request-specific routing, assign a function to Transport.Proxy. It receives the outgoing request and returns the proxy URL to use, or nil when that request should connect directly. Keep the selection policy in that function explicit—for example, base it on the request host—and return any parsing or policy errors rather than silently routing to an unintended destination.
Understand HTTPS proxying and proxy credentials
When an HTTPS destination uses an HTTP proxy, Go’s transport establishes a tunnel with the proxy using CONNECT, then carries the HTTPS connection through it. An ordinary call such as client.Get("https://example.com") does not require you to construct a CONNECT request yourself. The Transport documentation cautions against treating Transport as a general-purpose mechanism for issuing CONNECT requests.
The documented proxy URL schemes include http, https, socks5, and socks5h. The documentation says socks5 is treated like socks5h. If a proxy URL contains user information, Go sends those credentials in a Proxy-Authorization header. Handle proxy credentials as secrets: do not publish them in source code or logs, and restrict access to the configuration that supplies them.
Rank #4
Reuse clients and manage connections
Go’s net/http documentation states: “Clients and Transports are safe for concurrent use by multiple goroutines and for efficiency should only be created once and re-used.” A transport also caches connections for later use, so reusing it can avoid repeatedly establishing connections.
PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteIf an application contacts many hosts or needs to control idle connections, the transport documentation describes CloseIdleConnections, MaxIdleConnsPerHost, and DisableKeepAlives as relevant controls. Choose them based on the application’s connection-management needs rather than creating a fresh transport per request.
Quick Recap
Best Value
Troubleshoot a proxy that appears to be ignored
- Check the destination scheme. HTTP requests use
HTTP_PROXY; HTTPS requests useHTTPS_PROXY. - Check the process environment. The variables must be present in the environment of the running Go process, not only in an interactive shell.
- Check bypass rules. A matching
NO_PROXYentry can make the destination connect directly. - Check custom transports. If you supplied
&http.Transport{}, itsProxyfield is not automatically copied from the default transport. SetProxy: http.ProxyFromEnvironmentor provide a fixed or dynamic proxy function. - Check URL form and errors. Use a valid proxy URL or
host[:port]; malformed settings can fail proxy selection. - Check CGI deployments. The vendored proxy-selection implementation infers CGI from the presence of
REQUEST_METHOD. WhenHTTP_PROXYwould apply in that case, it returns an error because request-derived environment data could be malicious. This specific safeguard does not mean Go rejects every proxy variable in every runtime. See the implementation.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




