Go 在 linux 上安装(Go installation on Linux)

在 linux 安装 有两种方式。

GO
  • 一种是使用 apt-get 命令安装。
  • 第二种是使用 安装包安装

使用 apt-get 安装

sudo apt-get update  #更新安装列表

apt-get install golang  #安装golang

如果是 系统 ,推荐使用 安装,但是在中,不太建议使用 的方式进行安装。因为这样安装到 的版本号,会远远落后于

mac
brew
ubuntu
apt-get
GO
GO 的最新版本

使用安装包安装

本文安装,环境 ,其他 环境,安装过程大致相同

Ubuntu 20.04
linux

1. 下载压缩包

在写这篇文章的时候,Go 的最新版为 1.17.6。在我们下载安装包时,请浏览 Go 官方下载页面,并且检查一下是否有新的版本可用。

wget -c https://dl.google.com/go/go1.17.6.linux-amd64.tar.gz -O - | sudo tar -xz -C /usr/local

注意:

  • 一般电脑或者服务器都是 amd64 位系统,所以下载 amd64 版本
  • 解压到 usr/local 文件夹下。 此文件是存放手工安装的软件保存位置。linux 一般建议源码包软件安装在这个位置

2. 先设置 gopatch 和 goroot

echo 'export GOROOT="{你Golang的安装目录}"' >> ~/.bash_profile
echo 'export GOPATH="$HOME/.go" >> ~/.bash_profile
echo 'export PATH="$GOPATH/bin:$PATH"' >> ~/.bash_profile

注意

  • goroot: go 的安装目录
  • gopatch:默认采用和 $GOROOT 一样的值,但从 Go 1.1 版本开始,你必须修改为其它路径。它可以包含多个包含 Go 语言源码文件、包文件和可执行文件的路径,而这些路径下又必须分别包含三个规定的目录:src、pkg和 bin,这三个目录分别用于存放源码文件、包文件和可执行文件。

3. 设置代理

vim ~/.bash_profile         # 打开文件

export GO111MODULE=auto
export GOPROXY=https://goproxy.cn,https://goproxy.io,direct
export GONOSUMDB="*"

source ~/.bash_profile  # 重启配置文件生效

注意: 这个地方有个小坑,执行了 命令后,发现运行的环境还是不对,此时建议重启一下电脑试试。

source ~/.bash_profile

4. 我自己的完整目录如下

export GOPATH="$HOME/.go"
export GOROOT=/usr/local/go
export PATH=$PATH:/usr/local/go/bin
export PATH=$PATH:$GOPATH:$GOROOT:/bin
export GO111MODULE=on  
export GOPROXY=https://goproxy.cn,https://goproxy.io,direct
export GONOSUMDB="*"
export PATH="$GOPATH/bin:$PATH"
————————

There are two ways to install Linux.

GO
  • One is to use apt get command to install.
  • The second is to install using the installation package

使用 apt-get 安装

sudo apt-get update  #更新安装列表

apt-get install golang  #安装golang

If it is a system, it is recommended to use the installation method, but in, it is not recommended to use the installation method. Because the version number installed in this way will lag far behind

mac
brew
ubuntu
apt-get
GO
GO 的最新版本

Install using the installation package

The installation process of this article is roughly the same as that of other environments

Ubuntu 20.04
linux

1. Download the compressed package

At the time of writing this article, the latest version of go was 1.17.6. When we download the installation package, please browse the go official download page and check whether a new version is available.

wget -c https://dl.google.com/go/go1.17.6.linux-amd64.tar.gz -O - | sudo tar -xz -C /usr/local

< strong > note: < / strong >

  • Generally, computers or servers are amd 64 bit systems, so download the AMD 64 version
  • Unzip to the usr / local folder. This file is the storage location for manually installed software. Linux generally recommends that the source package software be installed in this location

2. 先设置 gopatch 和 goroot

echo 'export GOROOT="{你Golang的安装目录}"' >> ~/.bash_profile
echo 'export GOPATH="$HOME/.go" >> ~/.bash_profile
echo 'export PATH="$GOPATH/bin:$PATH"' >> ~/.bash_profile

be careful

  • goroot: go 的安装目录
  • Gopatch: the default value is the same as $goroot, but from go version 1.1, you must change it to another path. It can contain multiple paths containing go language source files, package files and executable files, and these paths must contain three specified directories: SRC, PKG and bin, which are used to store source files, package files and executable files respectively.

3. Set up agent

vim ~/.bash_profile         # 打开文件

export GO111MODULE=auto
export GOPROXY=https://goproxy.cn,https://goproxy.io,direct
export GONOSUMDB="*"

source ~/.bash_profile  # 重启配置文件生效

Note: there is a small pit in this place. After executing the command, it is found that the running environment is still wrong. At this time, it is recommended to restart the computer.

source ~/.bash_profile

4. My complete catalog is as follows

export GOPATH="$HOME/.go"
export GOROOT=/usr/local/go
export PATH=$PATH:/usr/local/go/bin
export PATH=$PATH:$GOPATH:$GOROOT:/bin
export GO111MODULE=on  
export GOPROXY=https://goproxy.cn,https://goproxy.io,direct
export GONOSUMDB="*"
export PATH="$GOPATH/bin:$PATH"