1.介紹
Golang的開發團隊制定了統一的官方代碼風格,並且推出了gofmt工具(gofmt或go fmt)來幫助開發者格式化他們的代碼到統一的風格。gofmt是一個cli程序,會優先讀取標准輸入,如果傳入了文件路徑的話,會格式化這個文件,如果傳入一個目錄,會格式化目錄中所有.go文件,如果不傳參數,會格式化當前目錄下的所有.go文件。
gofmt默認不對代碼進行簡化,使用-s參數可以開啟簡化代碼功能,具體來說會進行如下的轉換:
2.使用
(base) $ go help 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.
$ go fmt xxx.go