目录

Go 起步

Go 是互联网时代的 C 语言,此言非虚,但凡看看市场的招聘需求,不得不让人跃跃欲试,与其说是主动出击,倒不如说是裹挟前进。好吧,我表示学不动了。

Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.

Go 是有表现力的、简洁的、干净的和高效的。它的并发机制使编写程序更容易,以最大限度地利用多核和联网机器,而其新颖的类型系统使程序构造灵活和模块化。Go 可以快速编译成机器码,同时还具有垃圾收集的便利和运行时反射的强大功能。它是一种快速、静态类型的编译语言,感觉就像一种动态类型的解释语言。

Go 有很多特性,有一些是独特的,有一些是借鉴其它编程语言的:

  • 内置并发编程支持:
    • 使用协程(Goroutine)做为基本的计算单元。轻松地创建协和;
    • 使用通道(Channel)来实现协程间的同步和通信;
  • 内置了映射(Map)和切片(Slice)类型;
  • 支持多态(Polymorphism);
  • 使用接口(Interface)来实现装盒(Value Boxing)和反射(Reflection);
  • 支持指针(Pointer);
  • 支持函数闭包(Closure);
  • 支持方法(Method);
  • 支持延迟函数调用(Defer);
  • 支持类型内嵌(Type Embeding);
  • 支持类型推断(Type Deduction or Type Inference);
  • 内存安全;
  • 自动垃圾回收;
  • 良好的代码跨平台;
  • 比较简洁的语法;
  • 比较齐全的标准库;
  • 活跃和快速响应的社区,社区三方包和应用
  • 还有一个统一的称号 gopher - ['ɡoʊfə],即 地鼠,尽管其它语言也有类似的;

安装

Go installer

1
2
3
4
5
6
// 通过 brew 命令安装 Go
> brew install go

// 验证已经安装的 Go 版本
> go version
go version go1.17.2 darwin/arm64

命令

Go 语言内置的所有命令:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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:

	buildconstraint build constraints
	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
	packages        package lists and patterns
	private         configuration for downloading non-public code
	testflag        testing flags
	testfunc        testing functions
	vcs             controlling version control with GOVCS

Use "go help <topic>" for more information about that topic.

配置

  • 配置 Go PATH 路径:
1
2
3
// 打开 .zshrc 或者 .bashrc,之所以这样设置是因为本人不喜欢 go 目录直接设置到 $HOME 下,仅此而已
> vim ~/.zshrc
export GOPATH="$HOME/Codes/go"
  • 配置 Go ENV 环境变量:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// 开启 GO111MODULE
> go env -w GO111MODULE=on

// 配置代理,仅限大陆地区
> go env -w GOPROXY=https://goproxy.cn,https://goproxy.io,direct

// 查看环境变量
> go evn
GO111MODULE="on"
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/xxx/Library/Caches/go-build"
GOENV="/Users/xxx/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/xxx/Codes/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/xxx/Codes/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/opt/homebrew/Cellar/go/1.17.2/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/opt/homebrew/Cellar/go/1.17.2/libexec/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.17.2"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/nk/dtkbhx993b57y5kt49l4qsb40000gn/T/go-build1748431349=/tmp/go-build -gno-record-gcc-switches -fno-common"

运行

  • 切换到某个目录:
1
cd /path/to
  • 创建 Go 的源代码目录:
1
2
> mkdir hello
> cd hello
  • 启用依赖管理:
1
2
> go mod init github.com/xxx/hello/
go: creating new go.mod: module github.com/xxx/hello
  • 创建程序文件:
1
2
> touch main.go
> vim main.go
  • 编写简单程序:
1
2
3
4
5
6
7
package main

import "fmt"

func main() {
  fmt.Println("Hello World, Hello Go!")
}
  • 编译并运行程序:
1
2
3
4
5
> go build -o main ./main.go
> go run .
或者
> go run main.go
Hello World, Hello Go!

至此,一个 Hello World 的 Go 程序已经跑起来了!

模块

  • 在执行构建前自动分析源码中的依赖变化,识别新增依赖项并下载:
1
> go mod tidy
  • 查询发布版本:
1
2
> go list -m -versions github.com/sirupsen/logrus
github.com/sirupsen/logrus v0.1.0 v0.1.1 v0.2.0 v0.3.0 v0.4.0 v0.4.1 v0.5.0 v0.5.1 v0.6.0 v0.6.1 v0.6.2 v0.6.3 v0.6.4 v0.6.5 v0.6.6 v0.7.0 v0.7.1 v0.7.2 v0.7.3 v0.8.0 v0.8.1 v0.8.2 v0.8.3 v0.8.4 v0.8.5 v0.8.6 v0.8.7 v0.9.0 v0.10.0 v0.11.0 v0.11.1 v0.11.2 v0.11.3 v0.11.4 v0.11.5 v1.0.0 v1.0.1 v1.0.3 v1.0.4 v1.0.5 v1.0.6 v1.1.0 v1.1.1 v1.2.0 v1.3.0 v1.4.0 v1.4.1 v1.4.2 v1.5.0 v1.6.0 v1.7.0 v1.7.1 v1.8.0 v1.8.1
  • 安装最新版本:
1
> go get github.com/sirupsen/logrus
  • 安装指定版本:
1
> go get github.com/sirupsen/logrus@v1.7.0
  • 使用 go mod edit 指定版本(假如要修改多个包版本)并用 go mod tidy 批量更新:
1
2
> go mod edit -require=github.com/sirupsen/logrus@v1.7.0
> go mod tidy
  • 查询当前项目的所有依赖:
1
> go list -m all
  • 项目建立 vendor 依赖目录,如果基于 vendor 构建,需要在 go build 后面加上 -mod=vendor 参数:
1
> go mod vendor