去语言警告和错误 [英] Go language warnings and errors

查看:77
本文介绍了去语言警告和错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看起来GO语言没有警告。我观察到
几个实例。
1.已声明且未使用(如果变量已声明且未在任何地方使用
,则会导致错误并且不会编译程序)
2.已导入且未使用(类似如果软件包被导入,并且
没有在任何地方使用,那么它会给出错误并且不会编译程序)
有人可以帮忙。如果他们有任何指针。

解决方案


Go编程语言
常见问题



我可以停止这些关于我的未使用的变量/导入的投诉吗?

未使用的变量可能表示有错误,而未使用的
导入只会减慢编译的速度。在您的代码树中积累足够多的未使用的导入
,事情会变得非常缓慢。由于这些原因,Go
都不允许。



在开发代码时,通常会暂时创建这些情况
,并且可能很烦人在
程序编译之前将它们编辑出去。



有些人要求使用编译器选项来关闭这些支票,或者至少将
减少到警告。但这样的选项尚未添加,
,因为编译器选项不应影响
语言的语义,并且因为Go编译器不报告警告,所以
只包含阻止编译的错误。 / p>

没有警告有两个原因。首先,如果值得
抱怨,那么值得在代码中修复。 (如果不是
值得修复,那就不值得一提)。其次,让编译器
产生警告会鼓励实现警告弱
的情况,这可能会导致编译噪声,掩盖真正的错误那应该是
是固定的。



不过,很容易解决这种情况。

 导入未使用的$ b $使用空白标识
让未使用的东西在开发时保持不变b
//这个声明标记了通过引用包中的
//项来使用的导入。
var _ = unused.Item // TODO:提交前删除!

func main(){
debugData:= debug.Profile()
_ = debugData //仅在调试过程中使用。
....
}



It seems that GO language does not have warnings in it. I've observed few instances. 1. "declared and not used"(if variable is declared and not used anywhere it gives an error and does not compile the program) 2. "imported and not used"(similarly if package is imported and not used anywhere it gives an error and does not compile the program) Can somebody help. If they have any pointers.

The Go Programming Language FAQ

Can I stop these complaints about my unused variable/import?

The presence of an unused variable may indicate a bug, while unused imports just slow down compilation. Accumulate enough unused imports in your code tree and things can get very slow. For these reasons, Go allows neither.

When developing code, it's common to create these situations temporarily and it can be annoying to have to edit them out before the program will compile.

Some have asked for a compiler option to turn those checks off or at least reduce them to warnings. Such an option has not been added, though, because compiler options should not affect the semantics of the language and because the Go compiler does not report warnings, only errors that prevent compilation.

There are two reasons for having no warnings. First, if it's worth complaining about, it's worth fixing in the code. (And if it's not worth fixing, it's not worth mentioning.) Second, having the compiler generate warnings encourages the implementation to warn about weak cases that can make compilation noisy, masking real errors that should be fixed.

It's easy to address the situation, though. Use the blank identifier to let unused things persist while you're developing.

import "unused"

// This declaration marks the import as used by referencing an
// item from the package.
var _ = unused.Item  // TODO: Delete before committing!

func main() {
    debugData := debug.Profile()
    _ = debugData // Used only during debugging.
    ....
}

这篇关于去语言警告和错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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