Linux command
go-fmt 命令
文件
复制后可按需替换文件名、目录或参数。
常用示例
Format a Go file
go fmt [file.go]
Format all files in current package
go fmt
Format all packages recursively
go fmt ./...
Show files that would be changed
gofmt -l [file.go]
Show diff of changes
gofmt -d [file.go]
Simplify code
gofmt -s [file.go]
说明
go fmt formats Go source code according to standard Go style conventions. It is an alias for gofmt -l -w on the packages named by the import paths. The gofmt tool can also simplify code with -s and apply rewrite rules with -r for refactoring.
参数
- -n
- Print commands that would be executed.
- -x
- Print commands as they are executed.
- -l
- List files whose formatting differs (gofmt).
- -w
- Write result to source file (gofmt).
- -d
- Display diffs instead of rewriting (gofmt).
- -s
- Simplify code (gofmt).
- -r _rule_
- Apply rewrite rule before formatting (gofmt).
FAQ
What is the go-fmt command used for?
go fmt formats Go source code according to standard Go style conventions. It is an alias for gofmt -l -w on the packages named by the import paths. The gofmt tool can also simplify code with -s and apply rewrite rules with -r for refactoring.
How do I run a basic go-fmt example?
Run `go fmt [file.go]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -n do in go-fmt?
Print commands that would be executed.