如何禁用Golang未使用的函数错误?

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

How to disable Golang unused function error?

问题

我正在尝试通过注释掉一个函数来调试程序,以查看这对结果的影响。但是,注释掉这个函数意味着它未被使用,这会导致Go抛出一个"unused function"错误。我该如何临时禁用这个错误,以便我可以调试我的程序,而不必重写整个程序来调试一个小部分呢?

我知道未使用的导入和变量可以被忽略(如下所述),但找不到有关忽略未使用函数的任何信息。

要禁用/忽略未使用的导入错误,只需在包名前面加上_

import (
  "fmt" // 正常导入包的方式
  _ "log" // 导入包并忽略未使用的导入错误
)

要禁用/忽略未使用的变量,可以将变量重命名为_

myvar, _ := some_function()

但是如何忽略未使用的函数呢?

这是我收到的错误消息的屏幕截图

英文:

I'm trying to debug a program by commenting out a function and seeing how this affects the result. BUT commenting out this function means that it is unused which results in Go throwing an "unused function" error. How do I temporarily disable this error so that I can debug my program without having to re-write my entire program just to debug a small part?

I know that unused imports and variables can be ignored (detailed below), but can't find anything about ignoring unused functions.

To disable/ignore the unused import error simply prepend an _ to the package name.

 import (
   "fmt" // how you normally import packages
   _"log" // how you import packages and ignore the unused import error
)

To disable/ignore an unused variable you can re-name the variable _.

myvar, _ := some_function()

But how do you ignore an unused function?

Here's a screenshot of the error message I'm getting.

答案1

得分: 13

根据你的截图,看起来你正在使用Go Staticcheck(或者你的IDE正在使用)。如果你真的需要出于某种原因禁用这个警告,你可以参考Staticcheck文档。像这样的注释应该可以实现:

//lint:ignore U1000 Ignore unused function temporarily for debugging
英文:

From your screenshot, it looks like you are using Go Staticcheck (or your IDE is). If you really need to disable this warning for whatever reason, you can follow the Staticcheck documentation. A comment like this should do it:

//lint:ignore U1000 Ignore unused function temporarily for debugging

huangapple
  • 本文由 发表于 2021年12月3日 09:25:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/70208440.html
匿名

发表评论

匿名网友

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

确定