LinuxCommandLibrary

go-fmt

format Go source code to standard style

TLDR

Format a Go file

$ go fmt [file.go]
copy
Format all files in current package
$ go fmt
copy
Format all packages recursively
$ go fmt ./...
copy
Show files that would be changed
$ gofmt -l [file.go]
copy
Show diff of changes
$ gofmt -d [file.go]
copy
Simplify code
$ gofmt -s [file.go]
copy

SYNOPSIS

go fmt [-n] [-x] [packages]
gofmt [flags] [path ...]

DESCRIPTION

go fmt formats Go source code according to standard Go style conventions. It is an alias for gofmt -l -w on the packages named by the import paths. The gofmt tool can also simplify code with -s and apply rewrite rules with -r for refactoring.

PARAMETERS

-n

Print commands that would be executed.
-x
Print commands as they are executed.
-l
List files whose formatting differs (gofmt).
-w
Write result to source file (gofmt).
-d
Display diffs instead of rewriting (gofmt).
-s
Simplify code (gofmt).
-r rule
Apply rewrite rule before formatting (gofmt).

CAVEATS

go fmt and gofmt accept different parameters. Use gofmt directly for -s, -d, -l, and -r options.

SEE ALSO

go(1), goimports(1)

> TERMINAL_GEAR

Curated for the Linux community

Copied to clipboard

> TERMINAL_GEAR

Curated for the Linux community