GO lang: A Pythonista’s journey.
GO (or GOLang) is the language that many Tech Startups and Companies are transitioning into and adopting. In this article I talk about my journey from a full-time Python user to an Engineer who uses a mix of both. Also I have shared the path that you can follow that will make learning GOlang simpler and quicker.
What is GO ?
I am going to write this down in words that made most sense to me.
GO or GOlang is a compilable programming language that you get if Python and C have a baby. It has the sexiness of Python and athletic abilities of C.
It is a convention to use the term GOlang over GO because if one were to do a google search like the following example, Google’s search engine would not be able to interpret if you were talking about the language or something entirely different.
Example: if you run a Google search on phrase GO playground, Google would return a list of playgrounds. But if you were to say GOlang playground, Google would pull up links related to the programming language GO.
Why are Companies/Startups switching to it?
The following are the reasons why:
- Compiled Binaries: When you have written your web service or web app, GO has the ability to wrap up all the files and libraries that your app depends upon into a single file, a stand alone complied binary. That means if someone makes fundamental changes to the libraries after the app was compiled, the compiled binary will NOT break (given that it was working in the first place).
- GOlang is faster: As most Python users would know, even though Python is a phenomenal language with a massive community and library resources, it is slow. GOlang is probably one of the only languages that tries to utilize the all the available processors on a machine, by default.
- GOlang is built to handle High Concurrency: GOlang’s key selling point, at least according to me, is it’s ability to communicate with multiple processes and keep track of which processes requested for what and when. How does it do this? With something called context.
- GOlang can implement parallelism: GOlang is very good in running tasks in parallel. GOroutines, channels, atomic and wait-groups are all entities that exist in the language that make implementing parallelism super easy.
My thoughts on learning GO lang
Having used python as my work horse for most of my career to date, the first thing that popped up while learning GO lang was that the thought process while dealing with GO lang is slightly different than that of while working with Python.
To keep things simple, think of learning two languages, English and French, certain words over lap in both the languages but have different meanings in each and even though they look similar the grammar is different. The sentence “petite bites” has a very different meaning in both languages. This can be confusing especially if you toggle between the two languages (Python and GO lang) on a regular basis, but it becomes second nature over time.
- Package: When writing code in a GOlang package, everything in the package is looked at as if it is in one big file by the compiler, even if you have multiple files in a package. In other words, you can have a function sitting in file foo.go and use functions in another file, fob.go, without having to worry about importing the file, like one would in python. Variables, structures, interfaces, etc can be defined anywhere in a package as well.
- Context : It is by far one of the most useful things about GOlang. Here’s a good resource to learn more about it.
- GOroutines : 1 man takes 4 days to build a wall. 4 men take 1 day. This is exactly what GOroutines do. They help implement Parallelism.
- Channels and Wait-groups: Although both of of these are almost never used together, both of them help avoid race conditions, i.e. when 2 or more processes try to manipulate / access a resource at the same time. This is a good resource to give a visual understanding of Race Condition.
- Compiled Binaries : This insures two things, firstly all my dependencies and my code are compiled into one bundle whose contents will remain the same irrespective of future changes to both code and dependencies, as long you don’t re-compile. Secondly, since it is a binary, it’s executes faster.
- It’s strict : Even though you have to define every single struct, interface, data structure that you will use and it can be a pain to deal with at first. Over time as your project get’s more complex, this becomes a boon. Lesser chances of breaking code, when someone pushes their changes to production. It also doesn’t allow you to waste memory by defining a variable that you will never use.
- Memory Efficient : It is also very memory efficient, thanks to implementation of pointers.
- Built in Testing mechanism : Golang test files run tests that you define and in turn give you detailed document highlighting your test coverage , wether your tests Passed or Failed and the time take to run everything.
The Cons :
- It’s strict : Sometime I wish GOlang wasn’t as strict. This can be a hinderance when try to explore JSON responses, etc from a 3rd party API that has little to no documentation. GO expects you to define what the response will look like before actually making the response, this can be over looked using interfaces but it’s not the most comfortable process. But feature exists because when you do actually have your code in production then you would only want things that you define to be captured and not something that you weren’t expecting.
- Methods : When code is implemented in Factory Method pattern, then it becomes a pain to test newly added methods, without alot of overhead.
- Very Small community compared to Python and other languages: Most often you have to find things by digging through docs or the code and NOT stackoverflow.
- $GOPATH and $GOROOT : If you have used/setup GO before you know what I am talking about. GO requires you to build all your code under a certain path, that you define as GOPATH in your bash_profile.
What is the best route to learn GOlang (from a Python users point of view)
I found the below approach (in the given order) to work best:
A) Tour GOlang(Only for those who are too eager to dive in head first): This is the official guide to get started with GO lang and doesn’t require you to setup GO on your local machine. While it is not the best resource to get up to speed with the language, a quick run through to all the topics helps you get familiar with a few critical changes.
Go through the below 3 links, keep in mind cover a minimum of 2 and a maximum of 3 concepts per session, any more would just confuse you, especially if you are seasoned Python user.
Packages, Variables and Functions: https://tour.golang.org/basics/1
Flow Control statements: https://tour.golang.org/flowcontrol/1
Structs, Slices and Maps: https://tour.golang.org/moretypes/1
B) Youtube Series (up-till slices): Now that you are ready to install the go environment and start diving in slightly deeper, go through this series:
if the installation instructions in the video don’t work do the below:
brew install go
sudo nano .bash_profile
inside the .bash_profile file add this
C) Youtube video for maps, structs:
I found the below videos to be helpful in understanding maps
D) Webservice is GOlang:
The below video should be more than enough to get you started.
OR (either work)
The main thing is to Design, Architect and Engineer your product the right way and then pick each tool for task that it is better suited for. If you do it all right, the end result would be a work of art.