Este contenido no está disponible en el idioma seleccionado.
Chapter 3. The gofmt formatting tool
Instead of a style guide, the Go programming language uses the gofmt code formatting tool. gofmt automatically formats your code according to the Go layout rules.
3.1. Prerequisites Copiar enlaceEnlace copiado en el portapapeles!
-
Go Toolset is installed.
For more information, see Installing Go Toolset.
3.2. Formatting code Copiar enlaceEnlace copiado en el portapapeles!
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
To format your code in a given path, run:
On Red Hat Enterprise Linux 7:
scl enable go-toolset-1.18 'gofmt -w <code_path>'
$ scl enable go-toolset-1.18 'gofmt -w <code_path>'Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Replace
<code_path>with the path to the code you want to format.
-
Replace
On Red Hat Enterprise Linux 8:
gofmt -w <code_path>
$ gofmt -w <code_path>Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Replace
<code_path>with the path to the code you want to format.
-
Replace
On Red Hat Enterprise Linux 9:
gofmt -w <code_path>
$ gofmt -w <code_path>Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Replace
<code_path>with the path to the code you want to format.
-
Replace
To print the formatted code to standard output instead of writing it to the original file, omit the -w option.
3.3. Previewing changes to code Copiar enlaceEnlace copiado en el portapapeles!
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
To show differences in your code in a given path, run:
On Red Hat Enterprise Linux 7:
scl enable go-toolset-1.18 'gofmt -d <code_path>'
$ scl enable go-toolset-1.18 'gofmt -d <code_path>'Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Replace
<code_path>with the path to the code you want to compare.
-
Replace
On Red Hat Enterprise Linux 8:
gofmt -d <code_path>
$ gofmt -d <code_path>Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Replace
<code_path>with the path to the code you want to compare.
-
Replace
On Red Hat Enterprise Linux 9:
gofmt -d <code_path>
$ gofmt -d <code_path>Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Replace
<code_path>with the path to the code you want to compare.
-
Replace
3.4. Simplifying code Copiar enlaceEnlace copiado en el portapapeles!
You can use the gofmt formatting tool to simplify your code.
Procedure
To simplify code in a given path, run:
On Red Hat Enterprise Linux 7:
scl enable go-toolset-1.18 'gofmt -s <code_path>'
$ scl enable go-toolset-1.18 'gofmt -s <code_path>'Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Replace
<code_path>with the path to the code you want to simplify.
-
Replace
On Red Hat Enterprise Linux 8:
gofmt -s <code_path>
$ gofmt -s <code_path>Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Replace
<code_path>with the path to the code you want to simplify.
-
Replace
On Red Hat Enterprise Linux 9:
gofmt -s <code_path>
$ gofmt -s <code_path>Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Replace
<code_path>with the path to the code you want to simplify.
-
Replace
To apply the changes, run:
On Red Hat Enterprise Linux 7:
scl enable go-toolset-1.18 'gofmt -w <code_path>'
$ scl enable go-toolset-1.18 'gofmt -w <code_path>'Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Replace
<code_path>with the path to the code you want to format.
-
Replace
On Red Hat Enterprise Linux 8:
gofmt -w <code_path>
$ gofmt -w <code_path>Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Replace
<code_path>with the path to the code you want to format.
-
Replace
On Red Hat Enterprise Linux 9:
gofmt -w <code_path>
$ gofmt -w <code_path>Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Replace
<code_path>with the path to the code you want to format.
-
Replace
3.5. Refactoring code Copiar enlaceEnlace copiado en el portapapeles!
You can use the gofmt formatting tool to refactor your code by applying arbitrary substitutions.
Procedure
To refactor your code in a given path, run:
On Red Hat Enterprise Linux 7:
scl enable go-toolset-1.18 'gofmt -r <rewrite_rule> <code_path>'
$ scl enable go-toolset-1.18 'gofmt -r <rewrite_rule> <code_path>'Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Replace
<code_path>with the path to the code you want to refactor and<rewrite_rule>with the rule you want it to be rewritten by.
-
Replace
On Red Hat Enterprise Linux 8:
gofmt -r <rewrite_rule> <code_path>
$ gofmt -r <rewrite_rule> <code_path>Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Replace
<code_path>with the path to the code you want to refactor and<rewrite_rule>with the rule you want it to be rewritten by.
-
Replace
On Red Hat Enterprise Linux 9:
gofmt -r <rewrite_rule> <code_path>
$ gofmt -r <rewrite_rule> <code_path>Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Replace
<code_path>with the path to the code you want to refactor and<rewrite_rule>with the rule you want it to be rewritten by.
-
Replace
To apply the changes, run:
On Red Hat Enterprise Linux 7:
scl enable go-toolset-1.18 'gofmt -w <code_path>'
$ scl enable go-toolset-1.18 'gofmt -w <code_path>'Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Replace
<code_path>with the path to the code you want to format.
-
Replace
On Red Hat Enterprise Linux 8:
gofmt -w <code_path>
$ gofmt -w <code_path>Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Replace
<code_path>with the path to the code you want to format.
-
Replace
On Red Hat Enterprise Linux 9:
gofmt -w <code_path>
$ gofmt -w <code_path>Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Replace
<code_path>with the path to the code you want to format.
-
Replace