Code and command line
Go proxy setup
Go keeps proxy support inside the standard library. An http.Transport takes a function that returns the proxy URL for each request, and http.ProxyURL turns a fixed URL into that function. Credentials in the URL become the Proxy-Authorization header for both plain requests and CONNECT tunnels.
The safest way to build that URL is with url.UserPassword, which escapes the password for you. From there, the same client is safe to share across goroutines.
What do you need before adding a proxy in Go?
Tick these off before the first step. Your login and password appear in your account once the first top-up of $40 or more has been credited.
| Requirement | Detail |
|---|---|
| Go 1.21 or newer | Any supported Go release covers everything here. |
| Standard library only | net/http and net/url, no extra modules. |
| TopProxy login and password | Login such as tp7k2m9_country-us, password from an environment variable. |
| A module | go mod init to run the examples as a small program. |
Where do the proxy host, port and login go in Go?
Work through the steps in order. Swap YOUR_PASSWORD for your proxy password and tp7k2m9 for your account name.
Build the proxy URL safely
Fill a
url.URLstruct instead of formatting a string.url.UserPasswordescapes any symbol in the password, so you never have to think about @ or : inside it.proxyURL := &url.URL{ Scheme: "http", User: url.UserPassword("tp7k2m9_country-us", os.Getenv("TOPPROXY_PASSWORD")), Host: "proxy.topproxy.shop:8080", }Attach it to a Transport and a Client
Pass
http.ProxyURL(proxyURL)as the Transport proxy function. Set a timeout on the client, because the default is no timeout at all.package main import ( "fmt" "io" "net/http" "net/url" "os" "time" ) func main() { proxyURL := &url.URL{ Scheme: "http", User: url.UserPassword("tp7k2m9_country-us", os.Getenv("TOPPROXY_PASSWORD")), Host: "proxy.topproxy.shop:8080", } client := &http.Client{ Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)}, Timeout: 30 * time.Second, } resp, err := client.Get("https://ipinfo.io/json") if err != nil { fmt.Println("request failed:", err) return } defer resp.Body.Close() body, _ := io.ReadAll(resp.Body) fmt.Println(string(body)) }Use SOCKS5 through the same field
Set the scheme to
socks5and the port to 1080. The Transport dials the SOCKS5 proxy itself and passes the target hostname to it, so the lookup happens at the exit.socksURL := &url.URL{ Scheme: "socks5", User: url.UserPassword("tp7k2m9_country-se", os.Getenv("TOPPROXY_PASSWORD")), Host: "proxy.topproxy.shop:1080", } client := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(socksURL)}, Timeout: 30 * time.Second}Share one client across goroutines
An
http.Clientis safe for concurrent use. Create it once and let workers call it. A buffered channel works as a simple limit on how many requests run at the same time.sem := make(chan struct{}, 8) var wg sync.WaitGroup for _, u := range urls { wg.Add(1) go func(u string) { defer wg.Done() sem <- struct{}{} defer func() { <-sem }() resp, err := client.Get(u) if err != nil { return } io.Copy(io.Discard, resp.Body) resp.Body.Close() }(u) } wg.Wait()
How can you confirm the proxy is active in Go?
Decode the JSON from ipinfo.io and print it. The country must match the code in the login. A transport error at this point mentions the proxy, which narrows the problem quickly.
var info struct {
IP string `json:"ip"`
City string `json:"city"`
Country string `json:"country"`
}
json.NewDecoder(resp.Body).Decode(&info)
fmt.Println(info.IP, info.City, info.Country)The response shown here is shortened to the fields that matter. The region and time zone follow the exit, in this case Sweden.
{
"ip": "198.51.100.47",
"city": "Stockholm",
"region": "Stockholm",
"country": "SE",
"timezone": "Europe/Stockholm"
}198.51.100.47 is an address reserved for documentation. A live check prints a real exit.
How do you switch between rotating and held IPs in Go?
The Transport keeps idle connections and reuses them, so a rotating login can return the same exit for a run of requests. To force a new exit each time, turn keep-alives off. To hold one exit on purpose, use a session login and leave keep-alives on.
transport := &http.Transport{Proxy: http.ProxyURL(proxyURL), DisableKeepAlives: true}How the login is put together
Every setting lives in the login, joined with underscores. Nothing else in Go changes when you switch location or session.
| Part | Meaning | Rule |
|---|---|---|
| tp7k2m9 | Your account name | Always first. Yours is shown in your account and replaces tp7k2m9 in every example here. |
| _country-us | Exit country | Two-letter code in lowercase. 64 countries to choose from. |
| _city-new-york | Exit city, optional | Lowercase, spaces become hyphens. 172 cities in total. |
| _session-a1b2c3 | Session name, optional | Letters and digits you pick. Same name, same exit. New name, new exit. |
| _life-30 | Session life in minutes | Only with a session. Residential 1 to 120 minutes, mobile 1 to 60 minutes, long session ISP up to 12 hours (life 720). |
All parts togethertp7k2m9_country-us_city-new-york_session-a1b2c3_life-30
How do you choose an exit country or city in Go?
Build a different login for each location you need and give each its own Transport. Transports are cheap, but keep one per login rather than one per request.
User: url.UserPassword("tp7k2m9_country-kr", pw),Codes and city values for all 64 countries are listed on the proxy locations pages.
How do you fix proxy connection errors in Go?
Find the line that matches what you see. Most problems come down to the login, the port or the network you are on.
| Symptom | Likely cause | Fix |
|---|---|---|
proxyconnect tcp: ... Proxy Authentication Required | The gateway refused the login. | Check that the environment variable is set in the shell running the program and that the login is spelled right. |
net/url: invalid userinfo | The URL was formatted by hand with a raw symbol in the password. | Build it with url.UserPassword as shown. |
| Requests hang forever | No client timeout was set. | Set Timeout on the client, or use a context with a deadline per request. |
| Same IP for every call | Keep-alive reused one tunnel. | Set DisableKeepAlives: true when you want rotation per request. |
Still stuck? Run the same login through the cURL check. If cURL connects, the issue is in the Go settings. If it does not, write to support with the error text.
What kind of proxy pairs well with Go?
All products sit behind the same gateway and share one prepaid balance, so moving from one to another means buying the other product and changing the login, nothing more.
- Rotating Residential ProxiesHousehold exits for Go crawlers that spread load over many addresses.$0.23 per GB
- Private Datacenter ProxiesFixed, unmetered IPs for high-rate API clients written in Go.$0.17 per IP per month
- Long Session ISP ProxiesOne ISP address for hours with no cap on parallel connections.$0.13 per GB
Calling pages from code at scale? The TopProxy API docs cover the Scraping API, which renders pages and retries on its own.
What else do Go users ask about proxies?
Does http.ProxyFromEnvironment work with TopProxy?
Yes. Export HTTPS_PROXY with the full URL including login and password, then use http.ProxyFromEnvironment, which is also what the default transport does. Building the URL in code is clearer when you need several logins.
Do I need golang.org/x/net/proxy for SOCKS5?
Not for HTTP clients. net/http supports socks5 URLs in Transport.Proxy. The x/net package is useful when you dial raw TCP connections that are not HTTP.
Can I choose the proxy per request?
Yes. Transport.Proxy is a function that receives the request. Return a different URL based on the host, a context value or a header to route requests to different logins.
Does this work with Colly?
Yes. Colly accepts a proxy URL through SetProxy, and you can pass the same http URL with login and password. Its proxy switcher can rotate between several logins if you want to mix countries.
Is the login sent for plain http URLs too?
Yes. For http targets Go sends the request to the proxy with a Proxy-Authorization header. For https targets it sends that header on the CONNECT request.
Related setup guides
- cURLOne terminal command to prove your login works, then SOCKS5 with remote DNS and a reusable config file.
- Javajava.net.http.HttpClient with a ProxySelector and an Authenticator, the Basic tunnelling switch, and OkHttp.
- Node.jsfetch with an undici ProxyAgent, axios with https-proxy-agent, and SOCKS5 through socks-proxy-agent.
- PHPThe cURL extension with CURLOPT_PROXY and CURLOPT_PROXYUSERPWD, SOCKS5 hostname mode, and Guzzle.
Ready to connect Go?
Open an account, add $40 or more in crypto, and your login and password are waiting in the account area.