A command-line utility for cleaning and organizing Go import statements by removing unnecessary aliases and sorting imports alphabetically.
- Remove import aliases: Automatically removes custom aliases from import statements
- Sort imports: Organizes imports alphabetically for better readability
- Deduplicate imports: Removes duplicate import statements
- In-place editing: Modifies files directly, preserving the original structure
- Multiple import formats: Supports both single-line and multi-line import blocks
go get github.com/nayutalienx/go-import-cleanerOr build from source:
git clone https://github.com/nayutalienx/go-import-cleaner.git
cd go-import-cleaner
go build -o go-import-cleaner main.gogo-import-cleaner <path-to-go-file>Basic usage:
go-import-cleaner main.goGet help:
go-import-cleaner help
# or
go-import-cleaner --help
# or
go-import-cleaner -helpInput file:
package main
import (
"os"
filepath "path/filepath"
"strings"
core "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
"github.com/hyperledger/fabric-sdk-go/pkg/core/config/lookup"
pathvar "github.com/hyperledger/fabric-sdk-go/pkg/util/pathvar"
"github.com/spf13/cast"
)
func main() {
// Your code here
}Output file (after processing):
package main
import (
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
"github.com/hyperledger/fabric-sdk-go/pkg/core/config/lookup"
"github.com/hyperledger/fabric-sdk-go/pkg/util/pathvar"
"github.com/spf13/cast"
"os"
"path/filepath"
"strings"
)
func main() {
// Your code here
}The tool supports cleaning both single-line and multi-line import statements:
Multi-line imports (most common):
import (
alias "package/path"
"another/package"
)Single-line aliased imports:
import alias "package/path"- Only processes
.gofiles - Skips files with single imports without aliases
- Modifies files in-place (creates a backup if needed)
- Requires valid Go file structure
- Validation: Ensures the input file has a
.goextension - Parsing: Reads and analyzes import statements in the file
- Cleaning: Removes aliases while preserving the actual package paths
- Sorting: Alphabetically sorts all imports
- Deduplication: Removes any duplicate import entries
- Writing: Saves the cleaned imports back to the original file
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is open source and available under the MIT License.
To run the tool locally:
go run main.go <your-file.go>To build the executable:
go build -o go-import-cleaner main.go