mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2026-01-15 11:47:14 +08:00
reduce file size
This commit is contained in:
@@ -25,10 +25,6 @@
|
||||
|
||||
|
||||
|
||||
<link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
|
||||
|
||||
|
||||
|
||||
<link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
|
||||
|
||||
|
||||
@@ -48,7 +44,7 @@
|
||||
<body>
|
||||
|
||||
|
||||
<div class="book" data-level="10.1" data-chapter-title="簡介" data-filepath="ch10/ch10-01.md" data-basepath=".." data-revision="Thu Dec 24 2015 14:42:02 GMT+0800 (中国标准时间)">
|
||||
<div class="book" data-level="10.1" data-chapter-title="簡介" data-filepath="ch10/ch10-01.md" data-basepath=".." data-revision="Fri Dec 25 2015 12:32:44 GMT+0800 (中国标准时间)">
|
||||
|
||||
|
||||
<div class="book-summary">
|
||||
@@ -2050,14 +2046,6 @@
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
<script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
|
||||
|
||||
|
||||
|
||||
<script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
|
||||
|
||||
|
||||
|
||||
<script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
|
||||
|
||||
|
||||
@@ -2067,7 +2055,7 @@
|
||||
|
||||
<script>
|
||||
require(["gitbook"], function(gitbook) {
|
||||
var config = {"highlight":{},"search":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
|
||||
var config = {"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
|
||||
gitbook.start(config);
|
||||
});
|
||||
</script>
|
||||
|
||||
@@ -25,10 +25,6 @@
|
||||
|
||||
|
||||
|
||||
<link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
|
||||
|
||||
|
||||
|
||||
<link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
|
||||
|
||||
|
||||
@@ -48,7 +44,7 @@
|
||||
<body>
|
||||
|
||||
|
||||
<div class="book" data-level="10.2" data-chapter-title="導入路徑" data-filepath="ch10/ch10-02.md" data-basepath=".." data-revision="Thu Dec 24 2015 14:42:02 GMT+0800 (中国标准时间)">
|
||||
<div class="book" data-level="10.2" data-chapter-title="導入路徑" data-filepath="ch10/ch10-02.md" data-basepath=".." data-revision="Fri Dec 25 2015 12:32:44 GMT+0800 (中国标准时间)">
|
||||
|
||||
|
||||
<div class="book-summary">
|
||||
@@ -2060,14 +2056,6 @@
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
<script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
|
||||
|
||||
|
||||
|
||||
<script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
|
||||
|
||||
|
||||
|
||||
<script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
|
||||
|
||||
|
||||
@@ -2077,7 +2065,7 @@
|
||||
|
||||
<script>
|
||||
require(["gitbook"], function(gitbook) {
|
||||
var config = {"highlight":{},"search":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
|
||||
var config = {"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
|
||||
gitbook.start(config);
|
||||
});
|
||||
</script>
|
||||
|
||||
@@ -25,10 +25,6 @@
|
||||
|
||||
|
||||
|
||||
<link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
|
||||
|
||||
|
||||
|
||||
<link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
|
||||
|
||||
|
||||
@@ -48,7 +44,7 @@
|
||||
<body>
|
||||
|
||||
|
||||
<div class="book" data-level="10.3" data-chapter-title="包聲明" data-filepath="ch10/ch10-03.md" data-basepath=".." data-revision="Thu Dec 24 2015 14:42:02 GMT+0800 (中国标准时间)">
|
||||
<div class="book" data-level="10.3" data-chapter-title="包聲明" data-filepath="ch10/ch10-03.md" data-basepath=".." data-revision="Fri Dec 25 2015 12:32:44 GMT+0800 (中国标准时间)">
|
||||
|
||||
|
||||
<div class="book-summary">
|
||||
@@ -2063,14 +2059,6 @@
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
<script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
|
||||
|
||||
|
||||
|
||||
<script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
|
||||
|
||||
|
||||
|
||||
<script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
|
||||
|
||||
|
||||
@@ -2080,7 +2068,7 @@
|
||||
|
||||
<script>
|
||||
require(["gitbook"], function(gitbook) {
|
||||
var config = {"highlight":{},"search":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
|
||||
var config = {"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
|
||||
gitbook.start(config);
|
||||
});
|
||||
</script>
|
||||
|
||||
@@ -25,10 +25,6 @@
|
||||
|
||||
|
||||
|
||||
<link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
|
||||
|
||||
|
||||
|
||||
<link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
|
||||
|
||||
|
||||
@@ -48,7 +44,7 @@
|
||||
<body>
|
||||
|
||||
|
||||
<div class="book" data-level="10.4" data-chapter-title="導入聲明" data-filepath="ch10/ch10-04.md" data-basepath=".." data-revision="Thu Dec 24 2015 14:42:02 GMT+0800 (中国标准时间)">
|
||||
<div class="book" data-level="10.4" data-chapter-title="導入聲明" data-filepath="ch10/ch10-04.md" data-basepath=".." data-revision="Fri Dec 25 2015 12:32:44 GMT+0800 (中国标准时间)">
|
||||
|
||||
|
||||
<div class="book-summary">
|
||||
@@ -2074,14 +2070,6 @@
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
<script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
|
||||
|
||||
|
||||
|
||||
<script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
|
||||
|
||||
|
||||
|
||||
<script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
|
||||
|
||||
|
||||
@@ -2091,7 +2079,7 @@
|
||||
|
||||
<script>
|
||||
require(["gitbook"], function(gitbook) {
|
||||
var config = {"highlight":{},"search":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
|
||||
var config = {"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
|
||||
gitbook.start(config);
|
||||
});
|
||||
</script>
|
||||
|
||||
@@ -25,10 +25,6 @@
|
||||
|
||||
|
||||
|
||||
<link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
|
||||
|
||||
|
||||
|
||||
<link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
|
||||
|
||||
|
||||
@@ -48,7 +44,7 @@
|
||||
<body>
|
||||
|
||||
|
||||
<div class="book" data-level="10.5" data-chapter-title="匿名導入" data-filepath="ch10/ch10-05.md" data-basepath=".." data-revision="Thu Dec 24 2015 14:42:02 GMT+0800 (中国标准时间)">
|
||||
<div class="book" data-level="10.5" data-chapter-title="匿名導入" data-filepath="ch10/ch10-05.md" data-basepath=".." data-revision="Fri Dec 25 2015 12:32:44 GMT+0800 (中国标准时间)">
|
||||
|
||||
|
||||
<div class="book-summary">
|
||||
@@ -2115,14 +2111,6 @@ db, err = sql.Open(<span class="hljs-string">"sqlite3"</span>, dbname)
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
<script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
|
||||
|
||||
|
||||
|
||||
<script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
|
||||
|
||||
|
||||
|
||||
<script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
|
||||
|
||||
|
||||
@@ -2132,7 +2120,7 @@ db, err = sql.Open(<span class="hljs-string">"sqlite3"</span>, dbname)
|
||||
|
||||
<script>
|
||||
require(["gitbook"], function(gitbook) {
|
||||
var config = {"highlight":{},"search":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
|
||||
var config = {"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
|
||||
gitbook.start(config);
|
||||
});
|
||||
</script>
|
||||
|
||||
@@ -25,10 +25,6 @@
|
||||
|
||||
|
||||
|
||||
<link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
|
||||
|
||||
|
||||
|
||||
<link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
|
||||
|
||||
|
||||
@@ -48,7 +44,7 @@
|
||||
<body>
|
||||
|
||||
|
||||
<div class="book" data-level="10.6" data-chapter-title="包和命名" data-filepath="ch10/ch10-06.md" data-basepath=".." data-revision="Thu Dec 24 2015 14:42:02 GMT+0800 (中国标准时间)">
|
||||
<div class="book" data-level="10.6" data-chapter-title="包和命名" data-filepath="ch10/ch10-06.md" data-basepath=".." data-revision="Fri Dec 25 2015 12:32:44 GMT+0800 (中国标准时间)">
|
||||
|
||||
|
||||
<div class="book-summary">
|
||||
@@ -2076,14 +2072,6 @@
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
<script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
|
||||
|
||||
|
||||
|
||||
<script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
|
||||
|
||||
|
||||
|
||||
<script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
|
||||
|
||||
|
||||
@@ -2093,7 +2081,7 @@
|
||||
|
||||
<script>
|
||||
require(["gitbook"], function(gitbook) {
|
||||
var config = {"highlight":{},"search":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
|
||||
var config = {"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
|
||||
gitbook.start(config);
|
||||
});
|
||||
</script>
|
||||
|
||||
@@ -1,53 +0,0 @@
|
||||
### 10.7.1. 工作區結構
|
||||
|
||||
|
||||
對於大多數的Go用戶, 隻需要配置一個名叫GOPATH的環境變量, 用來指定根工作目録卽可. 當需要切換到不同工作區的時候, 隻要更新GOPATH就可以了. 例如, 我們在編寫本書時, 將GOPATH設置爲 `$HOME/gobook`:
|
||||
|
||||
```
|
||||
$ export GOPATH=$HOME/gobook
|
||||
$ go get gopl.io/...
|
||||
```
|
||||
|
||||
當你用前面介紹的命令下載本書全部的程序之後, 你的當前工作區的目録結構是這樣的:
|
||||
|
||||
```
|
||||
GOPATH/
|
||||
src/
|
||||
gopl.io/
|
||||
.git/
|
||||
ch1/
|
||||
helloworld/
|
||||
main.go
|
||||
dup/
|
||||
main.go
|
||||
...
|
||||
golang.org/x/net/
|
||||
.git/
|
||||
html/
|
||||
parse.go
|
||||
node.go
|
||||
...
|
||||
bin/
|
||||
helloworld
|
||||
dup
|
||||
pkg/
|
||||
darwin_amd64/
|
||||
...
|
||||
```
|
||||
|
||||
GOPATH對應的目録有三個子目録. 其中 src 子目録用於存儲源代碼. 每個包保存在$GOPATH/src的相對路徑爲包導入路徑的子目録中, 例如 gopl.io/ch1/helloworld 相對路徑. 我們看到, 一個GOPATH工作區的src目録中可能有多個獨立的版本控製, 例如 gopl.io 或 golang.org. 其中 pkg 子目録用於保存編譯後的包的目標文件, bin 子目録用於保存編譯後的可執行程序, 例如 helloworld 程序.
|
||||
|
||||
第二個環境變量 GOROOT 用來指定Go的安裝目録, 還有它自帶的標準庫包的位置. GOROOT 的目録結構和 GOPATH 類似, 因此存放 fmt 包的源代碼目録爲 $GOROOT/src/fmt. 用戶一般不需要設置 GOROOT, 默認情況下, Go工具會設置爲安裝的位置.
|
||||
|
||||
其中 `go env` 命令用於査看工具涉及的所有環境變量的值, 包括未設置環境變量的默認值. GOOS 用於指定目標操作繫統(例如 android, linux, darwin, 或 windows), GOARCH 用於指定處理器的類型, 例如 amd64, 386, 或 arm. 雖然 GOPATH 是唯一必需要設置的, 但是其它的也有偶爾用到.
|
||||
|
||||
```
|
||||
$ go env
|
||||
GOPATH="/home/gopher/gobook"
|
||||
GOROOT="/usr/local/go"
|
||||
GOARCH="amd64"
|
||||
GOOS="darwin"
|
||||
...
|
||||
```
|
||||
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
### 10.7.2. 下載包
|
||||
|
||||
使用Go工具, 不僅可以根據包導入路徑找到本地工作區的包, 甚至可以從互聯網上找到和更新包.
|
||||
|
||||
使用命令 `go get` 可以下載一個單一的包或者用 `...` 下載整個子目録里面的每個包. Go工具同時計算併下載所依賴的每個包, 這也是前一個例子中 golang.org/x/net/html 自動齣現在本地工作區目録的原因.
|
||||
|
||||
一旦 `go get` 命令下載了包, 然後就是安裝包或包對應的命令. 我們將在下一節再關註它的細節, 現在隻是展示下整個過程是如何的簡單. 第一個命令是穫取 golint 工具, 用於檢測Go源代碼的編程風格是否有問題. 第二個命令是用 golint 對 2.6.2節的 gopl.io/ch2/popcount 包代碼進行編碼風格檢査. 它友好地報告了忘記了包的文檔:
|
||||
|
||||
```
|
||||
$ go get github.com/golang/lint/golint
|
||||
$ $GOPATH/bin/golint gopl.io/ch2/popcount
|
||||
src/gopl.io/ch2/popcount/main.go:1:1:
|
||||
package comment should be of the form "Package popcount ..."
|
||||
```
|
||||
|
||||
`go get` 命令支持當前流行的託管網站 GitHub, Bitbucket, 和 Launchpad, 可以直接從它們的版本控製繫統請求代碼. 對於其他的網站, 你可能需要指定版本控製繫統的具體路徑和協議, 例如 Git 或 Mercurial. 運行 `go help importpath` 穫取更新的信息.
|
||||
|
||||
`go get` 穫取的代碼是眞實的本地存儲倉庫, 不僅僅隻是複製文件, 因此你依然可以使用版本管理工具比較本地代碼的變更, 或者切換到其他的版本. 例如 golang.org/x/net 目録對應一個 Git 倉庫:
|
||||
|
||||
```
|
||||
$ cd $GOPATH/src/golang.org/x/net
|
||||
$ git remote -v
|
||||
origin https://go.googlesource.com/net (fetch)
|
||||
origin https://go.googlesource.com/net (push)
|
||||
```
|
||||
|
||||
需要註意的是導入路徑含有的網站域名和本地Git倉庫遠程的Git服務地址併不相同, 眞實的Git地址是 go.googlesource.com. 這其實是Go工具箱的一個特性, 可以讓包用一個自定義的導入路徑, 但是眞實的代碼卻是由更通用的服務提供, 例如 googlesource.com 或 github.com. 頁面 https://golang.org/x/net/html 包含了如下的元數據, 告訴 Go 工具Git倉庫的眞實託管地址:
|
||||
|
||||
```
|
||||
$ go build gopl.io/ch1/fetch
|
||||
$ ./fetch https://golang.org/x/net/html | grep go-import
|
||||
<meta name="go-import"
|
||||
content="golang.org/x/net git https://go.googlesource.com/net">
|
||||
```
|
||||
|
||||
如果指定 `-u` 命令行標誌參數, `go get` 將確保所有的包和依賴的包的版本都是最新的, 然後編譯和安裝它們. 如果不包含該標誌參數, 如果包已經在本地存在, 那麽將不會被更新.
|
||||
|
||||
`go get -u` 命令隻是簡單地保證每個包是最新版本, 如果你是第一次下載則比較很方便的; 但是如果是發布程序則可能是不合適的, 因爲本地程序可能需要對依賴的包做精確的版本依賴管理. 通常的解決方案是使用 vendor 目録存儲固定版本的代碼, 對本地依賴的包的版本更新也是謹慎和持續可控的. 在 Go 1.5 之前, 一般需要脩改包的導入路徑, 所以複製後 golang.org/x/net/html 導入路徑可能會變爲 gopl.io/vendor/golang.org/x/net/html. 最新的Go工具已經支持 vendor 特性, 但限於篇幅這里併不討論細節. 不過可以通過 `go help gopath` 目録査看 Vendor 目録的幫助.
|
||||
|
||||
**練習 10.3:** 從 http://gopl.io/ch1/helloworld?go-get=1 穫取內容, 査看本書的代碼的眞實託管的網址(`go get`請求HTML頁面時包含了 `go-get` 參數, 以區别普通的瀏覽器請求.)
|
||||
|
||||
@@ -1,112 +0,0 @@
|
||||
### 10.7.3. 構建包
|
||||
|
||||
`go build` 命令編譯參數指定的每個包. 如果包是一個庫, 則忽略輸齣結果; 這可以用於檢測包的可以正確編譯的.
|
||||
如果包的名字是 main, `go build` 將調用連接器在當前目録創建一個可執行程序; 導入路徑的最後一段作爲可執行程序的名字.
|
||||
|
||||
因爲每個目録隻包含一個包, 因此每個可執行程序後者叫Unix術語中的命令, 會要求放到一個獨立的目録. 這些目録有時候會放在名叫 cmd 目録的子目録下面, 例如用於提供Go文檔服務的 golang.org/x/tools/cmd/godoc 命令 (§10.7.4).
|
||||
|
||||
每個包可以由它們的導入路徑指定, 就像前面看到的那樣, 或者有一個相對目録的路徑知道, 必鬚以 `.` 或 `..` 開頭. 如果沒有指定參數, 那麽默認指定爲當前的目録. 下面的命令用於構建同一個包, 雖然它們的寫法各不相同:
|
||||
|
||||
```
|
||||
$ cd $GOPATH/src/gopl.io/ch1/helloworld
|
||||
$ go build
|
||||
```
|
||||
|
||||
或者:
|
||||
|
||||
```
|
||||
$ cd anywhere
|
||||
$ go build gopl.io/ch1/helloworld
|
||||
```
|
||||
|
||||
或者:
|
||||
|
||||
```
|
||||
$ cd $GOPATH
|
||||
$ go build ./src/gopl.io/ch1/helloworld
|
||||
```
|
||||
|
||||
但不能這樣:
|
||||
|
||||
```
|
||||
$ cd $GOPATH
|
||||
$ go build src/gopl.io/ch1/helloworld
|
||||
Error: cannot find package "src/gopl.io/ch1/helloworld".
|
||||
```
|
||||
|
||||
也可以指定包的源文件列表, 一般這隻用於構建一些小程序或臨時性的實驗. 如果是main包, 將以第一個Go源文件的基礎文件名作爲可執行程序的名字.
|
||||
|
||||
```
|
||||
$ cat quoteargs.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Printf("%q\n", os.Args[1:])
|
||||
}
|
||||
$ go build quoteargs.go
|
||||
$ ./quoteargs one "two three" four\ five
|
||||
["one" "two three" "four five"]
|
||||
```
|
||||
|
||||
特别是對於這類一次性的程序, 我們繫統盡快的構建併運行它. `go run` 命令結合了構建和運行的兩個步驟:
|
||||
|
||||
```
|
||||
$ go run quoteargs.go one "two three" four\ five
|
||||
["one" "two three" "four five"]
|
||||
```
|
||||
|
||||
第一行的參數列表中第一個不是以 .go 結尾的將作爲可執行程序的參數運行.
|
||||
|
||||
默認情況下, `go build` 命令構建指定的包和它依賴的包, 然後丟棄所有除了最後的可執行文件之外的中間編譯結果. 依賴分析和編譯都是很快的, 但是隨着項目增加到幾十個包和成韆上萬行代碼, 依賴關繫分析和編譯時間的消耗將變的可觀, 可能需要幾秒種, 卽使這些依賴項沒有改變.
|
||||
|
||||
`go install` 命令和 `go build` 命令很相似, 但是它保存每個包的編譯成果, 而不是將它們都丟棄. 被編譯的包被保存到 $GOPATH/pkg 目録下和 src 目録對應, 可執行程序被保存到 $GOPATH/bin 目録. (很多用戶將 $GOPATH/bin 添加到可執行程序的蒐索列表中.) 還有, `go install` 命令和 `go build` 命令都不會重新編譯沒有發生變化的包, 這可以使後續構建更快捷. 爲了方便, `go build -i` 將安裝每個目標所依賴的包.
|
||||
|
||||
因爲編譯對應不同的操作繫統平颱和CPU架構, `go install` 會將編譯結果安裝到 GOOS 和 GOARCH 對應的目録. 例如, 在 Mac 繫統 golang.org/x/net/html 包將被安裝到 $GOPATH/pkg/darwin_amd64 目録下的 golang.org/x/net/html.a 文件.
|
||||
|
||||
針對不同操作繫統或CPU的交叉構建也是很簡單的. 隻需要設置好目標對應的GOOS 和 GOARCH, 然後運行構建目録卽可. 下面交叉編譯的程序將輸齣它在編譯時操作繫統和CPU類型:
|
||||
|
||||
```Go
|
||||
gopl.io/ch10/cross
|
||||
|
||||
func main() {
|
||||
fmt.Println(runtime.GOOS, runtime.GOARCH)
|
||||
}
|
||||
```
|
||||
|
||||
下面以64位和32位環境分别執行程序:
|
||||
|
||||
```
|
||||
$ go build gopl.io/ch10/cross
|
||||
$ ./cross
|
||||
darwin amd64
|
||||
$ GOARCH=386 go build gopl.io/ch10/cross
|
||||
$ ./cross
|
||||
darwin 386
|
||||
```
|
||||
|
||||
有些包可能需要針對不同平颱和處理器類型輸齣不同版本的代碼, 以便於處理底層的可移植性問題或提供爲一些特點代碼提供優化. 如果一個文件名包含了一個操作繫統或處理器類型名字, 例如 net_linux.go 或 asm_amd64.s, Go工具將隻在對應的平颱編譯這些文件. 還有一個特别的構建註釋註釋可以提供更多的構建控製. 例如, 文件中如果包含下面的註釋:
|
||||
|
||||
```Go
|
||||
// +build linux darwin
|
||||
```
|
||||
|
||||
在包聲明的前面(含包的註釋), 告訴 `go build` 隻在針對 Linux 或 Mac OS X 是纔編譯這個文件. 下面的構建註釋表示不編譯這個文件:
|
||||
|
||||
```Go
|
||||
// +build ignore
|
||||
```
|
||||
|
||||
For more details, see the Build Constraints section of the go/build package’s documentation:
|
||||
|
||||
更多細節, 可以參考 go/build 包的構建約束部分的文檔.
|
||||
|
||||
```
|
||||
$ go doc go/build
|
||||
```
|
||||
|
||||
|
||||
@@ -1,77 +0,0 @@
|
||||
### 10.7.4. 包文檔
|
||||
|
||||
Go的編碼風格鼓勵爲每個包提供良好的文檔. 包中每個導齣的成員和包聲明前都應該包含添加目的和用法説明的註釋.
|
||||
|
||||
Go中包文檔註釋一般是完整的句子, 第一行是包的摘要説明, 註釋後僅跟着包聲明語句. 函數的參數或其他的標識符併不需要額外的引號或其他標記註明. 例如, 下面是 fmt.Fprintf 的文檔註釋.
|
||||
|
||||
```Go
|
||||
// Fprintf formats according to a format specifier and writes to w.
|
||||
// It returns the number of bytes written and any write error encountered.
|
||||
func Fprintf(w io.Writer, format string, a ...interface{}) (int, error)
|
||||
```
|
||||
|
||||
Fprintf 函數格式化的細節在 fmt 包文檔中描述. 如果註釋後僅跟着包聲明語句, 那註釋對應整個包的文檔. 包文檔對應的註釋隻能有一個(譯註: 其實可以多個, 它們會組合成一個包文檔註釋.), 可以齣現在任何一個源文件中. 如果包的註釋內容比較長, 可以當到一個獨立的文件中; fmt 包註釋就有 300 行之多. 這個專門用於保證包文檔的文件通常叫 doc.go.
|
||||
|
||||
好的文檔併不需要面面俱到, 文檔本身應該是簡潔但可不忽略的. 事實上, Go的風格喜歡簡潔的文檔, 併且文檔也是需要想代碼一樣維護的. 對於一組聲明語句, 可以同一個精鍊的句子描述, 如果是顯而易見的功能則併不需要註釋.
|
||||
|
||||
在本書中, 隻要空間允許, 我們之前很多包聲明都包含了註釋文檔, 但你可以從標準庫中發現很多更好的例子. 有兩個工具可以幫到你.
|
||||
|
||||
`go doc` 命令打印包的聲明和每個成員的文檔註釋, 下面是整個包的文檔:
|
||||
|
||||
```
|
||||
$ go doc time
|
||||
package time // import "time"
|
||||
|
||||
Package time provides functionality for measuring and displaying time.
|
||||
|
||||
const Nanosecond Duration = 1 ...
|
||||
func After(d Duration) <-chan Time
|
||||
func Sleep(d Duration)
|
||||
func Since(t Time) Duration
|
||||
func Now() Time
|
||||
type Duration int64
|
||||
type Time struct { ... }
|
||||
...many more...
|
||||
```
|
||||
|
||||
或者是包的一個成員的註釋文檔:
|
||||
|
||||
```
|
||||
$ go doc time.Since
|
||||
func Since(t Time) Duration
|
||||
|
||||
Since returns the time elapsed since t.
|
||||
It is shorthand for time.Now().Sub(t).
|
||||
```
|
||||
|
||||
或者是包的一個方法的註釋文檔:
|
||||
|
||||
```
|
||||
$ go doc time.Duration.Seconds
|
||||
func (d Duration) Seconds() float64
|
||||
|
||||
Seconds returns the duration as a floating-point number of seconds.
|
||||
```
|
||||
|
||||
該工具併不需要輸入完整的包導入路徑或正確的大小寫. 下面的命令打印 encoding/json 包的 (*json.Decoder).Decode 方法的文檔:
|
||||
|
||||
```
|
||||
$ go doc json.decode
|
||||
func (dec *Decoder) Decode(v interface{}) error
|
||||
|
||||
Decode reads the next JSON-encoded value from its input and stores
|
||||
it in the value pointed to by v.
|
||||
```
|
||||
|
||||
第二個工具, 令人睏惑的也是名叫 godoc, 提供可以相互交叉引用的 HTML 頁面, 但是包含和 `go doc` 相同以及更多的信息. 10.1 節演示了 time 包的文檔, 11.6 節將看到godoc演示可以交互的示例程序. godoc 的在線服務 https://godoc.org, 包含了成韆上萬的開源包的檢索工具.
|
||||
|
||||
You can also run an instance of godoc in your workspace if you want to browse your own packages. Visit http://localhost:8000/pkg in your browser while running this command:
|
||||
|
||||
你也可以在自己的工作區目録允許 godoc 服務. 運行下面的命令, 然後在瀏覽器査看 http://localhost:8000/pkg 頁面:
|
||||
|
||||
```
|
||||
$ godoc -http :8000
|
||||
```
|
||||
|
||||
其中 `-analysis=type` 和 `-analysis=pointer` 命令行標誌參數用於打開文檔和代碼中關於靜態分析的結果.
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
### 10.7.5. 內部包
|
||||
|
||||
在Go程序中, 包的封裝機製是一個重要的特性. 爲導齣的標識符隻在同一個包內部可以訪問, 導齣的標識符則是面向全世界可見.
|
||||
|
||||
有時候, 一個中間的狀態可能也是有用的, 對於一小部分信任的包是可見的, 但併不是對所有調用者都可見. 例如, 當我們計劃將一個大的包拆分爲很多小的更容易管理的子包, 但是我們併不想將內部的子包結構也完全暴露齣去. 同時, 我們肯呢個還希望在內部子包之間共享一些通用的處理包. 或者我們隻是想實驗一個新包的還併不穩定的接口, 暫時隻暴露給一些受限製的客戶端.
|
||||
|
||||

|
||||
|
||||
爲了滿足這些需求, Go構建工具支持包含 internal 名字的路徑段的包導入路徑. 這種包叫 internal 包, 一個 internal 包隻能被有和internal目録有同一個父目録的包所導入. 例如, net/http/internal/chunked 內部包隻能被 net/http/httputil 或 net/http 導入, 但是不能被 net/url 包導入. 但是 net/url 包 可以導入 net/http/httputil.
|
||||
|
||||
```
|
||||
net/http
|
||||
net/http/internal/chunked
|
||||
net/http/httputil
|
||||
net/url
|
||||
```
|
||||
|
||||
@@ -1,119 +0,0 @@
|
||||
### 10.7.6. 査詢包
|
||||
|
||||
`go list` 工具可以報告可用包的信息. 其最簡單的形式, 可以測試包是否在工作區併打印他的導入路徑:
|
||||
|
||||
```
|
||||
$ go list github.com/go-sql-driver/mysql
|
||||
github.com/go-sql-driver/mysql
|
||||
```
|
||||
|
||||
`go list` 參數還可以用 `"..."` 表示匹配任意的包的導入路徑. 我們可以用它來列表工作區中的所有包:
|
||||
|
||||
```
|
||||
$ go list ...
|
||||
archive/tar
|
||||
archive/zip
|
||||
bufio
|
||||
bytes
|
||||
cmd/addr2line
|
||||
cmd/api
|
||||
...many more...
|
||||
```
|
||||
|
||||
或者是特定子目録下的所有包:
|
||||
|
||||
```
|
||||
$ 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
|
||||
```
|
||||
|
||||
或者是和某個主體相關的:
|
||||
|
||||
```
|
||||
$ go list ...xml...
|
||||
encoding/xml
|
||||
gopl.io/ch7/xmlselect
|
||||
```
|
||||
|
||||
`go list` 可以穫取每個包完整的元信息, 而不僅僅隻是導入路徑, 這些信息可以以不同格式提供給用戶. 其中 `-json` 標誌參數表示用JSON格式打印每個包的元信息.
|
||||
|
||||
```
|
||||
$ 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"
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
參數 `-f` 允許用戶使用 text/template (§4.6) 的模闆語言定義輸齣文本的格式. 下面的命令打印 strconv 包的依賴的包, 然後用 join 模闆函數鏈接爲一行, 用一個空格分隔:
|
||||
|
||||
{% raw %}
|
||||
```
|
||||
$ go list -f '{{join .Deps " "}}' strconv
|
||||
errors math runtime unicode/utf8 unsafe
|
||||
```
|
||||
{% endraw %}
|
||||
|
||||
譯註: 上面的命令在 Windows 的命令行運行會遇到 `template: main:1: unclosed action` 的錯誤. 産生錯誤的原因是因爲命令行對里面的 `" "` 參數進行轉義了. 按照下面的方法解決轉義字符串的問題:
|
||||
|
||||
{% raw %}
|
||||
```
|
||||
$ go list -f "{{join .Deps \" \"}}" strconv
|
||||
```
|
||||
{% endraw %}
|
||||
|
||||
下面的命令打印 compress 子目録下所有包的依賴包列表:
|
||||
|
||||
{% 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 %}
|
||||
|
||||
譯註: Windows 下同樣有問題, 要避免轉義字符串的問題:
|
||||
|
||||
{% raw %}
|
||||
```
|
||||
$ go list -f "{{.ImportPath}} -> {{join .Imports \" \"}}" compress/...
|
||||
```
|
||||
{% endraw %}
|
||||
|
||||
go list 命令對於一次性的交互式査詢或自動化構建和測試腳本都很有幫助. 我們將在 11.2.4節 中再次使用它. 更多的信息, 包括可設置的字段和意義, 可以用 `go help list` 命令査看.
|
||||
|
||||
在本章, 我們解釋了Go工具箱除了測試命令之外的所有重要的命令. 在下一章, 我們將看到如何用 `go test` 命令去測試Go程序.
|
||||
|
||||
**練習10.4:** 創建一個工具, 根據命令行指定的參數, 報告工作區所有依賴指定包的其他包集合. 提示: 你需要運行 `go list` 命令兩次, 一次用於初始化包, 一次用於所有包. 你可能需要用 encoding/json (§4.5) 包來分析輸齣的 JSON 格式的信息.
|
||||
|
||||
|
||||
|
||||
@@ -25,10 +25,6 @@
|
||||
|
||||
|
||||
|
||||
<link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
|
||||
|
||||
|
||||
|
||||
<link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
|
||||
|
||||
|
||||
@@ -48,7 +44,7 @@
|
||||
<body>
|
||||
|
||||
|
||||
<div class="book" data-level="10.7" data-chapter-title="工具" data-filepath="ch10/ch10-07.md" data-basepath=".." data-revision="Thu Dec 24 2015 14:42:02 GMT+0800 (中国标准时间)">
|
||||
<div class="book" data-level="10.7" data-chapter-title="工具" data-filepath="ch10/ch10-07.md" data-basepath=".." data-revision="Fri Dec 25 2015 12:32:44 GMT+0800 (中国标准时间)">
|
||||
|
||||
|
||||
<div class="book-summary">
|
||||
@@ -2311,14 +2307,6 @@ compress/zlib -> bufio compress/flate errors fmt hash hash/adler32 io
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
<script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
|
||||
|
||||
|
||||
|
||||
<script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
|
||||
|
||||
|
||||
|
||||
<script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
|
||||
|
||||
|
||||
@@ -2328,7 +2316,7 @@ compress/zlib -> bufio compress/flate errors fmt hash hash/adler32 io
|
||||
|
||||
<script>
|
||||
require(["gitbook"], function(gitbook) {
|
||||
var config = {"highlight":{},"search":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
|
||||
var config = {"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
|
||||
gitbook.start(config);
|
||||
});
|
||||
</script>
|
||||
|
||||
@@ -25,10 +25,6 @@
|
||||
|
||||
|
||||
|
||||
<link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
|
||||
|
||||
|
||||
|
||||
<link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
|
||||
|
||||
|
||||
@@ -48,7 +44,7 @@
|
||||
<body>
|
||||
|
||||
|
||||
<div class="book" data-level="10" data-chapter-title="包和工具" data-filepath="ch10/ch10.md" data-basepath=".." data-revision="Thu Dec 24 2015 14:42:02 GMT+0800 (中国标准时间)">
|
||||
<div class="book" data-level="10" data-chapter-title="包和工具" data-filepath="ch10/ch10.md" data-basepath=".." data-revision="Fri Dec 25 2015 12:32:44 GMT+0800 (中国标准时间)">
|
||||
|
||||
|
||||
<div class="book-summary">
|
||||
@@ -2049,14 +2045,6 @@
|
||||
<script src="../gitbook/app.js"></script>
|
||||
|
||||
|
||||
<script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
|
||||
|
||||
|
||||
|
||||
<script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
|
||||
|
||||
|
||||
|
||||
<script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
|
||||
|
||||
|
||||
@@ -2066,7 +2054,7 @@
|
||||
|
||||
<script>
|
||||
require(["gitbook"], function(gitbook) {
|
||||
var config = {"highlight":{},"search":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
|
||||
var config = {"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
|
||||
gitbook.start(config);
|
||||
});
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user