2015-12-09 07:45:11 +00:00
|
|
|
|
### 10.7.6. 査詢包
|
|
|
|
|
|
2016-01-08 14:03:49 +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-01-08 14:03:49 +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-01-08 14:03:49 +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-01-08 14:03:49 +00:00
|
|
|
|
或者是和某個主題相關的所有包:
|
2015-12-09 07:45:11 +00:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ go list ...xml...
|
|
|
|
|
encoding/xml
|
|
|
|
|
gopl.io/ch7/xmlselect
|
|
|
|
|
```
|
|
|
|
|
|
2016-01-08 14:03:49 +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-01-08 14:03:49 +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-01-08 14:03:49 +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-01-08 14:03:49 +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-01-08 14:03:49 +00:00
|
|
|
|
譯註:Windows下有同樣有問題,要避免轉義字符串的榦擾:
|
2015-12-09 07:45:11 +00:00
|
|
|
|
|
|
|
|
|
{% raw %}
|
|
|
|
|
```
|
|
|
|
|
$ go list -f "{{.ImportPath}} -> {{join .Imports \" \"}}" compress/...
|
|
|
|
|
```
|
|
|
|
|
{% endraw %}
|
|
|
|
|
|
2016-01-08 14:03:49 +00:00
|
|
|
|
`go list`命令對於一次性的交互式査詢或自動化構建或測試腳本都很有幫助。我們將在11.2.4節中再次使用它。每個子命令的更多信息,包括可設置的字段和意義,可以用`go help list`命令査看。
|
2015-12-09 07:45:11 +00:00
|
|
|
|
|
2016-01-08 14:03:49 +00:00
|
|
|
|
在本章,我們解釋了Go語言工具中除了測試命令之外的所有重要的子命令。在下一章,我們將看到如何用`go test`命令去運行Go語言程序中的測試代碼。
|
2015-12-09 07:45:11 +00:00
|
|
|
|
|
2016-01-08 14:03:49 +00:00
|
|
|
|
**練習 10.4:** 創建一個工具,根據命令行指定的參數,報告工作區所有依賴指定包的其它包集合。提示:你需要運行`go list`命令兩次,一次用於初始化包,一次用於所有包。你可能需要用encoding/json(§4.5)包來分析輸出的JSON格式的信息。
|