
Golang, aka Go, is a modern programming language known for its simplicity, ease of use, and ability to handle high-concurrency tasks. However, this simplicity can sometimes bring complexity to the code you write. As a software developer, I learned this while delving deeper into coding with Go.
Transitioning to a new programming language can be challenging, and Go is no exception. In this article, I’ll explore some of the challenges I faced when migrating to Go from another language.
Navigating the Unintended Complexity in Go
Go-ing Beyond Static Typing: Implementing Polymorphism
The most prominent area (in my experience) where Golang’s simplicity can cause complexity is in polymorphism. Other languages, like .NET, easily implement polymorphism using classes that inherit from a base class. But in Go, things work a little differently.
While Go has polymorphism, it is limited to interfaces only. This means developers have to create interfaces for every structure they build and make sure each class implements those interfaces. This introduces complexity when dealing with polymorphic JSON structures.
I encountered complexity while implementing templates for generating APIMatic’s Golang client SDKs. API definitions with inheritance in their model structures require generated code that supports inheritance and polymorphism. It was a challenging task to work with arrays of polymorphic objects, but I discovered that using custom unmarshaling with a discriminator could help. This discriminator tells the unmarshaller which type of object it is dealing with, so it can convert the interface type into the correct object type and then call that object unmarshaller. unmarshal it.
The Great Error Hunt in Go
Beyond polymorphism, there are other factors making the switch to Go a challenge. For example, Go has a unique style of handling errors. Instead of throwing simple exceptions like other languages, Golang uses multiple return values and error codes. This means an error propagates till it reaches the code that invoked it. This approach leads to more e