Code Monkey home page Code Monkey logo

goimportx's Introduction

goimportx

Go License: MIT Go Reference

中文|English

goimportx is a tool to help you manage your go imports.

Features

  • Automatically sort and group your go imports.
  • Support custom group rules.
  • Support write result to file.
  • Only support go module.
  • Use golang official sort algorithm.
  • Automatically remove parentheses when there is only one import.
  • Automatically remove duplicate empty new-line.
Features goimports goimportx
Automatically sort and group your go imports
Support custom group rules
Support write result to file
Automatically remove parentheses when there is only one import
Automatically remove duplicate empty new-line
Support mutilple files, mutilple dirtories

Installation

$ go install github.com/anqiansong/goimportx@latest

Usage

$ goimportx --dir /path/to/dir --file /path/to/file.go

Help

goimportx --help
sort and group go imports

Usage:
  goimportx [flags]

Examples:
goimportx --dir path/to/your/dir --file /path/to/file.go --group "system,local,third"

Flags:
  -d, --dir strings    file directory
  -f, --file string    file path
  -g, --group string   group rule, split by comma, only supports [system,local,third,others] (default "system,local,third")
  -h, --help           help for goimportx
  -v, --version        version for goimportx
  -w, --write          write result to (source) file instead of stdout

goimportx's People

Contributors

kesonan avatar

Stargazers

 avatar Siddhartha Basu avatar ccoVeille avatar Oleg Balunenko avatar shitposts avatar 当大灰 avatar Nikolay Dubina avatar myoung-su,shin avatar Sergey Cheung avatar emilien puget avatar

Watchers

ccoVeille avatar  avatar

Forkers

golang-libs

goimportx's Issues

Support Directory Arg

Nice project. I'd like to use it on my whole project.

Right now here is workaround.

find . -name '*.go' ! -path './vendor/*' -exec goimportx --write --file {} \;

Also it panicked against my vendor folder.

panic: invalid line number 0 (should be >= 1)

goroutine 1 [running]:
go/token.(*File).LineStart(0x1400010dbc0?, 0x3?)
	/opt/homebrew/Cellar/go/1.20.6/libexec/src/go/token/position.go:212 +0x1cc
go/ast.sortSpecs(0x1?, 0x1400019f8b8, {0x1400010dc80, 0x3, 0x4})
	/opt/homebrew/Cellar/go/1.20.6/libexec/src/go/ast/import.go:118 +0x18c
go/ast.SortImports(0x1?, 0x1400019f8b8)
	/opt/homebrew/Cellar/go/1.20.6/libexec/src/go/ast/import.go:39 +0x350
github.com/anqiansong/goimportx/pkg/importx.(*ImportSorter).Sort(0x1400013a1b0?, {0x14000148c00, 0x4, 0x1400019faf0?})
	/Users/ashtonian/go/pkg/mod/github.com/anqiansong/[email protected]/pkg/importx/sort.go:25 +0xd4
github.com/anqiansong/goimportx/pkg/importx.Sort({0x16f407139?, 0x0?}, {0x0?, 0x0?})
	/Users/ashtonian/go/pkg/mod/github.com/anqiansong/[email protected]/pkg/importx/importx.go:163 +0x49c
github.com/anqiansong/goimportx/cmd.glob..func1(0x100e9d0e0?, {0x100c37281?, 0x3?, 0x3?})
	/Users/ashtonian/go/pkg/mod/github.com/anqiansong/[email protected]/cmd/cmd.go:21 +0x4c
github.com/spf13/cobra.(*Command).execute(0x100e9d0e0, {0x1400010c050, 0x3, 0x3})
	/Users/ashtonian/go/pkg/mod/github.com/spf13/[email protected]/command.go:916 +0x5c8
github.com/spf13/cobra.(*Command).ExecuteC(0x100e9d0e0)
	/Users/ashtonian/go/pkg/mod/github.com/spf13/[email protected]/command.go:1044 +0x35c
github.com/spf13/cobra.(*Command).Execute(...)
	/Users/ashtonian/go/pkg/mod/github.com/spf13/[email protected]/command.go:968
github.com/anqiansong/goimportx/cmd.Execute()
	/Users/ashtonian/go/pkg/mod/github.com/anqiansong/[email protected]/cmd/cmd.go:50 +0xb4
main.main()
	/Users/ashtonian/go/pkg/mod/github.com/anqiansong/[email protected]/main.go:6 +0x1c

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.