Introduction to Go — Part 1

Ido Magor
7 min readAug 28, 2020

For the last year, a good friend of mine was working with Go and I’ve heard a lot of good things about it, so I had to check what the fuss is all about.

Go has a top-notch concurrency model that allows us to write concurrent programs with a very high throughput, which is kind of awesome regarding the development of backend applications and utilizing our cloud machine to its best.

From what I’ve so far experienced, Go tries to bring the best of worlds from programming in C and Python, which both of them I love because each one in their area has amazing features.

Honest disclaimer — I’m not a pro Go developer but I did want to share my few insights so far in a summarized way, so this is to help others who are trying to get into Go faster.

So without any further a due, let’s get down into business.

Compiled language

Go is actually a compiled language.
That means it gets compiled to machine code that can get executed immediately without any virtual environment or interpreter for that matter.

This gives a huge boost in terms of performance because we’re executing a machine code binary file.

But there’s also something else which is quite an amazing feature in Go.
Go does on the compilation phase detective work in order to see if our “code smells”.

What I mean by that it prevents us to run or compile our go program when we’ve got the following cases:

  • Imports that aren’t used.
  • Unused variables/functions.
  • Improper operations like dividing by zero.

These features are amazing because everyone who starts with a language, and even the pro ones, which are sometimes trying to release their application version at night with everyone dealing more and more stress on them, might do errors in terms of “code smells” and not on purpose.

This is overall, helps not only to prevent buggy or misread application but also to help every other fellow programmer to understand how to write code better.

More about compilation

One of my biggest headaches while developing in C/C++ is using libraries.

At first, we need to install our libraries for our relevant OS, and afterward configure our project to know where to find the .h and .c/.cpp files.

Sounds easy right?
Well, it does at first, but what about when we switch stations, or when we want to auto compile in our CI servers?
Those are simple cases, and there are many more complicated cases that cause a lot of more headaches that many of us needed to get through while developing in C/C++.

So how Go solves all of this?
Go treats the libraries we download in a similar way that Python does.
It stores our libraries in a well-defined file hierarchy structure he handles for us, isn’t that great?

What that actually does to us developers is preventing us to be a super-duper compilation process professors, and not deal with errors of compilation and linkage which doesn’t find our libraries properly.

The sweet power of simplicity

As with anything that it gets improved over time as it should be, there are improvements and mismatches between versions of software programming languages that we use today.

Not that’s a bad thing, but that is something that can cause programming languages to get complicated over time, and be a little hard for new developers or even older ones to stay updated with the language.

Just for the example, the main versions of C++ as of today are — 98, 03, 11, 14, 17, 20, and upcoming 23.
I heard a lot of cases in which some people only know C++ 98 and 03 but aren’t even familiar with other versions, and that happens quite a lot due to the need to adapt to new features and ways of coding in the new versions.

For the ones who love to learn on a professor level(Work hard, play hard!), there are more versions to it, so if you want to get more familiar with them which I highly recommend, simply go to this link.

That’s only an example of a programming language, and it happens in many more.
Of course, as said before that doesn’t say that C++ is a bad language because I think like any other programming language that it’s great, and for each one of them we need to choose the one that suits our needs the best.

Each language is getting improved in time, and that sometimes means deleting some features and of course adding new ones.

That mismatches of versions, removal of features, the addition of features, and many more are a subject that Go tries to prevent.

Go tries to stay as simple as possible in order to tell anyone what it has in a simple way. That leads to the ease of getting to know Go pretty fast, and starting to develop everything you want with high throughput in its concurrency model and the ease to start coding right away.

As of today, Go is planning to add generics to it which might seem like a crucial part for a programming language, but they only start to add it after hearing from the community that it’s needed, which is also a hard huge bonus to see that Go listens a lot to its developers.

Hello World!

Go as with many other languages but in a different name, we have packages.

Packages are a simple way for Go to wrap our *.go files in a package that is accessible across our application according to the package name.

A famous package name is actually the main package.
The main package could have only one *.go file, which says to Go that file should have inside a function called main which needs to run.

And a simple example would look like this:

The import keyword allows us to import packages and packages/libraries to use.

In this example, we used the famous “fmt” package which helps to do a lot of functionality regarding I/O operations like printing to the console.

Variables and stuff

Variables in Go can be instantiated in a dynamic way like Python.
That means we don’t need to specify the variable type but only declare and give it a value, which by default Go will decide its type according to the value it gets.

We have the option to declare variables in the following ways:

You might be right now as I was when I saw the “:=” on line 4, but don’t get frightened, everything will be clear in a second.

In Go we have of course the const variable which you might have guessed is a variable that cannot be altered.

About the var variable.
We have the option to declare a variable with it in two ways.
One is with a variable without telling the type, which would be decided by Go according to the value, and the other way is only declaring with the default value according to the type which is a must in order to understand which default value to put inside.

But what about line 4?!$
Actually it’s quite simple.
If we want to have the power of both worlds of var which is not declare the variable type and have a mutable variable, using the := can be a great way to declare a variable for us.

So if you ask now when to use “:=“ or var, you should simply see if you need to declare the type of the variable and not necessarily initialize it with a value then use var, and if not both of them could be great, just don't forget the extra “:” ;)

In conclusion

If you’ve reached this far it means you now know what is Go in a whole and what is the motivation behind it.

Go has a lot of more features to offer and we’re going to cover them in the next series which I highly recommend you read.

Regarding this piece, as we saw, Go came in order to solve many issues and combine a lot of great features in order to allow a very fast time to market and high performance.

Today Go community is growing by the day, and there’s a reason for that, so I hope in this piece I’ve intrigued you to read more about Go and what it does.

If you’re hungry for more, feel free to hit the №2 series part of this post:

--

--

Ido Magor

My biggest passions are learning and improving each day, in what I do and why I do it.