--- titile: Go入門 tags: Go --- # GO 入門 --- # [版本管理工具 g](https://github.com/voidint/g) ## go stable版本 ``` g ls-remote stable ``` ## 切換版本 ``` g use ``` ## 已安裝版本 ``` g ls ``` # # 單元測試(Unit Test) ## 程式範例(加法) ### 資料結構   ### 代碼 > 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 ```  #### 覆蓋測試 ``` go test -v ./calculate -cover ```  ``` go test -coverprofile=coverage.out . ```  ``` go tool cover -html=coverage.out ```  #### 效能測試 ``` go test -v ./calculate -bench="." ``` 
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up