环境centos7
设置环境变量
[root@localhost ~]# vim .bashrc
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
export GOPATH=/usr/local/Applications/Go
[root@localhost ~]# source .bashrc
安装依赖性
[root@localhost ~]# yum install bison ed gawk gcc libc6-dev make
下载go语言压缩包并解压
[root@localhost ~]# tar -zxf go1.15.linux-amd64.tar.gz -C /usr/local
解压完成后可以使用命令查看版本
[root@localhost ~]# go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/usr/local/Applications/Go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/usr/local/Applications/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"
GCCGO="gccgo"
AR="ar"
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-build103336769=/tmp/go-build -gno-record-gcc-switches"
下来构建 Go
[root@localhost ~]# cd /usr/local/go/src/
[root@localhost src]# ls
all.bash cmd fmt make.bash README.vendor testdata
all.bat cmp.bash go make.bat reflect testing
all.rc compress go.mod Make.dist regexp text
archive container go.sum make.rc run.bash time
bootstrap.bash context hash math run.bat unicode
bufio crypto html mime run.rc unsafe
buildall.bash database image net runtime vendor
builtin debug index os sort
bytes encoding internal path strconv
clean.bash errors io plugin strings
clean.bat expvar iostest.bash race.bash sync
clean.rc flag log race.bat syscall
[root@localhost src]# ./all.bash ##使用脚本进行构建
./make.bash:行165: /root/go1.4/bin/go: 没有那个文件或目录
碰见上诉错误
可能是因为 $GOROOT_BOOTSTRAP 变量没有设置。这个目录在安装 Go 1.5 版本及之后的版本时需要设置。
由于在 1.4 版本后,Go 编译器实现了自举,即通过 1.4 版本来编译安装之后版本的编译器。如果不设置该环境变量的话,会产生这样一个错误 Set $GOROOT_BOOTSTRAP to a working Go tree >= Go 1.4.
。
设置 $GOROOT_BOOTSTRAP 变量:
[root@localhost src]# export GOROOT_BOOTSTRAP=/usr/local/go1.4
设置完成后下载1.4版本到下面目录先进行编译
[root@localhost src]# git clone https://github.com/golang/go.git /usr/local/go1.4
[root@localhost local]# cd go1.4/
[root@localhost go1.4]# cd src/
[root@localhost src]# ls
all.bash cmd fmt make.bash README.vendor testdata
all.bat cmp.bash go make.bat reflect testing
all.rc compress go.mod Make.dist regexp text
archive container go.sum make.rc run.bash time
bootstrap.bash context hash math run.bat unicode
bufio crypto html mime run.rc unsafe
buildall.bash database image net runtime vendor
builtin debug index os sort
bytes encoding internal path strconv
clean.bash errors io plugin strings
clean.bat expvar iostest.bash race.bash sync
clean.rc flag log race.bat syscall
[root@localhost src]# ./make.bash ##编译1.4版本完成后再运行1.5版本
[root@localhost local]# cd go
go/ go1.4/
[root@localhost local]# cd go
[root@localhost go]# ls
api CONTRIBUTING.md favicon.ico misc README.md src
AUTHORS CONTRIBUTORS lib PATENTS robots.txt test
bin doc LICENSE pkg SECURITY.md VERSION
[root@localhost go]# cd src/
[root@localhost src]# ls
all.bash cmd fmt make.bash README.vendor testdata
all.bat cmp.bash go make.bat reflect testing
all.rc compress go.mod Make.dist regexp text
archive container go.sum make.rc run.bash time
bootstrap.bash context hash math run.bat unicode
bufio crypto html mime run.rc unsafe
buildall.bash database image net runtime vendor
builtin debug index os sort
bytes encoding internal path strconv
clean.bash errors io plugin strings
clean.bat expvar iostest.bash race.bash sync
clean.rc flag log race.bat syscall
[root@localhost src]# ./all.bash
安装完成后可以进行尝试
[root@localhost ~]# vim hello-world.go
package main
func main() {
println("Hello", "world")
}
[root@localhost ~]# go run hello-world.go ##运行后可以打印出说明已经完成
Hello world