Handler and Middleware Design Pattern in Golang

Alexandre Couëdelo
CodeX
Published in
5 min readMar 30, 2021

--

https://github.com/egonelbre/gophers
https://github.com/egonelbre/gophers

I recently started working on a SlackBot project of my own using Golang. As any of you would, I went on a quest for a library that would simplify my day and let me take advantage of what other community members have come up with to solve similar problems.

I came across the well-maintained slack-go library; I started coding my bot using the provided example. Everything worked fine; The code is producing the expected result. It is time to make another coffee and implements a few extra features.

Back at my desk, plunging myself bask into my code, something strikes me. Wait a minute! This way of working does not feel right! When coding REST APIs, I do not have to write any complex logic to handle my event because net/http is designed to help me control HTTP requests.

How can I leverage this specific design pattern to simplify my life and make my code clean and flexible? I asked myself.

In general, when using third-party libraries, always ask yourself if there is no better way to handle the problem. This is where designs pattern comes into play. They make you significantly more efficient by preventing you from reinventing the wheel.

--

--

Alexandre Couëdelo
CodeX

Software Supply Chain and Automation Specialist (aka. DevOps).