An Introduction to the Go Programming Language (And Why I Love It)
If you follow me on Twitter, you’ll no doubt know that I’m a huge fan of Go. You may have read my endless stream of Tweets ogling its brilliance, complaining about its few pitfalls, and bathing in the sweet, sweet light that is the over-indulgent default package set.
But why? Why am I constantly marveling over this language, and frequently choosing it above others? I hope to clear that point up in this blog, introduce you to Golang, its wonders, its pitfalls, and introduce you to how SendGrid is now embracing Go; and rebuilding some of our core features in the language!
Back to Basics! Go is a concurrent programming language introduced by Google in 2009. It’s been mentioned by the creators that Go was originally developed as a language for servers; but has grown, alongside its epic community, into something much more general purpose.
Coming from a C background, Go was an obvious choice for me. I had also previously been writing a lot of Clojure, as I needed a functional, concurrent language. Clojure will always have a place in my heart. I love the parentheses. The problem was the JVM. I simply hate Java and the JVM. I didn’t have a clue what was happening at compile time, and found it impossible to debug errors. When my fellow engineers started using Go, my interest was naturally sparked.
To me, Go is just like short-hand C. Anything I could write in C, I could write in Go 5x faster, with minimal errors and actually ENJOY writing it. (Rarely the case in C.) The garbage collection is very well done, and the lack of malloc makes me a happy boy! When it comes to writing Go, I find myself enjoying 99% of it, being pushed further and further, and finding inspiration where there once was none.
One of my favourite things about Go is the sheer speed it can achieve. Where my C code runs mega fast, my Go code is actually not too far behind! Before looking at Go benchmarking, I’d like to introduce you to common and basic Go syntax, some basic types, and compare it to C.
So you’re thinking Go sounds like an interesting and tempting venture? Good on you! Let me convince you a little more by showing you the beautiful syntax and comparing it to C to boast simplicity!
In Go, our variable declaration is incredibly concise. Instead of explicitly declaring the type of our variable, type inference assigns the type and initializes our variable for us. To declare a simple integer variable…
And in C:
See what I mean? Shorthand C! And that’s not nearly it…no more semicolons! Functions can return multiple named values, and returning a:
Pair is the standard way to handle errors in Go. Error handling is one of my favorite features in Go. It’s super nice to do and very simple to get a grasp of. Obviously, I don’t have the space here, nor do you want me to list ALL of Go’s types and features, so let’s move on to look at a Hello World program in Go, and one in C.
Now, I may have shot myself in the foot ever-so-slightly on this one, as I actually think the C example is better looking. Regardless of looks; functionally, I prefer the Go example as it sets us up more readily for packaging up our software. I however, did not write these examples to say which is nicer. I wrote them to show how similar they are.
When I call Go “Shorthand C,” it really makes sense when you look at writing more complex programs, dealing with large programs, and concurrency. There is a really neat blog post here comparing C and Go, and detailing some of the finer things about the Go vs. C syntax.
Alright, now that we’ve gone over some of the basics, and seen the Go syntax vs. the C syntax, let’s get heavy. LOAD TESTING! It’s time to see if Go can really hold a candle to other languages, and if the claims of many developers are indeed true. (Here’s a hint–they are.)
Now then, down to the interesting part–the nitty gritty, the part we’ve all been waiting for! How speedy is Go? From my experience having written both C and Go, I am already aware of Go’s speed relative to C. But let’s have a look at how Go stacks up next to Ruby running a variety of simple but repetitive programs.
YUP!! You read that right! As you can see from the stats above, Golang is incredibly more efficient than standard Ruby! In our first program, ‘fasta-redux’, the program took 110.37 seconds to execute and exit. That’s over a minute! Go, on the other hand, did it in an astonishing 1.79 seconds! Almost 100x times faster!
Despite what I may often say, it’s not all about the speed. Go also handles concurrency in a much more favorable way than most languages. Go uses very lightweight threads called Goroutines. These Goroutines can be spun up in their thousands and by using channels, they can concurrently pass messages around in your program.
So, we’ve got useful default packages, super fast speeds, an excellent syntax, awesome concurrency, and hundreds of great online resources to help you along your Go journey. So what’s missing?
Well, like anything, Go isn’t 100% perfect. In my opinion, some of the function outputs from the default packages aren’t as good as they could be, and the default testing suite leaves a lot to be desired. There are a few more things that niggle at me, but I’ll leave that to another blog post!
Going back to the good points of Go–it is now being widely adapted by startups and enterprise companies alike. Many Saas/Paas providers use Go in their projects, and SendGrid is now adopting it. At SendGrid, we’re rewriting features of our core system in Go. This aims to make SendGrid more robust, faster, and even more reliable!
To wrap this up, if you’re looking for a concurrent, parallel, simple, sexy, and awesome programming language to learn, look no further! Go is definitely worth taking a look at!
I’ve listed some excellent online resources to start your journey below!