1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00

feat: add Merge func

This commit is contained in:
dudaodong
2022-03-30 18:06:22 +08:00
parent 1af8fe8daf
commit 1a436aeb41
2 changed files with 35 additions and 0 deletions

View File

@@ -25,3 +25,16 @@ func Values[K comparable, V any](m map[K]V) []V {
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
}

View File

@@ -40,3 +40,25 @@ func TestValues(t *testing.T) {
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)
}