v1.8.91-d84675c
← Back to Hex Proxies

Go HTTP Proxy Setup

Last updated: April 2026

By Hex Proxies Engineering Team

Route Go HTTP requests through Hex Proxies using the standard net/http package. Covers proxy URL parsing, transport configuration, and SOCKS5 via golang.org/x/net.

intermediate15 minuteslanguage-integration

Prerequisites

  • Go 1.20 or later
  • Hex Proxies account with credentials

Steps

1

Parse the proxy URL

Use url.Parse to create a proxy URL with credentials embedded for gate.hexproxies.com:8080.

2

Configure the transport

Create an http.Transport with the Proxy field set to http.ProxyURL(proxyURL).

3

Build the HTTP client

Instantiate http.Client with the custom transport and a reasonable timeout.

4

Send a test request

Make a GET request to httpbin.org/ip to confirm the proxy is working.

5

Add concurrency

Use goroutines and sync.WaitGroup for parallel proxied requests.

6

Handle errors

Distinguish between network errors and HTTP status codes for proper retry logic.

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)

Tips

  • *Embed credentials in the proxy URL for simplicity, or use environment variables in production.
  • *Go reuses connections by default -- close idle connections if you need fresh IPs per request.
  • *For SOCKS5, use golang.org/x/net/proxy with a custom DialContext on the transport.
  • *Set both client.Timeout and transport.ResponseHeaderTimeout to avoid leaked goroutines.

Ready to Get Started?

Put this guide into practice with Hex Proxies.

Cookie Preferences

We use cookies to ensure the best experience. You can customize your preferences below. Learn more