Format Go source with gofmt
Enforce consistent Go code layout on Red Hat Enterprise Linux by using gofmt, the formatting tool in Go Toolset. The gofmt utility applies standard Go formatting rules so teams can share code without maintaining separate style guides.
Prerequisites Copy linkLink copied!
- Go Toolset is installed.
Format code Copy linkLink copied!
You can use the gofmt formatting tool to format code in a given path. When the path leads to a single file, the changes apply only to the file. When the path leads to a directory, all .go files in the directory are processed.
Procedure Copy linkLink copied!
$ gofmt -w <code_path>
Replace <code_path> with the path to the code you want to format.
To print the formatted code to standard output instead of writing it to the original file, omit the -w option.
Preview changes to code Copy linkLink copied!
You can use the gofmt formatting tool to preview changes done by formatting code in a given path. The output in unified diff format is printed to standard output.
Procedure Copy linkLink copied!
$ gofmt -d <code_path>
Replace <code_path> with the path to the code you want to compare.
Simplify code Copy linkLink copied!
You can use the gofmt formatting tool to simplify your code.
Procedure Copy linkLink copied!
Refactor code Copy linkLink copied!
You can use the gofmt formatting tool to refactor your code by applying arbitrary substitutions.