go命令工具补遗

可通过go help获取go命令,如下记录一些易忽略的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 help

https://github.com/golang/tools.git提供了golang内置的工具,有些会在golang二进制安装包中,如vet,godoc等,可通过如下命令安装:

包含工具unmarshal/stress/stringer/stingintconv/shadow/guru/godex/goimports/eg/cover/bundle等。

go get -u golang.org/x/tools/...

1. go build

缩小输出文件体积

go 编译出的文件,体积挺大的。一个重要原因是其中包含了调试信息,可以通过编译参数使其不包含调试信息。

# 移除 调试信息(-w) 和 符号表(-s)
go build -o main -ldflags "-w -s" main.go

上述操作使用 -ldflags 参数指定 -w 和 -s, 分别表示在编译时不包含调试信息和符号表,此举可以较好地缩减二进制文件体积。

编译时写入全局变量

go 可以通过编译参数,在编译时对变量进行赋值。一般情况下,这种操作可以让程序保留编译信息等数据。
通过 -ldflags 参数,设定 -X 操作,可以为全局变量赋值(一定要加-X)。

go build -ldflags "-X 'main.BuildTime=time006'" main.go
go build -ldflags "-X 'main.BuildTime=`date`' -X 'main.GitHash=`git rev-parse HEAD`'" main.go

go build -ldflags "-X cnca.HTTP2ClientTLSCAPath=/etc/openness/certs/ngc" -o "kubectl-cnca"

2. gofmt

统一的代码格式化工具

3. goimports

自动import依赖包工具,也对代码进行格式化,等于gofmt加上依赖包管理。

goimports -l -d -v -w .

4. go vet

内置错误检查工具

5. golint

静态代码检测工具。

6. golangci-lint 

静态代码质量检测工具,用于包的质量分析。go代码检查聚合工具,可定制。

推荐的方法是在基础库或者框架中使用 golint 进行静态检查(或者同时使用 golintgolangci-lint),在其他的项目中使用可定制化的 golangci-lint 来进行静态检查,因为在基础库和框架中施加强限制对于整体的代码质量有着更大的收益。

推荐Go 项目中使用 golint + golangci-lint 并开启全部的检查尽量尽早发现代码中包含文档在内的全部缺陷。 

代码检查应用

细看Kubernetes库,会发现,其会针对每个PR都做如下静态检查:

Kubernetes只利用了官方的几款工具, 在检测准确性上比较有保障。有了这些检查点,也能倒逼研发人员关注提交代码的质量,会迫使其在本地或者IDE上就配置好检查,确保每次提交的PR都能通过检查,不浪费CI资源。这也是合格工程师的基本要求。

参考:

1. 关于 golang build

2. 如何保障Go语言基础代码质量?

3. 在 GitHub 上构建一个看上去正规的 Golang 项目

4. 如何写出优雅的 Go 语言代码 

5. GO 命令教程  https://wiki.jikexueyuan.com/project/go-command-tutorial/

原文地址:https://www.cnblogs.com/embedded-linux/p/12820893.html