mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-09 07:02:29 +08:00
feat(maputil): add GetValue (#243)
* add maputil.GetValue * rename GetOrDefault
This commit is contained in:
@@ -657,3 +657,12 @@ func convertMap(src reflect.Value, dst reflect.Value) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetOrDefault returns the value of the given key or a default value if the key is not present.
|
||||
// Play: https://go.dev/play/p/99QjSYSBdiM
|
||||
func GetOrDefault[K comparable, V any](m map[K]V, key K, defaultValue V) V {
|
||||
if v, ok := m[key]; ok {
|
||||
return v
|
||||
}
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user