LinuxCommandLibrary

gofmt

Format Go source code

TLDR

Format a file and display the result to the console

$ gofmt [source.go]
copy

Format a file, overwriting the original file in-place
$ gofmt -w [source.go]
copy

Format a file, and then simplify the code, overwriting the original file
$ gofmt -s -w [source.go]
copy

Print all (including spurious) errors
$ gofmt -e [source.go]
copy

SYNOPSIS

gofmt [flags] [path ...]

PARAMETERS

-d
    Do not write to the standard output; instead, print the names of the files whose contents differ from gofmt's output.

-e
    Report all errors (not just the first 10 on different lines).

-l
    Do not write to the standard output; instead, print the names of the files that would be formatted.

-r 'rewrite_rule'
    Apply the rewrite rule to the source before formatting. The rule argument is an expression in the syntax of Go. (Added in go1.1)

-s
    Simplify code (after applying the rewrite rules, if any).

-w
    Do not write to the standard output; instead, write the result to the source file. (Added in go1.1)

-version
    Print gofmt's version.

DESCRIPTION

gofmt is a tool for automatically formatting Go source code. It enforces a consistent style, making Go programs easier to read and understand. It uses a set of rules to adjust spacing, indentation, and other stylistic elements, ensuring that all Go code adheres to the standard Go style guide. This enhances readability, reduces stylistic debates, and promotes a unified codebase across different projects and developers. The tool not only formats code but also simplifies it, where possible, without altering its functionality. gofmt is a crucial component of the Go ecosystem, encouraging consistent coding practices and making collaboration more efficient. It's typically run before committing changes to a repository. It also can rewrite the source files passed as command-line arguments. If no arguments are supplied, it processes standard input.

CAVEATS

gofmt rewrites source files in place when the -w flag is used. It's good practice to use version control (e.g., Git) to track changes and allow for easy rollback if needed.
It does not correct semantic errors in the code.

EXIT STATUS

gofmt exits with a zero status if no errors occur and all files are formatted according to the standard style. It exits with a non-zero status if there are errors, for example, if the input is not valid Go code.

REWRITE RULES

The -r flag allows for powerful transformations of Go source code based on structural patterns. It's useful for automated refactoring and code modernization. The expression syntax for the rule argument is described in the gofmt documentation.

HISTORY

gofmt was developed as part of the Go programming language project. It aims to automate source code formatting. It has been a standard tool in the Go toolchain since its early versions, promoting consistent coding styles across Go projects. Improvements and new features, such as rewrite rules, are added in newer Go versions.

SEE ALSO

go(1), golint(1)

Copied to clipboard