I am looking into golang as my new goto language for my web-backend. My current language there is PHP, where I implemented a datastructure resembling the Backbone Collection/Model structure.
excursus Collection / Model
This means I have a Collection
containing multiple Models
. Models
have an attributes
dictionary for storing the actual "public data". Collections
have multiple convenience methods operating on the array of Models
(sort, getByIndex, getByMatchingParameter, etc). Models
also have their share of convenience methods.
In daily life there will be 90% of "things" that are represented the same way. For example I can store an Address
the same way I store my User
. Having an AddressesCollection
containing my Addresses
I can simply ask my AddressesCollection
to give me all AddressModels
where city=Berlin
. And implementing this matching in my base class I can do the logically same thing on my UserCollection
giving me all my Users which are deactivated by deactivated=true
.
But sometimes (10%) I need a special method. getGeoData
for Addresses
for example. Now, in my oop-world, I can simply create a new class extending my BaseModel
adding my method and use my extended class everywhere where my base class was used.
My "i simply don't get it"
How does this work in Go. I read a lot introductions into embedding / composition. But simply do not get my head around this.
This is where I stand, keep in mind this is my playground for learning the "inheritance" concept, not for having a production ready Collection / Model implementation.
What I get is that I haven't "understood" how to write a "generic" method accepting not-quite-inherited types. I have the feeling that I have to use an interface there somewhere to get this "inheritance"/"extended class" feeling. But my current use of this is... well...
But maybe I am completely wrong in my concept of implementing my desired structure that way in go at all? Maybe there is a much more elegant way. Any hints greatly appreciated!