

GitHub - alexkohler/unimport: unimport is a Go static analysis tool to find unne...
source link: https://github.com/alexkohler/unimport
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

unimport
unimport is a Go static analysis tool to find unnecessary import aliases.
Installation
go get -u github.com/alexkohler/unimport
Usage
Similar to other Go static anaylsis tools (such as golint, go vet) , unimport can be invoked with one or more filenames, directories, or packages named by its import path. Unimport also supports the ...
wildcard.
unimport files/directories/packages
Currently, no flag are supported. A -w
flag may be added in the future to automatically remove aliases where possible (Similar to gofmt's -w flag).
Purpose
As noted in Go's Code Review comments:
Avoid renaming imports except to avoid a name collision; good package names should not require renaming. In the event of collision, prefer to rename the most local or project-specific import.
This tool will check if any import aliases are truly needed (by ensuring there is a name collision that would exist without the import alias). This tool will ignore import paths containing dashes and dots, as these are generally useful aliases while importing a specific revision. For example, in gometalinter, there are some imports like kingpin "gopkg.in/alecthomas/kingpin.v3-unstable"
. This is a reasonable import alias and will not be flagged.
Example
Running unimports
on the Go source:
$ unimport $GOROOT/src/...
cmd/go/pkg.go:18 unnecessary import alias pathpkg
go/build/build.go:19 unnecessary import alias pathpkg
go/internal/gcimporter/gcimporter.go:23 unnecessary import alias exact
os/pipe_test.go:14 unnecessary import alias osexec
os/os_windows_test.go:10 unnecessary import alias osexec
Below are some of the arguably unneeded import aliases it found:
// go/internal/gcimporter/gcimporter.go
import (
"bufio"
"errors"
"fmt"
"go/build"
"go/token"
"io"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"text/scanner"
exact "go/constant"
"go/types"
)
// os/pipe_test.go.go
import (
"fmt"
"internal/testenv"
"os"
osexec "os/exec"
"os/signal"
"syscall"
"testing"
)
- Unit tests
- Flagging of packages that contain an uppercase letter or underscore
- -w flag to write changes to file where/if possible
- Globbing support (e.g. unimport *.go)
Contributing
Pull requests welcome!
Recommend
-
178
Files Permalink Latest commit message Commit...
-
98
nakedret nakedret is a Go static analysis tool to find naked returns in functions greater than a specified function length. Installation go get -u github.com/alexkohler/nakedret Usage
-
124
prealloc prealloc is a Go static analysis tool to find slice declarations that could potentially be preallocated. Installation go get -u github.com/alexkohler/prealloc Usage Sim...
-
64
identypo is a Go static analysis tool to find typos in identifiers (functions, function calls, variables, constants, type declarations, packages, labels). - alexkohler/identypo
-
83
dogsled is a Go static analysis tool to find assignments/declarations with too many blank identifiers. - alexkohler/dogsled
-
15
Jun 22, 2021 - 8 minute read - Comments - Automation
-
8
GoKart - Go Security Static Analysis GoKart is a static analysis tool for Go that finds vulnerabilities using the SSA (single static assignment) form of Go source code. It is capable of tracing the source of variables and function ar...
-
6
Introduction This week, I work on my Static Site Generator (SSG) - Potato Generator. I plan to implement a source code formatter for my project, which is
-
14
Tools to improve and control code quality can be a key success factor in a complex software project implementation. Static analyzers belong to such tools. Nowadays, you can find various static analyzers: from free open-source to cross-functio...
-
10
My journey of building Skott, an open-source Node.js library (2 Part Series) ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK