GO111MODULE=on go get mvdan.cc/gofumpt
Enforce a stricter format than gofmt
, while being backwards compatible. That
is, gofumpt
is happy with a subset of the formats that gofmt
is happy with.
The tool is a modified fork of gofmt
, so it can be used as a drop-in
replacement. Running gofmt
after gofumpt
should be a no-op.
A drop-in replacement for goimports
is also available:
GO111MODULE=on go get mvdan.cc/gofumpt/gofumports
Most of the Go source files in this repository belong to the Go project.
The added formatting rules are in the format
package.
No empty lines at the beginning or end of a function
example
func foo() {
println("bar")
}
func foo() {
println("bar")
}
No empty lines around a lone statement (or comment) in a block
example
if err != nil {
return err
}
if err != nil {
return err
}
No empty lines before a simple error check
example
foo, err := processFoo()
if err != nil {
return err
}
foo, err := processFoo()
if err != nil {
return err
}
Composite literals should use newlines consistently
example
// A newline before or after an element requires newlines for the opening and
// closing braces.
var ints = []int{1, 2,
3, 4}
// A newline between consecutive elements requires a newline between all
// elements.
var matrix = [][]int{
{1},
{2}, {
3,
},
}
var ints = []int{
1, 2,
3, 4,
}
var matrix = [][]int{
{1},
{2},
{
3,
},
}
std
imports must be in a separate group at the top
example
import (
"foo.com/bar"
"io"
"io/ioutil"
)
import (
"io"
"io/ioutil"
"foo.com/bar"
)
Short case clauses should take a single line
example
switch c {
case 'a', 'b',
'c', 'd':
}
switch c {
case 'a', 'b', 'c', 'd':
}
Multiline top-level declarations must be separated by empty lines
example
func foo() {
println("multiline foo")
}
func bar() {
println("multiline bar")
}
func foo() {
println("multiline foo")
}
func bar() {
println("multiline bar")
}
Single var declarations should not be grouped with parentheses
example
var (
foo = "bar"
)
var foo = "bar"
Contiguous top-level declarations should be grouped together
example
var nicer = "x"
var with = "y"
var alignment = "z"
var (
nicer = "x"
with = "y"
alignment = "z"
)
Simple var-declaration statements should use short assignments
example
var s = "somestring"
s := "somestring"
The -s
code simplification flag is enabled by default
example
var _ = [][]int{[]int{1}}
var _ = [][]int{{1}}
Octal integer literals should use the 0o
prefix on modules using Go 1.13 and later
example
const perm = 0755
const perm = 0o755
Comments which aren't Go directives should start with a whitespace
example
//go:noinline
//Foo is awesome.
func Foo() {}
//go:noinline
// Foo is awesome.
func Foo() {}
Adjacent parameters with the same type should be grouped together
example
func Foo(bar string, baz string) {}
func Foo(bar, baz string) {}
gofumpt
is a replacement for gofmt
, so you can simply go get
it as
described at the top of this README and use it.
Using the language server is the recommended method of running gofumpt
. There is
no need of using gofumports
in this case, as import ordering is performed in a
different step. Some of the settings required to enable it are not yet recognized
by VS Code and it will complain about them, but they will still work. This is an
expected behaviour until gopls
gets a consistent set of settings, as stated in
its official documentation.
"go.useLanguageServer": true,
"gopls": {
"gofumpt": true,
},
"[go]": {
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": true,
},
},
"[go.mod]": {
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": true,
},
},
Alternatively, if you don't use the language server, you can still configure
the IDE to use either gofumpt
or goimports
. This change must be done through
the settings.json
file because the formatting tool parameter is shown as a
selector and not as a textbox in the interface. For this reason, VS Code will
complain about an invalid property value, but this warning can be safely ignored
and the correct tool will be used anyways.
"go.formatTool": "gofumports"
You can use gofumpt
instead of gofumports
if you don't need auto-importing
on-save. Remember to disable the language server, as formatting is completely
bypassed and delegated to gopls
if enabled.
It's possible to set up Goland IDE to automatically perform gofumpt
actions.
After gofumpt
installation, follow the following steps to enable it in Goland:
- Open Settings (File > Settings)
- Open the Tools section
- Find the File Watchers sub-section
- Click on the
+
on the right side to add a new file watcher - Choose Custom Template
A new windows will ask for settings, if you follow instructions below, your project files
will be gofumpt
ed automatically by file watcher directives.
- Name: Just choose the name you want to identify your file watcher
- File Types: Select all .go files
- Scope: Project Files
- Program: Select your
gofumpt
executable - Arguments:
-w $FilePath$
- Output path to refresh:
$FilePath$
- Working directory:
$ProjectFileDir$
- Environment variables:
GOROOT=$GOROOT$;GOPATH=$GOPATH$;PATH=$GoBinDirs$
To avoid unecessary runs, you must disable all checkboxes in the Advanced section.
Release v1.24 adds support for using gopls' gofumpt workspace setting via g:go_gopls_gofumpt
.
let g:go_fmt_command="gopls"
let g:go_gopls_gofumpt=1
This tool is a place to experiment. In the long term, the features that work
well might be proposed for gofmt
itself.
The tool is also compatible with gofmt
and is aimed to be stable, so you can
rely on it for your code as long as you pin a version of it.
Note that much of the code is copied from Go's gofmt
and goimports
commands.
You can tell which files originate from the Go repository from their copyright
headers. Their license file is LICENSE.google
.
gofumpt
's original source files are also under the 3-clause BSD license, with
the separate file LICENSE
.