GO开发环境的搭建(MAC系统下)

开发者的第一步骚操作就是搭建开发环境:

1.选择IDE

2.开发包和环境变量的搭建

     2.1  下载安装go工具包

    2.2  设置GOROOT环境变量

    2.3  设置GOPATH和GOBIN环境变量

    2.4  重新加载环境变量配置

说明:

1 、2 步是可以并行的。在选择IDE时候,我使用的是LITEIDE,下载地址:https://www.golangtc.com/download/liteide

言归正传,环境搭建。

2.1  下载安装go的工具包

下载安装go的工具包有两种方式:1.使用mac的 brew命令进行安装  2. 下载代码,编译安装。

本次采用第1种,使用mac的 brew命令进行安装。使用命令如下:

brew install go  # 默认安装最新版本

对于部分开发者,想要安装某一版本的工具包的话。可以使用brew search [包名]  来查看可以安装哪些版本的工具包

brew search go

go             [email protected]              [email protected]          [email protected]                 [email protected] 

可以采用    brew install  [email protected]       安装1.10 版的go工具包。

mac 系统会把go的工具包安装于/usr/local/Cellar/go 目录下

本人安装的是1.11 版本的,安装目录为

          /usr/local/Cellar/go/1.11/bin/go
          /usr/local/Cellar/go/1.11/bin/godoc
          /usr/local/Cellar/go/1.11/bin/gofmt
          /usr/local/Cellar/go/1.11/libexec/api/ (14 files)
          /usr/local/Cellar/go/1.11/libexec/bin/ (3 files)
          /usr/local/Cellar/go/1.11/libexec/doc/ (150 files)
          /usr/local/Cellar/go/1.11/libexec/lib/ (3 files)
          /usr/local/Cellar/go/1.11/libexec/misc/ (359 files)
          /usr/local/Cellar/go/1.11/libexec/pkg/ (1043 files)
          /usr/local/Cellar/go/1.11/libexec/src/ (4794 files)
          /usr/local/Cellar/go/1.11/libexec/test/ (1781 files)
          /usr/local/Cellar/go/1.11/libexec/ (6 files)

2.2  设置GOROOT环境变量

编辑.bash_profile文件( 目录:/Users/ [UserName] /.bash_profile)

使用vim  .bash_profile  

.bash_profile文件中,新增如下内容:

# setting path for  go   by tyltr
GOROOT=/usr/local/Cellar/go/1.11/libexec
export GOROOT 
export PATH=$PATH:$GOROOT/bin

说明,不能将GOROOT设置为/usr/local/bin/go,因为GOROOT表示的是go的安装路径,不是go工具包文件的路径。否则,在编译go源代码文件时就会报错。

2.3  设置GOPATH和GOBIN环境变量

GOPATH:日常开发的根目录,Go必须设置这个变量,而且不能和Go的安装目录一样,这个目录用来存放Go源码,Go的可运行文件,以及相应的编译之后的包文件,因此,这个目录下有三个子目录: src、bin、pkg  与之一一对应。

src : 主要存放go的源文件(比如:.go .c .h .s等 )

bin : 主要存放可执行文件(为了方便,可以把此目录加入到 $PATH 变量中)

pkg: 主要存放编译好的库文件(主要是*.a文件)

本人在磁盘中,新建了一个目录 goSpace 专门用来作为go的工作空间。在.bash_profile文件中,新增如下内容:

           export GOPATH=/Users/tyltr/goSpace
           export GOBIN=$GOPATH/bin
           export PATH=$PATH:$GOBIN

 

2.4  重新加载环境变量配置

tyltrinobugdeMacBook-Pro:~ tyltr$ source .bash_profile 

猜你喜欢

转载自blog.csdn.net/tylitianrui/article/details/82826720