diff --git a/docs/api/packages/maputil.md b/docs/api/packages/maputil.md index 62b5f78..298dedd 100644 --- a/docs/api/packages/maputil.md +++ b/docs/api/packages/maputil.md @@ -55,6 +55,8 @@ import ( - [ConcurrentMap_GetAndDelete](#ConcurrentMap_GetAndDelete) - [ConcurrentMap_Has](#ConcurrentMap_Has) - [ConcurrentMap_Range](#ConcurrentMap_Range) +- [GetOrSet](#GetOrSet) +
@@ -1483,3 +1485,41 @@ func main() { }) } ``` + + +### GetOrSet + +返回给定键的值,如果不存在则设置该值。
+ +函数签名: + +```go +func GetOrSet[K comparable, V any](m map[K]V, key K, value V) V +``` + +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/maputil" +) + +func main() { + m := map[int]string{ + 1: "a", + } + + result1 := maputil.GetOrSet(m, 1, "1") + result2 := maputil.GetOrSet(m, 2, "b") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // a + // b +} +``` \ No newline at end of file diff --git a/docs/en/api/packages/maputil.md b/docs/en/api/packages/maputil.md index 5a8e69b..d391d75 100644 --- a/docs/en/api/packages/maputil.md +++ b/docs/en/api/packages/maputil.md @@ -55,6 +55,7 @@ import ( - [ConcurrentMap_GetAndDelete](#ConcurrentMap_GetAndDelete) - [ConcurrentMap_Has](#ConcurrentMap_Has) - [ConcurrentMap_Range](#ConcurrentMap_Range) +- [GetOrSet](#GetOrSet) @@ -1501,3 +1502,40 @@ func main() { }) } ``` + +### GetOrSet + +Returns value of the given key or set the given value value if not present.
+ +Signature: + +```go +func GetOrSet[K comparable, V any](m map[K]V, key K, value V) V +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/maputil" +) + +func main() { + m := map[int]string{ + 1: "a", + } + + result1 := maputil.GetOrSet(m, 1, "1") + result2 := maputil.GetOrSet(m, 2, "b") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // a + // b +} +``` \ No newline at end of file diff --git a/maputil/map.go b/maputil/map.go index 6cb9f6a..b622be6 100644 --- a/maputil/map.go +++ b/maputil/map.go @@ -436,3 +436,15 @@ func ToSortedSlicesWithComparator[K comparable, V any](m map[K]V, comparator fun return keys, sortedValues } + +// GetOrSet returns value of the given key or set the given value value if not present +// Play: todo +func GetOrSet[K comparable, V any](m map[K]V, key K, value V) V { + if v, ok := m[key]; ok { + return v + } + + m[key] = value + + return value +} diff --git a/maputil/map_example_test.go b/maputil/map_example_test.go index cadb309..e385367 100644 --- a/maputil/map_example_test.go +++ b/maputil/map_example_test.go @@ -524,3 +524,19 @@ func ExampleToSortedSlicesWithComparator() { // [3 2 1] // [c b a] } + +func ExampleGetOrSet() { + m := map[int]string{ + 1: "a", + } + + result1 := GetOrSet(m, 1, "1") + result2 := GetOrSet(m, 2, "b") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // a + // b +} diff --git a/maputil/map_test.go b/maputil/map_test.go index 9737e36..30c7ae6 100644 --- a/maputil/map_test.go +++ b/maputil/map_test.go @@ -691,3 +691,19 @@ func TestToSortedSlicesWithComparator(t *testing.T) { }) } } + +func TestGetOrSet(t *testing.T) { + t.Parallel() + + assert := internal.NewAssert(t, "TestGetOrSet") + + m := map[int]string{ + 1: "a", + } + + result1 := GetOrSet(m, 1, "1") + result2 := GetOrSet(m, 2, "b") + + assert.Equal("a", result1) + assert.Equal("b", result2) +}