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 uses HTTPS_PROXY only. In my test, setting only HTTP_PROXY sent https://example.com direct (200 OK). With HTTPS_PROXY set, the same request failed with proxyconnect tcp: ... connection refused.
  • The value can be a full URL or just host[:port]. With host[:port], Go assumes http.
  • localhost is never proxied. ProxyFromEnvironment returns no proxy when the request host is localhost.
  • 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.com matches example.com and api.example.com)
  • a domain with a leading dot, which matches subdomains only (.example.com doesn't match example.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 
  • direct means fetch from the version control repository, off means 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.com lists module path patterns that should never be requested from a proxy. It's the default for both GONOPROXY and GONOSUMDB.
  • 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_PROXY is set for the process, and that a custom Transport sets Proxy.
  • An internal host still goes through the proxy → check NO_PROXY for typos. Remember the leading-dot rule.
  • go get is blocked by the network → set HTTPS_PROXY, not GOPROXY.
  • 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