为什么在Go语言中将变量赋值给_不会引发”declared but not used”错误?

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

Why does assigning a variable to _ doesn't throw declared but not used error in go?

问题

我已经声明了一个变量 x,但没有使用它。

在上面的程序中,我将 x 的值赋给了 _(空白标识符)。

空白标识符是一个只写变量,不能被使用,因此可以说在这里 x 没有被使用。

  1. 为什么它不会报错?难道 x 不是浪费内存吗?
  2. 编译器不应该将 x 视为已声明但未使用吗?
  3. 这是一个 bug 还是有意为之?
  4. 如果是有意为之,背后的原因是什么?
  5. 在上面的程序中,Go 编译器会自动添加任何指令来释放 x 的内存吗?
  6. 如果创建了许多这样的变量并赋值给 _,会对性能产生影响吗?
英文:

I have declared a variable 'x' but haven't used it.

package main

func main() {
 var x int = 10
 _ = x
}

In the above program I am assigning the value of x to _ (blank identifier).

A blank identifier is a write-only variable and cannot be used, as such it is as good as saying that x is not used here.

  1. But why doesn't it throw error? Is not x waste of memory?
  2. Should not the compiler, treat x as declared but not used?
  3. Is it a bug or is it intentional?
  4. If it is intentional, what is the reason behind this?
  5. Does go compiler automatically add any instruction to free x memory in the above program?
  6. If a lot of such variables are created and assigned to _ would it have any performance impact?

答案1

得分: 3

> 但为什么它不会抛出错误?

因为语言规范规定它不应该抛出错误。

> x 不是浪费内存吗?

这是一个实现细节,与语言规范无关。可以合理地假设它不会使用内存,因为那里没有变量,但是有许多 Go 的实现,每个实现都可以在这方面做出自己的决定。

> 编译器不应该将 x 视为已声明但未使用吗?

不应该。

> 这是一个 bug 还是有意为之?

这是有意为之的。

> 如果是有意为之,背后的原因是什么?

除了向语言作者提问,猜测是唯一的可能性,但这在 StackOverflow 上是离题的。

> 在上面的程序中,Go 编译器会自动添加任何指令来释放 x 的内存吗?

同样,这是一个实现细节,不由规范定义,并且可能会发生变化。

> 如果创建了许多这样的变量并将其赋值给 _,是否会对性能产生影响?

可能不会。但再次强调,这是一个实现细节,可能因实现而异。

英文:

> But why doesn't it throw error?

Because the language spec says it shouldn't.

> Is not x waste of memory?

This is an implementation detail, not related to the language spec. One can reasonably assume it's not going to use memory, since there's no variable there, but there are many Go implementations, and each can make its own decision in this regard.

> Should not the compiler, treat x as declared but not used?

No.

> Is it a bug or is it intentional?

It's intentional.

> If it is intentional, what is the reason behind this?

Aside from asking the language author(s), speculation is the only possibility, which is off-topic on StackOverflow.

> Does go compiler automatically add any instruction to free x memory in the above program?

Again, this is an implementation detail, not defined by the spec, and subject to change.

> If a lot of such variables are created and assigned to _ would it have any performance impact?

Probably not. But once again, this is an implementation detail, and could vary between implementations.

答案2

得分: 2

1- 这是一个声明的局部变量,并且被使用。为什么会出错?编译器可能选择通过消除它来进行优化。

2- x 被声明并使用。它的值被读取后被丢弃。

3/4- 这样使用 _ 赋值没有意义。它还有其他用途,例如在 range 语句中。

5- 在上面的例子中,x 没有逃逸到堆上,所以它被分配在栈上。不需要释放它。

英文:

1- It is a declared local variable, and it is used. Why would it be an error? The compiler may choose to optimize it by eliminating it.

2- x is declared, and used. It's value is read, and thrown away

3/4- There is no point in using _ assignment like this. There are other uses of it, for instance, in a range statement

5- Above, x did not escape to heap, so it is allocated on stack. There is no need to free it.

huangapple
  • 本文由 发表于 2021年6月11日 23:08:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/67939420.html
匿名

发表评论

匿名网友

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

确定