In my exploration with Golang, I have encountered different ways of returning values from a method. To make this easier, I'll play off an example. Assume that we have two methods that return a single Corgi
struct and a slice of Corgi
.
Coming from Java/C# land, one way of achieving this is to include the value or pointer as part of the return with something like this:
func GetCorgi(id int) (Corgi, error) {
// Do stuff and return a Corgi struct or error
}
func GetAllCorgis() ([]Corgi, error) {
// Do stuff and return a slice of Corgi structs or error
}
However, I've noticed other APIs like App Engine and MongoDB, use this approach for some of their methods where you pass a pointer which then gets populated.
App Engine Get
func Get(c context.Context, key *Key, dst interface{}) error
func (q *Query) One(result interface{}) (err error)
func (q *Query) All(result interface{}) error
Which in my case may look like this
func GetCorgi(id int, corgi *Corgi) error {
// Populate corgi and return error
}
func GetAllCorgis(corgis *[]Corgi) error {
// Populate corgi and return error
}
Is this a matter of preference/style? Or are there advantages with one approach?