Golang: Get the function caller’s name

Problem

Consider this code:

In the function foo, we want to get the name of the function (and preferably file name and number ) that called it.

Solution

We can get this information by traversing the stack trace (which Go’s “runtime” package has handy functions for:

Golang: Connect to Postgres and selecting data from a table

You will need to get the Postgres driver:

Connecting to Postgres

Selecting data from a table

After connecting to the database, you can do the following:

Earn more by learning Go!

According to this article a friend forwarded to me, software developers who learn Go, Python or Scala (along with tech like Apache Spark) have found it easier to find jobs with better salaries. Learning Scala caused the highest boost and in the second place was Go.

Learning python is a bit subjective in terms of getting a raise. It depends on what you are doing and which field you are in. So the increase in income with Python isn’t as consistent as it is with the other 2 languages.

Here is the article in detail: http://www.infoworld.com/article/3071623/salary/want-to-boost-your-salary-learn-scala-golang-or-python.html

Golang: Testing HTTP requests

Unit testing HTTP calls to external services is pretty easy in Go. Let’s say we have some code that makes HTTP requests like so:

We can test if the request is made and we get the response we want by mocking the external service. Below is the code:

Golang: Make HTTP requests

A simple GET request can be made like this:

POST request:

If more control is needed, like specifying headers, cookies, etc:

Golang: Polymorphism

Polymorphism is a bit different in Go. It supports polymorphism only through interfaces (and not through inheritance). To help you understand better, here is an example:

However, Polymorphism doesn’t work when driveACar(lammborgini) would not work if Car was a struct and not an interface. Here is an example:

Don’t waste time

A friend of mine sent me an interesting quote by Imam Ghazali which puts things in perspective.

Your time should not be without any structure, such that you occupy yourself arbitrarily with whatever comes along.
Rather, you must take account of yourself and order your worship during the day and the night, assigning to each period of time an activity that must not be neglected nor replaced by another activity.
By this ordering of time, the blessing in time will show itself. A person who leaves himself without a plan as animals do, not knowing what he is to do at any given moment, will spend most of his time fruitlessly.
Your time is your life, and your life is your capital: by it you make your trade, and by it you will reach the eternal bounties in the proximity of Allah.
Every single breath of yours is a priceless jewel, because it is irreplaceable; once it is gone, there is no return for it.
So do not be like fools who rejoice each day as their wealth increases while their lives decrease. What good is there in wealth that increases while one’s lifespan decreases?
Do not rejoice except in an increase of knowledge or an increase of good works.
Truly they are your two friends who will accompany you in your grave, when your spouse, your wealth, your children, and your friends will remain behind.
— Imam al-Ghazali

Go: Declaring and using objects

Go (or Golang) doesn’t have the concept of classes. The class equivalent in Go loos like this:

The above declaration can now be used as an object like so:

First impressions of Go

I have been in search for a language I can use for web programming, writing computationally intensive and general scripting among other things. The language that came close to fitting all of these things was Racket, a dialect of Chicken Lisp. It can even do geometry drawings. However, there were issues with it so I ended up concluding it wasn’t that language in was in search of.

I started learning Go some time ago and, at first, found its syntax backwards. Declaring types after variable names was hard to get used to. And, the syntax for writing “classes” is even more weird. However, when I found out that Go supported concurrency (and parallelism) natively, I was intrigued. On a side note: default Python and Ruby interpreters do not support parallelism. PHP has some support through pthreads but using it could mean you not being able to use other extensions.

If you want to have a true parallel app in Ruby or Python you have to get the interpreter that runs on JVM. So, you Ruby code is run by the Ruby interpreter which is running on JVM. Ruby and Python support concurrency but their threads run one at a time. So, no parallelism.

Once you get past the syntax weirdness, Go is actually a pretty cool language. It makes it easy to write concurrent apps. They call them “go routines”. From what I have learned, go routines are cheaper to create and maintain than normal threads and the way Go is designed, it is very easy to communicate between threads (using channels). And, all of this makes sense since one of the goals of Go was to make multi-threaded network applications.

Those used to dynamic languages might find it a bit difficult to work with Go in the beginning as it is strongly typed. I was, however, able to adjust pretty easily (then again I have done C++, Java and C# in the past). Your mileage may vary but I highly recommend trying it out. You might like it.

Programming Macros for Ergodox

The default configuration tool on Input Club or Massdrop don’t allow for macro programming. So you will have to get your hands dirty and mess with the firmware code a little. It’s not hard.

  1. Run the following commands on your terminal to clone Ben Blazak’s firmware code from github and create a custom layout for yourself

  2. Open keyboard/ergodox/options.mk and add qwerty–custom to the KEYBOARD_LAYOUTS and change the KEYBOARD_LAYOUT to qwerty–custom. The keyboard layout will look something like this :

  3. Edit keyboard/ergodox/layout/fragments/macros.part.h and create a function for your macro. As an example, we will do a macro that prints b4.

  4. Now that the macro is ready, it can be placed in the layout. Put m_b4 for whichever key you want the macro to trigger. Edit firmware/keyboard/ergodox/layouts/qwerty–custom.c and put your macro in it.

  5. In the terminal, run make It will create firmware.hex
    Upload the file to Ergodox and your macro will be ready for use.

You can look up the hex codes for keys here: http://www.usb.org/developers/hidpage/Hut1_12v2.pdf