SoFunction
Updated on 2025-03-01

GOPROXY settings in Go projects

In the Go language ecosystem,GOPROXYis an important environment variable that specifies the proxy server that the Go toolchain uses when obtaining dependencies. By default, Go directly gets dependencies from the source repository, but sometimes due to network problems, direct access to the source repository may encounter obstacles. At this time, it is particularly important to use a reliable proxy server.

What is GOPROXY?

GOPROXYIt is an environment variable used in Go language to control the behavior of module agents. It can be set to the following modes:

  • direct: Get dependencies directly from the source code repository, without using a proxy.
  • off: Close the module proxy and try to get the dependency from the local cache.
  • <proxy-url>: Specify the URL of a proxy server, and the Go toolchain will obtain the dependencies through this proxy server.

Why use GOPROXY?

In mainland China, due to the particularity of the network environment, you can directly access the official source code repository of Go language.You may encounter problems such as slow access speed or inaccessibility. Using a domestic proxy server can significantly improve access speed and stability.

Setting up GOPROXY

Set up in Go projectsGOPROXYThere are several ways to:

  • Environment variables: Set environment variables at the operating system level. For example, on Linux or macOS, you can.bashrcor.zshrcAdd the following line to the file:

    export GOPROXY=,direct
    
  • Go command line: In usegoWhen command, you can pass-GOPROXYParameters specify the proxy. For example:

    go get -GOPROXY=,direct some/module
    
  • Go environment configuration file:exist$HOME/.go/envSettings in the fileGOPROXYEnvironment variables.

use

is a domestic proxy server maintained by the Go community. It provides mirroring services for the official Go repository and can speed up the download speed of Go modules. When you setGOPROXYfor,directWhen the Go toolchain will give priority to trying to passGet dependencies, if they cannot be retrieved, they will fall back to the direct access to the source repository.

Example

Suppose you are developing a Go project and need to install a name called/example/modulemodule. You can run the following command in the root directory of the project to set up the proxy and get the module:

export GOPROXY=,direct
go get /example/module

In this way, the Go toolchain will first try to passGet the module, ifThere is no module on it, it will automatically fall back directly fromGet it.

in conclusion

Reasonable configurationGOPROXYIt can significantly improve the dependency management efficiency of Go projects, especially in areas where network environments are restricted. By using,directWith this setup, you can enjoy faster dependency download speeds while maintaining direct access to the source repository as an alternative.

This is the end of this article about GOPROXY settings in Go projects. For more information about Go GOPROXY settings, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!