# Configure TrueProxies with Go

> Configure TrueProxies with Go net/http using HTTP or SOCKS5 transports, authentication, timeouts, Residential IPv4 country targeting and sticky sessions.

Source: https://docs.trueproxies.com/integrations/go/

> **Use your own endpoints**
>
> The examples below use placeholders. Copy the connection host, ports, username, and proxy password from the **Username and password** tab of your service in the dashboard. See [How to connect](https://docs.trueproxies.com/proxy-instructions/how-to-connect/#connection-host-and-ports).

## Basic usage

**HTTP**

```go
package main

import (
    "fmt"
    "io"
    "net/http"
    "net/url"
)

func main() {
    proxyURL, err := url.Parse("http://your_username:your_password@YOUR_HOST:YOUR_HTTP_PORT")
    if err != nil {
        panic(err)
    }
    client := &http.Client{
        Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)},
    }

    resp, err := client.Get("https://httpbin.org/ip")
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()

    body, err := io.ReadAll(resp.Body)
    if err != nil {
        panic(err)
    }
    fmt.Println(string(body))
}
```

**SOCKS5**

```go
package main

import (
    "fmt"
    "io"
    "net/http"

    "golang.org/x/net/proxy"
)

func main() {
    auth := proxy.Auth{User: "your_username", Password: "your_password"}
    dialer, err := proxy.SOCKS5("tcp", "YOUR_HOST:YOUR_SOCKS_PORT", &auth, proxy.Direct)
    if err != nil {
        panic(err)
    }
    contextDialer, ok := dialer.(proxy.ContextDialer)
    if !ok {
        panic("SOCKS5 dialer does not support contexts")
    }

    client := &http.Client{
        Transport: &http.Transport{DialContext: contextDialer.DialContext},
    }

    resp, err := client.Get("https://httpbin.org/ip")
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()

    body, err := io.ReadAll(resp.Body)
    if err != nil {
        panic(err)
    }
    fmt.Println(string(body))
}
```

## Residential IPv4 country targeting

```go
proxyURL, _ := url.Parse("http://your_username-country-us:your_password@YOUR_HOST:YOUR_HTTP_PORT")
client := &http.Client{
    Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)},
}
```

## Residential IPv4 sticky sessions

```go
proxyURL, _ := url.Parse("http://your_username-session-abc123:your_password@YOUR_HOST:YOUR_HTTP_PORT")
client := &http.Client{
    Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)},
}
```

## With timeout

Add `"time"` to the imports, then set a timeout on the client:

```go
client := &http.Client{
    Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)},
    Timeout:   30 * time.Second,
}
```

## Related guides

- [Connection and username format](https://docs.trueproxies.com/proxy-instructions/how-to-connect/)
- [HTTP CONNECT and SOCKS5](https://docs.trueproxies.com/proxy-instructions/proxy-protocols/)
- [Trusted IPs](https://docs.trueproxies.com/proxy-instructions/trusted-ips/)
- [Troubleshooting checklist](https://docs.trueproxies.com/support/)
