Saturday, 15 September 2012

Backwards Thinking

I'm a golang retard. I can't help it; I keep thinking backwards. I keep thinking in old paradigms.

I had to serialise a structure into JSON and wanted to suppress nil values.

type Data struct {
    Batch []map[string]interface{}

Now, I tried using the omitempty tag and that didn't work as the library only operates on structure fields and not the elements of the array.

So....... I sat there...... and I thought......... right I need a method on this structure that will serialise it into JSON and omit the individual array elements that are not yet populated.

Then I sat there and thought............that's a 'lot' of typing........there must be a better way...........

I sat.....

I sat a bit more......

I got up for a mind a video I had seen a few months back.........

Ken: “The thing I'm most pleased about is slices”.


batch.Batch = batch.Batch[0:pos]
b, e := json.Marshal(batch)

This may have been immediately obvious to many people; but it wasn't to me.

Part of my brain is still stuck in the old world of OOP. Oh, I must have a method to work on an objects data. I can't copy arrays; it's too expensive. I must type pages and pages of code to do 'real work'.

I think it's time for me to re-read effective go.

Perhaps we all should. We're probably missing more than we realise.