Richard Bucker

GoLang when to use a struct and when to use a pointer?

Posted at — Sep 12, 2013

The GoLang language experts like using pointers and I have to agree that it’s better to pass around pointers than it is full data structures. Of course this upsets the garbage collector and it’s not very functional when you start talking about immutable data… On the other hand there are a number of one-liners that make programming fun.

Here is a pointer version:

query := new(Query)query.I = 1query.Select()

Here is a struct version:
Query{I:1}.Select()
In this case the Query structure is meant to hold a collection of named parameters for a well known DB query and it is very short lived. In the struct version the select() can be called as a one liner making the code very simple and short.