2

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 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
    }
    
Buthetleon
  • 1,205
  • 3
  • 14
  • 24
  • 5
    The general guidelines for value vs pointers apply to struct fields. See https://stackoverflow.com/q/23542989/5728991 – Charlie Tumahai Nov 07 '21 at 05:40
  • is this to do with `args` possibly a large struct hence we should pass a pointer instead? https://stackoverflow.com/questions/23542989/pointers-vs-values-in-parameters-and-return-values – Buthetleon Nov 07 '21 at 05:40
  • `args` as shown here is *not* big: it has one thing in it, which is the `*Person` (pointer to Person) named `req`. So in general if you're passing an `args` *value* to some function `f`, you'd just pass the value itself. That function `f` can then use `args.req` to access the `*Person`, which it can then use to access the target `Person` or even change the target `Person`. But it can't change the `*Person` *value* of the *caller*. If the caller needs `f` to *set* `args.req` to `&somebody`, then `f` needs `*args` and not just `args`. – torek Nov 07 '21 at 06:07
  • @torek I was about to write something similar as an answer. If you would write your comment as an answer I will +1 it, because I believe that the fact that the receiving function may change the values of the fields on `Person` is what makes the functional difference. Aside from the fact that the passed struct may be bigger or smaller. – Bazzz Nov 07 '21 at 08:48
  • @Bazzz well, yes, but it's covered in Cerise Limón's link too. – torek Nov 07 '21 at 08:52
  • @torek I still think your comment would be the correct answer to this question. And nevertheless, even if the correct answer can be found behind a certain link, it should still be written here, as links may die. Since you already wrote it as a comment, it may be a small effort to change it into an answer. – Bazzz Nov 07 '21 at 11:26

2 Answers2

1

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.

Preslav Rachev
  • 3,983
  • 6
  • 39
  • 63
  • If you pass a pointer to a function, the function can mutate the data at that address. If you pass a copy of the value, it can only mutate its copy. That's a quintessential use of pointers, whether or not they're in a `struct`. Pointer receivers can mutate their receiver through the pointer - passing in memory wouldn't work. Pointers also avoid memory allocations since you can pass one address instead of the values of all `struct` fields. – erik258 Nov 07 '21 at 15:42
  • Read the question again. It asks about the specific semantics of using a pointer a struct attribute. Even though you are totally right, I would seriously doubt that someone will pass a copy of a struct with a pointer inside to a function, only so that the function can make use of the pointer semantics. – Preslav Rachev Nov 07 '21 at 16:57
  • I'm also very new to go, and uderstood that pointer arguments for functions allow to modify the referenced "object". Do pointers to a nested struct also allow for easier modification of this nested struct? – dr jerry Jul 10 '22 at 13:27
0

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
}
Jakub Klimek
  • 433
  • 6
  • 18