From JavaScript to Go

From JavaScript to Go

Years ago I started programming in PHP - I hated it. I don't mean to dunk on PHP, I know lots of people use it & love it but it was not for me. A few years later I started again and went the route of JavaScript, I loved it and was quickly building both client & server apps.

The good parts of JavaScript

It's so damn easy. The learning curve is very shallow and it's one of the easiest languages to get productive with quickly. People who are learning might well disagree with this but they're wrong and I know why. JavaScript is really easy, but programming is hard and when you're first learning then you're struggling to learn programming and JavaScript at the same time. Once you've learned the basics of programming you'll understand what I mean.

JavaScript is dynamically typed & garbage collection. This reduces the learning curve and takes away one of the most painful aspects of programming - memory management. For those that aren't sure what I mean, Garbage Collection is the idea that once a variable is out of scope it's automatically removed from memory & made available to your computer or other programs. Not having to declare if a variable is a string, integer, array etc does help early on, you can focus on building and get wins much faster.

Support is by far the best reason to use JS early on. There are huge amounts of community support, YouTube videos, courses & information available and this makes it very approachable and allows anybody to be productive quickly. Whilst I don't think JS gives you great errors, I can almost guarantee that somebody has come across your problem and so 'Googling' is easy which helps with productivity & confidence building.

Finally, V8 from Google is one of the best reasons to use JS. The JavaScript engine that powers Chrome, NodeJS and other things has allowed JavaScript to become a 'universal' language - allowing users to build servers, web applications, mobile apps & many other things. Whilst I personally do not like the enthusiasm to put JavaScript everywhere, I do like the power that V8 has given many developers and the ability to use one language to build anything you can think of, is undeniably empowering.

The bad parts of JavaScript

It's not all good news though. JavaScript has some of the weirdest things to exist in programming. Here are just a few examples:

  • '==' vs '===' as equality operators

  • "11" + 1 = 111 so you would expect "11" - 1 to equal 1...right? Wrong. It equals 10

  • Adding "/g" to the end of Regex will maintain state, leading to some crazy cases if you ever have to reuse it.

  • for-of loop vs forEach loop both have the key & value the opposite way around. (Somebody needs to explain this to me)

  • 'null' is equal to typeOf 'object'

  • Using 'delete' keyword to remove an item from an array doesn't remove it. It simply sets it to 'undefined' which is still an assigned value.

I covered this earlier but the enthusiasm to put JavaScript everywhere is crazy to me. JavaScript is amazing & can do lots of things really well but the tendency to reach for JavaScript each & every time you need to solve a problem to me is mind-boggling.

Whilst it really helps you to get started, the dynamic type system of JavaScript is a real drawback after a while. Your inability to know the type of something, particularly when using 3rd party code/libraries leads to lots of issues after some time - particularly when you've had code in production as libraries & versions change over time. I know TypeScript exists & I am a fan, but it doesn't really solve the problem because TypeScript still compiles to JavaScript it cannot fundamentally change the underlying language.

Why I wanted to learn Go

I had lots of reasons & some of them I've mentioned above - JavaScript is great but it's not without flaws & it's really weird. Initially, I wanted to learn something really low-level & had an eye on C, C++ or maybe even Assembly. Then I switched my mind and wanted to do something powerful but higher level, both Rust & Zig had my eye for a short while. I finally settled on Go because it was offered the usability & simplicity that I enjoyed from JavaScript but offered the ability to have better control over memory, good built-in security and a great standard library.

What I've loved about Go

I've spent 6 months learning Go, it's been great. I've built loads of hobby projects including web servers, terminal apps, a web crawler, a pub-sub system and ran NodeJS files from Go.

One of the things I enjoyed was just how quickly I was productive with Go. I've been using JS for over 6 years but I was productive with Go inside 6 days - I'm not exaggerating. The syntax, compiler & type system are all very simple & tend to give you very good feedback when things aren't going right. Being a compiled language, you get lots of feedback from the Go tool chain before you even run your program.

Error handling absolutely sucks in JS. The style is commonly known as 'exception handling' and I will attempt to explain why it's so bad. If you have 3 functions in a try-catch block and each could throw an error, if you get an error, then how do you know which function has created that error? Sadly, it's not great in Go either. However, Go takes a different approach to JS and treats errors as values specifically as returned values, essentially meaning that you have to handle the possible errors which in general leads to safer & more scalable code. The downside is that you will be writing if err != nil a LOT in Go.

One of my favourite features is that when a Go program builds, it builds into a single executable file (otherwise known as binary). This means that you don't need to have a runtime environment to use your program eg you don't have to install Node on you server just to make sure your code will run, Go packages EVERYTHING it needs into a single executable that is also cross-platform.

From my initial use of the language, Go uses significantly less memory than NodeJS or JS in general. Typically this not only leads it to being much faster overall but also significantly less costly to deploy as memory is typically one of the more expensive resources any program will consume. Although not scientific, Go appears to use about 1/3 of the total memory of a comparable JS program - that's a lot.

Finally, Go routines are a really simple model of letting code run concurrently. Whilst they are strictly not true 'threads' they behaviour in a very similar way and allows Go programs to execute very fast & very efficiently making use of all of the resources it can from the computer it runs from. Whilst Node does have a concurrency model, I never found it to be nice to use or particularly effective.

Go or JavaScript?

Sorry to disappoint anybody but I won't be choosing one or the other. Go has been an incredible experience and I will be continuing to use it for lots of things, particularly building web servers & terminal apps. But JavaScript is widely used for a reason, it's hard to beat it for building UI's for web & mobile.

JavaScript has a huge amount of 3rd party libraries and when you need to build something quickly this is a huge benefit. So when it comes to prototyping, I would still choose JavaScript. I think Go really shines when you're building software for the long term & for production - you get 80% of the speed of JavaScript but with security & efficiency built in.

For any JavaScript dev thinking of learning Go, please do. And let me know what you think.