gofumpt

gofmt, the way it should be pronounced

Github星跟蹤圖

gofumpt

gofmt, the way it should be pronounced.

cd $(mktemp -d); go mod init tmp; 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:

cd $(mktemp -d); go mod init tmp; go get mvdan.cc/gofumpt/gofumports

Features

No empty lines at the beginning or end of a function

func foo() {
	println("bar")

}
func foo() {
	println("bar")
}

No empty lines around a lone statement (or comment) in a block

if err != nil {

	return err
}
if err != nil {
	return err
}

No empty lines before a simple error check

foo, err := processFoo()

if err != nil {
	return err
}
foo, err := processFoo()
if err != nil {
	return err
}

Composite literals with elements in separate lines must also separate both braces

var ints = []int{1, 2,
	3, 4}
var ints = []int{
	1, 2,
	3, 4,
}

std imports must be in a separate group at the top

import (
	"foo.com/bar"

	"io"

	"io/ioutil"
)
import (
	"io"
	"io/ioutil"

	"foo.com/bar"
)

Short case clauses should take a single line

switch c {
case 'a', 'b',
	'c', 'd':
}
switch c {
case 'a', 'b', 'c', 'd':
}

Multiline top-level declarations must be separated by empty lines

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

var (
	foo = "bar"
)
var foo = "bar"

Contiguous top-level declarations should be grouped together

var nicer = "x"
var with = "y"
var alignment = "z"
var (
	nicer     = "x"
	with      = "y"
	alignment = "z"

Simple var-declaration statements should use short assignments

var s = "somestring"
s := "somestring"

Comments which aren't Go directives should start with a whitespace

//go:noinline

//Foo is awesome.
func Foo() {}
//go:noinline

// Foo is awesome.
func Foo() {}

Installation

gofumpt is a replacement for gofmt, so you can simply go get it as
described at the top of this README and use it.

Alternatively, to use the tool with VS Code, add these settings:

"go.formatTool": "goimports",
"go.alternateTools": {
    "goimports": "gofumports",
},
"go.languageServerExperimentalFeatures": {
    "format": false
}

You can use gofmt instead of goimports and gofumpt instead of gofumports if you don't need auto-importing on-save.

Roadmap

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.

License

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.

主要指標

概覽
名稱與所有者mvdan/gofumpt
主編程語言Go
編程語言Go (語言數: 1)
平台
許可證BSD 3-Clause "New" or "Revised" License
所有者活动
創建於2019-03-31 01:17:51
推送於2025-04-21 18:43:47
最后一次提交2025-04-21 19:42:45
發布數11
最新版本名稱v0.8.0 (發布於 2025-04-13 23:00:14)
第一版名稱v0.1.0 (發布於 )
用户参与
星數3.6k
關注者數14
派生數114
提交數290
已啟用問題?
問題數210
打開的問題數28
拉請求數90
打開的拉請求數1
關閉的拉請求數16
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?