Categories

Why I use Go?

Johan Sim Jian An 18 May 2019 All Blogs

Background

I have been using Go for 4 years. For the most part, Go is a very great language. However, I always found something is lacking/missing. There are things that Go could have done better.

So I embarked on a journey to Rust and Elixir at the same time. Each of these languages bring something that Go doesn’t have. They are supposed to be better than Go. However, there is something in Go that kept me going back.

I have never try to properly note down what I feel about using Go but I am attempting to do it now. This should also serve as a reminder to myself on why I have settled on Go.

What is Go?

Go is just another programming language. But every programming language is just a programming language. So let’s look at what the creator says:

Go is a programming language designed by Google to help solve Google’s problems, and Google has big problems. — Rob Pike

So what are Google’s problem? To be honest, I doesn’t have Google’s problem because I don’t work for them. The companies that I have worked for and working on don’t have Google’s problem either. We have our own problems. So what is our problem? How Go is helping me on that?

Before Go

Before I am a Go programmer, I was a Python programmer. Python is a very great programmer language. But it is lacking a type system which is actually helping me a lot now. It is also a lot saner to work in a bigger system with type system as it solves a lot of headache trying to figure out different parts of the code. I broke out of Python into Go mainly because of that.

So what are the improvements that I have gotten?

More confidence in my code

If my code compiles, the likelihood of it breaking down because of some unexpected error is smaller. It is not to say that it is impossible. But just lower chance. That’s good enough.

Single binary and cross platform

To deliver what I have written, I can compile it down to single binary on my development machine and run it on the target machine. No more copying of the Python files and installing Python interpreter on the target machine.

Some people could argue that with the rise of Docker, this should not be an issue for Python as well. But when I started using Go, Docker is still picking up its steam.

So there are only 2 points? That’s because Python has definitely done a lot of good stuffs and Go is on par on those things. So what did I lost?

Established web framework

I was using Django to develop web application. I could easily add functionality into my application by adding third party libraries. There is a certain code structure convention that I can follow. I can run the server in interactive or hot code reload mode for faster development time. There is a single recommended database migration tool. Things are very straight forward.

So do I really miss that? No. for all parts, I could find an alternative in Go although they are not bundled as a single framework. To a certain extent, I can feel hates within Go community when someone is trying to propose a web framework. This is mainly based on the articles and comments that I read online and I could be wrong.

On Go

Since its inception, Go has clearly and slowly beginning to be a very popular programming language for a lot of different reasons. So let’s see why I use Go.

Concurrency

goroutine for the win. In the web application that I have developed, whenever I need to perform some background task within a single HTTP request, I can always use go easily to create a new goroutine.

Concurrency made easier (Playground)

Whenever I need to execute huge amount of requests to a server (API server, database server) concurrently, I can quickly setup a goroutine worker pool and send my task through channel for them to pick up.

Read this Go blog for more ideas on how you can utilise goroutine.

Clarity

It is very easy to understand a new Go codebase because there is very little room for magic. A lot of the web framework out there seems convenient or productive when starting out a new project. But as features being added on, things starts to get complicated and you could find that the framework is actually preventing you from doing certain things. When that happens, people will try to be smart and things start to get messy.

Go is a small language. Its specification can be read in short amount of time. That mean there is less way of doing things. You can understand the language easily. Go leaves a lot of modern language features out but that helps you to write programs that can be understood easily.

Another thing that help with clarity is the Go’s error handling. Instead of throwing exceptions and hopefully catch it somewhere in the code, Go uses multiple return value with the last one being error. Example:

Error handling (Playground)

Go disallows unused imports and variables during compilation. That means less clutter in the code that would get in the way when I am reading a code base.

Does not compile (Playground)

Productivity

Go compiles fast. This mean faster feedback cycle. As a programmer, I write code and I would test it and run it in a very short amount of time. This saves me significant amount of time. From my experience, I have not wait for my build for any longer than a minute. For most of the smaller applications that I have written, the compilation time is under 5 seconds.

Because Go is a small language, I can easily memorise all different syntax provided by the language. When coding, I can focus more on the problem to be solved rather than worrying about whether I can using the best features from the language.

Go’s standard packages is also big. I can build web server using the net/http package. You have encoding/json and encoding/xml to help with the JSON and XML body. The standard packages are also well thought and they complement each others in a nice way.

Go provides a lot of tools. Go pioneered the code formatting trend by gofmt. Then we have goimports and goreturns. We also have a list of useful code analysis tool, like go vetgolint and many other community built. We have great code editors support — Visual Studio CodeGoLand and others.

Summary

Go is not a perfect language but it is a good language for programming. By providing just enough, it enables programmers to write better codes.

Less is exponentially more. — Rob Pike

Go Programming

Leave a Reply

*
*
*