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
Release: 2024-12-25 12:46:09
Original
299 people have browsed it

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
Copy after login

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'
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template