1 Star 0 Fork 0

ghosind/utils

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
map.go 1.06 KB
一键复制 编辑 原始数据 按行查看 历史
ghosind 提交于 2023-08-18 00:51 +08:00 . doc: update docs.
package utils
// CloneMap creates a new map, and copies all the keys and their value from the source map into
// the new map. It will return nil if the source map is nil.
//
// oldMap := map[string]int{ "a": 1, "b": 2 }
// newMap := CloneMap(oldMap)
// log.Print(newMap)
// // [a:1 b:2]
func CloneMap[K comparable, V any](src map[K]V) map[K]V {
if src == nil {
return nil
}
dest := make(map[K]V, len(src))
for k, v := range src {
dest[k] = v
}
return dest
}
// CopyMap copies all keys and their value from the source map into the destination map, and it
// will overwrite the old value in the destination map if the key has existed. The function will
// return a nil destination error if the source map is not nil and the destination map is nil.
//
// src := map[string]int{ "a": 1, "b": 2 }
// dest := map[string]int{ "a": 10, "c": 3 }
// CopyMap(dest, src)
// log.Print(dest)
// // [a:1 b:2 c:3]
func CopyMap[K comparable, V any](dest, src map[K]V) error {
if src != nil && dest == nil {
return ErrNilDest
}
for k, v := range src {
dest[k] = v
}
return nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/ghosind/utils.git
git@gitee.com:ghosind/utils.git
ghosind
utils
utils
main

搜索帮助