Quick answer
To use a proxy with Go, follow four steps: (1) Parse your proxy URL with url.Parse; (2) Set Transport: &http.Transport{Proxy: http.ProxyURL(u)}; (3) Turn off keep-alives for a new IP per request; (4) Use socks5:// for SOCKS5, no extra packages. Take the host, port, username and password from a line in your ZenProxy generator.
You need Go and a ZenProxy proxy line from the generator. In the examples, replace HOST, PORT, USERNAME and PASSWORD for the values in your line (where to find them).
Quick setup
- 1Parse your proxy URL with url.Parse.
- 2Set Transport: &http.Transport{Proxy: http.ProxyURL(u)}.
- 3Turn off keep-alives for a new IP per request.
- 4Use socks5:// for SOCKS5, no extra packages.
Basic authenticated proxy
package main
import (
"fmt"
"io"
"net/http"
"net/url"
"time"
)
func main() {
proxyURL, err := url.Parse("http://USERNAME:PASSWORD@HOST:PORT")
if err != nil {
panic(err)
}
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 {
panic(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println(string(body))
}The Transport sends the URL’s username and password as Proxy-Authorization, both on the CONNECT request for HTTPS sites and on plain HTTP requests.
Your proxy details
After you buy a plan, open My proxies. Choose the Proxy Type (Rotating or Sticky; static IP plans skip this and show your list of IPs), the Protocol (HTTP, or SOCKS5 where your plan offers it) and a Format, then copy a line. The generator offers three formats (SOCKS5 lines always use the protocol:// format):
HOST:PORT:USERNAME:PASSWORD Paste into apps and anti-detect browsers that split the fields or auto-fill them.
USERNAME:PASSWORD@HOST:PORT Tools that want credentials before the address.
http://USERNAME:PASSWORD@HOST:PORT Code and command-line tools. This is the proxy URL used in the examples below.
In the examples on this page, replace USERNAME, PASSWORD, HOST and PORT with the values from your line. SOCKS5_PORT is the port the generator shows when you pick SOCKS5.
Residential lines are either rotating (a new IP on new connections) or sticky (one IP for the time you choose). Static Datacenter, Shared ISP and Private ISP plans give you a fixed list of IPs, one per line as IP:PORT:USERNAME:PASSWORD: Private ISP IPs are yours alone, while Shared ISP IPs can also be used by other customers. Rotating Datacenter and IPv6 plans give you one endpoint, with rotating or sticky lines; IPv6 only reaches sites that have an IPv6 address.
To test a line, open https://ipinfo.io/json through it: it shows the exit IP and country. For HTTP lines you can also use the free proxy checker.
Passwords with special characters
Characters such as @, #, / or a space in a password break url.Parse, sometimes without an error. Build the URL from parts instead, and Go encodes the credentials for you.
proxyURL := &url.URL{
Scheme: "http",
User: url.UserPassword("USERNAME", "PASSWORD"),
Host: "HOST:PORT",
}Rotating and sticky IPs
Rotation happens per connection. A rotating line gets a new IP when your client opens a new connection; requests sent through the same open HTTPS tunnel leave from the same IP. A Transport keeps connections alive, so one client can send many requests from one IP. Turn keep-alives off to get a new IP on every request from a rotating line.
To make sticky lines, open the generator, set Proxy Type to Sticky, choose a Sticky Duration if your plan offers one, set how many lines you want in Sticky Amount, then copy or download them. Each line keeps its IP for the chosen time. The session part of a sticky username differs by plan, so always generate sticky lines instead of editing usernames by hand. Use one Transport per sticky line.
transport := &http.Transport{
Proxy: http.ProxyURL(proxyURL),
DisableKeepAlives: true, // a new connection, and a new IP, for every request
}
client := &http.Client{Transport: transport, Timeout: 30 * time.Second}Target a country
Build the line in the generator: it writes the country, and a state, a city or a sticky session if you want one, into the username in the format your plan uses, and shows only the options your plan supports. On most residential plans the country is -country- and a two-letter code at the end of the username (for example USERNAME-country-gb), but older Unlimited Residential plans use a different format, so do not edit that username by hand. Datacenter and ISP IPs have a fixed location that you choose when you buy them.
proxyURL, _ := url.Parse("http://USERNAME-country-us:PASSWORD@HOST:PORT")SOCKS5
http.ProxyURL accepts SOCKS5 URLs too. You don’t need golang.org/x/net/proxy.
proxyURL, _ := url.Parse("socks5://USERNAME:PASSWORD@HOST:SOCKS5_PORT")
client := &http.Client{
Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)},
Timeout: 30 * time.Second,
}Go sends the hostname to the SOCKS5 proxy, so DNS is resolved at the exit. Recent Go versions also accept socks5h:// and treat it the same as socks5://.
Environment variables
http.DefaultTransport, used by http.Get, reads HTTP_PROXY, HTTPS_PROXY and NO_PROXY through http.ProxyFromEnvironment. Go reads them once, at the first request, so set them before your program starts.
Troubleshooting
-
Get "https://…": Proxy Authentication Required
The proxy rejected the credentials. Check them, or build the URL with
url.UserPassword.More on this error → -
net/url: invalid userinfo
The password has characters that aren’t allowed in a URL. Use
url.UserPassword. -
proxyconnect tcp: … connection refused
Go couldn’t reach the proxy. Check the host and port.More on this error →
-
The IP doesn’t change on a rotating line
Your client is reusing an open connection. Requests that share a tunnel share its IP. Open a new connection per request, or use one sticky line per task.More on this error →
-
Timeouts
Set an explicit timeout of 30–60 seconds, retry on a new connection, and lower concurrency.More on this error →
Seeing another error? Read proxy errors and how to fix them, or contact support.
Questions
How do I set a proxy for an http.Client in Go?
Create &http.Transport{Proxy: http.ProxyURL(u)} with your proxy URL and pass it as the client’s Transport.
Do I need golang.org/x/net/proxy for SOCKS5?
No. net/http supports socks5:// URLs, with username and password, in http.ProxyURL.
Can I pick a different proxy per request?
Yes. Transport.Proxy is a function of the request, so it can return a different URL each time. Keep in mind that connections already open to a proxy are reused.
Does Go read the HTTPS_PROXY environment variable?
Yes, through http.ProxyFromEnvironment, which http.DefaultTransport uses.
Try it with Go for $0.49
Get 1 GB of Residential Lite, then copy your rotating or sticky lines from the generator and paste them into the steps above.
Broken on our side? Fixed within 48 hours of your report, or the unused part refunded. A website blocking the proxies isn’t covered. Refund Policy