mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-23 13:52:26 +08:00
feat: add Merge func
This commit is contained in:
@@ -25,3 +25,16 @@ func Values[K comparable, V any](m map[K]V) []V {
|
|||||||
|
|
||||||
return values
|
return values
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Merge maps, next key will overwrite previous key
|
||||||
|
func Merge[K comparable, V any](maps ...map[K]V) map[K]V {
|
||||||
|
res := make(map[K]V, 0)
|
||||||
|
|
||||||
|
for _, m := range maps {
|
||||||
|
for k, v := range m {
|
||||||
|
res[k] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|||||||
@@ -40,3 +40,25 @@ func TestValues(t *testing.T) {
|
|||||||
|
|
||||||
assert.Equal([]string{"a", "a", "b", "c", "d"}, values)
|
assert.Equal([]string{"a", "a", "b", "c", "d"}, values)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMerge(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestMerge")
|
||||||
|
|
||||||
|
m1 := map[int]string{
|
||||||
|
1: "a",
|
||||||
|
2: "b",
|
||||||
|
}
|
||||||
|
m2 := map[int]string{
|
||||||
|
1: "1",
|
||||||
|
3: "2",
|
||||||
|
}
|
||||||
|
|
||||||
|
expected := map[int]string{
|
||||||
|
1: "1",
|
||||||
|
2: "b",
|
||||||
|
3: "2",
|
||||||
|
}
|
||||||
|
acturl := Merge(m1, m2)
|
||||||
|
|
||||||
|
assert.Equal(expected, acturl)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user