bluele / gcache

An in-memory cache library for golang. It supports multiple eviction policies: LRU, LFU, ARC

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Panics should never happen in a library

pcman312 opened this issue · comments

Panicking in a library is an anti-pattern in Go. It results in indeterministic and unpredictable code. The caller has no idea when a library is going to panic unless they are experts in the library's codebase.

Currently, this is being done in several places:

panic("gcache: size <= 0")

gcache/cache.go

Line 163 in bbe6d2a

panic("gcache: Unknown type " + cb.tp)

I believe a better option would be to allow for any size in the New() function, but then checking it in the Build() and returning an error. This makes it more idiomatic and removes the possibility that the caller's program crashes unexpectedly because of a bad input.