# Use a proxy with Go

HTTP proxy access is completed using net/http, environment variables, Transport, and request timeouts.

The Go standard library can be used by `http.Transport.Proxy` Use HTTP proxy. The account is closed. `us.zzhttp.com:7878` The code reads credentials from the environment.

## Go

```go
package main

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

func main() {
  username, password := os.Getenv("PROXY_USERNAME"), os.Getenv("PROXY_PASSWORD")
  if username == "" || password == "" { panic("Missing proxy credentials") }
  proxyURL, err := url.Parse("http://us.zzhttp.com:7878")
  if err != nil { panic(err) }
  proxyURL.User = url.UserPassword(username, password)

  transport := &http.Transport{Proxy: http.ProxyURL(proxyURL)}
  client := &http.Client{Transport: transport, Timeout: 30 * time.Second}
  response, err := client.Get("https://ipinfo.io/json")
  if err != nil { panic(err) }
  defer response.Body.Close()
  fmt.Println(response.StatusCode)
  if _, err := io.Copy(os.Stdout, response.Body); err != nil { panic(err) }
}
```

## Configuration

- Use `url.Parse` I'm going to take you to the next level. `PROXY_USERNAME` and `PROXY_PASSWORD` Inject credentials.
- Do not log the output of the parsed URL.

## Connection

- Set the client to timeout overall.
- Close Body after the response is complete.

## Reuse

- Reuse Transport under the same agent.
- Turn off idle connections when configuration changes.

## Error handling

- Distinguish between configuration analysis, network errors, and target status.
- Production code adds layering indicators for DNS, dial-up, and TLS.

html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}
