About unused variable in golang

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

About unused variable in golang

问题

为什么如果我们在切片中追加数据并且不使用它(程序中的变量mySlice),为什么不会抛出"golang未使用的变量"错误消息。谢谢

#示例

var mySlice []string

mySlice = append(mySlice, "halo")

并且与映射(map)具有相同的行为

#示例

var my = map[string]int{}

my["tes"] = 1
英文:

why if we append data in slice and not use it (variable mySlice in the program), why not throw the error message "unused variable" golang. Thankyou

#example

 var mySlice []string

 mySlice = append(mySlice,"halo")

and same behaviour with map

#example

var my = map[string]int{}

my["tes"]=1

答案1

得分: 5

规范:变量声明:

> 实现限制:如果变量从未被使用,编译器可能会禁止在函数体内声明变量。

正如你所看到的,编译器可能会接受或不接受未使用的变量。当前的Go编译器通过检查变量是否被读取来实现这一点。_读取_变量意味着变量被使用。

如果你不读取一个变量,编译器会给出"未使用的变量"(或者更准确地说是"声明但未使用的变量")的错误。

例如:

var i int
i = 3

尽管在上面的例子中给i赋了一个值,但由于它没有被读取,这是一个编译时错误。

使用append()向切片追加元素涉及到读取切片(切片被传递给append())。

给映射的键赋值涉及到读取映射的值。

如果不使用append(),而是直接给切片元素赋值:

var i = []int{0}
i[0] = 1

这是可以的,因为给i[0]赋值涉及到读取切片头部,以确定我们要赋值的元素的地址。

但是,如果我们像这样给i本身赋值:

var i = []int{0}
i = []int{1}

这个赋值不涉及到读取i,所以也是一个编译时错误。

如果我们使用结构体,例如:

type Point struct{ x, y int }

var p Point
p.x = 1

这仍然可以编译通过,即使赋值实际上并没有涉及读取p结构体。我们可以将其视为"识别" p.x 字段涉及到访问p变量。

这是一个有点"灰色地带",基于Go语言作者的观点来决定和实现的。

英文:

Spec: Variable declarations:

> Implementation restriction: A compiler may make it illegal to declare a variable inside a function body if the variable is never used.

As you can see, a compiler may or may not accept unused variables. The current Go compiler implements this by checking if the variable is read. Reading a variable means the variable is used.

The "unused variable" (or rather "declared but not used") is given by the compiler if you don't read a variable.

For example:

var i int
i = 3

Even though you are assigning a value to i in the above example, since it is not read, this is a compile-time error.

Appending to a slice using append() involves reading the slice (the slice is passed to append()).

Assigning a value to a map key involves reading the map value.

Same goes if you don't use append() but "directly" assign a value to a slice element:

var i = []int{0}
i[0] = 1

This is OK because assigning to i[0] involves reading the slice header to determine the (address of the) element to which we're assigning.

But if we were to assign to i itself like this:

var i = []int{0}
i = []int{1}

This assignment doesn't involve reading i so it is also a compile-time error.

If we were to use a struct for example:

type Point struct{ x, y int }

var p Point
p.x = 1

This still compiles, even though the assignment doesn't really involve reading the p struct. We might think of it as "identifying" the p.x field involves accessing the p variable.

This is a little bit of "gray area" and is based on opinion of the Go authors, but this is how they decided and implemented it.

huangapple
  • 本文由 发表于 2021年6月17日 17:37:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/68016729.html
匿名

发表评论

匿名网友

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

确定