Go基础笔记_3_常用命令

一.Go常用命令工具

go常用命令如下

常用命令 描述
go build 编译当前目录下 Go 源码,生成可执行的文件
go clean 清理当前目录源码包和关联源码包里面编译生成的文件
go run 编译源码,并且直接执行源码的 main() 函数,不会在当前目录留下可执行文件
gofmt 将代码按照Go语言官方提供的代码风格进行格式化
go install 将源码编译为可执行的文件
go get 获取源码包
go test 单元测试,性能测试

1. go build命令

使用:go build
编译具体文件: go build file.go
查看帮助输入:go help build

# go help build
The build flags are shared by the build, clean, get, install, list, run,
and test commands:

        -a      
                force rebuilding of packages that are already up-to-date.
                强制进行重新构建
        -n
                print the commands but do not run them.
                仅打印输出build需要的命令,不执行build动作
        -p n
                the number of programs, such as build commands or
                test binaries, that can be run in parallel.
                The default is the number of CPUs available.
                开多少核cpu来并行编译,默认为本机CPU核数
        -race
                enable data race detection.
                Supported only on linux/amd64, freebsd/amd64, darwin/amd64, windows/amd64,
                linux/ppc64le and linux/arm64 (only for 48-bit VMA).
                同时检测数据竞争状态
        -msan
                enable interoperation with memory sanitizer.
                Supported only on linux/amd64, linux/arm64
                and only with Clang/LLVM as the host C compiler.
                On linux/arm64, pie build mode will be used.
                启用与内存消毒器的互操作
        -v
                print the names of packages as they are compiled.
                打印出被编译的包名
        -work
                print the name of the temporary work directory and
                do not delete it when exiting.
                打印临时工作目录的名称,并在退出时不删除它
        -x
                print the commands.
                同时打印输出执行的命令名
省略...

2. go clean命令

清理当前目录下生成的与包名或者 Go 源码文件同名的可执行文件。

  1. 清理go build生成的如:“_obj”和“_test”目录,名称为“_testmain.go”、“test.out”、“build.out”或“a.out”的文件,名称以“.5”、“.6”、“.8”、“.a”、“.o”或“.so”为后缀的文件。
  2. 清理go test生成的包名加 “.test” 后缀为名的文件。
  3. 清理go install生成的,工作区中 pkg 和 bin 目录的相应归档文件和可执行文件。

查看帮助:执行 go help clean

clean removes the following files from each of the
source directories corresponding to the import paths:

        _obj/            old object directory, left from Makefiles
        _test/           old test directory, left from Makefiles
        _testmain.go     old gotest file, left from Makefiles
        test.out         old test log, left from Makefiles
        build.out        old test log, left from Makefiles
        *.[568ao]        object files, left from Makefiles

        DIR(.exe)        from go build
        DIR.test(.exe)   from go test -c
        MAINFILE(.exe)   from go build MAINFILE.go
        *.so             from SWIG
参数 描述
-i 清除关联的安装的包和可运行文件,
即通过go install安装的文件
-n 把需要执行的清除命令打印出来,但是不执行
-r 循环的清除在 import 中引入的包
-x 打印出来执行的详细命令,其实就是 -n 打印的执行版本
-cache 删除所有go build命令的缓存
-testcache 删除当前包所有的测试结果

3. go run命令

go run 命令的作用是直接运行 go 源码,不在当前目录下生成任何可执行的文件。
go run 只是将编译后生成的可执行文件放到临时目录中执行,工作目录仍然为当前目录。同时,go run 命令允许添加参数,这些参数将作为 go 程序的可接受参数使用。因此go run 命令同样会执行编译操作。

# 添加参数如
go run helloworld.go --filename hello.go

注意:go run不能使用“go run+包”的方式进行编译.
想要快速编译运行包,执行如下操作:
1.使用go build生成可执行文件。
2.运行可执行文件。

4. gofmt 命令

将代码按照Go语言官方提供的代码风格进行格式化。
gofmt 是一个 cli 程序,会优先读取标准输入。

  1. 参数是文件路径,会格式化这个文件。
  2. 参数是一个目录,会格式化目录中所有 .go 文件。
  3. 不传参数,会格式化当前目录下的所有 .go 文件。
参考地址 https://pkg.go.dev/cmd/gofmt#hdr-The_simplify_command
参数 描述
-l 仅把那些不符合格式化规范的、需要被命令程序改写的源码文件的绝对路径打印到标准输出。
而不是把改写后的全部内容都打印到标准输出。
-w 代码格式化,并用改写后的源码覆盖原有源码
-r rule 添加自定义的代码格式化规则(使用rule表示),格式为:pattern -> replacement
自定义某些额外的格式化规则可使用
-s 简化代码
-d 对比输出代码格式化前后的不同,依赖diff命令
-e 输出所有的语法错误,默认只会打印每行第1个错误,且最多打印10个错误
-comments 是否保留代码注释,默认值为true
-tabwidth x 用于指定代码缩进的空格数量(使用x表示),默认值为8,该参数仅在-tabs参数为false时生效
-tabs 用于指定代码缩进是否使用tab(“\t”),默认值为true
-cpuprofile filename 是否开启CPU用量分析,需要给定记录文件(使用filename表示),分析结果将保存在这个文件中

