Step by step, from zero to build the Go language development environment.
Go Go installation language and language development environment to build
download
download link
Go official website Download: https://golang.org/dl/
Go official mirror sites (recommended): https://golang.google.cn/dl/
Select versions
Windows and Mac platforms recommend downloading executable version, Linux platform, download the zip file version.
installation
Windows Installation
In this installation example 64位Win10
system installation Go1.11.5可执行文件版本
example.
The last step of the selected installation package downloaded to the local.
Double-click the downloaded file
Installation under Linux
We select and download the good version selection page 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 the /usr/local
directory:
mkdir -p /usr/local/go # 创建目录
tar -C /usr/lcoal/go zxvf go1.11.5.linux-amd64.tar.gz. # 解压
If you do not have permission prompts, plus sudo
another run as the root user. It can perform finished in /usr/local/
see go under the directory.
Configure the environment variables: There are two files you can configure the Linux environment variable, which /etc/profile
is in effect for all users; $HOME/.profile
is the current user according to their own situation choose to open a file, add the following two lines of code, save and exit.
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
Modified /etc/profile
after the restart to take effect, modify $HOME/.profile
using the source command to load the $HOME/.profile
file to take effect. an examination:
~ go version
go version go1.11.5 linux/amd64
Under Mac installation
Download the executable version, just click Next to be installed, the default will go to the installation /usr/local/go
directory.
an examination
After step installation process is finished, you can open a terminal window, enter the go version
command to view the Go version installed.
Configuration GOPATH
GOPATH
It is an environment variable to indicate the storage path you go to write the project (working directory).
GOPATH
路径最好只设置一个,所有的项目代码都放到GOPATH
的src
目录下。
Linux和Mac平台就参照上面配置环境变量的方式将自己的工作目录添加到环境变量中即可。 Windows平台按下面的步骤将D:\code\go
添加到环境变量:
在 Go 1.8 版本之前,GOPATH
环境变量默认是空的。从 Go 1.8 版本开始,Go 开发包在安装完成后会为 GOPATH
设置一个默认目录,参见下表。
GOPATH在不同操作系统平台上的默认值
平台 | GOPATH默认值 | 举例 |
---|---|---|
Windows | %USERPROFILE%/go | C:\Users\用户名\go |
Unix | $HOME/go | /home/用户名/go |
同时,我们将 GOROOT
下的bin目录及GOPATH
下的bin目录都添加到环境变量中。
Go项目结构
在进行Go语言开发的时候,我们的代码总是会保存在$GOPATH/src
目录下。在工程经过go build
、go install
或go get
等指令后,会将下载的第三方包源代码文件放在$GOPATH/src
目录下, 产生的二进制可执行文件放在 $GOPATH/bin
目录下,生成的中间缓存文件会被保存在 $GOPATH/pkg
下。
如果我们使用版本管理工具(Version Control System,VCS。常用如Git)来管理我们的项目代码时,我们只需要添加$GOPATH/src
目录的源代码即可。bin
和 pkg
目录的内容无需版本控制。
适合个人开发者
我们知道源代码都是存放在GOPATH
的src
目录下,那我们可以按照下图来组织我们的代码。
目前流行的项目结构
Go语言中也是通过包来组织代码文件,我们可以引用别人的包也可以发布自己的包,但是为了防止不同包的项目名冲突,我们通常使用顶级域名
来作为包名的前缀,这样就不担心项目名冲突的问题了。
因为不是每个个人开发者都拥有自己的顶级域名,所以目前流行的方式是使用个人的github用户名来区分不同的包。
举个例子:张三和李四都有一个名叫studygo
的项目,那么这两个包的路径就会是:
import "github.com/zhangsan/studygo"
和
import "github.com/lisi/studygo"
以后我们从github上下载别人包的时候,如:
go get github.com/jmoiron/sqlx
那么,这个包会下载到我们本地GOPATH
目录下的src/github.com/jmoiron/sqlx
。
适合企业开发者
Go开发编辑器
Go采用的是UTF-8编码的文本文件存放源代码,理论上使用任何一款文本编辑器都可以做Go语言开发,这里推荐使用VS Code
和Goland
。 VS Code
是微软开源的编辑器,而Goland
是jetbrains出品的付费IDE。
我们这里使用VS Code
加插件做为go语言的开发工具。
VS Code介绍
VS Code
全称Visual Studio Code
,是微软公司开源的一款免费现代化轻量级代码编辑器,支持几乎所有主流的开发语言的语法高亮、智能代码补全、自定义热键、括号匹配、代码片段、代码对比 Diff、GIT 等特性,支持插件扩展,支持 Win、Mac 以及 Linux平台。
虽然不如某些IDE功能强大,但是它添加Go扩展插件后已经足够胜任我们日常的Go开发。
下载与安装
VS Code
官方下载地址:https://code.visualstudio.com/Download
三大主流平台都支持,请根据自己的电脑平台选择对应的安装包。 双击下载好的安装文件,双击安装即可。
配置
安装中文简体插件
点击左侧菜单栏最后一项管理扩展
,在搜索框
中输入chinese
,选中结果列表第一项,点击install
安装。
安装完毕后右下角会提示重启VS Code
,重启之后你的VS Code就显示中文啦! VSCode
主界面介绍:
安装go扩展
现在我们要为我们的VS Code编辑器安装Go
扩展插件,让它支持Go语言开发。
第一个Go程序
Hello World
现在我们来创建第一个Go项目——hello
。在我们的GOPATH
下的src目录中创建hello目录。
在该目录中创建一个main.go
文件:
package main // 声明 main 包,表明当前是一个可执行程序
import "fmt" // 导入内置 fmt 包
func main(){ // main函数,是程序执行的入口
fmt.Println("Hello World!") // 在终端打印 Hello World!
}
go build
go build
表示将源代码编译成可执行文件。
在hello目录下执行:
go build
或者在其他目录执行以下命令:
go build hello
go编译器会去 GOPATH
的src目录下查找你要编译的hello
项目
编译得到的可执行文件会保存在执行编译命令的当前目录下,如果是windows平台会在当前目录下找到hello.exe
可执行文件。
可在终端直接执行该hello.exe
文件:
d:\code\go\src\hello>hello.exe
Hello World!
我们还可以使用-o
参数来指定编译后可执行文件的名字。
go build -o heiheihei.exe
go install
go install
表示安装的意思,它先编译源代码得到可执行文件,然后将可执行文件移动到GOPATH
的bin目录下。因为我们的环境变量中配置了GOPATH
下的bin目录,所以我们就可以在任意地方直接执行可执行文件了。
跨平台编译
默认我们go build
的可执行文件都是当前操作系统可执行的文件,如果我想在windows下编译一个linux下可执行文件,那需要怎么做呢?
只需要指定目标操作系统的平台和处理器架构即可:
SET CGO_ENABLED=0 // 禁用CGO
SET GOOS=linux // 目标平台是linux
SET GOARCH=amd64 // 目标处理器架构是amd64
And then execute go build
the command, the resulting executable file that can run on a Linux platform.
Mac compile under Linux and Windows 64-bit executable programs:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build
Linux compiler Mac and Windows 64-bit executable programs:
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build
Windows 64-bit compiler executable Mac platform:
SET CGO_ENABLED=0
SET GOOS=darwin
SET GOARCH=amd64
go build
Now, open your Go language learning journey. Life is short, let's Go.