一般在介绍语言的书中不会出现工程管理的内容,但只要讲到Go语言,我们就不应该把语法和工程管理区分开。因为Go语言在设计之初就考虑了在语言层面如何更好地解决当前工程管理中的一些常见问题,而自带的Go工具则更是从工程管理的方方面面来考虑,并提供了完善的功能。让学习者在学习语言阶段自然而然地养成了工程的习惯,避免出现学院派和工程派两种明显不同的侧重点。
归根到底,Go语言是一门工程语言。
1. Go 命令行工具
可以完成以下几类工作:
- 代码格式化
- 代码质量分析和修复
- 单元测试与性能测试
- 代码文档的提取和展示
- 依赖包管理
- 执行其它的包含指令,比如6g蹬
示例:
C:\Users\小白一生>go help
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
work workspace 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 get
go get
命令用于从远程代码仓库(如 GitHub 或 GitLab)下载 Go 语言的包及其依赖,并将其安装到本地的 Go 开发环境中。从 2019 年开始,随着 Go Modules 功能的普及和默认启用,go get
的行为有所变化:
-
对于使用 Go Modules 的项目(Go 1.11 版本以后),当你执行
go get
命令时,它会根据go.mod
文件管理依赖,并将依赖包下载至$GOPATH/pkg/mod
目录下(如果设置了GOPATH
)。同时,go get
也会在全局模块缓存中存储依赖,通常位于~/.cache/go-build
(对于 Unix 系统)或%USERPROFILE%\.cache\go-build
(对于 Windows 系统)。 -
如果你是在一个非模块化的项目(或者禁用了模块功能)并且设置了
GOPATH
,那么下载的源码将会存储在GOPATH/src
目录下相应的位置,例如:GOPATH=/home/user/go
执行
go get github.com/user/repo
后,源码会被放在:/home/user/go/src/github.com/user/repo
-
对于可执行程序(比如 main 包),如果使用 Go Modules,它们会被安装到一个与项目相关的临时目录(由 Go Modules 管理),而不再是传统的
GOPATH/bin
。若想将可执行文件安装到某个特定目录,可以使用go install
命令配合-i
标志(现在-i
不再需要,因为go install
默认包含了构建和安装的动作)和-o
标志指定输出路径。 -
如果需要查看已安装到可执行路径下的二进制文件,可以通过
go list -f '{{.Target}}' <package>
查看对应包编译后的二进制文件路径。 -
在 Go 1.13 版本后,为了更细粒度地控制依赖,推荐使用
go install
命令替代go get
来安装独立的命令行工具或库。
总之,go get
下载的包位置与项目的 Go Modules 设置和全局 GOPATH 设置紧密相关。随着 Go 语言的发展,最佳实践也在不断演进,因此建议始终查阅最新的官方文档了解最新行为。
2. 代码风格
“代码必须是本着写给人阅读的原则来编写,只不过顺便给机器执行而已。”
Go语言代码风格强制统一。
命名
命名规则涉及变量、常量、全局函数、结构、接口、方法等的命名。Go语言语法层面限定如下:
- 任何需要对外暴露的名字必须以大写字母开头,不需要对外暴露的则应该以小写字母开头。
- 拥护骆驼命名法而排斥下划线法
排列
go fmt
代码格式化工具,不加参数调整当前目录所有go文件,可跟指定包名
- 调整语句位置
- 重新拜访花括号位置
- 以制表符缩进代码
- 添加空格
导包
package main
import (
"fmt"
"github.com/mytem/exp/crc32"
)
导入远程包执行go build
或go install
之前需要通过go get
拉取包