map.go 711 B

123456789101112131415161718192021222324252627282930313233
  1. package utils
  2. import "reflect"
  3. func MergeMaps(destMap, sourceMap map[string]interface{}) map[string]interface{} {
  4. newMap := make(map[string]interface{})
  5. // 将目标 map 的元素复制到新 map 中
  6. for key, value := range destMap {
  7. newMap[key] = value
  8. }
  9. // 将源 map 中的元素合并到新 map 中
  10. for key, value := range sourceMap {
  11. newMap[key] = value
  12. }
  13. return newMap
  14. }
  15. func DeepCopyMap(original map[string]interface{}) map[string]interface{} {
  16. cloned := make(map[string]interface{})
  17. for k, v := range original {
  18. // 检查值是否为引用类型,递归处理
  19. switch reflect.TypeOf(v).Kind() {
  20. default:
  21. // 基本类型直接赋值
  22. cloned[k] = v
  23. }
  24. }
  25. return cloned
  26. }