你可以使用GolangCI来删除未使用的导入。

huangapple go评论99阅读模式
英文:

How do you remove unused imports using GolangCI

问题

我在我的 makefile 中启用了 goimports,通过使用 --fix 标签,我的 GolangCI 工具能够发现未使用的导入,但不能自动删除它们。如何使我的 golangCI 工具能够自动删除未使用的导入?

以下是我在 makefile 中用于 golangci 代码检查的命令,我使用了 --fix 标签:

##@ 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

以下是我的 golangci.yaml 文件,我将 remove-unused 设置为 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 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

答案1

得分: 3

我不确定 golangci-lint 是否可以进行原地修复。

最简单的方法是使用 goimports 工具来删除未使用的导入。

$ go install golang.org/x/tools/cmd/goimports@latest

使用 -w 选项调用它,可以直接在原地修复你的导入,例如:

 $ goimports -w sourcefile.go
英文:

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

答案2

得分: 1

我非常喜欢使用goimports-reviser。我觉得它比goimports更好,因为它不仅在使用-rm-unused标志调用时删除未使用的导入,还重新分组和排序导入。默认情况下,它将标准库、外部库和本地库导入分别放在不同的组中,并且还支持自定义公司前缀,如果你想为你的组织单独创建一个组。

英文:

I'm very happy using goimports-reviser. I find it superior to goimports as it not only removes unused imports when called with -rm-unused fla,g but also re-groups and sorts imports. By default, it arranges std, external and local imports in separate groups and there's also support for custom company prefix if you would like a separate group for your organization.

huangapple
  • 本文由 发表于 2023年3月5日 11:01:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75639971.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定