一. 命令行预览

Go命令行使用二级命令,格式go <command> [arguments]

shell
 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
$ 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
        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.

二. 命令行说明

1. bug

说明: 在浏览器,向Golang官方github提交bug报告。

2. build

说明: 编译项目和文件。

  • 支持10种操作系统:darwin,freebsd,linux,windows,android,dragonfly,netbsd,openbsd,plan9,solaris
  • 支持9种处理器架构:arm,arm64,386,amd64,ppc64,ppc64le,mips64,mips64le,s390x

交叉编译:

shell
1
2
$ CGO_ENABLED=0 GOOS=darwin  GOARCH=amd64 go build -o demo.darwin.amd64
$ CGO_ENABLED=0 GOOS=linux   GOARCH=amd64 go build -o demo.linux.amd64

3. clean

说明: 清理生成的可执行文件和包(不支持自定义的编译文件名),参考install示例。

  • -i 清除关联的安装的包和可运行文件,也就是通过go install安装的文件
  • -n 把需要执行的清除命令打印出来,但是不执行,这样就可以很容易的知道底层是如何运行的
  • -r 循环的清除在import中引入的包
  • -x 打印出来执行的详细命令,其实就是-n打印的执行版本

4. doc

说明: 查看源码文档,格式:go doc 包名.成员名,成员可以是常量、变量、结构体、函数、方法...,如:

shell
1
2
3
$ go doc fmt | less     #查看标准库文档,使用less输出(空格和w控制上下翻动)
$ go doc fmt.Println    #查看方法
$ go doc builtin        #查看内置包builtin的包结构

5.env

说明: 打印环境信息。

名称说明
CGO_ENABLED        指明cgo工具是否可用的标识
GOARCH程序构建环境的目标计算架构
GOBIN存放可执行文件的目录的绝对路径
GOCHAR程序构建环境的目标计算架构的单字符标识
GOEXE可执行文件的后缀
GOHOSTARCH程序运行环境的目标计算架构
GOOS程序构建环境的目标操作系统
GOHOSTOS程序运行环境的目标操作系统
GOPATH工作区目录的绝对路径
GORACE用于数据竞争检测的相关选项
GOROOTGo语言的安装目录的绝对路径
GOTOOLDIRGo工具目录的绝对路径

6. fix

说明: 源码库升级,可参考:go fix与go tool fix

7. fmt

说明: 格式化源码文件 查看参考文章

shell
1
$ go fmt main.go

8. generate

说明: 查找项目中所有源码,执行代码中诸如//go:generate cmd的注解命令 ,参考链接 。

go
1
2
3
4
5
6
//go:generate echo hello
//go:generate go run main.go
//go:generate  echo file=$GOFILE pkg=$GOPACKAGE
func main() {
	fmt.Println("main func")
}
shell
1
$ go generate #执行自动生成命令

9. get

说明: 下载安装源码包,如:

shell
1
go get github.com/go-redis/redis/v7
  • -d 只下载不安装
  • -f 只有在你包含了-u参数的时候才有效,不让-u去验证import中的每一个都已经获取了,这对于本地fork的包特别有用
  • -fix 在获取源码之后先运行fix,然后再去做其他的事情
  • -t 同时也下载需要为运行测试所需要的包
  • -u 强制使用网络去更新包和它的依赖包
  • -v 显示执行的命令

10. install

说明: 编译并安装

  • 第一步:生成结果文件(可执行文件或者.a包);
  • 第二步:把编译好的结果移到$GOPATH/bin;
shell
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# 创建main文件,并初始化mod
go mod init my-first-pro

# 项目目录
$ tree
.
├── go.mod
└── main.go

# 编译安装
# go install

# 查看生成到文件
$ ll $GOPATH/bin
$ ll $GOPATH/pkg/darwin_amd64

# 清理生成到文件
$ go  clean -x -i

11. list

说明: 查看包路径、名称、导入项、代码文件,测试文件,依赖项等信息。

