v1.10.82-f67ee7d
Skip to main content
← Back to Hex Proxies

Go Proxy Setup

Configure HTTP proxies in Go using net/http Transport. Complete Go proxy setup guide with authentication and Hex Proxies.

Requirements

  • Go 1.20+
  • Go modules enabled
  • net/http standard library

Installation

go mod init myproject

Code Example

package main

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))
}

Setup Steps

1

Parse the proxy URL

Use url.Parse() to create a *url.URL from your Hex Proxies gateway string with embedded credentials.

2

Configure the Transport

Create an http.Transport with Proxy: http.ProxyURL(proxyURL) and tune connection pool sizes.

3

Create the HTTP client

Build an http.Client with the custom Transport and a 30-second Timeout.

4

Test the connection

GET httpbin.org/ip and verify the response body shows a Hex Proxies IP.

5

Add error handling

Check err != nil after every call and use errors.Is() to handle specific network errors.

6

Use environment variables

Read proxy URL from os.Getenv("PROXY_URL") or use http.ProxyFromEnvironment for automatic detection.

Configuration Options

OptionDescription
Proxy Functionhttp.ProxyURL(proxyURL) for static proxy, or custom func(*http.Request) for dynamic routing.
MaxIdleConnsTotal idle connections in the pool. Increase for high-concurrency proxy workloads.
IdleConnTimeoutHow long idle connections stay in the pool before closing. Default 90 seconds.
TLSHandshakeTimeoutMaximum time for the TLS handshake phase. Increase for high-latency proxy routes.
DisableKeepAlivesSet true to force a new connection per request for maximum IP rotation.

Best Practices

  • Reuse a single http.Client across goroutines; it is safe for concurrent use.
  • Read proxy credentials from os.Getenv() or a config file, not hardcoded strings.
  • Set MaxIdleConnsPerHost to at least 10 for single-domain scraping to avoid connection starvation.
  • Use context.WithTimeout for per-request deadlines instead of relying solely on client.Timeout.
  • Always defer resp.Body.Close() and read the full body to return the connection to the pool.
  • For IP rotation, set DisableKeepAlives: true on the Transport to force new connections.
  • Use http.ProxyFromEnvironment in production for deployment-time proxy configuration.
  • Log response status and duration per request using a custom RoundTripper wrapper.

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.

  1. dial tcp: connect: connection refused
  2. - The proxy gateway is not accepting connections
  3. - Verify gate.hexproxies.com resolves correctly with net.LookupHost()
  4. - 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.

Error Handling

## Error Handling

Go returns proxy errors as *url.Error wrapping the underlying network error. Use errors.Is() and errors.As() for precise error handling.

1. 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.

Frequently Asked Questions

Does Go have built-in proxy support?

Yes. The net/http standard library supports proxies through http.Transport.Proxy with zero external dependencies.

How do I authenticate with a proxy in Go?

Embed credentials in the proxy URL: http://user:pass@gate.hexproxies.com:8080. Go sends them via the Proxy-Authorization header automatically.

Can I use different proxies per request?

Yes. Set Transport.Proxy to a custom function that returns different proxy URLs based on the *http.Request argument.

How do I handle connection pooling?

Configure MaxIdleConns, MaxIdleConnsPerHost, and IdleConnTimeout on the Transport. Reuse the same client across goroutines.

Ready to Get Started?

Set up Go with Hex Proxies in minutes.