Q:

golang: return a pointer or pass a reference

What is the best way to "build" an object.
Leme write some code:
type Car struct {
   Wheels int
   Doors  int
} 

This cars are stored somewhere, somehow. So should my interface be the type of 
func (s Store) GetCar() *Car

or should I make it
func (s Store) GetCar(*Car)

and pass a reference to a variable?
Im looking for some sort of rule of thumb.
Thanks!

A:

Go manage the heap/stack with looking the reference goes outside of scope. So you can return the pointer without any worries.
func (s *Store) GetCar() *Car {
    return &Car{Store: s}
}

A:

The most common way to do that would be to write it as:
func (s Store) GetCar() *Car

Or, if you don't want to use pointers, you can do it like:
func (s Store) GetCar() Car

The other alternative, making it GetCar(aCar *Car) might work, but it will not be as clear since it's not obvious that aCar should be sent empty and then populated by the function.

A:

In many cases this is preferable
func (s Store) GetCar() *Car

because it is more convenient and readable, but has consequences. All variables such as Car are created inside the function which means they are placed onto stack. After function return this memory of stack is marked as invalid and can be used again. It a bit differs for pointer values such as *Car. Because pointer is virtually means you want to share the value with other scope and return an address, the value has to be stored somewhere in order to be available for calling function. It is copied onto heap memory and stays there until garbage collection finds no references to it.
It implies overheads:

copying values from stack to heap 
additional work for garbage collection

The overheads is not significant if the value is relatively small. This is a reason why we have to pass an argument in io.Reader and io.Writer rather than have the value in return.
If you'd like to dive yourself into guts follow the links:
Language Mechanics On Stacks And Pointers
and 
  Bad Go: pointer returns

