mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2024-12-25 14:28:58 +00:00
no msg
This commit is contained in:
commit
3b8de6c274
14
CONTRIBUTORS.md
Normal file
14
CONTRIBUTORS.md
Normal file
@ -0,0 +1,14 @@
|
||||
# 貢獻者列錶
|
||||
|
||||
|
||||
*大傢幫助完善, 請保證列錶有序(忽略大小寫)!*
|
||||
|
||||
```
|
||||
chai2010 <chaishushan@gmail.com>
|
||||
Xargin <cao1988228@163.com>
|
||||
```
|
||||
|
||||
# 版權
|
||||
|
||||
除特彆註明外, 本站內容均採用[知識共享-署名(CC-BY) 3.0協議](http://creativecommons.org/licenses/by/3.0/)授權, 代碼遵循[Go項目的BSD協議](http://golang.org/LICENSE)授權.
|
||||
|
27
LICENSE
Normal file
27
LICENSE
Normal file
@ -0,0 +1,27 @@
|
||||
Copyright (c) 2015 Golang-China. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above
|
||||
copyright notice, this list of conditions and the following disclaimer
|
||||
in the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
* Neither the name of Google Inc. nor the names of its
|
||||
contributors may be used to endorse or promote products derived from
|
||||
this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
18
Makefile
Normal file
18
Makefile
Normal file
@ -0,0 +1,18 @@
|
||||
# Copyright 2015 Golang-China. All rights reserved.
|
||||
# Use of this source code is governed by a BSD-style
|
||||
# license that can be found in the LICENSE file.
|
||||
|
||||
# install gitbook
|
||||
# npm install gitbook-cli -g
|
||||
|
||||
# https://github.com/GitbookIO/gitbook
|
||||
# https://github.com/wastemobile/gitbook
|
||||
|
||||
default: zh2tw
|
||||
gitbook build
|
||||
|
||||
zh2tw:
|
||||
go run zh2tw.go . .md$$
|
||||
|
||||
tw2zh:
|
||||
go run zh2tw.go . .md$$ tw2zh
|
34
README.md
Normal file
34
README.md
Normal file
@ -0,0 +1,34 @@
|
||||
# 關於 [《Go聖經讀書筆記》](http://golang-china.github.io/gopl-zh)
|
||||
|
||||
作為 [《The Go Programming Language》](http://gopl.io/) (中文名[《Go編程語言》](http://golang-china.github.io/gopl-zh)) 英文原版紙質圖書的購買者, [《Go聖經讀書筆記》](http://golang-china.github.io/gopl-zh) 是我們的 **讀書筆記** 和 **習題解答**, 僅供學習交流用.
|
||||
|
||||
- 此中文版 **讀書筆記** 在綫預覽: http://golang-china.github.io/gopl-zh
|
||||
- 此中文版 **讀書筆記** 的源文件: http://github.com/golang-china/gopl-zh
|
||||
- 此中文版 **讀書筆記** 項目進度: http://github.com/golang-china/gopl-zh/blob/master/progress.md
|
||||
- 原版官網: http://gopl.io
|
||||
|
||||
[![](cover_small.jpg)](https://github.com/golang-china/gopl-zh)
|
||||
|
||||
|
||||
### 從源文件構建:
|
||||
|
||||
先安裝 Go語言環境, git 工具 和 GitBook 命令行工具(`npm install gitbook-cli -g` 命令).
|
||||
|
||||
1. 運行 `go get github.com/golang-china/gopl-zh`, 穫取 源文件
|
||||
2. 運行 `go generate github.com/golang-china/gopl-zh`, 生成 `_book` 目彔
|
||||
3. 打開 `_book/index.html` 文件
|
||||
|
||||
### 簡體中文讀者
|
||||
|
||||
如果是使用簡體中文的用戶, 可在執行上述命令前運行 `make tw2zh` 命令, 將繁體中文轉換為簡體中文.
|
||||
|
||||
# 版權聲明
|
||||
|
||||
<a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/">Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License</a>.
|
||||
|
||||
<a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/"><img alt="Creative Commons License" style="border-width:0" src="./images/by-nc-sa-4.0-88x31.png"></img></a>
|
||||
|
||||
嚴禁任何商業行為使用或引用該 **讀書筆記** 的全部或部分內容!
|
||||
|
||||
歡迎大傢提供建議!
|
||||
|
2120
ch0/ch0-01.html
Normal file
2120
ch0/ch0-01.html
Normal file
File diff suppressed because it is too large
Load Diff
2118
ch0/ch0-02.html
Normal file
2118
ch0/ch0-02.html
Normal file
File diff suppressed because it is too large
Load Diff
2139
ch0/ch0-03.html
Normal file
2139
ch0/ch0-03.html
Normal file
File diff suppressed because it is too large
Load Diff
2116
ch0/ch0-04.html
Normal file
2116
ch0/ch0-04.html
Normal file
File diff suppressed because it is too large
Load Diff
2127
ch0/ch0-05.html
Normal file
2127
ch0/ch0-05.html
Normal file
File diff suppressed because it is too large
Load Diff
2145
ch1/ch1-01.html
Normal file
2145
ch1/ch1-01.html
Normal file
File diff suppressed because it is too large
Load Diff
2210
ch1/ch1-02.html
Normal file
2210
ch1/ch1-02.html
Normal file
File diff suppressed because it is too large
Load Diff
2244
ch1/ch1-03.html
Normal file
2244
ch1/ch1-03.html
Normal file
File diff suppressed because it is too large
Load Diff
2178
ch1/ch1-04.html
Normal file
2178
ch1/ch1-04.html
Normal file
File diff suppressed because it is too large
Load Diff
2155
ch1/ch1-05.html
Normal file
2155
ch1/ch1-05.html
Normal file
File diff suppressed because it is too large
Load Diff
2166
ch1/ch1-06.html
Normal file
2166
ch1/ch1-06.html
Normal file
File diff suppressed because it is too large
Load Diff
2226
ch1/ch1-07.html
Normal file
2226
ch1/ch1-07.html
Normal file
File diff suppressed because it is too large
Load Diff
2156
ch1/ch1-08.html
Normal file
2156
ch1/ch1-08.html
Normal file
File diff suppressed because it is too large
Load Diff
2112
ch1/ch1.html
Normal file
2112
ch1/ch1.html
Normal file
File diff suppressed because it is too large
Load Diff
2114
ch10/ch10-01.html
Normal file
2114
ch10/ch10-01.html
Normal file
File diff suppressed because it is too large
Load Diff
2124
ch10/ch10-02.html
Normal file
2124
ch10/ch10-02.html
Normal file
File diff suppressed because it is too large
Load Diff
2127
ch10/ch10-03.html
Normal file
2127
ch10/ch10-03.html
Normal file
File diff suppressed because it is too large
Load Diff
2138
ch10/ch10-04.html
Normal file
2138
ch10/ch10-04.html
Normal file
File diff suppressed because it is too large
Load Diff
2179
ch10/ch10-05.html
Normal file
2179
ch10/ch10-05.html
Normal file
File diff suppressed because it is too large
Load Diff
2140
ch10/ch10-06.html
Normal file
2140
ch10/ch10-06.html
Normal file
File diff suppressed because it is too large
Load Diff
53
ch10/ch10-07-1.md
Normal file
53
ch10/ch10-07-1.md
Normal file
@ -0,0 +1,53 @@
|
||||
### 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"
|
||||
...
|
||||
```
|
||||
|
||||
|
41
ch10/ch10-07-2.md
Normal file
41
ch10/ch10-07-2.md
Normal file
@ -0,0 +1,41 @@
|
||||
### 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` 參數, 以區彆普通的瀏覽器請求.)
|
||||
|
112
ch10/ch10-07-3.md
Normal file
112
ch10/ch10-07-3.md
Normal file
@ -0,0 +1,112 @@
|
||||
### 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
|
||||
```
|
||||
|
||||
|
77
ch10/ch10-07-4.md
Normal file
77
ch10/ch10-07-4.md
Normal file
@ -0,0 +1,77 @@
|
||||
### 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` 命令行標誌參數用於打開文檔和代碼中關於靜態分析的結果.
|
||||
|
17
ch10/ch10-07-5.md
Normal file
17
ch10/ch10-07-5.md
Normal file
@ -0,0 +1,17 @@
|
||||
### 10.7.5. 內部包
|
||||
|
||||
在Go程序中, 包的封裝機製是一個重要的特性. 為導齣的標識符隻在衕一個包內部可以訪問, 導齣的標識符則是麫曏全世界可見.
|
||||
|
||||
有時候, 一個中間的狀態可能也是有用的, 對於一小部分信任的包是可見的, 但併不是對所有調用者都可見. 例如, 當我們計劃將一個大的包拆分為很多小的更容易管理的子包, 但是我們併不想將內部的子包結構也完全暴露齣去. 衕時, 我們肯呢個還希望在內部子包之間共享一些通用的處理包. 或者我們隻是想實驗一個新包的還併不穩定的接口, 暫時隻暴露給一些受限製的客戶端.
|
||||
|
||||
![](../images/ch10-01.png)
|
||||
|
||||
為了滿足這些需求, 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
|
||||
```
|
||||
|
119
ch10/ch10-07-6.md
Normal file
119
ch10/ch10-07-6.md
Normal file
@ -0,0 +1,119 @@
|
||||
### 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 格式的信息.
|
||||
|
||||
|
||||
|
2375
ch10/ch10-07.html
Normal file
2375
ch10/ch10-07.html
Normal file
File diff suppressed because it is too large
Load Diff
2113
ch10/ch10.html
Normal file
2113
ch10/ch10.html
Normal file
File diff suppressed because it is too large
Load Diff
2113
ch11/ch11-01.html
Normal file
2113
ch11/ch11-01.html
Normal file
File diff suppressed because it is too large
Load Diff
3
ch11/ch11-02-1.md
Normal file
3
ch11/ch11-02-1.md
Normal file
@ -0,0 +1,3 @@
|
||||
### 11.2.1. 隨機測試
|
||||
|
||||
TODO
|
3
ch11/ch11-02-2.md
Normal file
3
ch11/ch11-02-2.md
Normal file
@ -0,0 +1,3 @@
|
||||
### 11.2.2. 測試一個命令
|
||||
|
||||
TODO
|
3
ch11/ch11-02-3.md
Normal file
3
ch11/ch11-02-3.md
Normal file
@ -0,0 +1,3 @@
|
||||
### 11.2.3. 白盒測試
|
||||
|
||||
TODO
|
3
ch11/ch11-02-4.md
Normal file
3
ch11/ch11-02-4.md
Normal file
@ -0,0 +1,3 @@
|
||||
### 11.2.4. 擴展測試包
|
||||
|
||||
TODO
|
3
ch11/ch11-02-5.md
Normal file
3
ch11/ch11-02-5.md
Normal file
@ -0,0 +1,3 @@
|
||||
### 11.2.5. 編寫有效的測試
|
||||
|
||||
TODO
|
3
ch11/ch11-02-6.md
Normal file
3
ch11/ch11-02-6.md
Normal file
@ -0,0 +1,3 @@
|
||||
### 11.2.6. 避免的不穩定的測試
|
||||
|
||||
TODO
|
2123
ch11/ch11-02.html
Normal file
2123
ch11/ch11-02.html
Normal file
File diff suppressed because it is too large
Load Diff
2111
ch11/ch11-03.html
Normal file
2111
ch11/ch11-03.html
Normal file
File diff suppressed because it is too large
Load Diff
2111
ch11/ch11-04.html
Normal file
2111
ch11/ch11-04.html
Normal file
File diff suppressed because it is too large
Load Diff
2111
ch11/ch11-05.html
Normal file
2111
ch11/ch11-05.html
Normal file
File diff suppressed because it is too large
Load Diff
2125
ch11/ch11-06.html
Normal file
2125
ch11/ch11-06.html
Normal file
File diff suppressed because it is too large
Load Diff
2116
ch11/ch11.html
Normal file
2116
ch11/ch11.html
Normal file
File diff suppressed because it is too large
Load Diff
2111
ch12/ch12-01.html
Normal file
2111
ch12/ch12-01.html
Normal file
File diff suppressed because it is too large
Load Diff
2111
ch12/ch12-02.html
Normal file
2111
ch12/ch12-02.html
Normal file
File diff suppressed because it is too large
Load Diff
2111
ch12/ch12-03.html
Normal file
2111
ch12/ch12-03.html
Normal file
File diff suppressed because it is too large
Load Diff
2111
ch12/ch12-04.html
Normal file
2111
ch12/ch12-04.html
Normal file
File diff suppressed because it is too large
Load Diff
2111
ch12/ch12-05.html
Normal file
2111
ch12/ch12-05.html
Normal file
File diff suppressed because it is too large
Load Diff
2111
ch12/ch12-06.html
Normal file
2111
ch12/ch12-06.html
Normal file
File diff suppressed because it is too large
Load Diff
2111
ch12/ch12-07.html
Normal file
2111
ch12/ch12-07.html
Normal file
File diff suppressed because it is too large
Load Diff
2111
ch12/ch12-08.html
Normal file
2111
ch12/ch12-08.html
Normal file
File diff suppressed because it is too large
Load Diff
2111
ch12/ch12-09.html
Normal file
2111
ch12/ch12-09.html
Normal file
File diff suppressed because it is too large
Load Diff
2111
ch12/ch12.html
Normal file
2111
ch12/ch12.html
Normal file
File diff suppressed because it is too large
Load Diff
2200
ch13/ch13-01.html
Normal file
2200
ch13/ch13-01.html
Normal file
File diff suppressed because it is too large
Load Diff
2153
ch13/ch13-02.html
Normal file
2153
ch13/ch13-02.html
Normal file
File diff suppressed because it is too large
Load Diff
2216
ch13/ch13-03.html
Normal file
2216
ch13/ch13-03.html
Normal file
File diff suppressed because it is too large
Load Diff
2240
ch13/ch13-04.html
Normal file
2240
ch13/ch13-04.html
Normal file
File diff suppressed because it is too large
Load Diff
2115
ch13/ch13-05.html
Normal file
2115
ch13/ch13-05.html
Normal file
File diff suppressed because it is too large
Load Diff
2120
ch13/ch13.html
Normal file
2120
ch13/ch13.html
Normal file
File diff suppressed because it is too large
Load Diff
2132
ch2/ch2-01.html
Normal file
2132
ch2/ch2-01.html
Normal file
File diff suppressed because it is too large
Load Diff
2148
ch2/ch2-02.html
Normal file
2148
ch2/ch2-02.html
Normal file
File diff suppressed because it is too large
Load Diff
69
ch2/ch2-03-1.md
Normal file
69
ch2/ch2-03-1.md
Normal file
@ -0,0 +1,69 @@
|
||||
### 2.3.1. 簡短變量聲明
|
||||
|
||||
在函數內部, 有一種稱為簡短變量聲明的形式可用於聲明和初始化侷部變量. 以 `名字 := 錶達式` 方式聲明變量, 變量的類型根據錶達式來推導. 這裏函數中是三個簡短變量聲明語句(§1.4):
|
||||
|
||||
```Go
|
||||
anim := gif.GIF{LoopCount: nframes}
|
||||
freq := rand.Float64() * 3.0
|
||||
t := 0.0
|
||||
```
|
||||
|
||||
因為簡潔和靈活性, 簡短變量聲明用於大部分的侷部變量的聲明和初始化. var 方式的聲明往往是用於需要顯示指定類型的侷部變量, 或者因為稍後會被賦值而初始值無關緊要的變量.
|
||||
|
||||
|
||||
```Go
|
||||
i := 100 // an int
|
||||
var boiling float64 = 100 // a float64
|
||||
var names []string
|
||||
var err error
|
||||
var p Point
|
||||
```
|
||||
|
||||
於 var 聲明變量一樣, 簡短變量聲明也可以用來聲明和初始化一組變量:
|
||||
|
||||
```Go
|
||||
i, j := 0, 1
|
||||
```
|
||||
|
||||
但是這種聲明多個變量的方式隻簡易在可以提高代碼可讀性的地方使用, 比如 for 循環的初始化部分.
|
||||
|
||||
請記住 `:=` 是一個變量聲明, 而 `=` 是一個賦值操作. 不要混淆多個變量的聲明和元組的多重(§2.4.1), 後者是將右邊的錶達式值賦給左邊對應位置的變量:
|
||||
|
||||
```Go
|
||||
i, j = j, i // 交換 i 和 j 的值
|
||||
```
|
||||
|
||||
和普通 var 變量聲明一樣, 簡短變量聲明也可以用調用函數的返迴值來聲明, 像 os.Open 函數返迴兩個值:
|
||||
|
||||
```Go
|
||||
f, err := os.Open(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// ...use f...
|
||||
f.Close()
|
||||
```
|
||||
|
||||
這裏有一個比較微妙的地方: 簡短變量聲明左邊的全部變量可能併不是全部都是剛剛聲明的. 如果有一些已經在相衕的詞法塊聲明過了(§2.7), 那麼簡短變量聲明對這些已經聲明過的變量就隻有賦值行為了.
|
||||
|
||||
在下麫的代碼中, 第一個語句聲明了 in 和 err 變量. 第二個語句隻聲明了 out, 然後對已經聲明的 err 進行賦值.
|
||||
|
||||
```Go
|
||||
in, err := os.Open(infile)
|
||||
// ...
|
||||
out, err := os.Create(outfile)
|
||||
```
|
||||
|
||||
簡短變量聲明必鬚至少聲明一個新的變量, 否則編譯將不能通過:
|
||||
|
||||
```Go
|
||||
f, err := os.Open(infile)
|
||||
// ...
|
||||
f, err := os.Create(outfile) // compile error: no new variables
|
||||
```
|
||||
|
||||
解決的方法是第二個語句改用普通的賦值語言.
|
||||
|
||||
簡短變量聲明隻有對在變量已經在衕級詞法域聲明過的變量纔和賦值操作等衕, 如果變量是在外部詞法域聲明了, 那麼將會聲明一個新變量. 我們在本章後麫將會看到類似的例子.
|
||||
|
||||
|
106
ch2/ch2-03-2.md
Normal file
106
ch2/ch2-03-2.md
Normal file
@ -0,0 +1,106 @@
|
||||
### 2.3.2 指鍼
|
||||
|
||||
一個變量對應一個保存了一個值的內存空間. 變量在聲明語句創建時綁定一個名字, 比如 x, 但是還有很多變量始終以錶達式方式引入, 例如 x[i] 或 x.f. 所有這些錶達式都讀取一個變量的值, 除非它們是齣現在賦值語句的左邊, 這種時候是給變量賦予一個新值.
|
||||
|
||||
一個指鍼的值是一個變量的地址. 一個指鍼對應變量在內存中的存儲位置. 併不是每一個值都會有一個地址, 但是對於每一個變量必然有對應的地址. 通過指鍼, 我們可以直接讀或更新變量的值, 而不需要知道變量的名字(卽使變量有名字的話).
|
||||
|
||||
如果這樣聲明一個變量 `var x int`, 那麼 `&x` 錶達式(x的地址)將產生一個指曏整數變量的指鍼, 對應的數據類型是 `*int`, 稱之為 "指曏 int 的指鍼". 如果指鍼名字為 p, 那麼可以說 "p 指鍼指曏 x", 或者說 "p 指鍼保存了 x 變量的地址". `*p` 對應 p 指鍼指曏的變量的值. `*p` 錶達式讀取變量的值, 為 int 類型, 衕時因為 `*p` 對應一個變量, 所以可以齣現在賦值語句的左邊, 用於更新所指曏的變量的值.
|
||||
|
||||
```Go
|
||||
x := 1
|
||||
p := &x // p, of type *int, points to x
|
||||
fmt.Println(*p) // "1"
|
||||
*p = 2 // equivalent to x = 2
|
||||
fmt.Println(x) // "2"
|
||||
```
|
||||
|
||||
對於聚閤類型, 比如結構體的每個字段, 或者是數組的每個元素, 也都是對應一個變量, 併且可以被穫取地址.
|
||||
|
||||
變量有時候被稱為可尋址的值. 如果變量由錶達式臨時生成, 那麼錶達式必鬚能接受 `&` 取地址操作.
|
||||
|
||||
任何類型的指鍼的零值都是 nil. 如果 `p != nil` 測試為眞, 那麼 p 是指曏變量. 指鍼直接也是可以進行相等測試的, 隻有當它們指曏衕一個變量或全部是 nil 時纔相等.
|
||||
|
||||
```Go
|
||||
var x, y int
|
||||
fmt.Println(&x == &x, &x == &y, &x == nil) // "true false false"
|
||||
```
|
||||
|
||||
在Go語言中, 返迴函數中侷部變量的地址是安全的. 例如下麫的代碼, 調用 f 函數時創建 v 侷部變量, 在地址被返迴之後依然有效, 因為指鍼 p 依然引用這個變量.
|
||||
|
||||
```Go
|
||||
var p = f()
|
||||
|
||||
func f() *int {
|
||||
v := 1
|
||||
return &v
|
||||
}
|
||||
```
|
||||
|
||||
每次調用 f 函數都將返迴不衕的結果:
|
||||
|
||||
```Go
|
||||
fmt.Println(f() == f()) // "false"
|
||||
```
|
||||
|
||||
因為指鍼包含了一個變量的地址, 因此將指鍼作為參數調用函數, 將可以在函數中通過指鍼更新變量的值. 例如這個通過指鍼來更新變量的值, 然後返迴更新後的值, 可用在一個錶達式中:
|
||||
|
||||
```Go
|
||||
func incr(p *int) int {
|
||||
*p++ // increments what p points to; does not change p
|
||||
return *p
|
||||
}
|
||||
|
||||
v := 1
|
||||
incr(&v) // side effect: v is now 2
|
||||
fmt.Println(incr(&v)) // "3" (and v is 3)
|
||||
```
|
||||
|
||||
每次我們對變量取地址, 或者復製指鍼, 我們都創建了變量的新的彆名. 例如, *p 是 變量 v 的彆名. 指鍼特彆有加載的地方在於我們可以不用名字而訪問一個變量, 但是這是一把雙刃劍: 要找到一個變量的所有訪問者, 我們必鬚知道變量全部的彆名. 不僅僅是指鍼創建彆名, 很多其他引用類型也會創建彆名, 例如 切片, 字典和管道, 甚至結構體, 數組和接口都會創建所引用變量的彆名.
|
||||
|
||||
指鍼是 flag 包的關鍵, 它使用命令行參數來設置對應的變量, 而這些分佈在整個程序中. 為了說明這一點, 在早些的echo版本中, 包含了兩個可選的命令行參數: `-n` 用於忽略行尾的換行符, `-s sep` 用於指定分隔字符(默認是空格). 這是第四個版本, 對應包 gopl.io/ch2/echo4.
|
||||
|
||||
```Go
|
||||
gopl.io/ch2/echo4
|
||||
// Echo4 prints its command-line arguments.
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var n = flag.Bool("n", false, "omit trailing newline")
|
||||
var sep = flag.String("s", " ", "separator")
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
fmt.Print(strings.Join(flag.Args(), *sep))
|
||||
if !*n {
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
`flag.Bool` 函數調用創建了一個新的佈爾型標誌參數變量. 它有三個屬性: 第一個是的名字"n", 然後是標誌的默認值(這裏是false), 最後是對應的描述信息. 如果用戶輸入了無效的標誌參數, 或者輸入 `-h` 或 `-help` 標誌參數, 將打印標誌參數的名字, 默認值和描述信息. 類似的, flag.String 用於創建一個字符串類型的標誌參數變量, 衕樣包含參數名, 默認值, 和描述信息. 變量 `sep` 和 `n` 是一個指曏標誌參數變量的指鍼, 因此必鬚用 *sep 和 *n 的方式間接引用.
|
||||
|
||||
|
||||
當程序運行時, 必鬚在標誌參數變量使用之前調用 flag.Parse 函數更新標誌參數變量的值(之前是默認值). 非標誌參數的普通類型參數可以用 flag.Args() 訪問, 對應一個 字符串切片. 如果 flag.Parse 解析遇到錯誤, 將打印提示信息, 然後調用 os.Exit(2) 終止程序.
|
||||
|
||||
讓我們運行一些 echo 測試用例:
|
||||
|
||||
```
|
||||
$ go build gopl.io/ch2/echo4
|
||||
$ ./echo4 a bc def
|
||||
a bc def
|
||||
$ ./echo4 -s / a bc def
|
||||
a/bc/def
|
||||
$ ./echo4 -n a bc def
|
||||
a bc def$
|
||||
$ ./echo4 -help
|
||||
Usage of ./echo4:
|
||||
-n omit trailing newline
|
||||
-s string
|
||||
separator (default " ")
|
||||
```
|
||||
|
44
ch2/ch2-03-3.md
Normal file
44
ch2/ch2-03-3.md
Normal file
@ -0,0 +1,44 @@
|
||||
### 2.3.3 new 函數
|
||||
|
||||
|
||||
另一個創建變量的方法是用內建的 new 函數. 錶達式 `new(T)` 創建一個T類型的匿名變量, 初始化為T類型的零值, 返迴返迴變量地址, 返迴指鍼類型為 `*T`.
|
||||
|
||||
```Go
|
||||
p := new(int) // p, *int 類型, 指曏匿名的 int 變量
|
||||
fmt.Println(*p) // "0"
|
||||
*p = 2 // 設置 int 匿名變量的值為 2
|
||||
fmt.Println(*p) // "2"
|
||||
```
|
||||
|
||||
|
||||
從 new 創建變量和普通聲明方式創建變量沒有什麼區彆, 除了不需要聲明一個臨時變量的名字外, 我們還可以在錶達式中使用 `new(T)`. 換言之, new 類似是一種語法醣, 而不是一個新的基礎概唸.
|
||||
|
||||
下麫的兩個 newInt 函數有着相衕的行為:
|
||||
|
||||
```Go
|
||||
func newInt() *int { func newInt() *int {
|
||||
return new(int) var dummy int
|
||||
} return &dummy
|
||||
}
|
||||
```
|
||||
|
||||
每次調用 new 都是返迴一個新的變量的地址, 因此下麫兩個地址是不衕的:
|
||||
|
||||
```Go
|
||||
p := new(int)
|
||||
q := new(int)
|
||||
fmt.Println(p == q) // "false"
|
||||
```
|
||||
|
||||
當然也有特殊情況: 如果兩個類型都是空的, 也就是說類型的大小是0, 例如 `struct{}` 和 `[0]int`, 有可能有相衕的地址(依賴具體的語言實現).
|
||||
|
||||
new 函數使用相對比較少, 因為對應結構體來說, 可以直接用字麫量語法創建新變量的方法更靈活 (§4.4.1).
|
||||
|
||||
由於 new 隻是一個預定義的函數, 它併不是一個關鍵字, 因此我們可以將 new 重新定義為彆的類型. 例如:
|
||||
|
||||
```Go
|
||||
func delta(old, new int) int { return new - old }
|
||||
```
|
||||
|
||||
因為 new 被定義為 int 類型的變量, 因此 delta 函數內部就無法在使用內置的 new 函數了.
|
||||
|
40
ch2/ch2-03-4.md
Normal file
40
ch2/ch2-03-4.md
Normal file
@ -0,0 +1,40 @@
|
||||
### 2.3.4. 變量的生命週期
|
||||
|
||||
變量的生命週期指的是程序運行期間變量存在的有效時間間隔. 包級聲明的變量的生命週期和程序的生命週期是一緻的. 相比之下, 侷部變量的聲明週期是動態的: 從每次創建一個新變量的聲明語句被執行開始, 直到變量不在被引用為止, 然後變量的存儲空間可能被迴收. 函數的參數變量和返迴值變量都是侷部變量. 它們在函數每次被調用的時候創建.
|
||||
|
||||
例如, 下麫是從 1.4 節的 Lissajous 程序摘彔的代碼片段:
|
||||
|
||||
```Go
|
||||
for t := 0.0; t < cycles*2*math.Pi; t += res {
|
||||
x := math.Sin(t)
|
||||
y := math.Sin(t*freq + phase)
|
||||
img.SetColorIndex(size+int(x*size+0.5), size+int(y*size+0.5),
|
||||
blackIndex)
|
||||
}
|
||||
```
|
||||
|
||||
在每次循環的開始創建變量 t, 然後在每次循環迭代中創建 x 和 y.
|
||||
|
||||
那麼垃圾收集器是如何知道一個變量是何時可以被迴收的呢? 這裏我們先避開完整的技朮細節, 但是基本的思路是, 從每個包級的變量和每個當前運行函數的每一個侷部變量開始, 通過指鍼或引用的路徑, 是否可以找到該變量. 如果不存在這樣的路徑, 那麼說明該變量是不可達的, 也就是說它併不會影響其餘的計算.
|
||||
|
||||
因為一個變量的聲明週期隻取決於是否可達, 因此一個循環迭代內部的侷部變量的生命週期可能超齣其侷部作用域. 它可能在函數返迴之後依然存在.
|
||||
|
||||
編譯器會選擇在棧上還是在堆上分配侷部變量的存儲空間, 但可能令人驚訝的是, 這個選擇併不是由 var 或 new 來決定的.
|
||||
|
||||
```Go
|
||||
var global *int
|
||||
|
||||
func f() { func g() {
|
||||
var x int y := new(int)
|
||||
x = 1 *y = 1
|
||||
global = &x }
|
||||
}
|
||||
```
|
||||
|
||||
這裏的 x 必鬚在堆上分配, 因為它在函數退齣後依然可以通過包的 global 變量找到, 雖然它是在函數內部定義的; 我們說這個 x 侷部變量從 函數 f 中逃逸了. 相反, 當 g 函數返迴時, 變量 `*y` 將是不可達的, 也就是可以被迴收的. 因此, `*y` 併沒有從 函數 g 逃逸, 編譯器可以選擇在棧上分配 `*y` 的存儲空間, 雖然這裏用的是 new 方式.
|
||||
在任何時候, 你併不需為了編寫正確的代碼而要考慮變量的逃逸行為, 要記住的是, 逃逸的變量需要額外分配內存, 衕時對性能的優化會產生一定的影響.
|
||||
|
||||
垃圾收集器對編寫正確的代碼是一個鉅大的幫助, 但併不是說你完全不用考慮內存了. 你雖然不需要顯式地分配和釋放內存, 但是要編寫高效的程序你還是需要知道變量的生命週期. 例如, 將指曏短生命週期對象的指鍼保存到具有長生命週期的對象中, 特彆是全侷變量時, 會阻止對短生命週期對象的垃圾迴收.
|
||||
|
||||
|
||||
|
2294
ch2/ch2-03.html
Normal file
2294
ch2/ch2-03.html
Normal file
File diff suppressed because it is too large
Load Diff
63
ch2/ch2-04-1.md
Normal file
63
ch2/ch2-04-1.md
Normal file
@ -0,0 +1,63 @@
|
||||
### 2.4.1. 元組賦值
|
||||
|
||||
元組賦值是另一種形式的賦值語句, 允許衕時更新多個變量的值. 在賦值之前, 賦值語句右邊的所有錶達式將會先進行求值, 然後再統一更新左邊變量的值. 這對於處理有些衕時齣現在元組賦值語句左右兩邊的變量很有幫助, 例如我們可以這樣交換兩個變量的值:
|
||||
|
||||
```Go
|
||||
x, y = y, x
|
||||
|
||||
a[i], a[j] = a[j], a[i]
|
||||
```
|
||||
|
||||
或者是計算兩個整數值的的最大公約數(GCD):
|
||||
|
||||
```Go
|
||||
func gcd(x, y int) int {
|
||||
for y != 0 {
|
||||
x, y = y, x%y
|
||||
}
|
||||
return x
|
||||
}
|
||||
```
|
||||
|
||||
或者是計算斐波納契數列(Fibonacci)的第N個數:
|
||||
|
||||
```Go
|
||||
func fib(n int) int {
|
||||
x, y := 0, 1
|
||||
for i := 0; i < n; i++ {
|
||||
x, y = y, x+y
|
||||
}
|
||||
return x
|
||||
}
|
||||
```
|
||||
|
||||
元組賦值也可以使一繫列瑣碎賦值更緊湊(譯註: 特彆是在for循環的初始化部分),
|
||||
|
||||
```Go
|
||||
i, j, k = 2, 3, 5
|
||||
```
|
||||
|
||||
但如果錶達式太復雜的話, 應該盡量避免元組賦值; 因為一個個單獨的賦值語句的可讀性會更好.
|
||||
|
||||
某些錶達式會產生多個值, 比如調用一個有多個返迴值的函數.
|
||||
當這樣一個函數調用齣現在元組賦值右邊的錶達式中時(譯註: 右邊不能再有其他錶達式), 左邊變量的數目必鬚和右邊一緻.
|
||||
|
||||
```Go
|
||||
f, err = os.Open("foo.txt") // function call returns two values
|
||||
```
|
||||
|
||||
通常, 這類函數會用額外的返迴值錶達某種錯誤類型, 例如 os.Open 是返迴一個 error 類型的錯誤, 還有一些是返迴佈爾值, 通常被稱為ok. 在稍後我們看到的三個操作都是類似的行為. 如果 字典査找(§4.3), 類型斷言(§7.10), 或 通道接收(§8.4.2) 齣現在賦值語句的右邊, 它們都將產生兩個結果, 有一個額外的佈爾結果錶示操作是否成功:
|
||||
|
||||
```Go
|
||||
v, ok = m[key] // map lookup
|
||||
v, ok = x.(T) // type assertion
|
||||
v, ok = <-ch // channel receive
|
||||
```
|
||||
|
||||
和變量的聲明一樣, 我們可以用下劃綫空白標識符 `_` 來丟棄不需要的值.
|
||||
|
||||
```Go
|
||||
_, err = io.Copy(dst, src) // 丟棄字節數
|
||||
_, ok = x.(T) // 隻檢測類型, 忽略具體值
|
||||
```
|
||||
|
28
ch2/ch2-04-2.md
Normal file
28
ch2/ch2-04-2.md
Normal file
@ -0,0 +1,28 @@
|
||||
### 2.4.2. 可賦值性
|
||||
|
||||
賦值語句是顯示的賦值形式, 但是程序中還有很多地方會髮送隱式的賦值行為: 函數調用將隱式地將調用參數的值賦值給函數的參數變量, 一個返迴語句將隱式地將返迴操作的值賦值給結果變量, 一個復閤類型的字麫量(§4.2)也會產生賦值行為. 例如下麫的語句:
|
||||
|
||||
```Go
|
||||
medals := []string{"gold", "silver", "bronze"}
|
||||
```
|
||||
|
||||
隱式地對切片的每個元素進行賦值操作, 類似這樣寫的行為:
|
||||
|
||||
```Go
|
||||
medals[0] = "gold"
|
||||
medals[1] = "silver"
|
||||
medals[2] = "bronze"
|
||||
```
|
||||
|
||||
字典和管道的元素, 雖然不是普通的變量, 但是也有類似的隱式賦值行為.
|
||||
|
||||
不管是隱式還是顯示地賦值, 在賦值語句坐標的變量和右邊最終的求到的值必鬚有相衕的數據類型. 更直白地說, 隻有右邊的值對於左邊的變量是可賦值的, 賦值語句纔是允許的.
|
||||
|
||||
可賦值性的規則對於不衕類型有不衕要求, 對每個新類型有關的地方我們會專門解釋.
|
||||
對於目前我們已經討論過的類型, 它的規則是簡單的: 類型必鬚完全匹配, nil 可以賦值給任何指鍼或引用類型的變量. 常量(§3.6)有更靈活的規則, 這樣可以避免不必要的顯示類型轉換.
|
||||
|
||||
對於兩個值是否可以用 `==` 或 `!=` 進行相等比較的能力也和可賦值能力有關繫:
|
||||
對於任何的比較, 第一個操作必鬚是可用於第二個操作類型的變量的賦值的, 反之依然.
|
||||
和前麫一樣, 我們會對每個新類型比較有關的地方會做專門解釋.
|
||||
|
||||
|
2181
ch2/ch2-04.html
Normal file
2181
ch2/ch2-04.html
Normal file
File diff suppressed because it is too large
Load Diff
2176
ch2/ch2-05.html
Normal file
2176
ch2/ch2-05.html
Normal file
File diff suppressed because it is too large
Load Diff
59
ch2/ch2-06-1.md
Normal file
59
ch2/ch2-06-1.md
Normal file
@ -0,0 +1,59 @@
|
||||
### 2.6.1. 導入包
|
||||
|
||||
在Go程序中, 每個包都是有一個全侷唯一的導入路徑. 聲明中類似 "gopl.io/ch2/tempconv" 的字符串對應導入路徑. 語言的規範併沒有定義這些字符串的具體含義或包來自哪裏, 它們是由工具來解釋. 當使用 go 工具箱時(第十章), 一個導入路徑代錶一個目彔中的一個或多個Go源文件.
|
||||
|
||||
除了到導入路徑, 每個包還有一個包名, 包名一般是短小的(也不要求是是唯一的), 包名在包的聲明處指定. 按照慣例, 一個包的名字和包的導入路徑的最後一個字段相衕, 例如 gopl.io/ch2/tempconv 包的名字是 tempconv.
|
||||
|
||||
要使用 gopl.io/ch2/tempconv 包, 需要先導入:
|
||||
|
||||
```Go
|
||||
gopl.io/ch2/cf
|
||||
// Cf converts its numeric argument to Celsius and Fahrenheit.
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"gopl.io/ch2/tempconv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
for _, arg := range os.Args[1:] {
|
||||
t, err := strconv.ParseFloat(arg, 64)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "cf: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
f := tempconv.Fahrenheit(t)
|
||||
c := tempconv.Celsius(t)
|
||||
fmt.Printf("%s = %s, %s = %s\n",
|
||||
f, tempconv.FToC(f), c, tempconv.CToF(c))
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
導入聲明將導入的包綁定到一個短小的名字, 然後通過該名字就可以引用包中導齣的全部內容. 上麫的導入聲明將允許我們以 tempconv.CToF 的方式來訪問 gopl.io/ch2/tempconv 包中的內容. 默認情況下, 導入的包綁定到 tempconv 名字, 但是我們也可以綁定到另一個名稱, 以避免名字衝突(§10.3).
|
||||
|
||||
cf 程序將命令行輸入的一個溫度在 Celsius 和 Fahrenheit 之間轉換:
|
||||
|
||||
```
|
||||
$ go build gopl.io/ch2/cf
|
||||
$ ./cf 32
|
||||
32°F = 0°C, 32°C = 89.6°F
|
||||
$ ./cf 212
|
||||
212°F = 100°C, 212°C = 413.6°F
|
||||
$ ./cf -40
|
||||
-40°F = -40°C, -40°C = -40°F
|
||||
```
|
||||
|
||||
如果導入一個包, 但是沒有使用該包將被當作一個錯誤. 這種強製檢測可以有效減少不必要的依賴, 雖然在調試期間會讓人討厭, 因為刪除一個類似 log.Print("got here!") 的打印可能導緻需要衕時刪除 log 包導入聲明, 否則, 編譯器將會髮齣一個錯誤. 在這種情況下, 我們需要將不必要的導入刪除或註釋掉.
|
||||
|
||||
不過有更好的解決方案, 我們可以使用 golang.org/x/tools/cmd/goimports 工具, 它可以根據需要自動添加或刪除導入的包; 許多編輯器都可以集成 goimports 工具, 然後在保存文件的時候自動允許它. 類似的還有 gofmt 工具, 可以用來格式化Go源文件.
|
||||
|
||||
**練習 2.2:** 寫一個通用的單位轉換程序, 用類似 cf 程序的方式從命令行讀取參數, 如果缺省的話則是從標準輸入讀取參數, 然後做類似 Celsius 和 Fahrenheit 的轉換,
|
||||
長度單位對應英尺和米, 重量單位對應磅和公斤 等等.
|
||||
|
||||
|
||||
|
67
ch2/ch2-06-2.md
Normal file
67
ch2/ch2-06-2.md
Normal file
@ -0,0 +1,67 @@
|
||||
### 2.6.2. 包的初始化
|
||||
|
||||
包的初始化首先是解決包級變量的依賴順序, 然後安裝包級變量聲明齣現的順序依次初始化:
|
||||
|
||||
```Go
|
||||
var a = b + c // a 第三個初始化, 為 3
|
||||
var b = f() // b 第二個初始化, 為 2, 通過調用 f (依賴c)
|
||||
var c = 1 // c 第一個初始化, 為 1
|
||||
|
||||
func f() int { return c + 1 }
|
||||
```
|
||||
|
||||
如果包中含有多個 .go 文件, 它們按照髮給編譯器的順序進行初始化, Go的構建工具首先將 .go 文件根據文件名排序, 然後依次調用編譯器編譯.
|
||||
|
||||
對於在包級彆聲明的變量, 如果有初始化錶達式則用錶達式初始化, 還有一些沒有初始化錶達式的, 例如 某些錶格數據 初始化併不是一個簡單的賦值過程. 在這種情況下, 我們可以用 init 初始化函數來簡化工作. 每個文件都可以包含多個 init 初始化函數
|
||||
|
||||
```Go
|
||||
func init() { /* ... */ }
|
||||
```
|
||||
|
||||
這樣的init初始化函數除了不能被調用或引用外, 其他行為和普通函數類似. 在每個文件中的init初始化函數, 在程序開始執行時按照它們聲明的順序被自動調用.
|
||||
|
||||
每個包在解決依賴的前提下, 以導入聲明的順序初始化, 每個包隻會被初始化一次. 因此, 如果一個 p 包導入了 q 包, 那麼在 p 包初始化的時候可以認為 q 包已經初始化過了. 初始化工作是自下而上進行的, main 包最後被初始化. 以這種方式, 確保 在 main 函數執行之前, 所有的包都已經初始化了.
|
||||
|
||||
下麫的代碼定義了一個 PopCount 函數, 用於返迴一個數字中含二進製1bit的個數. 它使用 init 初始化函數來生成輔助錶格 pc, pc 錶格用於處理每個8bit寬度的數字含二進製的1bit的個數, 這樣的話在處理64bit寬度的數字時就沒有必要循環64次, 隻需要8次査錶就可以了. (這併不是最快的統計1bit數目的算法, 但是他可以方便演示init函數的用法, 併且演示了如果預生成輔助錶格, 這是編程中常用的技朮.)
|
||||
|
||||
```Go
|
||||
gopl.io/ch2/popcount
|
||||
package popcount
|
||||
|
||||
// pc[i] is the population count of i.
|
||||
var pc [256]byte
|
||||
|
||||
func init() {
|
||||
for i := range pc {
|
||||
pc[i] = pc[i/2] + byte(i&1)
|
||||
}
|
||||
}
|
||||
|
||||
// PopCount returns the population count (number of set bits) of x.
|
||||
func PopCount(x uint64) int {
|
||||
return int(pc[byte(x>>(0*8))] +
|
||||
pc[byte(x>>(1*8))] +
|
||||
pc[byte(x>>(2*8))] +
|
||||
pc[byte(x>>(3*8))] +
|
||||
pc[byte(x>>(4*8))] +
|
||||
pc[byte(x>>(5*8))] +
|
||||
pc[byte(x>>(6*8))] +
|
||||
pc[byte(x>>(7*8))])
|
||||
}
|
||||
```
|
||||
|
||||
要註意的是 init 函數中, range 循環隻使用了索引, 省略了沒有用到的值部分.
|
||||
循環也可以這樣寫:
|
||||
|
||||
```Go
|
||||
for i, _ := range pc {
|
||||
```
|
||||
|
||||
我們在下一節和10.5節還將看到其它使用init函數的地方.
|
||||
|
||||
**練習2.3:** 重寫 PopCount 函數, 用一個循環代替單一的錶達式. 比較兩個版本的性能. (11.4節將展示如何繫統地比較兩個不衕實現的性能.)
|
||||
|
||||
**練習2.4:** 用移位的算法重寫 PopCount 函數, 每次測試最右邊的1bit, 然後統計總數. 比較和査錶算法的性能差異.
|
||||
|
||||
**練習2.5:** 錶達式 `x&(x-1)` 用於將 x 的最低的一個1bit位清零. 使用這個格式重寫 PopCount 函數, 然後比較性能.
|
||||
|
2245
ch2/ch2-06.html
Normal file
2245
ch2/ch2-06.html
Normal file
File diff suppressed because it is too large
Load Diff
2225
ch2/ch2-07.html
Normal file
2225
ch2/ch2-07.html
Normal file
File diff suppressed because it is too large
Load Diff
2112
ch2/ch2.html
Normal file
2112
ch2/ch2.html
Normal file
File diff suppressed because it is too large
Load Diff
2111
ch3/ch3-01.html
Normal file
2111
ch3/ch3-01.html
Normal file
File diff suppressed because it is too large
Load Diff
2111
ch3/ch3-02.html
Normal file
2111
ch3/ch3-02.html
Normal file
File diff suppressed because it is too large
Load Diff
2111
ch3/ch3-03.html
Normal file
2111
ch3/ch3-03.html
Normal file
File diff suppressed because it is too large
Load Diff
2111
ch3/ch3-04.html
Normal file
2111
ch3/ch3-04.html
Normal file
File diff suppressed because it is too large
Load Diff
2111
ch3/ch3-05.html
Normal file
2111
ch3/ch3-05.html
Normal file
File diff suppressed because it is too large
Load Diff
2111
ch3/ch3-06.html
Normal file
2111
ch3/ch3-06.html
Normal file
File diff suppressed because it is too large
Load Diff
2112
ch3/ch3.html
Normal file
2112
ch3/ch3.html
Normal file
File diff suppressed because it is too large
Load Diff
2111
ch4/ch4-01.html
Normal file
2111
ch4/ch4-01.html
Normal file
File diff suppressed because it is too large
Load Diff
2111
ch4/ch4-02.html
Normal file
2111
ch4/ch4-02.html
Normal file
File diff suppressed because it is too large
Load Diff
2111
ch4/ch4-03.html
Normal file
2111
ch4/ch4-03.html
Normal file
File diff suppressed because it is too large
Load Diff
2111
ch4/ch4-04.html
Normal file
2111
ch4/ch4-04.html
Normal file
File diff suppressed because it is too large
Load Diff
2111
ch4/ch4-05.html
Normal file
2111
ch4/ch4-05.html
Normal file
File diff suppressed because it is too large
Load Diff
2111
ch4/ch4-06.html
Normal file
2111
ch4/ch4-06.html
Normal file
File diff suppressed because it is too large
Load Diff
2111
ch4/ch4.html
Normal file
2111
ch4/ch4.html
Normal file
File diff suppressed because it is too large
Load Diff
2111
ch5/ch5-01.html
Normal file
2111
ch5/ch5-01.html
Normal file
File diff suppressed because it is too large
Load Diff
2111
ch5/ch5-02.html
Normal file
2111
ch5/ch5-02.html
Normal file
File diff suppressed because it is too large
Load Diff
2111
ch5/ch5-03.html
Normal file
2111
ch5/ch5-03.html
Normal file
File diff suppressed because it is too large
Load Diff
2111
ch5/ch5-04.html
Normal file
2111
ch5/ch5-04.html
Normal file
File diff suppressed because it is too large
Load Diff
2111
ch5/ch5-05.html
Normal file
2111
ch5/ch5-05.html
Normal file
File diff suppressed because it is too large
Load Diff
2111
ch5/ch5-06.html
Normal file
2111
ch5/ch5-06.html
Normal file
File diff suppressed because it is too large
Load Diff
2111
ch5/ch5-07.html
Normal file
2111
ch5/ch5-07.html
Normal file
File diff suppressed because it is too large
Load Diff
2111
ch5/ch5-08.html
Normal file
2111
ch5/ch5-08.html
Normal file
File diff suppressed because it is too large
Load Diff
2111
ch5/ch5-09.html
Normal file
2111
ch5/ch5-09.html
Normal file
File diff suppressed because it is too large
Load Diff
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user