嵌套的Go语言结构体中指针的使用方法

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

Usage of pointers in nest golang structs

问题

我是你的中文翻译助手,以下是翻译好的内容:

我对golang还不熟悉,正在学习如何在嵌套的struct类型中进行解引用操作。
当我搜索嵌套结构时,通常会建议使用@OneOfOne在https://stackoverflow.com/questions/24809235/how-to-initialize-a-nested-struct中提到的方法。

然而,在阅读工作中的代码库时,我注意到团队也使用了嵌套指针。我对何时使用这种方式感到困惑。例如,嵌套结构req Person与嵌套指针req *Person之间的区别是什么?

示例:

  • Person

    type Person struct {
        Name string
    	Age  int8
    }
    
  • args

    type args struct {
        req *Person
    }
    
英文:

I'm new to golang and learning how dereferencing works in nested struct types.
When I searched for nested structs normally they would suggest the method suggested by @OneOfOne in https://stackoverflow.com/questions/24809235/how-to-initialize-a-nested-struct

However while reading the codebase at work i noticed the team also uses nested pointers. I'm confused when I should use this. ie. nested struct req Person vs a nested pointer req *Person?

example

  • Person

    type Person struct {
        Name string
    	Age  int8
    }
    
  • args

    type args struct {
        req *Person
    }
    

答案1

得分: 1

结构属性中指针的一个常见用例(除了结构大小之外)是该属性的可选性。虽然我不知道具体的用例,但我可以猜测它可能指的是某种可选关系。

例如:一个Customer结构有一个LastOrder结构属性。由于顾客可能还没有下过任何订单,将其保留为指针引用可能是有意义的,以表示它可能为空。

使用指针属性的另一个用例是在类似图形或引用数据结构中。想象一个Person结构,它有一个Mother和一个Father属性。如果将它们设置为Person类型,编译器会返回一个错误,因为结果结构将无限递归。在这种情况下,它们必须也被设置为指针。

希望这个答案有所帮助。

英文:

One common use case for pointers in struct attributes (besides struct size) is the optionality of the said attribute. While I do not know about particular use case, I can guess that it might refer to some sort of an optional relationship.

For example: A Customer struct having a LastOrder struct attribute. Since the customer may not even have made a single order yet, it might make sense to keep this as a pointer reference, in order ti signal that it may be nil.

Another use case of using pointer attributes is in graph-like or referential data structures. Think about a Person struct who has both a Mother and a Father attributes. If you set those to be of type Person, the compiler will come back with an error, because the resulting structure will recurse ad-infinitum. In that case, those must be set as pointers too.

Hope the answer helps.

答案2

得分: 0

当你想要一个可选字段时,应该使用指针(因为在初始化变量时可以简单地将其赋值为nil指针),当大小较大且你想要提高应用程序的性能时,或者如果你想要一个指向相同类型变量的字段时,你应该使用指针。例如:

type Node struct {
    Data int
    Left *Node
    Right *Node
}
英文:

You should use pointers when you want it to be an optional field (because you can simply assign a nil pointer to it when initializing a variable), when a size is big and you want to increase the app's performance, or if you want to have a field that points to a variable of the same type e.g:

type Node struct {
    Data int
    Left *Node
    Right *Node
}

huangapple
  • 本文由 发表于 2021年11月7日 13:31:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/69870054.html
匿名

发表评论

匿名网友

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

确定