4 ways GO is just different
Coming from a Java and Javascript programmer
GO is a programming language created by Google back in the late 2000’s. It is statically typed, which means you have type safety like Typescript, it is compiled like its parent, C and it has every modern language’s bells and whistles like memory safety and garbage collection. Basically it is a modern version of C, a fast language with a modern syntax and built in concurrency support.
Naturally, having this mix, GO is a language that is gaining popularity over time, specially because it is really flexible, being used everywhere from scripting to web development (there’s even a third-party transpiler for frontend development).
But what makes GO different beyond its C-like nature? Well one thing is that GO is multiparadigm, which increases its flexibility factor, but what does that mean? Well, it means that you can use go with a functional paradigm, an imperative one, Object-Oriented, etc.
So, from a guy that has worked with Java, a very strongly Object-Oriented language, and JavaScript using its functional side, here are 4 ways in which GO just feels different.
Composition vs Inheritance
So, starting with the Object-Oriented concepts, Inheritance is one of the most important concepts of OOP, it is the idea that the behaviour of a class should be inherited to those that extend it, this is to avoid boilerplate code. The thing is that, GO doesn’t quite really supports Inheritance, you could use inheritance but what you would be using deep down is Composition, which is the recommended way.
Composition is basically using a class that uses some other classes, like assembling the different components of a PC and making them work together. Composition in GO is normally implemented using dependency injection, in which you pass the dependencies or components to a class instead of letting the class instatiate it itself.
This can make the code much cleaner since you can track the dependencies to a common source instead of having them all over the place. It also helps you to build unit tests since you it’s easier to mock the dependencies.
Structs vs Classes
So, maybe the biggest difference is that GO does not provide Classes, it has Structs, which are similar and are rooted in C Structs, but are much more powerful. They are basically stripped down classes, bundles of data with methods.
So you have a struct, that struct can have many attributes and you can attatch methods that any instances of that struct can call. Methods, can also be public or private, as well as attributes using both UpperCamelCase for publics and lowerCamelCase for privates.
The three key differences between GO structs and classes is that the first ones do not allow inheritance (favouring composition as mentioned before), they do not have inherent constructors as classes do and that interfaces are implemented implicitly, which mean that you do not have to specify that a class implements an interface, if it implements the methods of the interface, it implies it implements the interface as well.
Data structures
So, Data Structures, one of the basic concepts that every software engineer is told to learn to apply and further advance its career. Technically, structs are one of those data structures, but the thing is that GO is missing many of the main data structures common in other languages like built-in trees and sets.
So, you’ll need to use the data structures you have to handle your data. First, you’ve got arrays and slices who behave in a very similar way with the main difference being that arrays are immutable. So they’re the closest to a tuple that you have in GO, they both can be iterated and slices behave much like regular JavaScript arrays but without the built in functions and the fact that they’re typed from its conception.
Then you have structs which we talked about in the last part so we’re gonna skip them for now. And finally, there’s the map, so, the map is pretty much like any other map or dictionary in any other language, you have a key-value data structure that is also typed.
Error handling
So, error handling is also really different in GO, first of all because GO lacks one key component in most programming languages: the try-catch statement. So GO doesn’t let you wrap your entire code into a single try-catch to screw up as much as you’d like (something that is advised in languages like Python, for example).
So the main reason for GO not having try-catch statements is precisely that, you should not wrap your entire code into a single excpetion and be like “yeah, just handle it all together”. If you think that a certain part of your code is risky or error-prone, you should handle that part individually with its one conditions and proper response.
So the way that GO does this is by having two responses on its functions, normally the standard says that you should retorn an error alongside your response and wherever you invoke that function, you’d either get that error and validate it with an if statement or you’ll just ignore it using a blank identifier (_) to move on.
Conclusion
So, GO is a very powerful programming language, it has a growing community and is very flexible. But there are a few caveats that you should know before fully getting into it. Maybe you’re really used to a certain programming style or the architecture you have in your mind doesn’t quite work with GO’s nature, that’s ok, there are many flavours out there and there is certainly one for you too, and if it turns out that it is GO, the community is open to recieve you.