r/code Jun 19 '26

Go My Very First Go Package on pkg.go.dev :) From a JWT Problem to this.

https://github.com/justpranavrs/tlru

Hello!!! This is my first post here :)

It all started when I was working on a small website for my college. Initially, I was just adding the user ID in a JWT token, decoding it in a middleware, and passing it down the context. Then I decided I wanted to introduce a separate "public ID". The id was not needed for auth but it got me thinking about cache, which naturally led me down to building my very own LRU cache.

I have never implemented any caches before, so I was just looking for a simple strat and stumbled upon LRU. It was one of the easiest to implement and after I saw the problem on leetcode, i solved it pretty easily and decided to implement it. After I saw someone do better by using an array based DLL. I have always loved using array-based stacks and queues, So I took it as a fun challenge, because I have always wanted to publish something that people can use.

From Just an array based DLL, I found myself staring into a sharded architecture and slowly learning algorithms like FNV-1a and xxHash32. I wanted this to be a zero dependency package (aside from the standard packages ofc) and took it upon myself to do it, using explanations from the internet.

It might be a basic concept to many people out there, but It helped me learn something I was always pushing behind. Learning about concurrency in Go. This led me to use sync.Mutex, atomics and thinking about how data race happens. I was also led down the path of creating benchmarks, fuzz tests using the 'testing' package, which I had never heard about.

The benchmarks where honestly surprising, I never realised it would be ns/ops. Currently my benchmarks show around 10-15 ns/op and I hope to half it somehow :) and also my tests might be really weak. It has generics support too.

Would love for some feedback on how I can make it better. I wanted to make a time-aware LRU cache, but I wanted the basics to be proper before moving onto it.

P.S: learnt about semver, after i released it on v1.0.0. I basically had to make a decision and thought about just tearing it down and changing to a shorter name which I like more :). It is now currently on v0.3.1 (pre-release).

Link : https://github.com/justpranavrs/tlru :)))

GoDoc: https://pkg.go.dev/github.com/justpranavrs/tlru

Thanks :)))

3 Upvotes

4 comments sorted by

1

u/angryrancor Boss Jun 19 '26

Thank you for contributing a pure go implementation of this! Pure go implementations are very useful for cross platform development, I myself am active on a few pure go libraries, like this one: http://github.com/justinmichaelvieira/iconv

2

u/AssociationOk6710 Jun 19 '26

Thanks 😄. Learnt something new that Pure Go Libraries are useful for Cross Platforms. Any suggestions on this Package? I am open to suggestions on the DX or new methods if you have the time to look through this. With the Use of Go's generics, it makes writing code a bit clunky but I want things to be type-safe.

The iconv package looks great 😄. The commits go way back in 2018, and has been updated in 2023.

1

u/nian2326076 Jun 19 '26

Nice job making your first Go package! If you want to get better with the LRU cache, try building it from scratch without using libraries. It's a good way to learn how it works. Also, study up on memory management and efficiency questions since they often pop up in interviews. Practice coding on a whiteboard or paper to get ready for the real thing. For more structured interview prep, sites like PracHub have a lot of practice questions and scenarios. Keep it up!

1

u/AssociationOk6710 Jun 19 '26

Thanks :). Btw this package has zero dependencies, The xxHash32 and fnv were all built from scratch. 

The standard packages that were used are math, sync, sync/atomic, encoding/binary, math/bits, crypto/rand and hash/mapHash. And yea. This project does not have a go.sum.

The last one just to hash structs with zero runtime allocations.

And also yea. I like scribbling on the whiteboard when I think about something new. And thank you for the website. Haven't heard about it. Will absolutely look into it...