Go net/http Proxy Integration
Go's standard library provides first-class proxy support through the `http.Transport` struct. You can configure a proxy URL directly, and the transport handles connection tunneling for both HTTP and HTTPS targets.
Basic Setup
import ( "fmt" "io" "net/http" "net/url" "time" )
func main() { proxyURL, err := url.Parse("http://YOUR_USERNAME:YOUR_PASSWORD@gate.hexproxies.com:8080") if err != nil { panic(err) }
transport := &http.Transport{ Proxy: http.ProxyURL(proxyURL), ResponseHeaderTimeout: 30 * time.Second, }
client := &http.Client{ Transport: transport, Timeout: 30 * time.Second, }
resp, err := client.Get("https://httpbin.org/ip") if err != nil { panic(err) } defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body) fmt.Printf("Status: %d\nBody: %s\n", resp.StatusCode, body) } ```
Sticky Sessions
Append a session identifier to the username so the gateway assigns the same IP for the duration of that session:
proxyURL, _ := url.Parse(
"http://YOUR_USERNAME-session-abc123:YOUR_PASSWORD@gate.hexproxies.com:8080",
)Per-Request Rotation
For maximum IP diversity, create a fresh transport or use the default rotation behavior. Each request through `gate.hexproxies.com` without a session tag automatically picks a new IP from the 10M+ pool.
SOCKS5 Proxy with golang.org/x/net
dialer, err := proxy.SOCKS5( "tcp", "gate.hexproxies.com:1080", &proxy.Auth{User: "YOUR_USERNAME", Password: "YOUR_PASSWORD"}, proxy.Direct, ) if err != nil { panic(err) }
transport := &http.Transport{ DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { return dialer.Dial(network, addr) }, } client := &http.Client{Transport: transport} ```
Concurrent Requests with Proxy
var wg sync.WaitGroup
urls := []string{
"https://httpbin.org/ip",
"https://httpbin.org/headers",
"https://httpbin.org/get",for _, u := range urls { wg.Add(1) go func(target string) { defer wg.Done() resp, err := client.Get(target) if err != nil { fmt.Printf("Error: %v\n", err) return } defer resp.Body.Close() body, _ := io.ReadAll(resp.Body) fmt.Printf("%s -> %d bytes\n", target, len(body)) }(u) } wg.Wait() ```
Error Handling
Check for network errors vs HTTP errors separately. Proxy authentication failures return a 407 status, while target-side blocks typically return 403 or 429. Implement exponential backoff for retryable errors.
Environment Variable Pattern
A clean production pattern reads the proxy URL from an environment variable:
proxyStr := os.Getenv("HEX_PROXY_URL")
// e.g. http://user:pass@gate.hexproxies.com:8080
proxyURL, err := url.Parse(proxyStr)