如下命令基於go版本1.15.3進行測試

基本命令

go help
go env
go version

交叉編譯

# Windows下編譯Linux

SET CGO_ENABLED=0
SET GOARCH=amd64
SET GOOS=linux

go build xx.go

編譯參數

go buildlinux

  • 當編譯包時,會自動忽略'_test.go'的測試文件。
# go build -o 指定編譯輸出的名稱 
# win下編譯生成指定名稱的可執行文件
go build -o custom.exe send.go

gofmt 和 go fmt

go fmt 是 gofmt的簡易封裝測試

go fmt 即 gofmt -l -w ui

傳入文件的話,會格式化這個文件。傳入目錄的話,會格式化該目錄下的全部.go文件this

// 經常使用go fmt
go fmt send.go
// gofmt 參數
usage: gofmt [flags] [path ...]
  -cpuprofile string
        write cpu profile to this file
  -d    display diffs instead of rewriting files
  -e    report all errors (not just the first 10 on different lines)
  -l    list files whose formatting differs from gofmt's
  -r string
        rewrite rule (e.g., 'a[b:len(a)] -> a[b:]')
  -s    simplify code
  -w    write result to (source) file instead of stdout
// go fmt 參數 
usage: go fmt [-n] [-x] [packages]

Fmt runs the command 'gofmt -l -w' on the packages named
by the import paths. It prints the names of the files that are modified.

For more about gofmt, see 'go doc cmd/gofmt'.
For more about specifying packages, see 'go help packages'.

The -n flag prints commands that would be executed.
The -x flag prints commands as they are executed.

To run gofmt with specific options, run gofmt itself.

See also: go fix, go vet.