Site icon Shuru

From JavaScript to Go: My Journey Transitioning from Node.js to Golang

Javascript

Introduction

I started my development journey with JavaScript, and pretty soon, Node.js became my go-to for backend work. It’s flexible, fast, and has a huge library of tools that make building things exciting. I worked on various projects with Node.js, from small APIs to bigger applications, and used NestJS to add more structure and type safety to my code.

Then, I joined Shuru and had an opportunity to work with Golang. It wasn’t a complete shock, thanks to my experience with NestJS, but Go had its own unique syntax and lower-level features that took some time to get used to. However, I started to appreciate Go’s simplicity, speed, and concurrency model over time.

In this blog, I share what it was like moving from Node.js to Go — the challenges, the things I learned, and how I got comfortable with it. If you’re a JavaScript developer thinking about learning Go, or just curious about the switch, I hope my experience helps you out.

Starting with JavaScript and Node.js

Back in college, I was all about C/C++ and Data Structures and Algorithms (DSA). These subjects taught me the basics like memory management and working with pointers, But things changed during my internship when I switched to JavaScript.

With JavaScript, I didn’t have to worry about managing memory anymore since it handled that with built-in garbage collection. Then I discovered Node.js, which felt easy to use. The main rule? Just don’t block the event loop.

Of course, like any language, JavaScript has its quirks. There’s even a GitHub repo dedicated to its strange behaviour (check it out: wtfjs). And don’t get me started on the node_modules folder — it can grow huge with too many dependencies. Getting ES Modules, TypeScript, and Jest to work well together can be a real headache.

Still, even with these issues, I always felt the good parts of Node.js outweighed the bad.

Why Golang?

For me, the reason for diving into Golang was tied to a new opportunity. When I joined Shuru Tech, I was assigned to a fintech project that was built using Go. This meant I had to learn and adapt quickly to keep up with the development needs of the project.

Golang was chosen for this project because of its performance, scalability, and simplicity. It’s particularly well-suited for building large, high-performance applications, which is critical in fintech where reliability and speed are essential. Although my switch to Go wasn’t planned, it turned out to be a rewarding experience, as I started to understand why Go is favoured for such projects.

Learning Golang: The Transition

I believe switching programming languages is manageable with strong fundamentals. Golang is a small, straightforward language that many find easy to learn, especially as a second language. While Go’s concepts are generally clear, there are still some challenges to navigate.

For learning Go, I find that reading books and writing code works best for me. The gofmt tool ensures a consistent code style, so you don’t have to worry about formatting. The compiler also enforces that all variables and dependencies are used, keeping your code clean.

One of the things I appreciate about Go is how consistent it is. This consistency makes it much easier to read other people’s code, allowing one to focus on solving problems instead of getting distracted by different coding styles. Although Go doesn’t have tuples, I’ve found that I can create similar structures using custom types. It’s a small trade-off for the simplicity that Go offers.

The Features of Go That I Appreciate

  1. The blank identifier, _, in Go, is a write-only placeholder used for explicitly ignoring a value. The reason the blank identifier is necessary in Go is primarily because Go does not permit (i.e. won’t compile with) unused imports or unused local variables. This single character is an incredibly versatile feature of Go, and I think it’s pretty neat.
func main() {
x := "hello"
for _, x := range x {
  x := x + 'A' - 'a'
  fmt.Printf("%c", x)
}
}Code language: CSS (css)
  1. Definitely! No article about Go would be complete without mentioning goroutines. Goroutines are another standout feature. I like Go’s goroutines because they make running multiple tasks at once super easy. They’re much lighter than traditional threads and managed efficiently by Go’s runtime. This means you can handle many operations smoothly and with less overhead, making your applications faster and more responsive.
  1. The Go standard library is impressive. I was amazed at how much I could accomplish with just Go’s built-in tools. Tasks like setting up an HTTP server or handling JSON are straightforward and don’t require external libraries, unlike with Node.js where additional tools often add complexity.
  1. Saving the best for last, defer is definitely my favourite feature in Go. It allows you to schedule a function to run just before the current function exits, no matter how it exits. This is perfect for tasks like cleaning up resources or handling errors. For example, you can use defer to ensure a file gets closed or a database transaction gets rolled back, all without the extra nesting and complexity found in other languages. It’s incredibly useful and keeps your code clean and maintainable, which I find really powerful and convenient.
defer fmt.Println("Goodbye!"// This runs last
fmt.Println("Hello!")          // This runs firstCode language: JavaScript (javascript)

Overall, Go’s features and design principles make it a pleasure to work with.

Comparing Key Aspects: Node.js vs Golang

While I’m still learning and haven’t explored everything Go has to offer, there are already a few areas where I can feel the difference between the two.

One thing that stands out early on is how Go feels faster. Since it’s a compiled language, Go turns into machine code, which gives it a clear speed advantage over Node.js, which runs on the V8 engine

I came across an interesting blog that delves into how stable Golang is compared to Node.js, specifically through the lens of sorting algorithms. The experiment revealed that Go is not only faster but also significantly more stable in its execution. As shown in the graphs shared in the blog, the Go algorithm consistently sorts arrays in almost a third of the time it takes Node.js. You can read the full post here.

Concurrency is a huge selling point for Go. Go’s goroutines are lightweight, and I can see how they could handle a lot of concurrent tasks smoothly. With Node.js, I’ve managed concurrency with the event loop and promises, which works well for I/O-bound tasks. But I’m curious to see how Go’s concurrency model will hold up, especially when I start dealing with more CPU-heavy workloads.

Both languages have their pros here, but I find Go’s simplicity refreshing. Node.js comes with npm, which is an amazing ecosystem, but it also means I often have to pick between dozens of libraries to do even basic things like setting up an HTTP server. In Go, the standard library does so much right out of the box. It’s a simpler, more streamlined experience so far. I need not deal with extra dependencies and rather focus on writing clean, efficient code.

This was a big adjustment for me. Coming from Node.js, where I used try-catch and promises to handle errors, Go’s explicit error handling took some getting used to. In Go, you return errors from functions and handle them right away. It feels a bit more repetitive, but I can see the long-term benefit of being forced to deal with errors upfront. It’s still a work in progress for me, but I’m slowly starting to appreciate it.

Node.js has a massive ecosystem. There’s a library for almost anything, and I’m used to relying on npm to speed up development. Go’s ecosystem, on the other hand, is smaller but feels more focused on performance and stability. So far, The standard library has already been enough for most of my needs. That simplicity helps me avoid the “paralysis by choice” problem I often face with Node.js.

Conclusion

Transitioning from Node.js to Go has been both challenging and rewarding. I still turn to Node.js for quick prototypes or when I need a rich set of libraries, but Go’s simplicity, speed, and efficiency in handling performance-heavy tasks have impressed me. While Node.js excels at rapid development, Go’s approach to concurrency and clean design makes it perfect for building high-performance, scalable systems.

Both languages have their strengths, and the best tool depends on the project at hand. Having experience in both allows me to make more informed choices and ultimately, I’ve learned that flexibility in selecting the right tool is one of the most valuable skills a developer can have.

References

Read more shuru tech blogs here

Author

Exit mobile version