Go GO install language learning and language development environment to build Go

One. download

1. Download

Go official website Download: https://golang.org/dl/

Go official mirror sites (recommended): https://golang.google.cn/dl/

2. The version of choice

Windows and Mac platforms recommend downloading executable version, Linux platform, download the zip file version.

The following diagram version number may not be the latest, but overall installation tutorial is similar.

 

two. installation

1. Windows installation

This installation instance to  64 Win10 system installation  Go1.11.5 executable version as an example.

The last step of the selected installation package downloaded to the local.

 

Double-click on the downloaded file, and then following the steps in FIG installed.

 

 

 

 

 

 

 

2. Linux installation

We chose to select the version of the page and download the good go1.11.5.linux-amd64.tar.gz file:

wget https://dl.google.com/go/go1.11.5.linux-amd64.tar.gz

 

Extract the downloaded file to / usr / local directory:

mkdir -p / usr / local / go # Create a directory tar -C / usr / local / go zxvf go1.11.5.linux-amd64.tar.gz. # unzip

如果提示没有权限,加上sudoroot用户的身份再运行。执行完就可以在/usr/local/下看到go目录了。

配置环境变量: Linux下有两个文件可以配置环境变量,其中/etc/profile是对所有用户生效的;$HOME/.profile是对当前用户生效的,根据自己的情况自行选择一个文件打开,添加如下两行代码,保存退出。

export GOROOT=/usr/local/goexport PATH=$PATH:$GOROOT/bin

 

修改/etc/profile后要重启生效,修改$HOME/.profile后使用source命令加载$HOME/.profile文件即可生效。 检查:

~ go version

go version go1.11.5 linux/amd64

 

windows 还需要配置环境变量:

 

三.配置GOPATH

 

GOPATH是一个环境变量,用来表明你写的go项目的存放路径(工作目录)。

 

GOPATH路径最好只设置一个,所有的项目代码都放到GOPATHsrc目录下。

 

补充说明:Go1.11版本之后,开启go mod模式之后就不再强制需要配置GOPATH了。

 

Linux和Mac平台就参照上面配置环境变量的方式将自己的工作目录添加到环境变量中即可。 Windows平台按下面的步骤将D:\code\go添加到环境变量:

 

 

 

 

 

 

 

 

 

 

Go 1.8 版本之前,GOPATH环境变量默认是空的。从 Go 1.8 版本开始,Go 开发包在安装完成后会为 GOPATH设置一个默认目录,参见下表。

GOPATH在不同操作系统平台上的默认值

 

 

同时,我们将 GOROOT下的bin目录及GOPATH下的bin目录都添加到环境变量中。

配置环境变量之后需要重启你电脑上已经打开的终端。(例如cmd、VS Code里面的终端和其他编辑器的终端)。

四.Go项目结构

  在进行Go语言开发的时候,我们的代码总是会保存在$GOPATH/src目录下。在工程经过go buildgo installgo get等指令后,会将下载的第三方包源代码文件放在$GOPATH/src目录下, 产生的二进制可执行文件放在 $GOPATH/bin目录下,生成的中间缓存文件会被保存在 $GOPATH/pkg 下。

  如果我们使用版本管理工具(Version Control System,VCS。常用如Git)来管理我们的项目代码时,我们只需要添加$GOPATH/src目录的源代码即可。bin 和 pkg 目录的内容无需版本控制。

1.适合个人开发者

我们知道源代码都是存放在GOPATHsrc目录下,那我们可以按照下图来组织我们的代码。

 

 

Guess you like

Origin www.cnblogs.com/hszstudypy/p/12323861.html