在GoLand中运行“goimports”时保存

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

Running 'goimports' on save in GoLand

问题

我正在使用GoLand IDE编写Go代码。
当我保存文件时,我希望未使用的导入会消失(就像VS Code一样)。

我已经在"Code" -> "Show reformat file dialog"中启用了"Optimize imports"选项。但是当我保存文件时,导入语句仍然存在。

英文:

I'm using GoLand IDE to code in Go.
When I save a file, I'd like that the unused imports disappear (like VS code)

I have enable "Optimize imports" in "Code" -> "Show reformat file dialog". But when I save the file, imports it's still present

答案1

得分: 43

从2021.3版本开始,文件监视器中默认添加了两个单独的条目。一个用于保存时对代码进行格式化,另一个用于保存时对导入进行优化。

关于导入的排序、分组等设置可以在Go的代码样式设置中进一步定义。

在Go的代码样式设置的"其他"选项卡中,还有一个小选项可以在重新格式化时运行gofmt

在2021.3版本之前,设置在文件监视器中设置gofmtgoimports的保存操作。在底部添加一个新的文件监视器,并从列表中选择gofmtgoimports

将其设置为goimports将同时进行以下操作:

  • 代码格式化(gofmt
  • 更新、排序和分组导入
英文:

As of version 2021.3

Two separate entries were added to the File Watchers that are enabled by default. One for formatting the code on save, one for optimising the imports on save:

在GoLand中运行“goimports”时保存

How the imports are sorted, grouped, etc. can be further defined in the Code Style settings for Go:

在GoLand中运行“goimports”时保存

There is another small option to run gofmt on reformat in the Other tab of Go's Code Style settings:

在GoLand中运行“goimports”时保存

Before version 2021.3

The setting to set up gofmt or goimports on save is in the File Watchers. Here you can add a new file watcher at the bottom and select gofmt or goimports from the list.

在GoLand中运行“goimports”时保存

Setting this to goimports will do both:

  • code formatting (gofmt)
  • updating, sorting and grouping imports

答案2

得分: 10

GogLand EAP 16中,"On Save"选项卡已被移除。

在更新的版本中,您需要转到设置-> 工具-> 文件监视器,然后在那里添加goimports

英文:

"On Save" tab was removed in GogLand EAP 16.

In newer versions you have to go to Settings -> Tools -> File Watchers and add goimports there.

答案3

得分: 7

对于在Goland中使用gofmt的问题(mat007的评论)

您可以双击文件监视器中的项目,以显示编辑监视器菜单。

取消选中“自动保存编辑的文件”以触发监视器,现在它只会在手动保存时触发。

在GoLand中运行“goimports”时保存
(Goland 2020.3)

英文:

For those fighting with gofmt on Goland autosave (mat007 comment)

You can double click the item in file watchers to show an edit watcher menu.

Uncheck Auto-save edited files to trigger watcher and it should now only trigger on manual save:

在GoLand中运行“goimports”时保存
(Goland 2020.3)

huangapple
  • 本文由 发表于 2017年8月9日 20:06:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/45590236.html
匿名

发表评论

匿名网友

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

确定