不知不覺用 Go 開發也兩年多了. 籌備點經驗匯總, 方便后面的同學能快速上手.
提綱
1. Go 安裝 2. Go ide 搭建 3. Go modules 模塊管理 4. Go unit test 5. Go debug 除錯 6. Go pprof 火焰圖 7. Go online 除錯 8. Go future 思考 1. Go 安裝
Golang 官網 https://golang.org/ Download 頁面 https://golang.org/dl/ Install 頁面 https://golang.org/doc/install
登錄 Golang 官網. 如果訪問有問題, 請激發這輩子最大潛力去深入突破此間魔障.
別問 問 就是不合適
別多想 想 就是沒緣分
然后[2019/10/21]點到下載頁面, 這里下載的是 go1.12.12.linux-amd64.tar.gz隨后按照安裝頁面提示操作. 這里簡單梳理以下腳本
sudo rm -rf /usr/local/gosudo tar -C /usr/local -xzf go1.12.12.linux-amd64.tar.gzsudo vi /etc/profile Shift + G i export PATH=$PATH:/usr/local/go/bin ~ wq!source /etc/profile 通過 go version 查看安裝版本
原本是想通過 window 平臺帶大家演示一遍提綱中內容. 不過經常待在 ubuntu 旁邊, 只能順手用 linux 環境給同學演示一遍. 有心人可自行選擇環境實操, 思路都差不多.2. Go ide 搭建
Download VSCode https://code.visualstudio.com/
這里推薦 VSCode 搭建 Golang 開發換機, 下載 deb 包并安裝.
sudo dpkg -i code_1.39.2-1571154070_amd64.deb籌備下面環境
點擊 [Install] -End-> [F1] -> [Go: Install/Update Tools] -> [全選] -> [確定]
大概齊都成功后, 對著專案來下 [F5] 跑起來
別驚訝, 咱們的 IDE 已經 OK 了!3. Go modules 模塊管理
Go modules 是 Go 解決包依賴管理方面特別棒的一次嘗試. 這里帶大家簡單先用起來.有心的同學可以查詢更多的相關資料, 或者通過 go mod help 自行晉升. 開始準備素材
code/arithmetic/div.go
package arithmeticimport "errors"// Div 除法操作func Div(a, b int) (c int, err error) { if b == 0 { err = errors.New("divisor is 0") return } c = a / b return} code/main.gopackage mainimport ( "fmt" "code/arithmetic")func main() { println("Hello, 世界") c, err := arithmetic.Div(1, 0) fmt.Printf("c = %d, err = %+v\n", c, err)}
有了上面這些, 開始著手構建go mod init codego build./code
越純粹越懂自己4. Go unit test
code/arithmetic/div_test.go
package arithmeticimport ( "testing" "math/rand")func TestDiv(t *testing.T) { var ( a, b, c int err error ) a, b = 1, 2 c, err = Div(a, b) t.Logf("a = %d, b = %d, c = %d, err = %+v", a, b, c, err) a, b = 1, 0 c, err = Div(a, b) t.Logf("a = %d, b = %d, c = %d, err = %+v", a, b, c, err)}func BenchmarkDiv(b *testing.B) { for i := 0; i < b.N; i++ { Div(rand.Int(), rand.Int()) }}Go 中單元測驗好簡單, 特殊一點要求就是命名, 總結起來有下面幾小點 1. 檔案命必須是 *_test.go 2. 測驗函式 func Test*(t *testing.T) { ... } 3. 性能測驗函式 func Benchmark*(b *testing.B) { for i := 0; i < b.N; i++ { ... } }
有測驗比沒有要好很多 /(ㄒoㄒ)/~~5. Go debug 除錯
別深究 深究 就是 F5 F10 F11 go -race

6. Go pprof 火焰圖
籌備素材
package mainimport ( "fmt" "log" "time" "net/http" _ "net/http/pprof" "code/arithmetic")func main() { println("Hello, 世界") c, err := arithmetic.Div(1, 0) fmt.Printf("c = %d, err = %+v\n", c, err) for i := 0; i < 1000000; i++ { go func() { time.Sleep(time.Second * 10) }() } http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {}) err = http.ListenAndServe(":8088", nil) if err != nil { log.Fatalf("ListenAndServe: %+v\n", err) }}啟動服務, 查看程式運行狀態 go pprof debug http://127.0.0.1:8088/debug/pprof/

更加詳細的可以一塊查看一下 cpu 運行火焰圖
go tool pprof http://127.0.0.1:8088/debug/pprof/profile -seconds 10隨后會生成一個 profile 相關檔案, 用工具打開 pprof.code.samples.cpu.001.pb.gzgo tool pprof -http=:8081 /home/zhi/pprof/pprof.code.samples.cpu.001.pb.gz
全有了, 是否如虎添翼不知道, 但知道, 你應對問題的方式方法會多一些.7. Go online 除錯
delve https://github.com/go-delve/delve
拋開看日志, 也可以在服務摘除后, 通過 delve 進行 b r n c ...
佛度有緣人8. Go future 思考
Go 許多理念都很普通, 實作方面也很取舍, 沒想到最后居然如此的實在, 開發工程杠杠的. 個人覺得其一大亮點是為實際生產的工程師服務, 其次為機器服務, 合理的解決二者的痛點.
關于 Go 未來的思考, 很期待泛型模式的到來.
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/69336.html
標籤:Go
上一篇:java呼叫動態庫例外 UnsatisfiedLinkError.
下一篇:GO基礎之變數的使用
