In Go there are various ways to return a struct
value or slice thereof. For individual ones I\'ve seen:
type MyStruct struct {
Val int
}
fu
A case where you generally need to return a pointer is when constructing an instance of some stateful or shareable resource. This is often done by functions prefixed with New.
Because they represent a specific instance of something and they may need to coordinate some activity, it doesn't make a lot of sense to generate duplicated/copied structures representing the same resource -- so the returned pointer acts as the handle to the resource itself.
Some examples:
In other cases, pointers are returned just because the structure may be too large to copy by default:
Alternatively, returning pointers directly could be avoided by instead returning a copy of a structure that contains the pointer internally, but maybe this isn't considered idiomatic: