go list命令的作用是列出指定的代码包的信息。

查看帮助文档

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
> go help list
usage: go list [-f format] [-json] [-m] [list flags] [build flags] [packages]

List lists the named packages, one per line.
The most commonly-used flags are -f and -json, which control the form
of the output printed for each package. Other list flags, documented below,
control more specific details.

The default output shows the package import path:

    bytes
    encoding/json
    github.com/gorilla/mux
    golang.org/x/net/html

The -f flag specifies an alternate format for the list, using the
syntax of package template. The default output is equivalent
to -f '{{.ImportPath}}'. The struct being passed to the template is:

...

go list -m 显示指定包信息在编译版本时应用如下:

1
2
3
GOOS=linux go build -mod=mod \
    -ldflags "-s -w -X \"${go list -m}/version.BuildVersion=${COMMIT_HASH}\" -X \"${go list -m}/version.BuildDate=${BUILD_DATE}\"" \
    -o ./bin/${APP_NAME} ./cmd/${APP_NAME}

用来查看依赖包的版本信息

1
2
> go list -m go.uber.org/zap
go.uber.org/zap v1.25.0 => ./lib/go.uber.org/zap@v1.24.0

详细说明List packages or modules

参考