此内容没有您所选择的语言版本。

Chapter 3. gofmt


gofmt is a code formatting tool for the Go programming language, packaged together with the go compiler.

Go Toolset is distributed with gofmt 1.10.3.

3.1. Installing gofmt

In Go Toolset, gofmt is provided by the go-toolset-1.10-golang package and is automatically installed with the go-toolset-1.10 package. See Section 1.4, “Installing Go Toolset”.

3.2. Formatting Code

To format all code in the code_path path, run the gofmt tool as follows:

Copy to Clipboard Toggle word wrap
$ scl enable go-toolset-1.10 'gofmt -w code_path'

This command will directly change code in the code_path path. When code_path is a single file, the changes apply only to the file. When code_path is a directory, all .go files in the directory are processed.

When the code_path is omitted, gofmt reads standard input instead.

To print the formatted code to standard output instead of writing to the original file, omit the -w option.

It is possible to invoke gofmt through the go compiler with the fmt command. To achieve the same results as the command above, run:

Copy to Clipboard Toggle word wrap
$ scl enable go-toolset-1.10 'go fmt -l -w -s code_path'

3.3. Previewing Changes to Code

To preview changes done by formatting code in a given path code_path, run the gofmt tool with the -d option as follows:

Copy to Clipboard Toggle word wrap
$ scl enable go-toolset-1.10 'gofmt -d code_path'

The output in unified diff format is printed to standard output.

It is possible to combine both the -d and -w options.

3.4. Simplifying Code

To simplify code in a given path code_path, run the gofmt tool with the -s option as follows:

Copy to Clipboard Toggle word wrap
$ scl enable go-toolset-1.10 'gofmt -s code_path'

The code under code_path is simplified. Use the -d option to show the differences, and use the -w option to apply the changes to the code.

3.5. Refactoring code

The gofmt tool can be used to refactor code by applying arbitrary substitutions. To refactor code in a given path code_path according to a rule rewrite_rule, run the gofmt tool with the -r option as follows:

Copy to Clipboard Toggle word wrap
$ scl enable go-toolset-1.10 'gofmt -r rewrite_rule code_path'

The code under code_path is refactored according to the rule rewrite_rule. Use the -d option to show the differences, and use the -w option to apply the changes to the code. The additional options must be placed after the rule rewrite_rule:

Copy to Clipboard Toggle word wrap
$ scl enable go-toolset-1.10 'gofmt -r rewrite_rule -d code_path'

Detailed description of the rewrite rules is beyond the scope of this book. For more information, see the resources listed in Section 3.6, “Additional Resources”.

3.6. Additional Resources

A detailed description of the gofmt tool and its features is beyond the scope of this book. For more information, see the resources listed below.

Online Documentation

See Also

返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat, Inc.