shell
1
$ go list -json errors|less
json
 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
{
        "Dir": "/usr/local/go/src/errors",
        "ImportPath": "errors",
        "Name": "errors",
        "Doc": "Package errors implements functions to manipulate errors.",
        "Target": "/usr/local/go/pkg/darwin_amd64/errors.a",
        "Root": "/usr/local/go",
        "Match": [
                "errors"
        ],
        "Goroot": true,
        "Standard": true,
        "GoFiles": [
                "errors.go",
                "wrap.go"
        ],
        "Imports": [
                "internal/reflectlite"
        ],
        "Deps": [
                "internal/bytealg",
                "internal/cpu",
                "internal/reflectlite",
                "runtime",
                "runtime/internal/atomic",
                "runtime/internal/math",
                "runtime/internal/sys",
                "unsafe"
        ],
        "XTestGoFiles": [
                "errors_test.go",
                "example_test.go",
                "wrap_test.go"
        ],
        "XTestImports": [
                "errors",
                "fmt",
                "os",
                "reflect",
                "testing",
                "time"
        ]
}

12. mod

说明: 依赖包管理, 参考链接 。

13. run

说明: 运行Golang程序。

14. test

说明: 执行单元测试。

  • -bench regexp 执行相应的benchmarks,例如 -bench=.
  • -cover 开启测试覆盖率
  • -run regexp 只运行regexp匹配的函数,例如 -run=Array 那么就执行包含有Array开头的函数
  • -v 显示测试的详细命令
shell
1
2
3
$ go test demo_test.go demo.go           # 单元测试
$ go test --cover demo_test.go demo.go   # 单元测试覆盖率
$ go test -bench=. demo_test.go demo.go  # 性能测试

15. tool

说明: 运行指定的go工具, 如:go tool pprof 。

16. version

说明: 打印golang版本信息。

17. vet

说明: 用于检查静态错误,即编译不易发现的错误。查看参考文章

shell
1
$ go tool vet <directory|files>
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
func main() {
   // 1.可疑代码
   var i = 0
   fmt.Println(i != 0 || i != 1)
   fmt.Println(i == 0 && i == 1)

   //// 2.格式化参数错误
   fmt.Printf("%d\n", "hello")

   // 3.除零错误
   fmt.Println(2 / 0)

   // 4. 读不到的代码
   goto OVER
   fmt.Println("hello")
OVER:

   // 5.竞态代码
   words := []string{"foo", "bar", "baz"}
   for _, word := range words {
      go func() {
         fmt.Println(word)
      }()
   }
}

18. godoc

说明: 用于展示指定代码包的文档。

shell
1
2
3
4
5
6
$ godoc fmt
$ godoc fmt Printf
$ godoc fmt Printf Println
$ godoc -src fmt Printf
$ godoc cmd/go
$ godoc -http=:9999  #启动$GOROOT\doc下的帮助站点

19. help

说明: 帮助命令

shell
1
2
$ go help modules
$ go help vet

三. 条件编译

可以通过代码注释和文件后缀实现条件编译。

1. 代码注释实现条件编译:

  1. 可以多条多行写,也可多条单行写。
  2. 逗号与逻辑;空格或逻辑;叹号非逻辑。
  3. 编译标签和包声明之间要有换行。
go
1
2
3
4
+build linux darwin freebsd openbsd
+build linux,amd64
+build !windows
+build ignore

我们还可以自定义编译标签,如:
demopkg/my_debug.go

go
1
2
3
4
5
6
7
// +build debug

package demopkg

func Foo() {
	fmt.Println("from debug")
}

demopkg/my_prod.go

go
1
2
3
4
5
6
7
// +build !debug

package demopkg

func Foo()  {
	fmt.Println("from prod")
}

执行go run --tags=debug main.go,指定不同标签后会编译或执行不同的文件。

2. 文件后缀实现条件编译:

build注解与文件后缀同时存在时,以后缀优先

go
1
2
3
*_GOOS			//如:file_linux.go,表示只在Linux下编译
*_GOARCH		//如:file_amd64.go,表示只在amd64下编译
*_GOOS_GOARCH	//如:file_linux_amd64.go,表示只在Linux且amd64下编译

四. 特殊注释

go
1
2
3
// TODO: 等待去做
// FIXME: 优化
// Deprecated: 已废弃或不推荐使用

参考:
https://github.com/hyper0x/go_command_tutorial


「真诚赞赏,手留余香」

Hollson码夫庄园

嗯,我的梦想是被读者的稿费包养~

使用微信扫描二维码完成支付

Hollson

评论