https://tecadmin.net/install-go-on-centos/
这个是一个比较完整的安装过程说明
官网说明,一定权威啦!
本次下载的源码版本为1.12.7,下载地址:
https://golang.org/doc/install?download=go1.12.7.linux-amd64.tar.gz
(二)开始记录
1、利用yum直接进行安装
yum install golang
用yum install golang时出现提示:没有可用软件包 golang,Google一下发现是yum源中没有该软件包,但可以用epel-release(https://blog.csdn.net/upshi/article/details/78987509),简单明了,直接安装epel-release:
yum install epel-release
epel-release安装成功后可以直接用yum进行安装:
yum install golang
现在应该可直接成功了,安装成功后用go version查看其版本为"go version go1.11.5 linux/amd64"到官网一看发现最新版本是1.12.7,心里感觉有点不爽,就想着把yum安装的结果卸载再重新安装。查了下卸载方法(https://golang.org/doc/install?download=go1.12.7.linux-amd64.tar.gz#uninstall):大概意思就是先直接删除go目录,再删除相应的环境变量:
“To remove an existing Go installation from your system delete the go
directory. This is usually /usr/local/go
under Linux, macOS, and FreeBSD or c:\Go
under Windows.
You should also remove the Go bin
directory from your PATH
environment variable. Under Linux and FreeBSD you should edit /etc/profile
or $HOME/.profile
. If you installed Go with the macOS package then you should remove the /etc/paths.d/go
file. Windows users should read the section about setting environment variables under Windows.”
这里我直接使用的是yum remove golang,再直接删除相应环境变量。
2、利用源码进行安装:
2.1下载源码
go1.12.7.linux-amd64.tar.gz
2.2对源码包进行解压,目标目标为/usr/local
tar -C /usr/local -zxf /mnt/go1.12.7.linux-amd64.tar.gz
2.3设置环境变量GOROOT, GOPATH和PATH
Now you need to set up Go language environment variables for your project. Commonly you need to set 3 environment variables as GOROOT, GOPATH and PATH.
GOROOT is the location where Go package is installed on your system.
export GOROOT=/usr/local/go
GOPATH is the location of your work directory. For example my project directory is ~/Projects/Proj1 .
export GOPATH=$HOME/Projects/Proj1
Now set the PATH variable to access go binary system wide.
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH
All the above environment will be set for your current session only. To make it permanent add above commands in ~/.bash_profile file.
这里利用的是直接编辑.bash_profile文件,进行编辑后大概如下所示:
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export GOROOT=/usr/local/go
export GOPATH=$HOME/Projects/GoProject
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH
由于设置环境变量后需要再次登录才有效,可以使用如下命令强制刷新:
source
~/.bash_profile.