build compile packages and dependencies
最常用的go command之一,编译go文件
跨平台编译: env GOOS=linux GOARCH=amd64 go build
install
install compile and install packages and dependencies
也是编译,与build最大的区别是编译后会将输出文件打包成库放在pkg下
常用于本地打包编译的命令: go install
get
get download and install packages and dependencies
用于获取go的第三方包,通常会默认从git repo上pull最新的版本
常用命令如: go get -u github.com/go-sql-driver/mysql (从github上获取mysql的driver并安装到本地)
fmt
fmt gofmt (reformat) package sources
类似于C中的lint, 统一代码风格和排版
常用命令如: go fmt
test
test test packages
运行当前包目录下的tests
常用命令如: go test 或go test -v等
golang的test
main.go
main_test.go
Go的test一般以xxx_test.go为文件名
xxx的部分一般为xxx_test.go所要测试的代码文件名
Go并没有特别要求xxx的部分必须是要测试的文件名
go test -v
package main
import (
"testing"
"fmt"
)
func TestPrint(t *testing.T){
res := Print1to20()
fmt.Println("hey")
if res != 210{
t.Errorf("Wrong result of Print1to20")
}
}