Golang installation and golang.org package installation

Golang installation

Golang official website provides binary packages for each platform, which can be downloaded and installed directly. Due to some force majeure, golang.org is blocked and cannot be accessed directly, but it can be downloaded from the official domestic official website golang.google.cn provided by the government .

In addition to installing directly through the binary package, it can also be installed through the package manager. Such as use in Ubuntu apt-get install golang, or brew install godownload and install in MacOS .

In addition, you can also download the source code through the official website or GitHub and compile and install it. You can refer to the official source code installation document .

Non-pre-installed official package

When using Go, in addition to the Go standard library, Google also provides some extension packages, but they are not included in the binary installation package and need to go getbe installed with additional commands. For example, when installing Golang-related extension plug-ins in VSCode, you need to install some third-party packages, and most of them will use the official non-preinstalled extension packages. The naming of official non-preinstalled expansion packs golang.org/x/begins. For example golang.org/x/tools, the official non-preinstalled expansion packs and their uses are listed below:

  • benchmarks: performance testing support
  • blog: blog.golang.org source code implementation
  • build: build.golang.org source code implementation
  • crypto: additional cryptography support
  • debug: Experimental Go debugger
  • image: additional image support
  • mobile: mobile terminal support
  • net: additional network support
  • perf: performance analysis tool
  • pkgsite: pkg.go.dev source code implementation
  • review: Gerrit code review tool support
  • sync: additional concurrency support
  • sys: system call support
  • text: text processing support
  • time: additional time-related support
  • tools: godoc, goimports and other tools
  • tour: tour.golang.org source code implementation
  • exp: experimental and backward features

Installation of golang.org package

Because golang.organd google.golang.orgcannot be accessed in China, in order to install these packages, you can download the source code through GitHub to install it or install it through settings in Go13 and above GOPROXY:

Download the source code and install it via GitHub

golang.orgThe package has a corresponding mirror image on GitHub, for example golang.org/x/tools, the GitHub mirror corresponding to the package is https://github.com/golang/tools, and google.golang.org/grpcthe mirror corresponding to the package is https://github.com/grpc/grpc-go.

After downloading the source code in the corresponding mirror, you need to place it in GOPATHthe directory specified by the environment variable. For example, golang.org/x/toolsafter downloading the package, you need to move it to the $GOPATH/src/golang.org/x/toolsdirectory.

The following is golang.org/x/imagean example to demonstrate how to install:

# 下载源码,使用--depth=1只克隆最后一次commit
$ git clone https://github.com/golang/image.git --depth=1
$ mkdir -p $GOPATH/src/golang.org/x
$ mv image $GOPATH/src/golang.org/x

Set up proxy service installation through GOPROXY (recommended)

Go11 added the go module package dependency management tool, and added the GOPROXYenvironment variable setting proxy service. After setting the environment variable, the go getdownload package will be downloaded through the set proxy address when using the download package. Currently available proxy services include https://goproxy.ioAlibaba Cloud, https://mirrors.aliyun.com/goproxy/etc., which can be selected according to needs.

Before using GOPROXY, you need to turn on the go module. The way to turn on the go module is to set GO111MODULEthe value of the environment variable on:

$ export GO111MODULE="on"

If you are using Go13 and above, you can use the following command to set up the proxy service:

$ go env -w GOPROXY="<proxy>,direct"

If you use Go11 or Go12, you need to use the following command to set:

$ export GOPROXY="<proxy>"

The exportcommand used will only be effective during this login. If you need to be effective for a long time, you need to write it into a profilefile (for example, Bash .bash_profileor zsh .zshrc).

It can be set by the following command in Windows:

$env:GOPROXY="<proxy>"

When using the command, you need to <proxy>replace the address in the command with the address of the corresponding proxy service. After setting GOPROXY, you can use the go getcommand to download the installation golang.orgpackage normally .

Reference

Guess you like

Origin blog.csdn.net/ghosind/article/details/108795624