如何禁用删除未使用的导入? [英] How to disable removing unused imports?

查看:111
本文介绍了如何禁用删除未使用的导入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用goimports自动重新格式化我的代码,使其干净,并保持导入顺序.但是有时,它与IDE自动保存一起以不舒服的方式工作.就像在这种情况下一样:

I use goimports to automatically reformat my code and make it clean and also keep import in order. But sometimes it works in an incomfortable way together with IDE autosave. Like in this scenario:

  1. 我想将新程序包添加到我的代码中.

  1. I want to add new package to my code.

然后我用dep ensure安装它并刷新我的IDE缓存.

Then I install it with dep ensure and refresh my IDE cache.

我可以轻松地使用具有IDE自动完成和导航功能的软件包.

I can comfortably use the package with IDE autocomplete and navigation.

goimports和IDE会发生什么?

What happens with goimports and IDE?

  1. 我添加了新包裹
  2. 要运行dep ensure,我必须保存一个文件
  3. goimports查找未使用的软件包并将其从导入中删除.不好.
  1. I add new package
  2. In order to run dep ensure I have to save a file
  3. goimports finds unused package and removes it from imports. Not good.

解决方法:

A.使用dep ensure -add %packagename%

B.禁用goimports并移至gofmt-一般.

B. Disable goimports and move to gofmt - so-so.

C.不要在保存时自动格式化代码-不可取.

C. Do not autoformat code on save - undesirable.

如何设置更方便的东西?

How to setup things more convenient?

P.S.不确定标签godeps,但DEP在这里肯定是错误的.

P.S. Unsure about tag godeps but DEP is definitly wrong here.

推荐答案

正确的解决方案是自己调用dep ensure -add package/name,然后开始使用包而不手动导入,IDE会指出您要使用该软件包,并将自动添加导入.您正在遇到的当前行为正是IDE所期望的,因为goimports是在保存时调用的,并且由于未使用导入,因此它将被自动删除.

The correct solution would be to invoke dep ensure -add package/name yourself and then just start using the package without manually importing it, the IDE will figure out that you want to use that package and will add the import automatically. The current behavior you are experiencing is exactly what's expected from the IDE since goimports is invoked on save and because the import is not used it will be automatically removed.

这篇关于如何禁用删除未使用的导入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