feat: add HasKey

This commit is contained in:
dudaodong committed 2023-07-10 10:33:04 +08:00
1 parent 7b744c299e
commit 6f48b00c88
3 files changed
+49

No files matched your search

+18
View File
@@ -289,3 +289,21 @@ func MapValues[K comparable, V any, T any](m map[K]V, iteratee func(key K, value
return result
}
// HasKey checks if map has key or not.
// This function is used to replace the following boilerplate code:
// _, haskey := amap["baz"];
//
// if haskey {
// fmt.Println("map has key baz")
// }
//
// Play: todo
func HasKey[K comparable, V any](m map[K]V, key K) bool {
_, haskey := m[key]
if haskey {
return true
}
return false
}