• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.github/H11-Mar-2021-

format/H11-Mar-2021-

gofumports/H11-Mar-2021-

internal/diff/H11-Mar-2021-

testdata/scripts/H11-Mar-2021-

.gitattributesH A D11-Mar-202181

CHANGELOG.mdH A D11-Mar-20211.4 KiB

LICENSEH A D11-Mar-20211.5 KiB

LICENSE.googleH A D11-Mar-20211.4 KiB

README.mdH A D11-Mar-20216.4 KiB

doc.goH A D11-Mar-2021419

flag.goH A D11-Mar-2021394

gen.goH A D11-Mar-202110.5 KiB

go.modH A D11-Mar-2021265

go.sumH A D11-Mar-20213.8 KiB

gofmt.goH A D11-Mar-20217.5 KiB

internal.goH A D11-Mar-20215 KiB

main_test.goH A D11-Mar-2021751

rewrite.goH A D11-Mar-20218.1 KiB

simplify.goH A D11-Mar-20214.7 KiB

version.goH A D11-Mar-2021569

README.md

1# gofumpt
2
3	GO111MODULE=on go get mvdan.cc/gofumpt
4
5Enforce a stricter format than `gofmt`, while being backwards compatible. That
6is, `gofumpt` is happy with a subset of the formats that `gofmt` is happy with.
7
8The tool is a modified fork of `gofmt`, so it can be used as a drop-in
9replacement. Running `gofmt` after `gofumpt` should be a no-op.
10
11Most of the Go source files in this repository belong to the Go project.
12The added formatting rules are in the `format` package.
13
14### Added rules
15
16No empty lines at the beginning or end of a function
17
18<details><summary><i>example</i></summary>
19
20```
21func foo() {
22	println("bar")
23
24}
25```
26
27```
28func foo() {
29	println("bar")
30}
31```
32
33</details>
34
35No empty lines around a lone statement (or comment) in a block
36
37<details><summary><i>example</i></summary>
38
39```
40if err != nil {
41
42	return err
43}
44```
45
46```
47if err != nil {
48	return err
49}
50```
51
52</details>
53
54No empty lines before a simple error check
55
56<details><summary><i>example</i></summary>
57
58```
59foo, err := processFoo()
60
61if err != nil {
62	return err
63}
64```
65
66```
67foo, err := processFoo()
68if err != nil {
69	return err
70}
71```
72
73</details>
74
75Composite literals should use newlines consistently
76
77<details><summary><i>example</i></summary>
78
79```
80// A newline before or after an element requires newlines for the opening and
81// closing braces.
82var ints = []int{1, 2,
83	3, 4}
84
85// A newline between consecutive elements requires a newline between all
86// elements.
87var matrix = [][]int{
88	{1},
89	{2}, {
90		3,
91	},
92}
93```
94
95```
96var ints = []int{
97	1, 2,
98	3, 4,
99}
100
101var matrix = [][]int{
102	{1},
103	{2},
104	{
105		3,
106	},
107}
108```
109
110</details>
111
112Empty field lists should use a single line
113
114<details><summary><i>example</i></summary>
115
116```
117var V interface {
118} = 3
119
120type T struct {
121}
122
123func F(
124)
125```
126
127```
128var V interface{} = 3
129
130type T struct{}
131
132func F()
133```
134
135```
136var ints = []int{
137	1, 2,
138	3, 4,
139}
140
141var matrix = [][]int{
142	{1},
143	{2},
144	{
145		3,
146	},
147}
148```
149
150</details>
151
152`std` imports must be in a separate group at the top
153
154<details><summary><i>example</i></summary>
155
156```
157import (
158	"foo.com/bar"
159
160	"io"
161
162	"io/ioutil"
163)
164```
165
166```
167import (
168	"io"
169	"io/ioutil"
170
171	"foo.com/bar"
172)
173```
174
175</details>
176
177Short case clauses should take a single line
178
179<details><summary><i>example</i></summary>
180
181```
182switch c {
183case 'a', 'b',
184	'c', 'd':
185}
186```
187
188```
189switch c {
190case 'a', 'b', 'c', 'd':
191}
192```
193
194</details>
195
196Multiline top-level declarations must be separated by empty lines
197
198<details><summary><i>example</i></summary>
199
200```
201func foo() {
202	println("multiline foo")
203}
204func bar() {
205	println("multiline bar")
206}
207```
208
209```
210func foo() {
211	println("multiline foo")
212}
213
214func bar() {
215	println("multiline bar")
216}
217```
218
219</details>
220
221Single var declarations should not be grouped with parentheses
222
223<details><summary><i>example</i></summary>
224
225```
226var (
227	foo = "bar"
228)
229```
230
231```
232var foo = "bar"
233```
234
235</details>
236
237Contiguous top-level declarations should be grouped together
238
239<details><summary><i>example</i></summary>
240
241```
242var nicer = "x"
243var with = "y"
244var alignment = "z"
245```
246
247```
248var (
249	nicer     = "x"
250	with      = "y"
251	alignment = "z"
252)
253```
254
255</details>
256
257
258Simple var-declaration statements should use short assignments
259
260<details><summary><i>example</i></summary>
261
262```
263var s = "somestring"
264```
265
266```
267s := "somestring"
268```
269
270</details>
271
272
273The `-s` code simplification flag is enabled by default
274
275<details><summary><i>example</i></summary>
276
277```
278var _ = [][]int{[]int{1}}
279```
280
281```
282var _ = [][]int{{1}}
283```
284
285</details>
286
287
288Octal integer literals should use the `0o` prefix on modules using Go 1.13 and later
289
290<details><summary><i>example</i></summary>
291
292```
293const perm = 0755
294```
295
296```
297const perm = 0o755
298```
299
300</details>
301
302Comments which aren't Go directives should start with a whitespace
303
304<details><summary><i>example</i></summary>
305
306```
307//go:noinline
308
309//Foo is awesome.
310func Foo() {}
311```
312
313```
314//go:noinline
315
316// Foo is awesome.
317func Foo() {}
318```
319
320</details>
321
322#### Extra rules behind `-extra`
323
324Adjacent parameters with the same type should be grouped together
325
326<details><summary><i>example</i></summary>
327
328```
329func Foo(bar string, baz string) {}
330```
331
332```
333func Foo(bar, baz string) {}
334```
335
336</details>
337
338### Installation
339
340`gofumpt` is a replacement for `gofmt`, so you can simply `go get` it as
341described at the top of this README and use it.
342
343When using an IDE or editor with Go integrations, it's best to use `gofumpt` as
344part of `gopls`. The instructions below show how to do that for some of the
345major editors out there.
346
347#### Visual Studio Code
348
349Enable the language server following [the official docs](https://github.com/golang/tools/blob/master/gopls/doc/vscode.md),
350and then enable gopls's `gofumpt` option. Note that VS Code will complain about
351the `gopls` settings, but they will still work.
352
353```json
354"go.useLanguageServer": true,
355"gopls": {
356	"gofumpt": true,
357},
358```
359
360#### Goland
361
362Once `gofumpt` is installed, follow the steps below:
363
364- Open **Settings** (File > Settings)
365- Open the **Tools** section
366- Find the *File Watchers* sub-section
367- Click on the `+` on the right side to add a new file watcher
368- Choose *Custom Template*
369
370When a window asks for settings, you can enter the following:
371
372* File Types: Select all .go files
373* Scope: Project Files
374* Program: Select your `gofumpt` executable
375* Arguments: `-w $FilePath$`
376* Output path to refresh: `$FilePath$`
377* Working directory: `$ProjectFileDir$`
378* Environment variables: `GOROOT=$GOROOT$;GOPATH=$GOPATH$;PATH=$GoBinDirs$`
379
380To avoid unecessary runs, you should disable all checkboxes in the *Advanced* section.
381
382#### Vim-go
383
384Ensure you are at least running version
385[v1.24](https://github.com/fatih/vim-go/blob/master/CHANGELOG.md#v124---september-15-2020),
386and set up `gopls` for formatting code with `gofumpt`:
387
388```vim
389let g:go_fmt_command="gopls"
390let g:go_gopls_gofumpt=1
391```
392
393#### Govim
394
395With a [new enough version of govim](https://github.com/govim/govim/pull/1005),
396simply configure `gopls` to use `gofumpt`:
397
398```vim
399call govim#config#Set("Gofumpt", 1)
400```
401
402### Roadmap
403
404This tool is a place to experiment. In the long term, the features that work
405well might be proposed for `gofmt` itself.
406
407The tool is also compatible with `gofmt` and is aimed to be stable, so you can
408rely on it for your code as long as you pin a version of it.
409
410### License
411
412Note that much of the code is copied from Go's `gofmt` and `goimports` commands.
413You can tell which files originate from the Go repository from their copyright
414headers. Their license file is `LICENSE.google`.
415
416`gofumpt`'s original source files are also under the 3-clause BSD license, with
417the separate file `LICENSE`.
418