《Go in Action》第三章讀書筆記

本文為《Go in Action》的第三章讀書筆記闲孤。
第二章主要是介紹package和工具相關(guān)的事情。

package

翻譯一下就是包

Q: 什么是package?有什么用套啤?

A: 原文:

All Go programs are organized into groups of files called packages, so that code has the ability to be included into other projects as smaller reusable pieces.

package就是a group of file骗爆。也就是一組文件碉哑。其作用就是package作為一個可重用的代碼片段,被其他項目所使用鸵熟。

Q: package如何定義勉吻?

A: 幾點:

  • 每個文件的第一行使用pakage <name>定義package。這里的第一行指的是除了空行和注釋的第一行旅赢。也就是說package那一行的上面還可以有注釋或者空行齿桃,但是不能有其他代碼。
  • 同一個文件夾下面不能有多個package煮盼。
  • 同一個package不能跨多個文件夾短纵。
  • 一個文件夾下面的所有文件必須屬于同一個package。
  • 通用做法是:文件夾的名字就是package的名字
  • package的名字應(yīng)當是簡潔的小寫字母
  • 在代碼中調(diào)用pacakge里面的東西時僵控,默認使用pakage的名字香到,不過也可以覆蓋。當你引入多個package报破,其名字是一樣的時候悠就,就需要覆蓋了。

Q: package main特殊性充易?

A: 如下:

  • 告訴go的命令梗脾,需要將文件編譯成為可執(zhí)行文件
  • package main里面必須要有main函數(shù)

Q: go的hello world怎么寫?

A: 如下:

  • 首先在$GOPATH/src目錄下盹靴,建立hello文件夾
  • 創(chuàng)建hello.go文件
  • 寫入如下內(nèi)容:
package main

import "fmt"

func main() {
  fmt.Println("Hello World!")
}
  • 在hello文件夾下運行go build炸茧,會生成一個hello的可執(zhí)行文件
  • 運行該可執(zhí)行文件瑞妇,輸出Hello World

Q: pacakge如何引入?

A: 簡單來說梭冠,就是使用import "<path/name>"這個語法辕狰。
單行:

import "fmt"

多行:

import (
  "fmt"
  "strings"
)

如下:

  • import尋找的路徑是go環(huán)境使用目錄的相對路徑
  • 標準庫里面的import路徑是在go的安裝路徑下,比如可能是/usr/local/go
  • 其余的庫的import路徑是在GOPATH下
  • 首先會從go的安裝路徑下去找

兩個特殊點的import:
顯示聲明名字:

import (
  fmt
  myfmt from "mylib/fmt"
} 

使用下劃線(稱為blank identifier):

import (
  fmt
  _ from "mylib/nothing"
)

remote imports

當import的內(nèi)容是比如“github.com/spf13/viper”的時候控漠,可以使用go get命令獲取遠程包到對應(yīng)的目錄下蔓倍,以讓本地可以用。
go get還是recursive的盐捷,即可以下載包及其依賴偶翅。

init函數(shù)

原文:

Each package has the ability to provide as many init functions as necessary to be invoked at the beginning of execution time. All the init functions that are discovered by the compiler are scheduled to be executed prior to the main function being executed.
When a program imports this package, the init function will be called

每個包可以有多個init函數(shù)(沒明白...)。每個init函數(shù)都會在main函數(shù)被調(diào)用前執(zhí)行毙驯。具體執(zhí)行時間是在包被import的時候。

使用go tools

go的幾個命名:

go #打印g的幫助信息
go build hello.go  #編譯hello.go灾测,生成可執(zhí)行文件
go clean hello.go  #清理生成的文件
go build github.com/goinaction/code/chapter3/wordcount
go build github.com/goinaction/code/chapter3/...

go build wordcount.go 
go build .

go run wordcount.go

go vet main.go #檢查代碼錯誤
go format main.go

go doc tar
godoc -http=:6060 #

給package加注釋爆价,注釋會展示在go doc中:

/*

Package usb provides types and functions for working with USB devices. To connect to a USB device start by creating a new USB connection with NewConnection ...

*/ 
package usb

分享代碼

幾條:

  • 代碼可以上傳到github,在github的repository的根目錄直接創(chuàng)建package媳搪。不要建什么code或者src铭段,這樣import的時候路徑會比較長
  • package可以是很小的,實現(xiàn)簡單的功能秦爆,只有些許代碼
  • 使用go fmt格式化代碼
  • 給代碼加注釋

依賴管理

簡單來說就是管理go的依賴包

什么是可重復(fù)制作(reproducible)序愚?

簡單來說,就是在我的電腦上編寫的代碼等限,在你的電腦上依然是同樣的東西爸吮。為什么不一樣呢?比如我們的代碼存放位置不同望门,引用的import路徑不同形娇,引用的庫的版本不同等。

vendoring

原文:

Community tools such as godep and vendor have solved the dependency problem by using a technique called vendoring and import path rewriting. The idea is to copy all the dependencies into a directory inside the project repo, and then rewrite any import paths that reference those dependencies by providing the location inside the project itself.

簡單翻譯:社區(qū)的一些如godep和vendor的工具在解決依賴問題的時候筹误,使用了一種稱為vendoring和重寫import路徑的技術(shù)桐早。大意為將所有的依賴拷貝到項目中的一個目錄下,然后重寫所有的import路徑厨剪,指向項目內(nèi)部的那些拷貝過來的包哄酝。

這樣項目使用到的所有代碼都在項目目錄下了,直接zip一下就包含了源文件和引用到的其他庫文件了祷膳。

在vendor前:

package main

import (
  "bitbucket.org/ww/goautoneg"
  "github.com/beorn7/perks"
)

vendor之后:

package main

