diff --git a/README.md b/README.md index 59fc70a..66a8ead 100644 --- a/README.md +++ b/README.md @@ -1016,6 +1016,9 @@ import "github.com/duke-git/lancet/v2/maputil" - **SortByKey** : sorts the map by its keys and returns a new map with sorted keys. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/maputil.md#SortByKey)] [[play](todo)] +- **GetOrDefault** : returns the value of the given key or a default value if the key is not present. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/maputil.md#GetOrDefault)] + [[play](todo)]

13. Mathutil package implements some functions for math calculation.        index

diff --git a/README_zh-CN.md b/README_zh-CN.md index b883cc0..c460179 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -1018,7 +1018,9 @@ import "github.com/duke-git/lancet/v2/maputil" - **SortByKey** : 对传入的map根据key进行排序。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/maputil.md#SortByKey)] [[play](todo)] - +- **GetOrDefault** : 返回给定键的值,如果键不存在,则返回默认值。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/maputil.md#GetOrDefault)] + [[play](todo)]

13. mathutil 包实现了一些数学计算的函数。       回到目录

diff --git a/docs/api/packages/maputil.md b/docs/api/packages/maputil.md index 44345b9..4642454 100644 --- a/docs/api/packages/maputil.md +++ b/docs/api/packages/maputil.md @@ -77,6 +77,7 @@ import ( - [ConcurrentMap_Range](#ConcurrentMap_Range) - [GetOrSet](#GetOrSet) - [SortByKey](#SortByKey) +- [GetOrDefault](#GetOrDefault)
@@ -2263,4 +2264,44 @@ func main() { // Output: // map[1:a 2:b 3:c 4:d] } +``` + +### GetOrDefault + +

返回给定键的值,如果键不存在,则返回默认值。

+ +函数签名: + +```go +func GetOrDefault[K comparable, V any](m map[K]V, key K, defaultValue V) V +``` + +示例:[运行](todo) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/maputil" +) + +func main() { + m := map[int]string{ + 3: "c", + 1: "a", + 4: "d", + 2: "b", + } + + result1 := maputil.GetOrDefault(m, 1, "default") + result2 := maputil.GetOrDefault(m, 6, "default") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // a + // default +} ``` \ No newline at end of file diff --git a/docs/en/api/packages/maputil.md b/docs/en/api/packages/maputil.md index 03a633f..6c2b447 100644 --- a/docs/en/api/packages/maputil.md +++ b/docs/en/api/packages/maputil.md @@ -78,7 +78,7 @@ import ( - [ConcurrentMap_Range](#ConcurrentMap_Range) - [GetOrSet](#GetOrSet) - [SortByKey](#SortByKey) - +- [GetOrDefault](#GetOrDefault)
@@ -2281,4 +2281,44 @@ func main() { // Output: // map[1:a 2:b 3:c 4:d] } +``` + +### GetOrDefault + +

returns the value of the given key or a default value if the key is not present.

+ +Signature: + +```go +func GetOrDefault[K comparable, V any](m map[K]V, key K, defaultValue V) V +``` + +Example:[运行](todo) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/maputil" +) + +func main() { + m := map[int]string{ + 3: "c", + 1: "a", + 4: "d", + 2: "b", + } + + result1 := maputil.GetOrDefault(m, 1, "default") + result2 := maputil.GetOrDefault(m, 6, "default") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // a + // default +} ``` \ No newline at end of file diff --git a/maputil/map.go b/maputil/map.go index b70edbc..363a109 100644 --- a/maputil/map.go +++ b/maputil/map.go @@ -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: todo +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 +} diff --git a/maputil/map_test.go b/maputil/map_test.go index a78b818..4a6c5f2 100644 --- a/maputil/map_test.go +++ b/maputil/map_test.go @@ -869,3 +869,22 @@ func TestBaseType(t *testing.T) { MapTo(tc["u64"], &number) assert.EqualValues(64, number) } + +func TestGetOrDefault(t *testing.T) { + + t.Parallel() + + assert := internal.NewAssert(t, "GetOrDefault") + + m1 := map[int]string{ + 3: "c", + 1: "a", + 4: "d", + 2: "b", + } + result1 := GetOrDefault(m1, 1, "123") + assert.Equal("a", result1) + + result2 := GetOrDefault(m1, 5, "123") + assert.Equal("123", result2) +}