Gowed (With expiration date) provides a series of functions for managing data (single value, slices, maps) that may have an expiration date.
Let's start with a struct Expirer
that implements the gowed.Expirer
interface. The IsExpired
method checks if the current time is after the ExpirerAt
time.
Here are some examples of how to use the gowed.HasExpiredData
function:
// Implement `Expirer`
type Expirer struct {
ExpirerAt *time.Time
}
var _ gowed.Expirer = (*Expirer)(nil)
func (e *Expirer) IsExpired() bool {
if e.ExpirerAt == nil {
return false
}
now := time.Now()
return now.After(*e.ExpirerAt)
}
...
// Create an instance of `Expirer` with `ExpirerAt` set to one day before the current time
t := time.Now().AddDate(0, 0, -1)
obj := &Expirer{ExpirerAt: &t}
// Use `gowed.HasExpiredData` to check if the `Expirer` instance has expired
if gowed.HasExpiredData(obj) {
fmt.Println("obj has expired with expired time: ", t)
} else {
fmt.Println("obj has not expired with expired time: ", t)
}
// Set `ExpirerAt` to one day after the current time
t = time.Now().AddDate(0, 0, 1)
obj.ExpirerAt = &t
// Use `gowed.HasExpiredData` to check if the `Expirer` instance has expired
if gowed.HasExpiredData(obj) {
fmt.Println("obj has expired with expired time: ", t)
} else {
fmt.Println("obj has not expired with expired time: ", t)
}
// For basic types and `nil`, `gowed.HasExpiredData` returns `false`, indicating they have not expired
if gowed.HasExpiredData(1) {
fmt.Println("1 has not expired")
}
if gowed.HasExpiredData(nil) {
fmt.Println("nil has not expired")
}