SetFunc sets (or overwrite if already exists) a new value resolved from the function callback for the provided key. The function callback receives as argument the old store element value (if exists). If there is no old store element, the argument will be the T zero value. Example: s := store.New
(key K, fn func(old T) T)
| 161 | // return old + 1 |
| 162 | // }) |
| 163 | func (s *Store[K, T]) SetFunc(key K, fn func(old T) T) { |
| 164 | s.mu.Lock() |
| 165 | defer s.mu.Unlock() |
| 166 | |
| 167 | if s.data == nil { |
| 168 | s.data = make(map[K]T) |
| 169 | } |
| 170 | |
| 171 | s.data[key] = fn(s.data[key]) |
| 172 | } |
| 173 | |
| 174 | // GetOrSet retrieves a single existing value for the provided key |
| 175 | // or stores a new one if it doesn't exist. |