From Bland to Spiky: How Generics Made My Service Super Robust

Get ready to witness a code transformation that's more exciting than watching a hedgehog unfurl! Generics in Go were introduced in March '22, but aside from scratching the surface, there haven't been many presentations about using them on a larger scale. In my talk, I will explore the challenges we faced, how we used clever generic design patterns to overcome them, and the benefits we gained.

LEVEL: Advanced

Place
GoLab Deep
Length
40 min
When
November 12th, 2024
16:00

Abstract

DESCRIPTION:

The introduction of generics was met with lots of opinions but I could never find a good spot to use them in my code that wasn’t more than slices.Contains[T]() or such; small functions and not a whole journey of functionality through several modules. When the opportunity provided itself I was keen to grab it and was delighted to find just how useful generics could be!

This talk is about generics on a larger scale, from the API layer to business logic and database. How using generics helped us write less code, what problems they introduced and how we went about solving them.

I will present the audience with my initial problem, having a lot of different endpoints with different payloads but similar functionality, and how I wanted to avoid making a `switch payload.(type)` -kind of code or generate lots of code to deal with different data structures. Generics provided the solution, but it was a path of learning to deal with the limitations they brought on, and in my talk, I will explain them and how we solved them.

I hope to inspire any developers facing similar issues to look at generics as a solution and to learn from our expectations, problems and solutions to have a better view of how they could be used. My examples demonstrate to other developers how generics can be easy to use to write robust and resilient code with ease. I walk them through the initial solution of having a single model that affects the underlying functionality and then expand on it to have several: the original model and the derived models for API responses and requests.

The ‘spiky’ in the name of the talk is because I plan to use lots of hedgehog images in my talk due to my love of them, and provide a way of storytelling the whole talk as a hedgehog going on an adventure, facing challenges and overcoming them in the quest to write clean, efficient, robust and resilient code. At the start, the hedgehog faces a problem with many similar endpoints and data structures and decides to create a template to produce a lot of files with minor adjustments, but feels like this is not the right solution. She stumbles into generics and they provide her with the perfect solution for writing elegant and robust code with easily understandable structure and without messy switch..case or if statements. She meets more problems when she needs to implement request and response models derived from the original one, but the solution is as simple as eating a mealworm for lunch! In the end, she gains the admiration and respect of the rest of the forest.

Video

GoLab is a conference made by Develer.
Develer is a company based in Campi Bisenzio, near Florence. Our motto is : "Technology to give life to your products". We produce hardware and software to create exceptional products and to improve industrial processes and people's well being.
In Develer we have passion for the new technologies and we offer our clients effective solutions that are also efficient, simple and safe for the end users. We also believe in a friendly and welcoming environment where anybody can give their contribution. This passion and this vision are what we've been driven to organize our conference "made by developers for developers".


Subscribe to our newsletter

We hate spam just as much as you do, which is why we promise to only send you relevant communications. We respect your privacy and will never share your information with third parties.
©2024 GoLab | The international conference on Go in Florence-Design & devCantiere Creativo-Made withDatoCMS