在结构体字段中使用指针?

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

Having pointers inside struct fields?

问题

如果我需要访问结构体中的一个整数(int),我可以使用指针接收器(pointer receiver),但我也可以使用一个带有指向结构体字段的指针的接收器,如下所示:

type Something struct {
	counter  *int
	counter1 int
}

所以上述的counter实际上是一个指针,因此我可以通过接收器来访问它并更新它。

当然,另一种方式是将其保留为非指针,并使用指针接收器。

据我所知,将指针放入结构体字段中的原因是为了确保您不会得到默认值-对于int类型,int将得到0作为默认值,但也许您想表示它是缺失的。

但是,仅仅为了避免使用指针接收器而向结构体添加指针会导致问题或不推荐吗?

考虑到Go语言中的惯例是如果一个接收器是指针接收器,那么所有的接收器都应该是指针接收器。

提前感谢您的回答。

我已经证明了使用指针接收器和仅使用带有接收器的结构体指针会得到相同的结果。

英文:

If I need access to an int on a struct then I can use a pointer receiver, but I can also use a receiver with a pointer add to the struct field i.e.

type Something struct {
	counter  *int
	counter1 int
}

So the above counter, is actually a pointer so I can get access to it and update it from a receiver.

Of course this other way is to leave it as a non pointer and use a pointer recevier.

As far as I ma aware, the reasons for placing pointers into fields of a struct is to ensure you don't get a default value - in case of an int, the int would get 0 as a default value but maybe you want to present that it is missing.

But adding a pointer to a struct just so you don't need to use a pointer receiver would cause problems or is recommended ?

Considering the pattern in go is if 1 receiver is a pointer receiver then all receviers should be pointer receivers

Thanks in advance

I have proved that both a pointer receiver and just a pointer on a struct with a receiver, gives the same results.

答案1

得分: 1

这个问题中有几个问题:

通常情况下,如果你想要修改结构体中的值,你需要使用指针接收器。确实,如果结构体中有指针值,你可以修改它们所指向的值,但你仍然不能修改它们指向的位置。

将指针放入结构体的原因因情况而异,防止默认值只是其中之一。

以下陈述是错误的:

“考虑到Go语言中的模式,如果一个接收器是指针接收器,那么所有的接收器都应该是指针接收器”

使用指针接收器定义的方法只能用于可寻址的类型实例。使用值接收器定义的方法可以用于指针和值类型的实例。你可以同时定义使用指针和值接收器的方法。

通常情况下,如果一个方法修改了结构体的值,那么该方法必须使用指针接收器。

简而言之,如何在结构体中使用指针字段取决于具体情况。没有通用的规则或模式需要遵循。

英文:

There are several issues within this question:

In general, if you want to change the values in a struct, you have to use a pointer receiver. It is correct that if the struct has pointer values you can modify the value they are pointing to, but you still cannot modify where it points.

The reason for putting pointers varies from situation to situation, and preventing a default value is just one of them.

The following statement is wrong:

"Considering the pattern in go is if 1 receiver is a pointer receiver then all receviers should be pointer receivers"

Methods defined with pointer receivers are only defined for addressable instances of the type. Methods defined with value receivers are defined for both pointer and value instances of a type. You can define methods with both pointer and value receivers.

In general, if a method modifies the values of a struct, that method must use a pointer receiver.

So, in short, how you use pointer fields in a struct depends on the situation. There is no general rule or pattern you need to follow.

huangapple
  • 本文由 发表于 2023年7月13日 00:39:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76672790.html
匿名

发表评论

匿名网友

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

确定