Go Proxy Setup
Go's standard library includes first-class proxy support through the http.Transport type, requiring zero external dependencies. The Transport.Proxy field accepts a function that returns a proxy URL for each request, enabling dynamic proxy selection based on the destination, request headers, or custom logic. This function-based approach is more flexible than static configuration and is unique to Go among popular languages.
Go's concurrency model with goroutines makes it exceptionally well-suited for high-throughput proxy workloads. A single Go process can efficiently manage thousands of concurrent proxied connections with minimal memory overhead, where Python or Node.js would require complex async frameworks.
Prerequisites
Before you begin, make sure you have: - An active Hex Proxies account with proxy credentials - Go 1.20+ - Go modules enabled - net/http standard library
Basic Proxy Configuration
Set the Transport.Proxy field to http.ProxyURL() with your parsed proxy URL. Credentials are embedded in the URL and sent via the Proxy-Authorization header automatically.
import ( "fmt" "io" "net/http" "net/url" "time" )
func main() { proxyURL, _ := url.Parse( "http://user:pass@gate.hexproxies.com:8080", )
transport := &http.Transport{ Proxy: http.ProxyURL(proxyURL), MaxIdleConns: 100, IdleConnTimeout: 90 * time.Second, TLSHandshakeTimeout: 10 * 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.Println(string(body)) } ```
Transport Configuration for High Throughput
Go's http.Transport manages a pool of idle connections per host. For proxy workloads, tune MaxIdleConns (total across all hosts), MaxIdleConnsPerHost (per destination), and IdleConnTimeout. Setting MaxIdleConnsPerHost=50 prevents connection starvation when scraping a single domain through the proxy.
Dynamic Proxy Selection
Replace http.ProxyURL() with a custom function for per-request proxy logic. For example, route requests to different Hex Proxies endpoints based on the target domain or implement round-robin across multiple proxy credentials.
Environment Variable Fallback
Go's http.ProxyFromEnvironment reads HTTP_PROXY and HTTPS_PROXY environment variables automatically. This works with Hex Proxies: export HTTPS_PROXY=http://user:pass@gate.hexproxies.com:8080.
Configuration Options
- **Proxy Function** -- http.ProxyURL(proxyURL) for static, or custom func(*http.Request) for dynamic routing.
- **MaxIdleConns** -- Total idle connections across all hosts. Default 100; increase for high-concurrency scraping.
- **IdleConnTimeout** -- Duration before idle connections are closed. Default 90s; lower for rotation-heavy workloads.
- **TLSHandshakeTimeout** -- Timeout for the TLS negotiation phase. Default 10s; increase for high-latency regions.
- **Client Timeout** -- Overall request timeout including body read. Set 30s minimum for proxied traffic.
Error Handling
Go returns proxy errors as *url.Error wrapping the underlying network error. Use errors.Is() and errors.As() for precise error handling.
- dial tcp: connect: connection refused
- - The proxy gateway is not accepting connections
- - Verify gate.hexproxies.com resolves correctly with net.LookupHost()
- - Check outbound firewall rules for port 8080
2. context deadline exceeded (Client.Timeout) - The overall request including proxy negotiation timed out - Increase client.Timeout from 30s to 60s - Use context.WithTimeout for per-request timeout control
3. proxyconnect tcp: 407 Proxy Authentication Required - Credentials in the proxy URL are incorrect or missing - Verify url.Parse() succeeded without error - Check that special characters in passwords are percent-encoded
4. tls: failed to verify certificate - The destination's TLS certificate failed validation - Update your Go installation to get current root certificates - Do not set InsecureSkipVerify: true in production
5. read: connection reset by peer - The proxy or destination terminated the connection - Implement retry logic with time.Sleep between attempts - Create a new Transport for persistent reset errors ```
Always check err != nil after every HTTP call and defer resp.Body.Close() to prevent resource leaks.