By Leo Lin, Product Manager at FlashIP (https://www.flaship.com/), Singapore. Disclosure: I work on FlashIP.
Go has two settings with "proxy" in the name. They do different jobs, and people mix them up all the time. HTTPS_PROXY is a network proxy that your traffic passes through. GOPROXY is a module mirror that go get downloads code from. If you're behind a corporate gateway and go mod download times out, changing GOPROXY won't help. If your company runs a private module mirror, setting HTTPS_PROXY won't point Go at it.
Below is how each one works, based on the official Go docs and a few tests I ran on Go 1.27 against a dead proxy at 127.0.0.1:9. Hostnames and credentials are placeholders.
1. net/http reads HTTP_PROXY, HTTPS_PROXY and NO_PROXY
http.DefaultTransport, which http.Get and http.DefaultClient use, picks up its proxy from the environment through http.ProxyFromEnvironment. It reads HTTP_PROXY, HTTPS_PROXY and NO_PROXY, or their lowercase versions, and chooses the proxy by the request's scheme:
export HTTPS_PROXY=http://user:pass@proxy.example.com:8080 export HTTP_PROXY=http://user:pass@proxy.example.com:8080 export NO_PROXY=localhost,127.0.0.1,.internal.example.com,10.0.0.0/8 go run . Some details that trip people up:
- The scheme decides. An
https://URL usesHTTPS_PROXYonly. In my test, setting onlyHTTP_PROXYsenthttps://example.comdirect (200 OK). WithHTTPS_PROXYset, the same request failed withproxyconnect tcp: ... connection refused. - The value can be a full URL or just
host[:port]. Withhost[:port], Go assumeshttp. - localhost is never proxied.
ProxyFromEnvironmentreturns no proxy when the request host islocalhost. - The environment is read once. In my test, calling
os.Setenv("HTTPS_PROXY", ...)after the first request had no effect, and the second request still went direct. Set the variables before the process starts, or set the proxy in code (section 3).
2. NO_PROXY syntax in Go
According to the golang.org/x/net/http/httpproxy docs, NO_PROXY is a comma-separated list. Each entry can be:
- an IP prefix (
1.2.3.4) or a CIDR block (10.0.0.0/8) - a domain name, which matches the name and all its subdomains (
example.commatchesexample.comandapi.example.com) - a domain with a leading dot, which matches subdomains only (
.example.comdoesn't matchexample.com) - an IP or domain with a port (
1.2.3.4:80) -
*, which turns proxying off completely
Parsing is best effort and errors are ignored. A typo won't throw an error. Go just ignores that entry and proxies the request.
3. Set the proxy in code (including SOCKS5)
To use a proxy that doesn't depend on the environment, set Transport.Proxy:
proxyURL, _ := url.Parse("http://user:pass@proxy.example.com:8080") client := &http.Client{ Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)}, } resp, err := client.Get("https://example.com") Transport.Proxy supports the http, https, socks5 and socks5h schemes. If the scheme is empty, Go uses http. Go treats socks5 the same as socks5h. Username and password in the URL are sent in a Proxy-Authorization header. If Proxy is nil, Go doesn't use a proxy. So a Transport you build yourself without setting Proxy ignores HTTPS_PROXY entirely. To keep the environment behavior on a custom transport, set Proxy: http.ProxyFromEnvironment.
My test confirmed SOCKS5 works from both places: http.ProxyURL with a socks5:// URL and HTTPS_PROXY=socks5://127.0.0.1:9 both tried the dead proxy and failed.
4. GOPROXY is a module mirror, not a network proxy
GOPROXY tells the go command where to download modules. Its default value is:
https://proxy.golang.org,direct -
directmeans fetch from the version control repository,offmeans don't fetch at all, and the separator controls fallback. After a comma, Go tries the next entry only on a 404 or 410 response. After a pipe (|), it tries the next entry on any error, timeouts included. -
GOPRIVATE=*.corp.example.comlists module path patterns that should never be requested from a proxy. It's the default for bothGONOPROXYandGONOSUMDB. - A private mirror can take credentials in the URL:
GOPROXY=https://user:pass@proxy.corp.example.com.
To save these settings, use go env -w GOPROXY=.... It writes to a config file in your user config directory, and GOENV changes where that file lives. go env -w only accepts Go's own variables, though. go env -w HTTPS_PROXY=... failed with unknown go command variable HTTPS_PROXY, so the network proxy still belongs in your shell or service environment.
The go command does honor HTTPS_PROXY when it talks to a module mirror. With a dead proxy in HTTPS_PROXY, go mod download failed with a proxyconnect error against proxy.golang.org. For direct fetches Go runs git, and Git has its own http.proxy setting. Its docs describe that setting as overriding http_proxy/https_proxy/all_proxy.
Quick triage
- curl uses the proxy but your Go binary doesn't → check that
HTTPS_PROXYis set for the process, and that a customTransportsetsProxy. - An internal host still goes through the proxy → check
NO_PROXYfor typos. Remember the leading-dot rule. -
go getis blocked by the network → setHTTPS_PROXY, notGOPROXY. - Private modules return 404/410 or fail checksum lookups → set
GOPRIVATE.
If you're testing against an external proxy, FlashIP (https://www.flaship.com/) offers HTTP(S) and SOCKS5 endpoints with username:password or IP whitelist authentication. The URL formats above work for either type.
Sources
- net/http package docs: https://pkg.go.dev/net/http
- httpproxy package (NO_PROXY rules): https://pkg.go.dev/golang.org/x/net/http/httpproxy
- Go Modules Reference (GOPROXY, GOPRIVATE): https://go.dev/ref/mod
- cmd/go (go env -w, GOENV): https://pkg.go.dev/cmd/go
- git-config (http.proxy): https://git-scm.com/docs/git-config

