download
download link
Go official website Download: https://golang.org/dl/
Go official mirror sites (recommended): https://golang.google.cn/dl/
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
Configuration GOPATH
GOPATH
It is an environment variable to indicate the storage path you go to write the project (working directory).
GOPATH
The best path set only one, all of the items code into GOPATH
the src
directory.
Additional information: After Go1.11 version, open go mod
after the model is no longer mandatory to configure GOPATH
a.
Linux and Mac platforms on the reference configuration environment variable way above to add your own working directory to the environment variable can be. Windows platform, follow the steps below to D:\code\go
add to environment variable:
Go project structure
Go during the time of language development, our code will always be stored in $GOPATH/src
the directory. After the works go build
, go install
or go get
the like instructions, third party package will download the source code files in $GOPATH/src
the directory, resulting in binary executable files $GOPATH/bin
directory, the generated intermediate file will be stored in the cache $GOPATH/pkg
under.
If we use the version management tool (Version Control System, VCS. As usual Git) to manage our project code, we only need to add the $GOPATH/src
source code to the directory. bin
And pkg
directory content without having to version control.
Go Development Editor
Download and install
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语言开发工具包
拷贝到自己电脑上的 GOROOT/bin
目录下。 go-tools百度云下载链接,密码:vjx2。
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
然后再执行go build
命令,得到的就是能够在Linux平台运行的可执行文件了。
Mac 下编译 Linux 和 Windows平台 64位 可执行程序:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build
Linux 下编译 Mac 和 Windows 平台64位可执行程序:
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
原文地址:https://www.liwenzhou.com/posts/Go/install_go_dev/