Go Go installation language and language development environment to build

Step by step, from zero to build the Go language development environment.

Go Go installation language and language development environment to build

undefined

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.

undefined

installation

Windows Installation

In this installation example 64位Win10system installation Go1.11.5可执行文件版本example.

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

undefined

Double-click the downloaded file has beenNext

undefined

undefined

Installation under Linux

We select and download the good version selection page go1.11.5.linux-amd64.tar.gzfile:

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

Extract the downloaded file to the /usr/localdirectory:

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 sudoanother 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/profileis in effect for all users; $HOME/.profileis 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/profileafter the restart to take effect, modify $HOME/.profileusing the source command to load the $HOME/.profilefile 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/godirectory.

undefined

an examination

After step installation process is finished, you can open a terminal window, enter the go versioncommand to view the Go version installed.

Configuration GOPATH

GOPATHIt is an environment variable to indicate the storage path you go to write the project (working directory).

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

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

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

undefined

undefined

1550294111480

image.png

undefined

在 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目录都添加到环境变量中。

配置环境变量之后需要重启终端(cmd、VS Code里面的终端和其他编辑器的终端)。

Go项目结构

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

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

适合个人开发者

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

undefined

目前流行的项目结构

Go语言中也是通过包来组织代码文件,我们可以引用别人的包也可以发布自己的包,但是为了防止不同包的项目名冲突,我们通常使用顶级域名来作为包名的前缀,这样就不担心项目名冲突的问题了。

因为不是每个个人开发者都拥有自己的顶级域名,所以目前流行的方式是使用个人的github用户名来区分不同的包。

undefined

举个例子:张三和李四都有一个名叫studygo的项目,那么这两个包的路径就会是:

import "github.com/zhangsan/studygo"

import "github.com/lisi/studygo"

以后我们从github上下载别人包的时候,如:

go get github.com/jmoiron/sqlx

那么,这个包会下载到我们本地GOPATH目录下的src/github.com/jmoiron/sqlx

适合企业开发者

undefined

Go开发编辑器

Go采用的是UTF-8编码的文本文件存放源代码,理论上使用任何一款文本编辑器都可以做Go语言开发,这里推荐使用VS CodeGolandVS Code是微软开源的编辑器,而Golandjetbrains出品的付费IDE

我们这里使用VS Code 加插件做为go语言的开发工具。

VS Code介绍

VS Code全称Visual Studio Code,是微软公司开源的一款免费现代化轻量级代码编辑器,支持几乎所有主流的开发语言的语法高亮、智能代码补全、自定义热键、括号匹配、代码片段、代码对比DiffGIT等特性,支持插件扩展,支持 Win、Mac 以及 Linux平台。

虽然不如某些IDE功能强大,但是它添加Go扩展插件后已经足够胜任我们日常的Go开发。

下载与安装

VS Code官方下载地址:https://code.visualstudio.com/Download

三大主流平台都支持,请根据自己的电脑平台选择对应的安装包。

undefined

双击下载好的安装文件,双击安装即可。

配置

安装中文简体插件

点击左侧菜单栏最后一项管理扩展,在搜索框中输入chinese ,选中结果列表第一项,点击install安装。

安装完毕后右下角会提示重启VS Code,重启之后你的VS Code就显示中文啦!

undefined

VSCode主界面介绍:

undefined

安装go扩展

现在我们要为我们的VS Code编辑器安装Go扩展插件,让它支持Go语言开发。

undefined

第一个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目录,所以我们就可以在任意地方直接执行可执行文件了。

跨平台编译

Our default go buildexecutable files are current operating system executable files, executable files if I want to compile a windows under linux, you need to how to do it?

Only you need to specify the target operating system platforms and processor architectures can:

SET CGO_ENABLED=0  // 禁用CGO
SET GOOS=linux  // 目标平台是linux
SET GOARCH=amd64  // 目标处理器架构是amd64

And then execute go buildthe 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.

Guess you like

Origin www.cnblogs.com/Dr-wei/p/11735183.html