注意gofmt和go fmt两个是不同的命令
go fmt 命令是 gofmt 的封装

  1. -n ,仅输出格式化时(go fmt)执行的命令。
  2. -x ,既打印出go fmt命令又执行命令。

5. go install 命令

与 go build 命令类似,附加参数绝大多数都可以与 go build 通用。

  1. go install 命令在编译源码后,会将可执行文件或库文件安装到指定的目录下。
  2. go install 命令生成的可执行文件使用包名来命名。
    默认将可执行文件安装到 GOPATH\bin 目录下
# go help install
Executables are installed in the directory named by the GOBIN environment
variable, which defaults to $GOPATH/bin or $HOME/go/bin if the GOPATH
environment variable is not set. Executables in $GOROOT
are installed in $GOROOT/bin or $GOTOOLDIR instead of $GOBIN.

When module-aware mode is disabled, other packages are installed in the
directory $GOPATH/pkg/$GOOS_$GOARCH. When module-aware mode is enabled,
other packages are built and cached but not installed.

示例

go install helloworld.go
在$GOPATH/bin下生成了helloworld.exe文件

pkg 目录放置的是编译期间的中间文件。
go install 是建立在 GOPATH 上的,无法在独立的目录里使用 go install。
go install 输出目录始终为 GOPATH 下的 bin 目录。

6. go get 命令

可以借助代码管理工具通过远程拉取或更新代码包及其依赖包,并自动完成编译和安装。
go get命令进行了如下操作

  1. 下载源码包
  2. 执行 go install命令

执行go get命令前确保GOPATH已经设置。

参数 描述
-d 只下载不安装
-f 在执行-u参数操作时,不验证导入的每个包的获取状态,对于本地 fork 的包特别有用
-fix 在下载源码包后先执行fix操作,然后再去做其他的事情
-t 获取运行测试所需要的包
-u 更新源码包到最新版本
-u=patch 只小版本地更新源码包,如从1.1.0到1.1.16
-v 执行获取并显示实时日志
-insecure 允许通过未加密的HTTP方式获取

从 Go 1.17 开始,go get不推荐使用安装可执行文件。 go install可以代替使用。
在未来的 Go 版本中,go get将不再构建包;它只会用于添加、更新或删除go.mod. 具体来说, go get就像-d启用了标志一样。

介绍 https://docs.studygolang.com/doc/go-get-install-deprecation

7. go test命令

单元测试和性能测试系统。
go test 命令,会自动读取源码目录下面名为 *_test.go 的文件,生成并运行测试用的可执行文件。
注意 文件命名规则:以_test结尾。
每个测试用例函数需要以Test为前缀,一个文件可有多个测试用例函数

// 文件hello_test.go
// 方法名Test前缀
package hello1
import "testing"
func TestHelloWorld(t *testing.T){
    
    
    t.Log("hello world")
}
  1. 执行go test hello_test.go
go test hello_test.go
ok      command-line-arguments  0.820s

//ok表示成功,command-line-arguments 是测试用例需要用到的一个包名
  1. 执行具体函数
// 执行TestHelloWorld函数 

go test -v -run TestHelloWorld hello_test.go
=== RUN   TestHelloWorld
    hello_test.go:4: hello world
--- PASS: TestHelloWorld (0.00s)
PASS
ok      command-line-arguments  0.805s

默认的情况下,go test命令不需要任何的参数,它会自动把你源码包下面所有 test 文件测试完毕,当然你也可以带上参数。

测试用例文件不会参与正常源码编译,不会被包含到可执行文件中。
测试用例文件使用go test指令来执行,没有也不需要 main() 作为函数入口。所有在以_test结尾的源码内以Test开头的函数会自动被执行。
测试用例可以不传入 *testing.T 参数。

1. go test参数

参数 描述
-bench regexp 执行相应的 benchmarks,例如 -bench=.
-cover 开启测试覆盖率
-run regexp 只运行 regexp 匹配的函数,例如 -run=Array 那么就执行包含有 Array 开头的函数
-v 显示测试的详细命令

2. 测试函数方法

  1. 若存在多个TestHelloWorld开头的函数,一次执行。
    执行go test -v -run TestHelloWorld hello_test.go后,这些函数都运行
go test -v -run TestHelloWorld hello_test.go
=== RUN   TestHelloWorld
    hello_test.go:5: hello world
--- PASS: TestHelloWorld (0.00s)
=== RUN   TestHelloWorldB
    hello_test.go:9: hello world B
--- PASS: TestHelloWorldB (0.00s)
=== RUN   TestHelloWorldC
    hello_test.go:13: hello world C
--- PASS: TestHelloWorldC (0.00s)
PASS
ok      command-line-arguments  0.321s
  1. 要想只执行一个函数。
    如使用-run TestHelloWorld$ 即只执行 TestHelloWorld 测试用例
go test -v -run TestHelloWorld$ hello_test.go
=== RUN   TestHelloWorld
    hello_test.go:5: hello world
--- PASS: TestHelloWorld (0.00s)
PASS
ok      command-line-arguments  0.908s

猜你喜欢

转载自blog.csdn.net/u010895512/article/details/124783029