goimports-reviser

repository·master·Indexed 20 days ago

https://github.com/incu6us/goimports-reviser

A Golang tool that sorts imports into logical groups (std, general, company, project) and provides code formatting. It serves as a more configurable alternative to goimports and gofmt, offering features such as removing unused imports, handling versioned package aliases via -set-alias, and integration as a go vet tool.

Tokens
3.1K
Snippets
10
Records
12
Agent score
68%

What's inside goimports-reviser

  1. Configure import groups and ordering

    master

    The tool sorts imports into groups. By default, the order is std,general,company,project. You can customize this using the -imports-order and -company-prefixes flags.

    Import Groups

    • std: Standard library imports.
    • general: General purpose third-party libraries.
    • company: Inter-org or company-specific libraries (requires -company-prefixes).
    • project: Your local project dependencies.
    • blanked: Imports using the _ alias.
    • dotted: Imports using the . alias.

    Customizing Groups

    Use -company-prefixes with comma-separated values to define which packages belong to the company group. Use -imports-order to define a custom sequence of the groups listed above.

    goimports-reviser -imports-order std,general,company,project,blanked,dotted -company-prefixes github.com/my-org,gitlab.com/my-org
  2. Install goimports-reviser

    master

    You can install goimports-reviser using Go, Homebrew, or Snap.

    ### With Go
    ```bash
    go install -v github.com/incu6us/goimports-reviser/v3@latest

    With Brew

    brew tap incu6us/homebrew-tap
    brew install incu6us/homebrew-tap/goimports-reviser

    With Snap

    snap install goimports-reviser
  3. Run goimports-reviser as a `go vet` tool

    master

    You can use goimports-reviser as a linter by running it through go vet. This allows you to integrate the import reviser into your standard Go verification workflow. You must point to the specific binary built for your OS and architecture using the -vettool flag.

    go vet -vettool=bin/macos-amd64/goimportsreviserlint ./...
  4. Run goimports-reviser on files or directories

    master

    You can run the tool on a specific file, a directory, or recursively using the ./... pattern. Common flags include -rm-unused to remove unused imports and -set-alias to handle versioned package aliases.

    Basic usage on a single file

    goimports-reviser -rm-unused -set-alias -format ./reviser/reviser.go

    Recursive usage on a directory

    goimports-reviser -rm-unused -set-alias -format -recursive reviser

    Recursive usage on the current project

    goimports-reviser -rm-unused -set-alias -format ./...

    Multiple targets

    goimports-reviser -rm-unused -set-alias -format ./reviser/reviser.go ./pkg/...
    goimports-reviser -rm-unused -set-alias -format ./reviser/reviser.go
    
    goimports-reviser -rm-unused -set-alias -format -recursive reviser
    
    goimports-reviser -rm-unused -set-alias -format ./...
    
    goimports-reviser -rm-unused -set-alias -format ./reviser/reviser.go ./pkg/...
  5. How the output mode works

    master

    The -output flag determines how the tool handles the processed content:

    • stdout: Prints the formatted content to standard output.
    • file: Writes the formatted content back to the original file.
    • write: Similar to file, but when used with -list-diff, it will also list the filename in addition to writing the changes back to the file.
  6. Use goimports-reviser CLI

    master

    The goimports-reviser CLI tool is used to sort and format Go imports according to specific rules. You can provide file paths or directories as arguments. If you provide - as an argument, it reads from stdin.

    Basic Usage:

    goimports-reviser [options] <file_or_directory>

    Common patterns:

    • Format a directory recursively: Use -recursive.
    • Check for changes without applying them: Use -list-diff to list files that need formatting and -set-exit-status to return exit code 1 if changes are needed.
    • Write changes back to files: Use -output write (or -output file) in combination with -list-diff to both list the files and apply the fixes.
    goimports-reviser -rm-unused -set-alias -format goimports-reviser/main.go
  7. Use -set-alias to handle versioned packages

    master

    The -set-alias flag automatically rewrites import aliases for versioned packages (e.g., github.com/go-pg/pg/v9 becomes pg "github.com/go-pg/pg/v9").

    // Before
    import "github.com/go-pg/pg/v9"
    
    // After using -set-alias
    import pg "github.com/go-pg/pg/v9"
  8. Use -separate-named to isolate named imports

    master

    The -separate-named flag separates named imports from their group with a new line.

    // Before
    import (
        "fmt"
        "github.com/incu6us/goimports-reviser/pkg"
        extpkg "google.com/golang/pkg"
        extslice "github.com/PeterRK/slices"
        "golang.org/x/exp/slices"
    )
    
    // After using -separate-named
    import (
        "fmt"
    
        "github.com/incu6us/goimports-reviser/pkg"
        "golang.org/x/exp/slices"
    
        extpkg "google.com/golang/pkg"
        extslice "github.com/PeterRK/slices"
    )
  9. Configure import grouping and ordering

    master

    You can customize how import groups are sorted using the -imports-order flag. The available groups are:

    • std: Standard library imports.
    • general: General purpose libraries.
    • company: Inter-org or company-specific libraries. If you define -company-prefixes, a 4th group (project) will be split separately. Otherwise, company libs are part of general.
    • project: Your local project dependencies.
    • blanked: Imports using the _ alias.
    • dotted: Imports using the . alias.

    To define what constitutes a 'company' package, use the -company-prefixes flag with a comma-separated list of prefixes.

    goimports-reviser -company-prefixes github.com/mycompany -imports-order std,general,company,project
  10. Reference: goimports-reviser CLI options

    master

    Full list of available command-line flags for goimports-reviser.

    Usage of goimports-reviser:
      -apply-to-generated-files
        	Apply imports sorting and formatting(if the option is set) to generated files. Generated file is a file with first comment which starts with comment '// Code generated'. Optional parameter.
      -company-prefixes string
        	Company package prefixes which will be placed after 3rd-party group by default(if defined). Values should be comma-separated. Optional parameters.
      -excludes string
        	Exclude files or dirs, example: '.git/,proto/*.go'.
      -file-path string
        	Deprecated. Put file name as an argument(last item) of command line.
      -format
        	Option will perform additional formatting. Optional parameter.
      -imports-order string
        	Your imports groups can be sorted in your way.
        	std - std import group;
        	general - libs for general purpose;
        	company - inter-org or your company libs(if you set '-company-prefixes'-option, then 4th group will be split separately. In other case, it will be the part of general purpose libs);
        	project - your local project dependencies;
        	blanked - imports with "_" alias;
        	dotted - imports with "." alias.
        	Optional parameter. (default "std,general,company,project")
      -list-diff
        	Option will list files whose formatting differs from goimports-reviser. Optional parameter.
      -local string
        	Deprecated
      -output string
        	Can be "file", "write" or "stdout". Whether to write the formatted content back to the file or to stdout. When "write" together with "-list-diff" will list the file name and write back to the file. Optional parameter. (default "file")
      -project-name string
        	Your project name(ex.: github.com/incu6us/goimports-reviser). Optional parameter.
      -recursive
        	Apply rules recursively if target is a directory. In case of ./... execution will be recursively applied by default. Optional parameter.
      -rm-unused
        	Remove unused imports. Optional parameter.
      -separate-named
        	Separate named imports from their group with a new line. Optional parameter.
      -set-alias
        	Set alias for versioned package names, like 'github.com/go-pg/pg/v9'. In this case import will be set as 'pg "github.com/go-pg/pg/v9"'. Optional parameter.
      -set-exit-status
        	set the exit status to 1 if a change is needed/made. Optional parameter.
      -use-cache
        	Use cache to improve performance. Optional parameter.
      -version
        	Show version.
  11. Reference: goimports-reviser CLI flags

    master

    The following flags are available for the goimports-reviser CLI:

    FlagDescription
    -company-prefixesComma-separated company package prefixes.
    -excludesExclude files or dirs (e.g., .git/,proto/*.go).
    -formatPerform additional formatting.
    -imports-orderSort import groups (std, general, company, project, blanked, dotted).
    -list-diffList files whose formatting differs from the tool's rules.
    -outputWhere to send output: file, write, or stdout.
    -project-nameYour project name (e.g., github.com/user/repo).
    -recursiveApply rules recursively to directories.
    -rm-unusedRemove unused imports.
    -set-aliasSet aliases for versioned package names (e.g., pg "github.com/go-pg/pg/v9").
    -set-exit-statusExit with status 1 if a change is needed/made.
    -separate-namedSeparate named imports from the rest of the imports per group.
    -use-cacheUse a local cache to improve performance.
    -apply-to-generated-filesApply rules to files starting with // Code generated.

    Deprecated Flags:

    • -local: Use -company-prefixes instead.
    • -file-path: Put the file name as the last argument instead.