Go Fmt
go fmt
用于形成规范的代码格式,例如在variable
和operator
之间加入空格。
一、用法
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
一般用法:
gofmt -w /path/to/file
也可以使用:
go fmt /path/to/file
该命令相当于gofmt -w -l /path/to/file
。
二、Goland集成
Goland
内置了gofmt
的功能,点击Tools->Go Tools->Go Format Project
即可对整个项目应用go fmt
。如果想设置文件编辑后自动执行go fmt
,可以参考,添加配置时使用默认的设置即可。
Last updated
Was this helpful?