Lazy programmers — Way to GO!. I am not a programmer. I belong to a…
By: Ganesh Rangamani
Tags: Programming, Go, Golang

I am not a programmer. I belong to a profession that most guys prefer to keep secret, especially if the question is asked in a nice bar with lots of pretty gals around. Yes, I am an Accountant.

Having said that, I do dabble in programming often to solve specific client issues. This could involve some AWK stuff to massage text files or Excel VBA macros that makes the pain of spreadsheets somewhat bearable. They may not pass muster from real geeks but they get the job done. Some of them have lived on for years, surprising me. Some business software that I use involve scripting in VB or VB-like proprietary languages.

For a recent gig, I had to write a simple program that loads text files to a SQL table. Yes, that is all it does. I had someone do that in Python as I was busy with other aspects of that assignment.

What I got back horrified me! Yes, the code worked, but then I had to install not just Python but various other stuff that involved some 25 million small files that took ages to zip or backup. OK, I am exaggerating, but you get the point. Needless to say, all this had to be installed on the customers’ end as well.

Then I thought of Node. But I realised this too involved installing lots of modules just to get some basic stuff done.

I briefly toyed with the idea of using C# to create a Windows program either command line or with some rudimentary UI but gave up soon. The learning curve, at least to me, was a bit too steep. Perhaps now that I have a working code, I might try that route just to see how they compare.

That’s when I discovered GO. As they say, the rest is history!

Get GO-ing with GOLANG

I decided to give it a try, not having written a single line of GO prior to that. In fact, I barely knew it existed.

I downloaded it, installed it and started Googling for some code samples and tips. I am not the type that reads manuals so I did not bother to go through any “Hello, World!” type tutorials. I knew what I wanted and I knew enough of basics like variables, functions, for-next, do-while and stuff. Jump in, learn along the way!

I used Visual Studio Code as my editor. Even that was new to me and I had to get over its annoying features that actually help you, once you get it. For example, adding a import reference first and then using it causes the line to be removed if you save the file. You do it the other way around. Use it and VSC adds it for you!

The beauty of VS Code is that, it integrates very well with Git. Yet another aspect of programming I had never used before. After all Excel VBA macros can’t use Git. I learned a few basics such as staging, commit and branches. That was more than enough. I was able to try out new variations and features without messing up the core code that works.

In other words, I could build incrementally and get back to the start if I screwed up. That’s a lot better than “Code_V12_20201201_1530.txt” type of backups I used to do!

Starting with just getting basic connectivity to SQL Server working to logging errors, reading configuration from JSON files, using command line parameters, zipping up files, I incorporated several cool stuff with the least effort!

Soon I had a functional program that worked well and super fast. Granted my use-case was fairly straightforward.

Features I loved

The biggest plus point is, it creates a small (as in really small) executable that just runs! Mine is still under 8k and it does a lot of useful work. Deploying is trivial and requires nothing on the customer’s servers or PCs. The code is cross platform, although that is not my requirement. Reading JSON based configuration info such as server names and table names is also fairly trivial. It’s all built-in. Database access via SQL is built-in too. You just need a driver. Yes, ADODB or other Microsoft technologies work like that in principle, but then you end up deploying a lot of DLLs that may not work if something changes. In GO everything is self-contained. I could create multiple log files with barely any effort! The defer statement is convenient. It leaves the worry of closing resources to the program. It knows when to do it. We either forget or end up doing it too early or late. Variables can be created on the go using := syntax. While this may sound dangerous ( I use Option Explicit in VBA just to be safe), it works well. Error handling is quite powerful, easy and different from other languages. Functions can, and usually do, return both a value and an error. This dual return concept is quite powerful. For example: conn, err := sql.Open(“sqlserver”, connString). This encourages me to include error handling in practically every line of working code by habit. Creating and manipulating zip files is also built in. So is handling files, folders and other similar things. There are numerous “packages” that come standard with GO. I tried creating a HTML report of actions performed and summary of results, using a template. The template can have predefined content, formatting and marker text like {{ .item1 }} that get replaced by the program during run. This is extremely powerful and straightforward!. It is perhaps unique to GO. This is again a built-in feature. Simple conventions like using function names that begin with Upper case decide if it is available to outside programs. Date and time handling is superb. Again, this is built-in. You can parse dates and format them in various ways.

I could GO on and on. To cut a long story short, I was hooked.

There’s a long way to GO

There is obviously a lot more to learn. I should perhaps go back, read a real manual or tutorial and learn more about things like slices and pointers, just to get the foundation strong. But the fact that I could get a useful program going without doing so, speaks a lot of the versatility of GOLANG.

Surprisingly, I got by without even figuring out how to do basic debugging with VS Code. It is a lot more complex than pressing F8 in Excel VBA. I just used log entries and guess-work to identify bugs. Naturally this wont work for bigger projects.

I have also not yet figured out the module concept well. A lot of documentation and online search results give you old style answers. This is no fault of GO just my own lack of effort.

Most tutorials seem to spend a lot of time on writing test routines parallel to developing code. I skimmed through some of them but I failed to understand the point. May be I am missing something. Most errors happen when users do things you never even imagined they would do! I would rather spend time throwing things at my program to see where it breaks.

The bad

Of course, GO is not without its issues.

One big minus to me is that it doesn’t make it possible to generate Windows front ends. So writing a simple program that has a rudimentary UI to capture input is impossible, at least to my knowledge.

The one way to get around this is to use a web front end. Thankfully GO seems to make this easy too. I have not yet gone down this route. For now I stick to command line.

As I mentioned earlier, while VS Code has great integration with GO and Git, it still makes debugging complex. I am yet to figure it out. Editors made for GO are not freeware.

Tips for newcomers (or is it new goers?)

Try it out! Just identify a practical use case and get GO-ing. That is a lot better than reading tutorials that deal with artificially simplified scenarios. Perhaps taking an existing, working program and converting is a good way to start. Use Git from the word GO! Try to use lots of small functions not pages of code. That way parts can be upgraded, replaced on the GO! Check for errors and produce logs. Don’t panic! Just assume anything user keys in is crap and even dangerous. GO makes it easy to use parameter driver queries (like SELECT Field1 FROM myTable WHERE ValueDate = @P1) instead of concatenating strings. This supposedly is safer from attacks. Use it! Log extensively. This can be turned off when you are more sure. It is also a nice way to de-bug.

Happy learning!