How to disable Golang unused import error

前端 未结 8 2056
我寻月下人不归
我寻月下人不归 2020-12-05 03:30

By default, Go treats unused import as error, forcing you to delete the import. I want to know if there exists some hope to change to this behavior, e.g. reducing it to warn

8条回答
  •  眼角桃花
    2020-12-05 04:16

    I have the same problem. I understand the reasoning for why they implemented the language to disallow unused imports and variables, but I personally find this feature annoying when writing my code. To get around this, I changed around my compiler to allow optional flags for allowing unused variables and imports in my code.

    If you are interested, you can see this at https://github.com/dtnewman/modified_golang_compiler.

    Now, I can simply run code with a command such as go run -gcflags '-unused_pkgs' test.go and it will not throw these "unused import" errors. If I leave out these flags, then it returns to the default of not allowing unused imports.

    Doing this only required a few simple changes. Go purists will probably not be happy with these changes since there is good reason to not allow unused variables/imports, but I personally agree with you that this issue makes it much less enjoyable to code in Go which is why I made these changes to my compiler.

提交回复
热议问题