3 GO111MODULE=on go get mvdan.cc/gofumpt
5 Enforce a stricter format than `gofmt`, while being backwards compatible. That
6 is, `gofumpt` is happy with a subset of the formats that `gofmt` is happy with.
8 The tool is a modified fork of `gofmt`, so it can be used as a drop-in
9 replacement. Running `gofmt` after `gofumpt` should be a no-op.
11 A drop-in replacement for `goimports` is also available:
13 GO111MODULE=on go get mvdan.cc/gofumpt/gofumports
15 Most of the Go source files in this repository belong to the Go project.
16 The added formatting rules are in the `format` package.
20 No empty lines at the beginning or end of a function
22 <details><summary><i>example</i></summary>
39 No empty lines around a lone statement (or comment) in a block
41 <details><summary><i>example</i></summary>
58 No empty lines before a simple error check
60 <details><summary><i>example</i></summary>
63 foo, err := processFoo()
71 foo, err := processFoo()
79 Composite literals should use newlines consistently
81 <details><summary><i>example</i></summary>
84 // A newline before or after an element requires newlines for the opening and
86 var ints = []int{1, 2,
89 // A newline between consecutive elements requires a newline between all
105 var matrix = [][]int{
116 `std` imports must be in a separate group at the top
118 <details><summary><i>example</i></summary>
141 Short case clauses should take a single line
143 <details><summary><i>example</i></summary>
154 case 'a', 'b', 'c', 'd':
160 Multiline top-level declarations must be separated by empty lines
162 <details><summary><i>example</i></summary>
166 println("multiline foo")
169 println("multiline bar")
175 println("multiline foo")
179 println("multiline bar")
185 Single var declarations should not be grouped with parentheses
187 <details><summary><i>example</i></summary>
201 Contiguous top-level declarations should be grouped together
203 <details><summary><i>example</i></summary>
222 Simple var-declaration statements should use short assignments
224 <details><summary><i>example</i></summary>
237 The `-s` code simplification flag is enabled by default
239 <details><summary><i>example</i></summary>
242 var _ = [][]int{[]int{1}}
252 Octal integer literals should use the `0o` prefix on modules using Go 1.13 and later
254 <details><summary><i>example</i></summary>
266 Comments which aren't Go directives should start with a whitespace
268 <details><summary><i>example</i></summary>
286 #### Extra rules behind `-extra`
288 Adjacent parameters with the same type should be grouped together
290 <details><summary><i>example</i></summary>
293 func Foo(bar string, baz string) {}
297 func Foo(bar, baz string) {}
304 `gofumpt` is a replacement for `gofmt`, so you can simply `go get` it as
305 described at the top of this README and use it.
307 Alternatively, to use the tool with VS Code, add these settings:
310 "go.formatTool": "goimports",
311 "go.alternateTools": {
312 "goimports": "gofumports",
314 "go.languageServerExperimentalFeatures": {
319 You can use `gofmt` instead of `goimports` and `gofumpt` instead of `gofumports`
320 if you don't need auto-importing on-save.
324 This tool is a place to experiment. In the long term, the features that work
325 well might be proposed for `gofmt` itself.
327 The tool is also compatible with `gofmt` and is aimed to be stable, so you can
328 rely on it for your code as long as you pin a version of it.
332 Note that much of the code is copied from Go's `gofmt` and `goimports` commands.
333 You can tell which files originate from the Go repository from their copyright
334 headers. Their license file is `LICENSE.google`.
336 `gofumpt`'s original source files are also under the 3-clause BSD license, with
337 the separate file `LICENSE`.