[英]go tour when to not use pointer to struct literal in a variable
Per the Go tour page 28 and page 53 按照围棋游览第28 页和第53页
They show a variable that is a pointer to a struct literal. 它们显示一个变量,它是指向struct literal的指针。 Why is this not the default behavior? 为什么这不是默认行为? I'm unfamiliar with C, so it's hard to wrap my head around it. 我不熟悉C,所以我很难绕过它。 The only time I can see when it might not be more beneficial to use a pointer is when the struct literal is unique, and won't be in use for the rest program and so you would want it to be garbage collected as soon as possible. 我唯一能看到使用指针时更有利的时候是struct literal是唯一的,并且不会用于其余的程序,所以你希望它尽快被垃圾收集。 I'm not even sure if a modern language like Go even works that way. 我甚至不确定像Go这样的现代语言是否能以这种方式运作。
My question is this. 我的问题是这个。 When should I assign a pointer to a struct literal to a variable, and when should I assign the struct literal itself? 我应该何时将指向结构文字的指针分配给变量,何时应该分配结构文字本身?
Thanks. 谢谢。
Using a pointer instead of just a struct literal is helpful when 使用指针而不仅仅是结构文字是有用的
In other cases, it's fine to simply use the struct literal. 在其他情况下,只需使用struct literal即可。 For a small struct, you can think about the question just as using an int
or an *int
: most of the times the int is fine but sometimes you pass a pointer so that the receiver can modify your int variable. 对于一个小结构,你可以考虑使用int
或*int
:大多数时候int很好但有时你传递一个指针,以便接收者可以修改你的int变量。
In the Go tour exercises you link to, the Vertex struct is small and has about the same semantic than any number. 在链接到的Go游览练习中,Vertex结构很小,并且语义与任何数字大致相同。 In my opinion it would have been fine to use it as struct directly and to define the Scaled
function in #53 like this : 在我看来,将它直接用作结构并在#53中定义Scaled
函数就好了,如下所示:
func (v Vertex) Scaled(f float64) Vertex {
v.X = v.X * f
v.Y = v.Y * f
return v
}
because having 因为有
v2 := v1.Scaled(5)
would create a new vertex just like 会创建一个新的顶点就像
var f2 float32 = f1 * 5
creates a new float
. 创造一个新的float
。
This is similar to how is handled the standard Time
struct (defined here ), which is usually kept in variables of type Time
and not *Time
. 这与处理标准Time
结构( 此处定义)的方式类似,后者通常保存在Time
类型的变量中,而不是*Time
。
But there is no definite rule and, depending on the use, I could very well have kept both Scale
and Scaled
. 但是没有明确的规则,根据使用情况,我很可能保持Scale
和Scaled
。
You're probably right that most of the time you want pointers, but personally I find the need for an explicit pointer refreshing. 大多数时候你想要指针,你可能是对的,但我个人觉得需要一个明确的指针刷新。 It makes it so there's no difference between int
and MyStruct
. 它使得int
和MyStruct
之间没有区别。 They behave the same way. 他们的行为方式相同。
If you compare this to C# - a language which implements what you are suggesting - I find it confusing that the semantics of this: 如果你将它与C#(一种实现你所建议的语言)进行比较 - 我发现它的语义令人困惑:
static void SomeFunction(Point p)
{
p.x = 1;
}
static void Main()
{
Point p = new Point();
SomeFunction(p);
// what is p.x?
}
Depend on whether or not Point
is defined as a class
or a struct
. 取决于Point
是否被定义为class
或struct
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.