Top Related Projects
[mirror] Go Tools
Fast linters runner for Go
Staticcheck - The advanced Go linter
Quick Overview
Gofumpt is an enhanced version of the Go formatter, gofmt. It enforces a stricter subset of Go formatting rules and adds additional style rules to make Go code more consistent and readable. Gofumpt is designed to be a drop-in replacement for gofmt, making it easy to integrate into existing workflows.
Pros
- Stricter formatting rules for more consistent code
- Backwards compatible with gofmt
- Configurable with minimal options
- Fast performance
Cons
- Some developers may find the additional rules too opinionated
- May require adjustments to existing codebases
- Limited configuration options compared to other formatters
Code Examples
- Basic usage:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
This code will be formatted according to gofumpt's rules, which are stricter than gofmt.
- Formatting multiline function calls:
someFunction(
arg1,
arg2,
arg3,
)
Gofumpt ensures consistent formatting for multiline function calls, with trailing commas and proper indentation.
- Handling import groups:
import (
"fmt"
"os"
"example.com/mypackage"
)
Gofumpt automatically separates standard library imports from third-party imports with a blank line.
Getting Started
To use gofumpt, follow these steps:
-
Install gofumpt:
go install mvdan.cc/gofumpt@latest
-
Run gofumpt on your Go files:
gofumpt -w .
-
To integrate with your editor, configure it to use gofumpt instead of gofmt. For example, in VS Code, add the following to your settings:
"go.formatTool": "gofumpt"
-
For CI/CD pipelines, you can use gofumpt in check mode:
gofumpt -l -d .
This will list files that need formatting and show the differences without modifying them.
Competitor Comparisons
[mirror] Go Tools
Pros of tools
- Comprehensive suite of Go development tools and utilities
- Official Go project, ensuring long-term support and compatibility
- Includes advanced analysis tools like vet and guru
Cons of tools
- Larger and more complex codebase, potentially harder to contribute to
- May include tools not needed by all developers, increasing overhead
Code comparison
gofumpt:
func example() {
if x {
foo()
}
}
tools (gofmt):
func example() {
if x {
foo()
}
}
Summary
tools is a comprehensive suite of official Go development tools, offering a wide range of utilities for Go programmers. It includes advanced analysis tools and ensures long-term support. However, its larger codebase may be more complex to work with.
gofumpt, on the other hand, is a focused tool that extends gofmt with additional formatting rules. It's simpler and more opinionated, making it easier to maintain consistent code style across projects. The main difference in formatting is the use of tabs vs. spaces for indentation, as shown in the code comparison.
Choose tools for a complete set of Go development utilities, or gofumpt for a stricter, more opinionated code formatter that builds upon gofmt's functionality.
Fast linters runner for Go
Pros of golangci-lint
- Comprehensive linting tool that integrates multiple linters
- Highly configurable with options to enable/disable specific linters
- Supports running linters in parallel for improved performance
Cons of golangci-lint
- More complex setup and configuration compared to gofumpt
- Potentially slower execution due to running multiple linters
- May produce more false positives or conflicting suggestions
Code comparison
golangci-lint configuration example:
linters:
enable:
- gofmt
- golint
- govet
disable:
- errcheck
gofumpt usage example:
gofumpt -l -w .
Key differences
- golangci-lint is a meta-linter that combines multiple tools, while gofumpt focuses solely on code formatting
- gofumpt is more opinionated and aims to provide a consistent formatting style, whereas golangci-lint offers more flexibility
- golangci-lint covers a broader range of code quality issues, including potential bugs and style violations, while gofumpt primarily addresses code formatting
Use cases
- Choose golangci-lint for comprehensive code analysis and linting in larger projects
- Opt for gofumpt when focusing specifically on consistent code formatting with minimal configuration
Both tools can be valuable in a Go development workflow, and some teams may choose to use them in combination for optimal code quality and formatting.
Staticcheck - The advanced Go linter
Pros of go-tools
- Offers a wider range of tools and analyzers for Go code
- Includes more advanced static analysis capabilities
- Provides deeper insights into potential issues and optimizations
Cons of go-tools
- May have a steeper learning curve due to its broader scope
- Could be considered overkill for simple formatting tasks
- Potentially slower execution time for large codebases
Code Comparison
gofumpt:
func example() {
if x {
doSomething()
}
}
go-tools (using staticcheck):
func example() {
if x {
doSomething()
}
// staticcheck: SA4006: this value of x is never used
}
Summary
go-tools is a comprehensive suite of Go analysis tools, offering more advanced features and deeper insights compared to gofumpt. While gofumpt focuses primarily on code formatting, go-tools provides a broader range of static analysis capabilities. This makes go-tools more powerful but potentially more complex to use. gofumpt may be preferred for quick formatting tasks, while go-tools is better suited for thorough code analysis and optimization. The choice between the two depends on the specific needs of the project and the desired level of analysis.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
gofumpt
go install mvdan.cc/gofumpt@latest
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 fork of gofmt
as of Go 1.23.0, and requires Go 1.22 or later.
It can be used as a drop-in replacement to format your Go code,
and running gofmt
after gofumpt
should produce no changes.
For example:
gofumpt -l -w .
Some of the Go source files in this repository belong to the Go project.
The project includes copies of go/printer
and go/doc/comment
as of Go 1.23.0
to ensure consistent formatting independent of what Go version is being used.
The added formatting rules are implemented in the format
package.
vendor
and testdata
directories are skipped unless given as explicit arguments.
Similarly, the added rules do not apply to generated Go files unless they are
given as explicit arguments.
Finally, note that the -r
rewrite flag is removed in favor of gofmt -r
,
and the -s
flag is hidden as it is always enabled.
Added rules
No empty lines following an assignment operator
Example
func foo() {
foo :=
"bar"
}
func foo() {
foo := "bar"
}
No empty lines around function bodies
Example
func foo() {
println("bar")
}
func foo() {
println("bar")
}
Functions should separate ) {
where the indentation helps readability
Example
func foo(s string,
i int) {
println("bar")
}
// With an empty line it's slightly better, but still not great.
func bar(s string,
i int) {
println("bar")
}
func foo(s string,
i int,
) {
println("bar")
}
// With an empty line it's slightly better, but still not great.
func bar(s string,
i int,
) {
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,
},
}
Empty field lists should use a single line
Example
var V interface {
} = 3
type T struct {
}
func F(
)
var V interface{} = 3
type T struct{}
func F()
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() {}
Composite literals should not have leading or trailing empty lines
Example
var _ = []string{
"foo",
}
var _ = map[string]string{
"foo": "bar",
}
var _ = []string{
"foo",
}
var _ = map[string]string{
"foo": "bar",
}
Field lists should not have leading or trailing empty lines
Example
type Person interface {
Name() string
Age() int
}
type ZeroFields struct {
// No fields are needed here.
}
type Person interface {
Name() string
Age() int
}
type ZeroFields struct {
// No fields are needed here.
}
Extra rules behind -extra
Adjacent parameters with the same type should be grouped together
Example
func Foo(bar string, baz string) {}
func Foo(bar, baz string) {}
Installation
gofumpt
is a replacement for gofmt
, so you can simply go install
it as
described at the top of this README and use it.
When using an IDE or editor with Go integration based on gopls
,
it's best to configure the editor to use the gofumpt
support built into gopls
.
The instructions below show how to set up gofumpt
for some of the
major editors out there.
Visual Studio Code
Enable the language server following the official docs,
and then enable gopls's gofumpt
option. Note that VS Code will complain about
the gopls
settings, but they will still work.
"go.useLanguageServer": true,
"gopls": {
"formatting.gofumpt": true,
},
GoLand
GoLand doesn't use gopls
so it should be configured to use gofumpt
directly.
Once gofumpt
is installed, follow the steps below:
- 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
When a window asks for settings, you can enter the following:
- 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 unnecessary runs, you should disable all checkboxes in the Advanced section.
Vim
The configuration depends on the plugin you are using: vim-go or govim.
vim-go
To configure gopls
to use gofumpt
:
let g:go_fmt_command="gopls"
let g:go_gopls_gofumpt=1
govim
To configure gopls
to use gofumpt
:
call govim#config#Set("Gofumpt", 1)
Neovim
When using lspconfig
, pass the gofumpt
setting to gopls
:
require('lspconfig').gopls.setup({
settings = {
gopls = {
gofumpt = true
}
}
})
Emacs
For lsp-mode users on version 8.0.0 or higher:
(setq lsp-go-use-gofumpt t)
For users of lsp-mode
before 8.0.0
:
(lsp-register-custom-settings
'(("gopls.gofumpt" t)))
For eglot users:
(setq-default eglot-workspace-configuration
'((:gopls . ((gofumpt . t)))))
Helix
When using the gopls
language server, modify the Go settings in ~/.config/helix/languages.toml
:
[language-server.gopls.config]
"formatting.gofumpt" = true
Sublime Text
With ST4, install the Sublime Text LSP extension according to the documentation,
and enable gopls
's gofumpt
option in the LSP package settings,
including setting lsp_format_on_save
to true
.
"lsp_format_on_save": true,
"clients":
{
"gopls":
{
"enabled": true,
"initializationOptions": {
"gofumpt": true,
}
}
}
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.
Frequently Asked Questions
Why attempt to replace
gofmt
instead of building on top of it?
Our design is to build on top of gofmt
, and we'll never add rules which
disagree with its formatting. So we extend gofmt
rather than compete with it.
The tool is a modified copy of gofmt
, for the purpose of allowing its use as a
drop-in replacement in editors and scripts.
Why are my module imports being grouped with standard library imports?
Any import paths that don't start with a domain name like foo.com
are
effectively reserved by the Go toolchain.
Third party modules should either start with a domain name,
even a local one like foo.local
, or use a reserved path prefix.
For backwards compatibility with modules set up before these rules were clear,
gofumpt
will treat any import path sharing a prefix with the current module
path as third party. For example, if the current module is mycorp/mod1
, then
all import paths in mycorp/...
will be considered third party.
How can I use
gofumpt
if I already usegoimports
to replacegofmt
?
Most editors have replaced the goimports
program with the same functionality
provided by a language server like gopls
. This mechanism is significantly
faster and more powerful, since the language server has more information that is
kept up to date, necessary to add missing imports.
As such, the general recommendation is to let your editor fix your imports -
either via gopls
, such as VSCode or vim-go, or via their own custom
implementation, such as GoLand. Then follow the install instructions above to
enable the use of gofumpt
instead of gofmt
.
If you want to avoid integrating with gopls
, and are OK with the overhead of
calling goimports
from scratch on each save, you should be able to call both
tools; for example, goimports file.go && gofumpt file.go
.
Contributing
Issues and pull requests are welcome! Please open an issue to discuss a feature before sending a pull request.
We also use the #gofumpt
channel over at the
Gophers Slack to chat.
When reporting a formatting bug, insert a //gofumpt:diagnose
comment.
The comment will be rewritten to include useful debugging information.
For instance:
$ cat f.go
package p
//gofumpt:diagnose
$ gofumpt f.go
package p
//gofumpt:diagnose v0.1.1-0.20211103104632-bdfa3b02e50a -lang=go1.16
License
Note that much of the code is copied from Go's gofmt
command. 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
.
Top Related Projects
[mirror] Go Tools
Fast linters runner for Go
Staticcheck - The advanced Go linter
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot