Intro to Golang


Posted on

An Introduction to the Go Programming Language (And Why I Love It)

golang gopherIf 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!

Ready!

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.

Steady!

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…

In Go:

myVar := 33

And in C:

int myVar = 33;

See what I mean? Shorthand C! And that’s not nearly it…no more semicolons! Functions can return multiple named values, and returning a:

result, err

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.

helloworld.c

helloworld.go

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.)

Go!

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.

Golang vs Ruby Stats

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?

Summary

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!

– @rbin


SendGrid Devangelist. London based polyglot hacker, mainly playing with Golang and Ruby. Musical Hacker, Hardware hacker, Hackathon lover, API abuser and NoSQL user.

Robin Johnson on Twitter
Have thoughts on this post?
Chat with us about it on Twitter and Google+