Go(又称Golang)是Google开发的一种静态强类型、编译型、并发型,并具有垃圾回收功能的编程语言,
Go语言当前支持FreeBSD、Linux、macOS和Windows,最新版本信息,可以参考http://docs.studygolang.com/doc/install,
安装包可以从https://golang.google.cn/找到和下载,
我使用Windows的版本,进行测试,go1.13.7.windows-amd64.msi,是最新的安装,
Windows下是“傻瓜式“安装,除了选择安装的路径外(默认C:\Go),其他无脑“下一步”即可。
GOPATH是一个环境变量,存储的是go项目的存放路径(工作目录),所有的项目代码都放到GOPATH的src目录下。在Go 1.8版本之前,GOPATH环境变量默认是空的。从Go 1.8版本开始,Go开发包在安装完成后会为GOPATH设置一个默认目录,
Windows如下所示,其中USERPROFILE是"C:\Users\用户名",
可以变更位置,
GOPATH路径下可创建三个文件夹,
bin,用来存放编译后生成的可执行文件。
pkg,用来存放编译后生成的归档文件。
src,用来存放源码文件。
命令行执行go,显示了帮助信息,
C:\Users\Liu>go
Go is a tool for managing Go source code.
Usage:
go <command> [arguments]
The commands are:
bug start a bug report
build compile packages and dependencies
clean remove object files and cached files
doc show documentation for package or symbol
env print Go environment information
fix update packages to use new APIs
fmt gofmt (reformat) package sources
generate generate Go files by processing source
get add dependencies to current module and install them
install compile and install packages and dependencies
list list packages or modules
mod module maintenance
run compile and run Go program
test test packages
tool run specified go tool
version print Go version
vet report likely mistakes in packages
Use "go help <command>" for more information about a command.
Additional help topics:
buildmode build modes
c calling between Go and C
cache build and test caching
environment environment variables
filetype file types
go.mod the go.mod file
gopath GOPATH environment variable
gopath-get legacy GOPATH go get
goproxy module proxy protocol
importpath import path syntax
modules modules, module versions, and more
module-get module-aware go get
module-auth module authentication using go.sum
module-private module configuration for non-public modules
packages package lists and patterns
testflag testing flags
testfunc testing functions
Use "go help <topic>" for more information about that topic.
使用任何一款文本编辑器,都可以写Go,推荐使用Visual Studio Code,可以安装Go组件,
第一个Go程序,当然是我们最熟悉的Hello World,文件名hello.go,
package main // 声明main包,表明当前是一个可执行程序
import "fmt" // 导入内置fmt包
func main() { // main函数,是程序执行的入口
fmt.Println("Hello World") // 在终端打印Hello World
}
到hello.go所在文件夹,执行go build编译,执行生成的二进制文件hello.exe,
D:\Work\Dev\Go\code\src\hello>go build
D:\Work\Dev\Go\code\src\hello>hello.exe
Hello World
在Windows下安装Go环境,还是比较简单,当然简单的程序,不能展示出Go语言的强大,需要继续挖掘。