import (
"github.ardanstudios.com/myproject/Godeps/_workspace/src/ bitbucket.org/ww/goautoneg"
"github.ardanstudios.com/myproject/Godeps/_workspace/src/ github.com/beorn7/perks"
)

gb

原文:

Gb is a whole new class of build tool being developed by members of the Go community. Gb takes a different approach to solving the reproducible-build problem, which starts with the understanding that wrapping the Go tooling is not an option.

gb代表了一類新的構(gòu)建工具陶衅,其表示對go tooling進行又一層封裝,并不是解決reproducible問題的一種可選項直晨。

The philosophy behind gb stems from the idea that Go doesn’t have reproducible builds because of the import statement. The import statement drives go get, but import doesn’t contain sufficient information to identify which revision of a package should be fetched any time go get is called. The possibility that go get can fetch a different version of code for any given package at any time makes supporting the Go tooling in any reproducible solution complicated and tedious at best.

gb認為go無法保證reproducible builds的原因在于import万哪。因為go get從import獲取信息侠驯,但是import并沒有表示包版本的信息,所以go get也不知道獲取哪個特定版本的庫奕巍,很有可能獲取了一個不同版本的依賴庫吟策。

A gb project is simply a directory on disk that contains a subdirectory named src/. The symbol $PROJECT refers to the root directory on disk where the src/ directory is located and is only used as a shortcut for describing the location on disk for the project.

一個gb的項目簡單來說就是一個具有src子目錄的目錄,也就是一個目錄下面有一個叫src的子目錄的止。$PROJECT指向了該目錄的路徑檩坚。

Gb projects differentiate between the code you write and the code your code depends on. The code your code depends on is called vendored code. A gb project makes a clear distinction between your code and vendored code.

Gb規(guī)定用戶編寫的代碼叫code,其依賴的代碼叫vendored code诅福。其存放路徑為:

$PROJECT/src
$PROJECT/vendor/src

gb不需要改動源碼里面的import的路徑匾委。

The gb tool will look inside the PROJECT/vendor/src/ directory for these imports if they can’t be located inside thePROJECT/src/ directory first. The entire source code for the project is located within a single repo and directory on disk, split between the src/ and vendor/src/ subdirectories.

gb工具會去PROJECT/vendor/src去尋找import的庫。所有的代碼都放在了PROJECT目錄下氓润。

這些目錄結(jié)構(gòu)的約定都是gb自己定的赂乐,go工具不認識,因此不能使用go工具咖气,需要使用gb這個工具挨措。比如編譯一個項目:

gb build all
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市崩溪,隨后出現(xiàn)的幾起案子浅役,更是在濱河造成了極大的恐慌,老刑警劉巖伶唯,帶你破解...
    沈念sama閱讀 216,470評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件觉既,死亡現(xiàn)場離奇詭異,居然都是意外死亡瞪讼,警方通過查閱死者的電腦和手機粹断,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,393評論 3 392
  • 文/潘曉璐 我一進店門姿染,熙熙樓的掌柜王于貴愁眉苦臉地迎上來悬赏,“玉大人,你說我怎么就攤上這事盾戴”啵” “怎么了?”我有些...
    開封第一講書人閱讀 162,577評論 0 353
  • 文/不壞的土叔 我叫張陵盆顾,是天一觀的道長您宪。 經(jīng)常有香客問我奠涌,道長,這世上最難降的妖魔是什么溜畅? 我笑而不...
    開封第一講書人閱讀 58,176評論 1 292
  • 正文 為了忘掉前任慈格,我火速辦了婚禮,結(jié)果婚禮上峦椰,老公的妹妹穿的比我還像新娘汰规。我一直安慰自己,他們只是感情好滔金,可當我...
    茶點故事閱讀 67,189評論 6 388
  • 文/花漫 我一把揭開白布茂嗓。 她就那樣靜靜地躺著述吸,像睡著了一般。 火紅的嫁衣襯著肌膚如雪道批。 梳的紋絲不亂的頭發(fā)上隆豹,一...
    開封第一講書人閱讀 51,155評論 1 299
  • 那天璃赡,我揣著相機與錄音,去河邊找鬼塌计。 笑死夺荒,一個胖子當著我的面吹牛良蒸,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播剿吻,決...
    沈念sama閱讀 40,041評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼串纺!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起纺棺,我...
    開封第一講書人閱讀 38,903評論 0 274
  • 序言:老撾萬榮一對情侶失蹤榄笙,失蹤者是張志新(化名)和其女友劉穎祷蝌,沒想到半個月后茅撞,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,319評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡米丘,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,539評論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了糊啡。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片拄查。...
    茶點故事閱讀 39,703評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖棚蓄,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情梭依,我是刑警寧澤睛挚,帶...
    沈念sama閱讀 35,417評論 5 343
  • 正文 年R本政府宣布扎狱,位于F島的核電站,受9級特大地震影響匠抗,放射性物質(zhì)發(fā)生泄漏故源。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,013評論 3 325
  • 文/蒙蒙 一矢腻、第九天 我趴在偏房一處隱蔽的房頂上張望门驾。 院中可真熱鬧,春花似錦多柑、人聲如沸奶是。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,664評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽聂沙。三九已至,卻和暖如春初嘹,著一層夾襖步出監(jiān)牢的瞬間及汉,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,818評論 1 269
  • 我被黑心中介騙來泰國打工屯烦, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留坷随,地道東北人。 一個月前我還...
    沈念sama閱讀 47,711評論 2 368
  • 正文 我出身青樓漫贞,卻偏偏與公主長得像甸箱,于是被迫代替她去往敵國和親育叁。 傳聞我的和親對象是個殘疾皇子迅脐,可洞房花燭夜當晚...
    茶點故事閱讀 44,601評論 2 353

推薦閱讀更多精彩內(nèi)容