Skip to content

This tool aims to automatically fix the order of golang imports. It will split your imports into three categories: inbuilt, local and external

License

Notifications You must be signed in to change notification settings

jmartac/goimportssort

 
 

Repository files navigation

Fork of goimportssort

This is a fork of AanZee/goimportssort I made to add some personal preferences.

Changes

  • Added a new flag -o to specify the desired order of the imports blocks (default is std,local,external, being those the only valid values).
  • Upgraded the Go version to 1.22.

Installation

$ go install github.com/jmartac/goimportssort

or build it from source:

$ go build -o goimportssort

Usage

$ goimportssort -w -o std,external,local ./...

Original README

go-imports-sort GitHub tag (latest by date) Test golangci-lint

This tool aims to automatically fix the order of golang imports. It will split your imports into three categories.

Features

  • Automatically split your imports in three categories: inbuilt, external and local.
  • Written fully in Golang, no dependencies, works on any platform.
  • Detects Go module name automatically.
  • Orders your imports alphabetically.
  • Removes additional line breaks.
  • No more manually fixing import orders.

Why use this over goimports?

Goimports will not categorize your imports when wrongly formatted. PRs to add in the functionality were denied.

Installation

$ go get -u github.com/AanZee/goimportssort

Usage

usage: goimportssort [flags] [path ...]
  -l    write results to stdout (default false)
  -local string
        put imports beginning with this string after 3rd-party packages; comma-separated list
(default tries to get module name of current directory)
  -v    verbose logging (default false)
  -w    write result to (source) file (default false)

Imports will be sorted according to their categories.

$ goimportssort -v -w ./..

For example:

package main

import (
	"fmt"
	"log"
	APZ "bitbucket.org/example/package/name"
	APA "bitbucket.org/example/package/name"
	"github.com/AanZee/goimportssort/package2"
	"github.com/AanZee/goimportssort/package1"
)
import (
	"net/http/httptest"
)

import "bitbucket.org/example/package/name2"
import "bitbucket.org/example/package/name3"
import "bitbucket.org/example/package/name4"

will be transformed into:

package main

import (
    "fmt"
    "log"
    "net/http/httptest"

    APA "bitbucket.org/example/package/name"
    APZ "bitbucket.org/example/package/name"
    "bitbucket.org/example/package/name2"
    "bitbucket.org/example/package/name3"
    "bitbucket.org/example/package/name4"

    "github.com/AanZee/goimportssort/package1"
    "github.com/AanZee/goimportssort/package2"
)

About

This tool aims to automatically fix the order of golang imports. It will split your imports into three categories: inbuilt, local and external

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%