gosortimports

command module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2023 License: MIT Imports: 26 Imported by: 0

README

gosortimports

Command gosortimports is a more aggressive version of goimports that handles very badly grouped imports and adds gofmt's simplify code option (-s). Otherwise it is the same as [goimports].

Note: Since gosortimports undoes any existing import groupings it is recommended to run it with the -local flag so that it can properly group local imports. At some point gosortimports will support automatically deducing the value of the local flag.

Installation

go install github.com/charlievieth/gosortimports

Examples

The two following examples shows how gosortimports more aggressively handles oddly grouped imports compared to goimports.

import (
	"io"

	"m/a"

	"math"

	"fmt"
	"m/b"
)

// formatted with: `goimports -local=m/`

import (
	"io"

	"m/a"

	"math"

	"fmt"

	"m/b"
)

// formatted with: `gosortimports -local=m/`

import (
	"fmt"
	"io"
	"math"

	"m/a"
	"m/b"
)

Documentation

Overview

Command gosortimports is a more aggressive version of goimports that handles very badly grouped imports and adds gofmt's simplify code option (-s). Otherwise it is the same as goimports.

$ go install https://github.com/charlievieth/gosortimports

In addition to fixing imports, gosortimports also formats your code in the same style as gofmt so it can be used as a replacement for your editor's gofmt-on-save hook.

For emacs, make sure you have the latest go-mode.el:

https://github.com/dominikh/go-mode.el

Then in your .emacs file:

(setq gofmt-command "gosortimports")
(add-hook 'before-save-hook 'gofmt-before-save)

For vim, set "gofmt_command" to "gosortimports":

https://golang.org/change/39c724dd7f252
https://golang.org/wiki/IDEsAndTextEditorPlugins
etc

For other editors, you probably know what to do.

To exclude directories in your $GOPATH from being scanned for Go files, gosortimports respects a configuration file at $GOPATH/src/.goimportsignore which may contain blank lines, comment lines (beginning with '#'), or lines naming a directory relative to the configuration file to ignore when scanning. No globbing or regex patterns are allowed. Use the "-v" verbose flag to verify it's working and see what gosortimports is doing.

Happy hacking!

Jump to

Keyboard shortcuts

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