I enabled goimports in my GolangCI tool using my makefile and it's able to spot unused imports but is not automatically remove them. How do I enable my golangCI tool to remove the unused imports automatically?
Below is my makefile command for the golangci linting, I am using the --fix
tag:
##@ Linting
lint:
@echo "lint via golangci-lint in " $(OUTPUT_DIR)/src
docker run --rm -v $(PWD):/local \
-w /local golangci/golangci-lint:latest \
golangci-lint run --fix --config .golangci.yaml $(OUTPUT_DIR)/src/*.go
Below is my golangci.yaml file, I am setting remove-unused
to true :
run:
timeout: 5m
modules-download-mode: readonly
linters:
enable:
- errcheck
- goimports
- revive
- govet
- staticcheck
# Configuration for the goimports linter
goimports:
# Set to true to remove unused imports automatically
remove-unused: true
# Configuration for the revive linter
revive:
# Add any custom rules you want to use
rules:
- id: 'import-shadowing'
severity: warning
match: '\bimport\s+\.\s+\S+'
message: 'Importing packages using dot notation (.) is discouraged.'
issues:
exclude-use-default: false
max-issues-per-linter: 0
max-same-issues: 0
I'm not sure if golangci-lint
can do in-place fixes.
The easiest way to remove unused imports would be to use the goimports
tool.
$ go install golang.org/x/tools/cmd/goimports@latest
Call it with the "-w" option to fix your imports directly in-place, e.g.
$ goimports -w sourcefile.go