一、通过apt-get安装
1、安装
sudo apt-get install golang
2、设置GOPATH变量
GOPATH是扩展库的目录,Go先搜索标准库目录,然后搜索GOPATH库目录,所有的非标准库都放在GOPATH下。以GOPATH为“/usr/local/go”目录为例子:
(1)把export GOPATH=/opt/go加入到~/.bashrc(或者/etc/profile)文件的最后。
(2)source ~/.bashrc(或者/etc/profile) 重新加载配置
使用apt-get安装无法保证我们下载的版本。下面介绍源码安装思路。
二:源码安装
1.下载安装包
国外的
国内的
选择要下载的版本。
- 解压
tar xzvf go1.**.*.linux-amd64.tar.gz -C /usr/lib/
-
在
~/.bashrc(
或者/etc/profile)
中,添加环境变量(如果是别的sh,请在别的sh中配置#GOROOT export GOROOT=/usr/lib/go #GOPATH go项目路径 export GOPATH=/root/gocode #GOPATH bin export PATH=$PATH:$GOPATH/bin #GOPATH root bin export PATH=$PATH:$GOROOT/bin
source ~/.bashrc(
或者/etc/profile)
-
验证
[root@compute84 bin]# go version go version go1.12.7 linux/amd64 [root@compute84 bin]# [root@compute84 bin]# go env GOARCH="amd64" GOBIN="" GOCACHE="/root/.cache/go-build" GOEXE="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOOS="linux" GOPATH="/root/gocode" GOPROXY="https://goproxy.io" GORACE="" GOROOT="/usr/lib/go" GOTMPDIR="" GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64" GCCGO="gccgo" CC="gcc" CXX="g++" CGO_ENABLED="1" GOMOD="" 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-build670179