“已声明但未使用的变量”编译错误

发布于 2024-08-10 17:54:30 字数 505 浏览 8 评论 0原文

我正在学习 Google 的新语言 Go。我只是在尝试一些东西,我注意到 如果您声明一个变量并且不对其执行任何操作,则 go 编译器(在我的例子中为 8g)将无法
编译时出现以下错误:hello.go:9: error声明但未使用。我对此感到惊讶,因为大多数语言编译器只是警告您未使用的变量,但仍然编译。

无论如何我可以解决这个问题吗?我检查了编译器的文档,没有看到任何会改变此行为的内容。有没有办法只删除 error 以便编译?

package main

import "fmt"
import "os"

func main()
{
     fmt.Printf("Hello World\n");
     cwd, error := os.Getwd();
     fmt.Printf(cwd);
}

I am learning Google's new language Go. I am just trying stuff out and I noticed
that if you declare a variable and do not do anything with it the go compiler (8g in my case) fails to
compile with this error: hello.go:9: error declared and not used. I was suprised at this since most language compilers just warn you about unused variables but still compile.

Is there anyway I can get around this? I checked the documentation for the compiler and I don't see anything that would change this behaviour. Is there a way to just delete error so that this will compile?

package main

import "fmt"
import "os"

func main()
{
     fmt.Printf("Hello World\n");
     cwd, error := os.Getwd();
     fmt.Printf(cwd);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

最佳男配角 2024-08-17 17:54:30

你可以尝试这个:

cwd, _ := os.Getwd();

但似乎最好保留朱莉的答案中的错误,这样你就知道是否出了问题。

You could try this:

cwd, _ := os.Getwd();

but it seems like it would be better to keep the error like in Jurily's answer so you know if something went wrong.

不再见 2024-08-17 17:54:30

这可能会让开发变得有点痛苦。有时我运行的代码已声明但未使用变量(但将使用)。

在这些情况下,我简单地这样做:

fmt.Printf("%v %v %v",somevar1,somevar2,somevar3)

在那里,它们被“使用”。

我希望看到 go 工具有一个标志,可以让我在开发时抑制此错误。

this can make development a bit of a pain. sometimes i run code that has variables declared but not used (but will be used).

in these cases i simple do this:

fmt.Printf("%v %v %v",somevar1,somevar2,somevar3)

and there, they are "used".

i'd like to see a flag to the go tools that lets me suppress this error while developing.

凉月流沐 2024-08-17 17:54:30

这有效吗?

cwd, error := os.Getwd();
if error == nil {
    fmt.Printf(cwd);
}

Does this work?

cwd, error := os.Getwd();
if error == nil {
    fmt.Printf(cwd);
}
清晨说晚安 2024-08-17 17:54:30

来找出错误是什么?

cwd, err := os.Getwd();
if err != nil {
    fmt.Printf("Error from Getwd: %s\n", err)
}

您可以通过导入“fmt”并使用它打印什么

You can find out what the error is by importing "fmt" and using

cwd, err := os.Getwd();
if err != nil {
    fmt.Printf("Error from Getwd: %s\n", err)
}

What does it print?

┈┾☆殇 2024-08-17 17:54:30

我和你有同样的情况。来自文档

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

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

在开发代码时,临时创建这些情况是很常见的,并且在程序编译之前必须将它们编辑掉可能会很烦人。

有些人要求提供编译器选项来关闭这些检查或至少将其减少为警告。不过,这样的选项尚未添加,因为编译器选项不应影响语言的语义,并且 Go 编译器不会报告警告,只会报告阻止编译的错误。

没有警告的原因有两个。首先,如果值得抱怨,就值得在代码中修复。 (如果不值得修复,那就不值得一提。)其次,让编译器生成警告会鼓励实现对可能使编译产生噪音的弱情况发出警告,从而掩盖应该修复的实际错误。

不过,解决这种情况很容易。使用空白标识符可以让未使用的内容在开发过程中保留下来。

导入“未使用”

// 该声明将导入标记为通过引用来使用
// 包裹中的物品。
var _ =used.Item // TODO:提交前删除!

函数主() {
    debugData := debug.Profile()
    _ = debugData // 仅在调试期间使用。
    ....
}

I had the same situation as you. From the docs:

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.
    ....
}
风流物 2024-08-17 17:54:30

如果你真的只是想消除编译错误,你可以尝试类似“a = a”或“error = error”的操作。

来自这里一些人的论点指出,此类编译错误很棒,因为它们可以防止很多麻烦,这在大多数情况下都是正确的,因此您应该避免此类构造。另一方面,我很喜欢测试我编写的代码是否真的可以编译!在这种情况下,这很好,不必删除所有声明的 & 。未使用的变量。因此,很少使用“a = a”结构,并且不要将它们留在那里!

If you really just wanna remove the compile error, you can try something like 'a = a', or 'error = error'.

The arguments coming from some people here, stating that such compile errors are great because they prevent a lot of cruft are true for most situation, so you should avoid such constructs. On the other side I quite like to test, whether the code I write does actually compile! And in that case it's good, not having to remove all declared & unused variables. So use the 'a = a' construct rarely and don't leave them there!

我也只是我 2024-08-17 17:54:30

您可以通过以下两种方法之一解决未使用变量的问题。

  1. 通过解决错误

    cwd, error := os.Getwd() if error !=nil{ fmt.Println(error) }

  2. 这里我们不需要值本身,所以我们忽略它
    空白标识符“_”

    cwd, _ := os.Getwd()

You can resolve unused variable issue by following one of these two methods.

  1. By solving the error

    cwd, error := os.Getwd() if error !=nil{ fmt.Println(error) }

  2. Here we didn’t need the value itself, so we ignored it with the
    blank identifier "_"

    cwd, _ := os.Getwd()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文