June 19, 2018

What I like about go generate

I like when a project that uses it, compiles and runs before and after go generate. That is a powerful concept, in the example you have constants shown as numbers when they are printed, but after go generate the stringer generated prints the names of that constants.

Go generate is like having make instructions inside your code. Of course you need a lot of imagination to think that a go generate comment is equivalent to  a makefile instruction.

Besides there is not dependency control at all. Like when run depends on build and stuff like that. But it allows you to run commands using the command go generate.

Anything else are just helper functions

Well I am over simplifying here, the fact that go comes with packages to parse, modified and write updated code is very nice but that part could be done with any other tools( like antlr or lex and yacc).

In the last project

It was decided to have a config to describe what to take from a HTTP request (get something from query, header or cookie), then we describe functions that receive that data and returns other data and finally we specified which data need to be stored and how.


So the first implementation was a Proof of concept in which show how to do all the process using a config file. In the past a new service was created to do the same work and there was 4 or 8 of them in production. So the POC shows that it would be easier to port them to this new service just by adding some simple configurations.

The first implementation

The first implementation was though to cover any case possible. It will be super generic code. The idea was just implement what is needed but left all the indirections in place so it would be easy to just implement other parts. 

This didn't finish good, because time bugdet issues. We finish with a room with foundations of a cathedral that had space for the future buttress that maybe would not be required. There was a communication problem because there was time.

The second implementation

So it starts as a POC but it was so simple that it stays and now is running in production. Basically now that the config was well specified, A generator was created to output http.Handlers base on configurations.

The service runs without go generate (not handlers registered). But after it runs, it register the handlers.

All the indirections were removed. Thanks that I received an advice from someone that I respect, he recommend just make something that works and test it, refine and test it and so on.

My life were never more easy I was preparing a service for the future but the future is uncertain.  The only thing we can do is fail fast (Implement and receive feedback as soon as possible.). 

Which is now my new motto.

No comments: