Linux installation go language environment

foreword

Due to the needs of our project development, we often need to deploy projects in development, demonstration, and official environment servers (linux environment). Since the services are all developed by Go in the near future, it is inevitable to install the go operating environment in the linux environment. Record it.

environment version

Deployment environment: linux
Operating system: CentOS_7.7
Go version: 1.17.6

Pre-knowledge preparation

The Go workspace is a directory, which contains three subdirectories:
src ---- Each subdirectory in it is a package. Inside the package is the source code file
pkg of Go ---- generated after compilation, and the target file
bin of the package ---- the generated executable file
insert image description here

deployment steps

install wget

yum install -y wget

insert image description here

Download the compressed package and extract it to the specified path /usr/local

The path can be downloaded in China, and decompressed to the specified path /usr/local (in fact, the default decompression will also put this location, but if you want to adjust it yourself, you can change it to your own designated location) ps: Attach the GO language Chinese website
address , no external network restrictions (other server environments or versions can use this to find their own suitable version or operating system): https://studygolang.com/dl

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

insert image description here
You can see one more go folder under the /usr/local path, and its content is as follows
insert image description here

Configure environment variables

Create GOPATH folder

cd /usr/local/
mkdir gopath 

insert image description here

Add PATH environment variable and set GOPATH environment variable

1. Open the configuration file

vi /etc/profile 

2. Change configuration information

# 在/etc/profile最后一行添加,GOPATH路径更换成上面创建的路径
# i 切换更改模式
export GOROOT=/usr/local/go
export GOPATH=/usr/local/gopath
export PATH=$PATH:$GOROOT/bin
export GO111MODULE="on" # 开启 Go moudles 特性
export GOPROXY=https://goproxy.cn,direct # 安装 Go 模块时,国内代理服务器设置
# esc 推出更改模式
# :wq 保存更改后退出

insert image description here

3. Then execute the following command to make the settings of the above environment variables take effect immediately:

source /etc/profile

View deployment results

1. Check the version number

success as shown

go version

insert image description here

2. Check the environment configuration information

View go environment configuration information, the configuration information can be checked

go env

insert image description here

Guess you like

Origin blog.csdn.net/ic_xcc/article/details/127305940