goc 學習:原始碼部署和簡單使用

zyanycall發表於2020-06-23

首先,向專案推廣者@CarlJi 和專案的所有contributors致敬。
目前我GO語言使用接觸還很淺,遇到錯誤多多批評。

github(goc):[https://github.com/qiniu/goc]
github(simple-go-server):[https://github.com/CarlJi/simple-go-server]

背景鋪墊

我正在學習GO語言,遇到好的工具就簡單下載部署了一下,正好是程式碼覆蓋率測試所避不開的,同時還是七牛出品,特別想看看試試。
我發現看別人的原始碼,對GO語言的學習提高特別有效。

開發環境

GoLand

GoLand是收費的,但是如果你在github上有一年以上的開源專案,能申請到免費試用一年的註冊碼。
我也沒想著安裝goc,而是打算使用原始碼來直接除錯和使用。

開始

目前都是在goland的終端來執行,因為設定了GOPROXY

啟動:

切換到 simple-go-server的資料夾下,執行:

go run /Users/xxx/work/Git/goc/goc.go run .

當前版本1.0版本下,這裡必須設定 . 這個目錄,我本來以為可以寫絕對路徑的。

這是第一步,使用goc的命令指令,來執行啟動了 simple-go-server 的程式。
注意,這裡可能會報錯提示連線超時,需要配置go的代理:

GOPROXY=https://goproxy.cn,direct;GO111MODULE=on

控制檯會列印:

localhost:simple-go-server xxx$ go run /Users/xxx/work/Git/goc/goc.go run .
[goc] goc server started: http://127.0.0.1:50856
http: 2020/06/23 15:58:51 Simple go server
http: 2020/06/23 15:58:51 Version:
http: 2020/06/23 15:58:51 GitTag:
http: 2020/06/23 15:58:51 GitCommit:
http: 2020/06/23 15:58:51 GitTreeState:
http: 2020/06/23 15:58:51 Server is starting...
http: 2020/06/23 15:58:51 Server is ready to handle requests at :5000

goc 的 server 應該是goc 自己自帶的,也會預設設定成為center。埠號貌似是隨機的,應該可以設定。

接著:

go run /Users/xxx/work/Git/goc/goc.go profile --center="http://127.0.0.1:50856"

再執行:

go run /Users/xxx/work/Git/goc/goc.go profile --center="http://127.0.0.1:50856" > coverage.out && go tool cover -func=coverage.out

這樣就會輸出沒有改變的覆蓋率如:

enricofoltran/simple-go-server/main.go:30:      main            82.4%
enricofoltran/simple-go-server/main.go:90: index 12.5%
enricofoltran/simple-go-server/main.go:103: healthz 20.0%
enricofoltran/simple-go-server/main.go:113: logging 25.0%
enricofoltran/simple-go-server/main.go:128: tracing 25.0%
total: (statements) 54.0%

執行的就是程式碼的行號,還有方法的覆蓋率。

這時候可以訪問

http: 2020/06/23 15:58:51 Server is ready to handle requests at :5000

中得到的埠,訪問:http://127.0.0.1:5000/

再執行一次:

go run /Users/xxx/work/Git/goc/goc.go profile --center="http://127.0.0.1:50856" > coverage.out && go tool cover -func=coverage.out

得到改變的覆蓋率,可以看到百分比改變,很強大:

enricofoltran/simple-go-server/main.go:30:      main            85.3%
enricofoltran/simple-go-server/main.go:90: index 75.0%
enricofoltran/simple-go-server/main.go:103: healthz 20.0%
enricofoltran/simple-go-server/main.go:113: logging 87.5%
enricofoltran/simple-go-server/main.go:128: tracing 100.0%
total: (statements) 81.0%

後面又使用這一套測試了我自己的測試程式的覆蓋率:

xxxdeMacBook-Pro:test01 xxx$ pwd
/Users/xxx/work/Git/XxxClient/xxx/test01
xxxdeMacBook-Pro:test01 xxx$ go run /Users/xxx/work/Git/goc/goc.go run .

接著檢視覆蓋率:

xxxdeMacBook-Pro:test01 xxx$  go run /Users/xxx/work/Git/goc/goc.go profile --center="http://127.0.0.1:51447"
mode: count
XxxClient/xxx/test01/main.go:8.13,35.2 12 1
XxxClient/xxx/test01/main.go:49.24,51.2 1 0
XxxClient/xxx/test01/main.go:53.39,55.2 1 1
XxxClient/xxx/test01/main.go:57.31,58.17 1 0
XxxClient/xxx/test01/main.go:58.17,60.3 1 0
XxxClient/xxx/test01/main.go:60.8,62.3 1 0
XxxClient/xxx/test01/main.go:66.30,67.17 1 0
XxxClient/xxx/test01/main.go:67.17,71.3 3 0
XxxClient/xxx/test01/main.go:71.8,73.3 1 0
XxxClient/xxx/test01/main.go:83.46,85.17 1 1
XxxClient/xxx/test01/main.go:85.17,87.3 1 0
XxxClient/xxx/test01/main.go:88.2,91.37 3 1
XxxClient/xxx/test01/main.go:91.37,92.19 1 7
XxxClient/xxx/test01/main.go:92.19,97.4 3 7
XxxClient/xxx/test01/main.go:99.3,101.20 3 7
XxxClient/xxx/test01/main.go:103.2,103.15 1 1
xxxdeMacBook-Pro:test01 xxx$ go run /Users/xxx/work/Git/goc/goc.go profile --center="http://127.0.0.1:51447" > coverage.out && go tool cover -func=coverage.out
XxxClient/xxx/test01/main.go:8: main 100.0%
XxxClient/xxx/test01/main.go:49: add 0.0%
XxxClient/xxx/test01/main.go:53: setVal 100.0%
XxxClient/xxx/test01/main.go:57: print 0.0%
XxxClient/xxx/test01/main.go:66: recu 0.0%
XxxClient/xxx/test01/main.go:83: preorderTraversal 92.3%
total: (statements) 71.4%

coverage.out檔案解釋

比如,XxxClient/xxx/test01/main.go:8.13,35.2 12 1 的解析

  • 8.13:從main.go第8行的13列(12個字元結束)開始。
  • 35.2:到main.go第35行的第2列(1個字元結束)結束。
  • 12:一共12行程式碼。
  • 1:被執行覆蓋了1次。

其他引數

goc提供了很多其他的引數,如果要使用,可以試試:

go run /Users/xxx/work/Git/goc/goc.go list --center="http://127.0.0.1:50856"

等等。

GoLand除錯工具設定


這樣就可以除錯原始碼了。

再次感謝

這個工具其實挺有想象力了,很棒,點贊。
還有很多引數,比如diff我沒試過,應該也很強大,很棒。

相關文章