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

3.2. Formatting code

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, enter:

    $ gofmt -w <code_path>
    Copy to Clipboard Toggle word wrap

    Replace <code_path> with the path to the code you want to format.

    Note

    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

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

  • Show differences in your code in a given path:

    $ gofmt -d <code_path>
    Copy to Clipboard Toggle word wrap

    Replace <code_path> with the path to the code you want to compare.

3.4. Simplifying code

You can use the gofmt formatting tool to simplify your code.

Procedure

  1. To simplify code in a given path, enter:

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

    Replace <code_path> with the path to the code you want to simplify.

  2. To apply the changes, enter:

    $ gofmt -w <code_path>
    Copy to Clipboard Toggle word wrap

    Replace <code_path> with the path to the code you want to format.

3.5. Refactoring code

You can use the gofmt formatting tool to refactor your code by applying arbitrary substitutions.

Procedure

  1. To refactor your code in a given path, enter:

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

    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.

  2. To apply the changes, enter:

    # gofmt -w <code_path>
    Copy to Clipboard Toggle word wrap

    Replace <code_path> with the path to the code you want to format.

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