2016-02-15 03:06:34 +00:00
|
|
|
|
### 10.7.6. 查询包
|
2015-12-09 07:45:11 +00:00
|
|
|
|
|
2016-02-15 03:06:34 +00:00
|
|
|
|
`go list`命令可以查询可用包的信息。其最简单的形式,可以测试包是否在工作区并打印它的导入路径:
|
2015-12-09 07:45:11 +00:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ go list github.com/go-sql-driver/mysql
|
|
|
|
|
github.com/go-sql-driver/mysql
|
|
|
|
|
```
|
|
|
|
|
|
2016-10-10 07:17:50 +00:00
|
|
|
|
`go list`命令的参数还可以用`"..."`表示匹配任意的包的导入路径。我们可以用它来列出工作区中的所有包:
|
2015-12-09 07:45:11 +00:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ go list ...
|
|
|
|
|
archive/tar
|
|
|
|
|
archive/zip
|
|
|
|
|
bufio
|
|
|
|
|
bytes
|
|
|
|
|
cmd/addr2line
|
|
|
|
|
cmd/api
|
|
|
|
|
...many more...
|
|
|
|
|
```
|
|
|
|
|
|
2016-02-15 03:06:34 +00:00
|
|
|
|
或者是特定子目录下的所有包:
|
2015-12-09 07:45:11 +00:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ go list gopl.io/ch3/...
|
|
|
|
|
gopl.io/ch3/basename1
|
|
|
|
|
gopl.io/ch3/basename2
|
|
|
|
|
gopl.io/ch3/comma
|
|
|
|
|
gopl.io/ch3/mandelbrot
|
|
|
|
|
gopl.io/ch3/netflag
|
|
|
|
|
gopl.io/ch3/printints
|
|
|
|
|
gopl.io/ch3/surface
|
|
|
|
|
```
|
|
|
|
|
|
2016-02-15 03:06:34 +00:00
|
|
|
|
或者是和某个主题相关的所有包:
|
2015-12-09 07:45:11 +00:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ go list ...xml...
|
|
|
|
|
encoding/xml
|
|
|
|
|
gopl.io/ch7/xmlselect
|
|
|
|
|
```
|
|
|
|
|
|
2016-02-15 03:06:34 +00:00
|
|
|
|
`go list`命令还可以获取每个包完整的元信息,而不仅仅只是导入路径,这些元信息可以以不同格式提供给用户。其中`-json`命令行参数表示用JSON格式打印每个包的元信息。
|
2015-12-09 07:45:11 +00:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ go list -json hash
|
|
|
|
|
{
|
|
|
|
|
"Dir": "/home/gopher/go/src/hash",
|
|
|
|
|
"ImportPath": "hash",
|
|
|
|
|
"Name": "hash",
|
|
|
|
|
"Doc": "Package hash provides interfaces for hash functions.",
|
|
|
|
|
"Target": "/home/gopher/go/pkg/darwin_amd64/hash.a",
|
|
|
|
|
"Goroot": true,
|
|
|
|
|
"Standard": true,
|
|
|
|
|
"Root": "/home/gopher/go",
|
|
|
|
|
"GoFiles": [
|
|
|
|
|
"hash.go"
|
|
|
|
|
],
|
|
|
|
|
"Imports": [
|
|
|
|
|
"io"
|
|
|
|
|
],
|
|
|
|
|
"Deps": [
|
|
|
|
|
"errors",
|
|
|
|
|
"io",
|
|
|
|
|
"runtime",
|
|
|
|
|
"sync",
|
|
|
|
|
"sync/atomic",
|
|
|
|
|
"unsafe"
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2016-02-15 03:06:34 +00:00
|
|
|
|
命令行参数`-f`则允许用户使用text/template包(§4.6)的模板语言定义输出文本的格式。下面的命令将打印strconv包的依赖的包,然后用join模板函数将结果链接为一行,连接时每个结果之间用一个空格分隔:
|
2015-12-09 07:45:11 +00:00
|
|
|
|
|
|
|
|
|
{% raw %}
|
|
|
|
|
```
|
|
|
|
|
$ go list -f '{{join .Deps " "}}' strconv
|
|
|
|
|
errors math runtime unicode/utf8 unsafe
|
|
|
|
|
```
|
|
|
|
|
{% endraw %}
|
|
|
|
|
|
2016-02-15 03:06:34 +00:00
|
|
|
|
译注:上面的命令在Windows的命令行运行会遇到`template: main:1: unclosed action`的错误。产生这个错误的原因是因为命令行对命令中的`" "`参数进行了转义处理。可以按照下面的方法解决转义字符串的问题:
|
2015-12-09 07:45:11 +00:00
|
|
|
|
|
|
|
|
|
{% raw %}
|
|
|
|
|
```
|
|
|
|
|
$ go list -f "{{join .Deps \" \"}}" strconv
|
|
|
|
|
```
|
|
|
|
|
{% endraw %}
|
|
|
|
|
|
2016-02-15 03:06:34 +00:00
|
|
|
|
下面的命令打印compress子目录下所有包的依赖包列表:
|
2015-12-09 07:45:11 +00:00
|
|
|
|
|
|
|
|
|
{% raw %}
|
|
|
|
|
```
|
|
|
|
|
$ go list -f '{{.ImportPath}} -> {{join .Imports " "}}' compress/...
|
|
|
|
|
compress/bzip2 -> bufio io sort
|
|
|
|
|
compress/flate -> bufio fmt io math sort strconv
|
|
|
|
|
compress/gzip -> bufio compress/flate errors fmt hash hash/crc32 io time
|
|
|
|
|
compress/lzw -> bufio errors fmt io
|
|
|
|
|
compress/zlib -> bufio compress/flate errors fmt hash hash/adler32 io
|
|
|
|
|
```
|
|
|
|
|
{% endraw %}
|
|
|
|
|
|
2016-02-15 03:06:34 +00:00
|
|
|
|
译注:Windows下有同样有问题,要避免转义字符串的干扰:
|
2015-12-09 07:45:11 +00:00
|
|
|
|
|
|
|
|
|
{% raw %}
|
|
|
|
|
```
|
|
|
|
|
$ go list -f "{{.ImportPath}} -> {{join .Imports \" \"}}" compress/...
|
|
|
|
|
```
|
|
|
|
|
{% endraw %}
|
|
|
|
|
|
2016-02-15 03:06:34 +00:00
|
|
|
|
`go list`命令对于一次性的交互式查询或自动化构建或测试脚本都很有帮助。我们将在11.2.4节中再次使用它。每个子命令的更多信息,包括可设置的字段和意义,可以用`go help list`命令查看。
|
2015-12-09 07:45:11 +00:00
|
|
|
|
|
2016-02-15 03:06:34 +00:00
|
|
|
|
在本章,我们解释了Go语言工具中除了测试命令之外的所有重要的子命令。在下一章,我们将看到如何用`go test`命令去运行Go语言程序中的测试代码。
|
2015-12-09 07:45:11 +00:00
|
|
|
|
|
2016-10-10 07:17:50 +00:00
|
|
|
|
**练习 10.4:** 创建一个工具,根据命令行指定的参数,报告工作区所有依赖包指定的其它包集合。提示:你需要运行`go list`命令两次,一次用于初始化包,一次用于所有包。你可能需要用encoding/json(§4.5)包来分析输出的JSON格式的信息。
|