Skip to content

Commit

Permalink
add maputil.GetValue
Browse files Browse the repository at this point in the history
  • Loading branch information
yunxuanhao committed Sep 10, 2024
1 parent 9824db0 commit e2a9342
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
41 changes: 41 additions & 0 deletions docs/api/packages/maputil.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ import (
- [ConcurrentMap_Range](#ConcurrentMap_Range)
- [GetOrSet](#GetOrSet)
- [SortByKey](#SortByKey)
- [GetValues](#GetValues)


<div STYLE="page-break-after: always;"></div>
Expand Down Expand Up @@ -2261,4 +2262,44 @@ func main() {
// Output:
// map[1:a 2:b 3:c 4:d]
}
```

### <span id="GetValue">GetValue</span>

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

<b>函数签名:</b>

```go
func GetValue[K comparable, V any](m map[K]V, key K, defaultValue V) V
```

<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/dNo6YBqtLa0)</span></b>

```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.GetValue(m, 1, "default")
result2 := maputil.GetValue(m, 6, "default")

fmt.Println(result1)
fmt.Println(result2)

// Output:
// a
// default
}
```
41 changes: 41 additions & 0 deletions docs/en/api/packages/maputil.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ import (
- [ConcurrentMap_Has](#ConcurrentMap_Has)
- [ConcurrentMap_Range](#ConcurrentMap_Range)
- [GetOrSet](#GetOrSet)
- [GetValue](#GetValue)

<div STYLE="page-break-after: always;"></div>

Expand Down Expand Up @@ -2276,4 +2277,44 @@ func main() {
// Output:
// map[1:a 2:b 3:c 4:d]
}
```

### <span id="GetValue">GetValue</span>

<p>returns the value of the given key or a default value if the key is not present.</p>

<b>Signature:</b>

```go
func GetValue[K comparable, V any](m map[K]V, key K, defaultValue V) V
```

<b>Example:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/dNo6YBqtLa0)</span></b>

```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.GetValue(m, 1, "default")
result2 := maputil.GetValue(m, 6, "default")

fmt.Println(result1)
fmt.Println(result2)

// Output:
// a
// default
}
```
9 changes: 9 additions & 0 deletions maputil/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,3 +473,12 @@ func SortByKey[K constraints.Ordered, V any](m map[K]V, less func(a, b K) bool)

return
}

// GetValue returns the value of the given key or a default value if the key is not present.
// Play: https://go.dev/play/p/dNo6YBqtLa0
func GetValue[K comparable, V any](m map[K]V, key K, defaultValue V) V {
if v, ok := m[key]; ok {
return v
}
return defaultValue
}
19 changes: 19 additions & 0 deletions maputil/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -751,3 +751,22 @@ func TestSortByKey(t *testing.T) {

assert.Equal(expected2, result2)
}

func TestGetValue(t *testing.T) {

t.Parallel()

assert := internal.NewAssert(t, "GetValue")

m1 := map[int]string{
3: "c",
1: "a",
4: "d",
2: "b",
}
result1 := GetValue(m1, 1, "123")
assert.Equal("a", result1)

result2 := GetValue(m1, 5, "123")
assert.Equal("123", result2)
}

0 comments on commit e2a9342

Please sign in to comment.