Windows 平台下 Go 语言的安装和环境变量设置
jopen
9年前
1. Go 语言 SDK 安装包下载和安装
最新稳定版 1.5.3 安装包 go1.5.3.windows-amd64.msi下载地址 https://golang.org/dl/,大小约 69 MB(作者上传了一份该版本安装包到 CSDN 资源,嫌国外网速慢可以去下载: http://download.csdn.net/detail/defonds/9408855),下载之后直接运行安装,安装路径就使用它默认的 C:\Go\ 即可,之后一路 Next 并完成安装。
2. Go 语言环境变量设置
Go 语言需要配置 GOROOT 和 Path 两个环境变量:GOROOT 和 GOPATH。2.1 根目录 GOROOT 和 Path
如果第一步安装使用的是默认安装目录 C:\Go\,那么安装程序就已经将 GOROOT 和 Path 两个环境变量设置好了,无须再对其进行手工设置。

如果你第一步没有使用默认安装目录,那么需要对上述两个变量进行手工配置,注意 Path 配置时不要影响到其他安装程序(如 Java,Oracle,Maven 等等)。
2.2 Go 工作目录 GOPATH
这个是后续代码目录,新建系统变量 GOPATH,将其指向你的代码目录:
2.3 命令行对环境变量进行验证
打开 CMD 终端,输入 go env 查看变量是否设置成功:
注意红框标记部分是否正确。
3. 编写第一个 Go 程序
创建 hello.go 文件并编辑其内容如下:package main import "fmt" func main() { fmt.Printf("hello, world\n") }
保存后进入该目录,执行 go run hello.go:

看到 "hello, world" 证明我们的 go 安装成功了。