1. 安装 Go

Go 项目开源在 Github,我们可以直接到 国际官网 或 国内官网 下载安装程序即可

1.1 Windows 安装 go1.15

go1.15.2.windows-amd64.msiC:\Go\PATHC:\Go\binCtrl + RCMDgo
Microsoft Windows [版本 10.0.14393]
(c) 2016 Microsoft Corporation。保留所有权利。

C:\Users\wangy>go version
go version go1.15.2 windows/amd64

1.2 Linux 安装 go1.15

如果你想在 Linux 系统中安装 Go:

1. 下载与解压
# wget https://golang.google.cn/dl/go1.15.2.linux-amd64.tar.gz
# tar -xzf go1.15.2.linux-amd64.tar.gz -C /usr/local

2. 设置环境变量
# echo -e "export PATH=$PATH:/usr/local/go/bin" >> /etc/profile
# source /etc/profile

3. 验证
# go version
2. 熟悉 Go 相关子命令
go编译(go build)安装(go install)运行(go run)下载第三方包(go get)
bug         start a bug report
build       compile packages and dependencies
clean       remove object files
doc         show documentation for package or symbol  查看包文档
env         print Go environment information  查看或修改 Go 环境变量
fix         run go tool fix on packages
fmt         run gofmt on package sources  使用官方风格格式化 Go 代码
generate    generate Go files by processing source
get         download and install packages and dependencies  使用 Git 等代码版本控制软件将一个远程 Go 代码库拉取到本地,以便将其做为第三方 Go 包引入并使用
install     compile and install packages and dependencies
list        list packages
mod         module maintenance  维护 Go 模块
run         compile and run Go program
test        test packages  运行单元测试和基准测试
tool        run specified go tool
version     print Go version
vet         run go tool vet on packages  检查可能的代码逻辑错误
go help env
C:\Users\wangy>go env
...
set GOOS=windows
set GOROOT=D:\Go1.15
set GOPATH=C:\Users\wangy\go
set GOPROXY=https://proxy.golang.org,direct
...

设置:

C:\Users\wangy>go env -w GOPATH=D:\GoCodes

取消设置:

C:\Users\wangy>go env -u GOPATH
go
3. 设置 Go 模块代理: GOPROXY
GOPROXYhttps://proxy.golang.orggolang.org/x/*golang.org/x/sysgolang.org/x/netgolang.org/x/tour
https://goproxy.cnhttps://goproxy.ioGORPOXY
C:\Users\wangy>go env -w GOPROXY=https://goproxy.cn,direct
GolandFileSettins...GoGo Modules (vgo)Proxyhttps://goproxy.cn,direct
4. 编辑器插件和IDE
5. Hello World
C:\Users\wangy>D:
D:\>cd GoCodes
D:\GoCodes>mkdir hello
D:\GoCodes>cd hello
D:\GoCodes\hellohello.go
package main  // 定义 main 包

import "fmt" // 导入标准库中的 fmt 包

func main() { // main 函数
    fmt.Println("Hello, World!") // 调用 fmt 包中导出的 Println() 函数
}
%GOROOT%\srcC:\Go\srcfmtC:\Go\src\fmt\

然后在 CMD 中运行:

D:\GoCodes\hello>go run hello.go
Hello, World!
6. Go Modules
go help modules
rsc.io/quote
package main

import (
    "fmt"

    "rsc.io/quote"
)

func main() {
    fmt.Println(quote.Go())
}

再次在 CMD 中运行:

D:\GoCodes\hello>go run hello.go
hello.go:6:2: cannot find module providing package rsc.io/quote: working directory is not part of a module
模块(module)GO111MODULE=onGo ModulesGo Modules
D:\GoCodes\hello>go mod init github.com/wangy8961/hello
go: creating new go.mod: module github.com/wangy8961/hello
go.mod
module github.com/wangy8961/hello  // 模块路径(module path)

go 1.15
go rungo buildGo Modules%GOPATH%\pkg\modD:\GoCodes\pkg\mod
D:\GoCodes\hello>go run hello.go
go: finding module for package rsc.io/quote
go: found rsc.io/quote in rsc.io/quote v1.5.2
Don't communicate by sharing memory, share memory by communicating.
go.mod
module github.com/wangy8961/hello

go 1.15

require rsc.io/quote v1.5.2 // indirect
requirereplaceexclude
go.sum
<module> <version> <hash>
或:
<module> <version>/go.mod <hash>

分别表示模块路径、模块版本号、以 h1: 开头的模块 Hash 值

如果远程模块使用了 tag 表示版本号时:

github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=

未使用版本号时:

golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
go mod
D:\GoCodes\hello>go help mod
Go mod provides access to operations on modules.

Note that support for modules is built into all the go commands,
not just 'go mod'. For example, day-to-day adding, removing, upgrading,
and downgrading of dependencies should be done using 'go get'.
See 'go help modules' for an overview of module functionality.

Usage:

        go mod <command> [arguments]

The commands are:

        download    download modules to local cache  修改 go.mod 文件后,下载更新的依赖模块
        edit        edit go.mod from tools or scripts
        graph       print module requirement graph
        init        initialize new module in current directory
        tidy        add missing and remove unused modules  移除掉 go.mod 中没被使用的 require 模块
        vendor      make vendored copy of dependencies
        verify      verify dependencies have expected content
        why         explain why packages or modules are needed

Use "go help mod <command>" for more information about a command.
7. Go Playground
Go Playground
8. 入门必读
A Tour of Go
Effective Go