Este contenido no está disponible en el idioma seleccionado.

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.11.5.

3.1. Installing gofmt

In Go Toolset, gofmt is provided by the go-toolset-1.11-golang package and is automatically installed with the go-toolset-1.11 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:

  • For Red Hat Enterprise Linux 7:

    $ scl enable go-toolset-1.11 'gofmt -w code_path'
    Copy to Clipboard Toggle word wrap
  • For Red Hat Enterprise Linux 8:

    $ gofmt -w code_path
    Copy to Clipboard Toggle word wrap

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:

  • For Red Hat Enterprise Linux 7:

    $ scl enable go-toolset-1.11 'gofmt -l -w -s code_path'
    Copy to Clipboard Toggle word wrap
  • For Red Hat Enterprise Linux 8:

    $ gofmt -l -w -s code_path
    Copy to Clipboard Toggle word wrap

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:

  • For Red Hat Enterprise Linux 7:

    $ scl enable go-toolset-1.11 'gofmt -d code_path'
    Copy to Clipboard Toggle word wrap
  • For Red Hat Enterprise Linux 8:

    $ gofmt -d code_path
    Copy to Clipboard Toggle word wrap

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:

  • For Red Hat Enterprise Linux 7:

    $ scl enable go-toolset-1.11 'gofmt -s code_path'
    Copy to Clipboard Toggle word wrap
  • For Red Hat Enterprise Linux 8:

    $ gofmt -s code_path
    Copy to Clipboard Toggle word wrap

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:

  • For Red Hat Enterprise Linux 7:

    $ scl enable go-toolset-1.11 'gofmt -r rewrite_rule code_path'
    Copy to Clipboard Toggle word wrap
  • For Red Hat Enterprise Linux 8:

    $ gofmt -r rewrite_rule code_path
    Copy to Clipboard Toggle word wrap

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:

  • For Red Hat Enterprise Linux 7:

    $ scl enable go-toolset-1.11 'gofmt -r rewrite_rule -d code_path'
    Copy to Clipboard Toggle word wrap
  • For Red Hat Enterprise Linux 8:

    $ gofmt -r rewrite_rule -d code_path
    Copy to Clipboard Toggle word wrap

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

Volver arriba
Red Hat logoGithubredditYoutubeTwitter

Aprender

Pruebe, compre y venda

Comunidades

Acerca de la documentación de Red Hat

Ayudamos a los usuarios de Red Hat a innovar y alcanzar sus objetivos con nuestros productos y servicios con contenido en el que pueden confiar. Explore nuestras recientes actualizaciones.

Hacer que el código abierto sea más inclusivo

Red Hat se compromete a reemplazar el lenguaje problemático en nuestro código, documentación y propiedades web. Para más detalles, consulte el Blog de Red Hat.

Acerca de Red Hat

Ofrecemos soluciones reforzadas que facilitan a las empresas trabajar en plataformas y entornos, desde el centro de datos central hasta el perímetro de la red.

Theme

© 2025 Red Hat