Go(Golang)安装教程

个人推荐采用源码安装或者第三方工具安装。

一、源码安装

1.设置Go的环境变量

GOROOT_BOOTSTRAP 这个目录在安装 Go 1.5 版本及之后的版本时需要设置。由于在 1.4 版本后,Go 编译器实现了自举,即通过 1.4 版本来编译安装之后版本的编译器。如果不设置该环境变量的话,会产生这样一个错误“Set $GOROOT_BOOTSTRAP to a working Go tree >= Go 1.4.”。

# 下载源码包
cd /usr/local
wget https://storage.googleapis.com/golang/go1.4-bootstrap-20170531.tar.gz  
tar -xf go1.4-bootstrap-20170531.tar.gz

# 安装
cd go/src  
./make.bash  

mv go go1.4
vim /etc/profile

# 添加:
export GOROOT_BOOTSTRAP=/usr/local/go1.4

source /etc/profile

2.下载源码包

cd /usr/local
wget https://storage.googleapis.com/golang/go1.10.src.tar.gz  
tar -xf go1.10.src.tar.gz

3.安装Go

cd go/src  
./all.bash

4.设置环境变量

vim /etc/profile
export GOROOT=/usr/local/go1.10
export GOBIN=$GOROOT/bin
export PATH=$PATH:$GOBIN

5.设置工作目录

export GOPATH=/usr/local/mygo
export PATH=$PATH:${GOPATH//://bin:}/bin
source /etc/profile
go version

至此,我们的Golang已经安装好了

6.工作目录介绍

GOPATH允许多个目录,当有多个目录时,默认会将go get的内容放在第一个目录下。
$GOPATH 目录约定有三个子目录:
src 存放源代码(比如:.go .c .h .s等)
pkg 编译后生成的文件(比如:.a)
bin 编译后生成的可执行文件(为了方便,可以把此目录加入到 $PATH 变量中,如果有多个gopath,那么使用${GOPATH//://bin:}/bin添加所有的bin目录)

二、第三方工具安装

1.GVM

gvm是第三方开发的Go多版本管理工具,类似ruby里面的rvm工具。使用起来相当的方便,安装gvm使用如下命令:

bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)
安装完成后我们就可以安装go了:
gvm install go1.10
gvm use go1.10
也可以使用下面的命令,省去每次调用gvm use的麻烦
gvm use go1.10 --default

执行完上面的命令之后GOPATH、GOROOT等环境变量会自动设置好,这样就可以直接使用了。

2.homebrew

homebrew是Mac系统下面目前使用最多的管理软件的工具,目前已支持Go,可以通过命令直接安装Go,为了以后方便,应该把 git mercurial 也安装上:

安装homebrew

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

安装go

brew update && brew upgrade
brew install go
brew install git
brew install mercurial //可选安装
发布了86 篇原创文章 · 获赞 70 · 访问量 23万+

猜你喜欢

转载自blog.csdn.net/wuxing26jiayou/article/details/79694735