Home >Backend Development >Golang >How to Configure Go to Use a Proxy for Package Installation?

How to Configure Go to Use a Proxy for Package Installation?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-25 12:46:09304browse

How to Configure Go to Use a Proxy for Package Installation?

Using Proxies for Go Commands

Context:

When installing packages using the go install command, you may encounter the need to connect through a proxy server for internet access. This guide will demonstrate how to configure Go to utilize a proxy.

Environment Variables:

Go programs inherently recognize the HTTP_PROXY and NO_PROXY environment variables. However, this alone is insufficient because packages are often retrieved from source control managers such as Mercurial and Git. To address this, you must configure proxy settings for the respective SCMs as well.

Setting Proxy Settings:

  • Mercurial: Use this command hg config http.proxy http://proxy.example.com:8080.
  • Git: Use this command git config --global http.proxy http://proxy.example.com:8080.

Environment Variable Values:

  • HTTP_PROXY: The proxy address in the format http://user:password@host:port/. User, password, and port are optional.
  • NO_PROXY: A comma-separated list of servers that should bypass the proxy. For example: foo.com,bar.net:4000.

Integrating into Shell Environment:

You can set these environment variables permanently in your shell profile (e.g., .bashrc, .zshrc). Alternatively, you can temporarily set them for specific go commands:

$ http_proxy=127.0.0.1:8080 go get code.google.com/p/go.crypto/bcrypt

Creating an Alias:

If you frequently use a proxy, you can create an alias to simplify the process:

$ alias go='http_proxy=127.0.0.1:8080 go'

This allows you to use the go command normally while automatically connecting through your designated proxy.

The above is the detailed content of How to Configure Go to Use a Proxy for Package Installation?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn