--- titile: Go入門 tags: Go --- # GO 入門 --- # [版本管理工具 g](https://github.com/voidint/g) ## go stable版本 ``` g ls-remote stable ``` ## 切換版本 ``` g use ``` ## 已安裝版本 ``` g ls ``` # # 單元測試(Unit Test) ## 程式範例(加法) ### 資料結構 ![](https://i.imgur.com/YZyff5c.png) ![](https://i.imgur.com/KoHGvhr.png) ### 代碼 > calculate/add.go ``` package calculate func Add(a, b int) int { return a + b } ``` > calculate/add_test.go ``` package calculate import "testing" func TestAdd(t *testing.T) { if Add(1, 2) != 3 { t.Errorf("1+2=%d wrong number", Add(1, 2)) } } ``` > main.go ``` package main import ( "0216/calculate" "fmt" ) func main() { fmt.Println(calculate.Add(1, 2)) } ``` ### 測試指令 #### 功能測試 ``` go test -v ./calculate ``` ![](https://i.imgur.com/RTHlz5q.png) #### 覆蓋測試 ``` go test -v ./calculate -cover ``` ![](https://i.imgur.com/slWxvF9.png) ``` go test -coverprofile=coverage.out . ``` ![image](https://hackmd.io/_uploads/S1h-6Atfye.png) ``` go tool cover -html=coverage.out ``` ![image](https://hackmd.io/_uploads/ByDXpCYMkg.png) #### 效能測試 ``` go test -v ./calculate -bench="." ``` ![](https://i.imgur.com/CwQvIcZ.png)