Golang vs Node Js

Having worked with Javascript and Node Js for a while, I began learning Go recently and a question I keep asking myself is why do these programming languages exist? Why do we have Go when there is Node or vice versa.

Image for post
Image for post

I started to appreciate both languages for what they do after some time. Please note, this is not an attempt to point out why a language is better than the other, that is not my belief. I believe languages are analogous to tools in a carpenter workshop. While a tool might be the best for driving a nail into wood, it might perform poorly in cutting that same wood.

Golang

According to her official documentation- “The Go programming language is an open source project to make programmers more productive.

Go is expressive, concise, clean, and efficient. It’s concurrency mechanisms make it easy to write programs that get the most out of multicore and networked machines, while it’s novel type system enables flexible and modular program construction. Go compiles quickly to machine code yet has the convenience of garbage collection and the power of run-time reflection. It’s a fast, statically typed, compiled language that feels like a dynamically typed, interpreted language.”

Node Js

“A platform built on Chrome’s JavaScript runtime for easily building fast and scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.”

Let’s discuss both of them under the same light

  1. Approach to Object Oriented Programming
Image for post
Image for post

How does Node and Golang approach OOP? Is Go even object oriented?

Well, first Go tries to blend different concepts in procedural programming, functional programming and object oriented programming. So Yes and No. Golang approach to OOP is slightly different

1.1 Structs for Classes

Go tries to replace classes with structs while is a concept borrowed from C language but Go implements it in a more powerful way. Struct types plus their associated methods serve the same goal of a traditional class in Node js.

Let us declare a struct for a person with several properties

type Person struct {ID string `json: “id”`FirstName string `json: “firstname”`Lastname string `json: “lastname”`}

where the struct only holds the state, not the behavior, and the methods provide them behavior, by allowing to change the state.

Node implements this as:

class Person {      constructor(firstname, lastname){
this.FirstName = firstname
this.LastName = lastname
}}

1.2 Encapsulation

One of the best features of Go: capitalized fields, methods and functions are public. All the other fields are local to the package and not exported. With a single look you know if something is public or private. There’s no protection because there is no inheritance.

Take for example this Message utility function in go:

func Message (status bool, message string) (map[string] interface{}){     return map[string] interface{} { 
“message” : message, “status” : status
}
}

By declaring the message function func Message in capital letter, Go automatically knows its a public function and allows you to export in in other packages, hence we eliminate the need for `module.exports` present in Node Js. A Go function and provides clarity on public and private functions at a glance

2. Inheritance

No inheritance

There is no concept of inheritance. From the Go FAQ:

Object-oriented programming, at least in the best-known languages, involves too much discussion of the relationships between types, relationships that often could be derived automatically. Go takes a different approach.

3. Speed/Performance

As Node.js is run time environment on JavaScript, it is a bit slower than other compiled languages.

Whereas Golang is light-weighted and faster than Node.js as it is based on plain characteristics of C & C++.

So we can say that in terms of raw performance of CPU and memory-bound tasks, Golang is a better option. However, both technologies display equal performance in real-life tasks.

4. Error Handling

Image for post
Image for post

Error handling in Go is more intentional in the sense that you have to explicitly check for the error. This can be more difficult than the Node Js Try and Catch approach to error handling. But it allows for accuracy and flawlessness in the long run

5. Community

Image for post
Image for post

While both languages were introduced in the same year with just months

As Node.js is an interpreted language based on JavaScript, it is a bit slower than other compiled languages. Whereas Golang is light-weighted and faster than Node.js as it is based on the characteristics of C & C++.

So we can say that in terms of raw performance of CPU and memory-bound tasks, Golang is a better option. However, both technologies display equal performance in real-life tasks.

According to Stack Overflow 2020 developer survey, javascript (node js) still remains more popular than Golang

Image for post
Image for post
source: Stack Overflow 2020 developer survey

Conclusion

Like I mentioned, the essence of this article is not to convince you about which tool is better. It is to help you make the best decisions in the long run. Both languages are powerful in their own space.

Written by

Software Engineer. But I am interested in everything from software to pizzas

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store