Go language environment construction (Windows+Linux)

Insert picture description here


table of Contents


1. Windows installation and configuration

1️⃣Download SDK

SDK The full name is Software Development Kit (software development kit), which contains the toolkit corresponding to the development language
image-20210405003749354
Under Windows: Download according to whether your system is 32-bit or 64-bit
  • 32-bit system: windows-386.zip
  • 64-bit system: windows-amd64.zip
After downloading, unzip and get the following directory
image-20210405004835586
The meaning of each folder in the directory is shown in the following table
Directory nameDescription
apiDifferences in API changes for each version
binCompiler (go), documentation tool (godoc), formatting tool (gofmt) compiled from go source package
docEnglish version of Go documentation
libSome library files referenced
miscFiles for miscellaneous purposes, such as Android platform compilation, git commit hooks, etc.
pkgIntermediate files compiled on Windows platform
srcThe source code of the standard library
testTest case

2️⃣Configure environment variables

Environment variableDescription
GOROOTSpecify the SDK installation path D:\Go Environment\go
PathAdd SDK's /bin directory D:\Go Environment\go\bin
GOPATHWorking directory, the working path of our go project in the future
Add GOROOTthe installation path of the corresponding SDK in the system variables
image-20210405010738034
Then add the GOPATHcorresponding working directory
image-20210405011035657
Finally, add the SDK/bin directory in Path
image-20210405010842724
Finally enter go version/ go envsee if the configuration is successful in cmd , if the following result appears, it is successful
C:\Users\zsr204>go version
go version go1.16.3 windows/amd64

C:\Users\zsr204>go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\zsr204\AppData\Local\go-build
set GOENV=C:\Users\zsr204\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=D:\学习\Go project\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=D:\学习\Go project
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=D:\Go Environment\go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=D:\Go Environment\go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.16.3
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\zsr204\AppData\Local\Temp\go-build3653787397=/tmp/go-build -gno-record-gcc-switches

3️⃣Configure go proxy

By go getthe time command to download all kinds of dependency, often need to access github / google and other sites. After configuring the proxy, we can download and access quickly and stably. We recommend GOPROXY.IO-a global proxy born for the Go module
goproxy.io is one of the earliest Go modules mirroring proxy services in the world. It uses CDN acceleration services to provide developers with dependent downloads. This service is developed and maintained by a group of young people who love open source and the Go language. Starting from Go 1.11, the Go language began to support Go modules to solve the dependency management problem that everyone has long criticized. At present, the functions of Go modules are in line with the production environment standards.
image-20210527133434546
Configuration method:
1. 右键 我的电脑 -> 属性 -> 高级系统设置 -> 环境变量
2. 在 “[你的用户名]的用户变量” 中点击 ”新建“ 按钮
3. 在 “变量名” 输入框并新增 “GOPROXY”
4. 在对应的 “变量值” 输入框中新增 “https://goproxy.io,direct”
5. 最后点击 “确定” 按钮保存设置
image-20210527133728882
Use the go envcommand again to see the configured proxy
image-20210527133953613

2. Linux installation and configuration

1️⃣Download SDK

The environment here is Tencent Cloud CentOS7 server, it is recommended to download from the Go language Chinese website
  • Foreign official: https://golang.org/dl/
  • Chinese mirror: https://golang.google.cn/dl/
  • Chinese website: https://studygolang.com/dl (recommended)
image-20210525113741523
Upload to cloud server via xftp after download
image-20210525114339780
Then unzip it to the usr/localdirectory
tar -zxvf go1.16.4.linux-amd64.tar.gz -C /usr/local 

2️⃣Configure environment variables

Environment variableDescription
GOROOTSpecify the SDK installation path export GOROOT=/usr/local/bin
GOPATHWorking directory, the working path of our go project in the future export GOPATH=$HOME/go
PATHAdd the /bin directory of the SDK to the system environment variables export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
Then modify the bashrcfiles in the root directory
[[email protected] ~]# vim ~/.bashrc 
[[email protected] ~]# cat ~/.bashrc 
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
	. /etc/bashrc
fi

# 设置go语言的路径
export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
Then reload the file
[[email protected] ~]# source ~/.bashrc
At this point, the environment variables are configured, and then enter go version/ go envinstructions to see if the installation is successful
[[email protected] go]# go version
go version go1.16.4 linux/amd64
[[email protected] go]# go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/root/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/root/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.16.4"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2887101268=/tmp/go-build -gno-record-gcc-switches"

3️⃣Configure go proxy

By go getthe time command to download all kinds of dependency, often need to access github / google and other sites. After configuring the proxy, we can download and access quickly and stably. We recommend GOPROXY.IO-a global proxy born for the Go module
goproxy.io is one of the earliest Go modules mirroring proxy services in the world. It uses CDN acceleration services to provide developers with dependent downloads. This service is developed and maintained by a group of young people who love open source and the Go language. Starting from Go 1.11, the Go language began to support Go modules to solve the dependency management problem that everyone has long criticized. At present, the functions of Go modules are in line with the production environment standards.
image-20210527133434546
Configuration method:
# 设置你的 bash 环境变量
echo "export GOPROXY=https://goproxy.io,direct" >> ~/.bashrc && source ~/.bashrc
Use the go envcommand again to see the configured proxy
image-20210527134444629