gofumpt

command module
v0.0.0-...-5a67fa7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 24, 2020 License: BSD-3-Clause Imports: 20 Imported by: 0

README

gofumpt (adapted)

This is a version of the standard gofumpt package with slightly changed behaviour:

  • Don't change var x=y to x:=y
  • In gofumports a bug existed where imports were first ordered into 3 groups and then back into 2. This has been fixed to maintain the original 3 groups (system, local, external)

Fetching Gofumpt

GO111MODULE=on go get github.com/tnmurphy/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 github.com/tnmurphy/gofumpt/gofumports

Most of the Go source files in this repository belong to gofmt and goimports. The added formatting rules are in the format package.

Features

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"
)

Adjacent parameters with the same type should be grouped together

example
func Foo(bar string, baz string) {}
func Foo(bar, baz string) {}

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() {}
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.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
Package format exposes gofumpt's formatting in an API similar to go/format.
Package format exposes gofumpt's formatting in an API similar to go/format.
internal/event
Package event provides a set of packages that cover the main concepts of telemetry in an implementation agnostic way.
Package event provides a set of packages that cover the main concepts of telemetry in an implementation agnostic way.
internal/event/core
Package core provides support for event based telemetry.
Package core provides support for event based telemetry.
internal/fastwalk
Package fastwalk provides a faster version of filepath.Walk for file system scanning tools.
Package fastwalk provides a faster version of filepath.Walk for file system scanning tools.
internal/gocommand
Package gocommand is a helper for calling the go command.
Package gocommand is a helper for calling the go command.
internal/gopathwalk
Package gopathwalk is like filepath.Walk but specialized for finding Go packages, particularly in $GOPATH and $GOROOT.
Package gopathwalk is like filepath.Walk but specialized for finding Go packages, particularly in $GOPATH and $GOROOT.
internal/imports
Package imports implements a Go pretty-printer (like package "go/format") that also adds or removes import statements as necessary.
Package imports implements a Go pretty-printer (like package "go/format") that also adds or removes import statements as necessary.
internal/module
Package module defines the module.Version type along with support code.
Package module defines the module.Version type along with support code.
internal/semver
Package semver implements comparison of semantic version strings.
Package semver implements comparison of semantic version strings.
internal/telemetry/event
Package event provides support for event based telemetry.
Package event provides support for event based telemetry.
internal
diff
Package diff implements a Diff function that compare two inputs using the 'diff' tool.
Package diff implements a Diff function that compare two inputs using the 'diff' tool.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL