I was pairing with another engineer the other day and we needed to download some largish mp4 files an  d process the headers. The service could possib

Using Go Generics. I was pairing with another engineer the… | by Scott White | Aug, 2021 | Medium

submited by
Style Pass
2021-08-18 00:00:04

I was pairing with another engineer the other day and we needed to download some largish mp4 files an d process the headers. The service could possibly get multiple requests at once, so we wanted to deduplicate the expensive requests and return the same value to all the concurrent requesters. I’ve often written similar code and with Go’s excellent concurrency primitives it’s usually a pretty simple task. After about an hour we had the code working and it seemed pretty straight-forward. The next day when I was reviewing the PR, I noticed a possible deadlock. I guess this wasn’t as simple as I had first thought. After fixing the deadlock, I decided to extract the core of the code into a reusable library so the next person wouldn’t make the same mistake.

I think you might be guessing where this is going. Once the package was extracted to be reusable, the concrete types we originally used for our specific use case all became the empty interface (interface{}).

Leave a Comment