Trying Generics in Go through implementing Tiny Cache Client
Feb 16, 2022
Introduction
The Go1.18 with Generics will come soon. So i’m going to try Generics.
We can use Generics in Go Playground by setting “go dev branch”.
Implementation
1. syntax
You can use Generics by writing bracket and type.
(I’m wondering why generics syntax is bracket [T]
instead of gt and lt<T>
. )
2. declaring cache client
Cache value is allowed all type by any
.
3. Goal
Current Cache Client’s value is often interface{}
, so we have to cast the cache value. However now you have a typed cache client!
Conclusion
Done! The following link is full code.
https://go.dev/play/p/F-6ifZQ9N6s?v=gotip
Next, i want to try declaring constraint by custom type or interface.