func deepCopyJSON(src map[string]interface{}, dest map[string]interface{}) error {
if src == nil {
return errors.New("src is nil. You cannot read from a nil map")
}
if dest == nil {
return errors.New("dest is nil. You cannot insert to a nil map")
}
jsonStr, err := json.Marshal(src)
if err != nil {
return err
}
err = json.Unmarshal(jsonStr, &dest)
if err != nil {
return err
}
return nil
}

  

------------------------

How to copy a map to another map?

To copy a map content need to execute a for loop and fetch the index value 1 by 1 with element and assign it to another map. Below is a short example.
 
package main
 
import (
"fmt"
)
func main() {  
    map1 := map[string]int{
        "x":1,
        "y":2,
    }
    map2 := map[string]int{}       
     
    /* Copy Content from Map1 to Map2*/
    for index,element := range map1{       
         map2[index] = element
    }
     
    for index,element := range map2{
        fmt.Println(index,"=>",element) 
    }
}
C:\golang\codes>go run example.go
x => 1
y => 2

C:\golang\codes>

go deep copy map的更多相关文章

  1. [Algo] 132. Deep Copy Undirected Graph

    Make a deep copy of an undirected graph, there could be cycles in the original graph. Assumptions Th ...

  2. [Algo] 131. Deep Copy Linked List With Random Pointer

    Each of the nodes in the linked list has another pointer pointing to a random node in the list or nu ...

  3. C# Bitmap deep copy

    今天在研究一个关于 Bitmap deep copy 的问题, 经过一系列的查询,在StackOverFlow上面找到了答案,遂记录下来: public static Bitmap DeepCopyB ...

  4. shallow copy 和 deep copy 的示例

    本文属原创,转载请注明出处:http://www.cnblogs.com/robinjava77/p/5481874.html   (Robin) Student package base; impo ...

  5. python deep copy and shallow copy

    Python中对于对象的赋值都是引用,而不是拷贝对象(Assignment statements in Python do not copy objects, they create bindings ...

  6. Deep Copy cv::StereoBM 深度拷贝

    在使用OpenCV的三维立体重建的库时,一个重要的步骤就是生成左右视图的差异图Disparity,而控制生成disparity的参数的类是cv::StereoBM,我们有时候需要拷贝一份cv::Ste ...

  7. shallow copy & deep copy

    1.深复制与浅复制的概念 ->浅复制(shallow copy)概念   在SDK Guides中(搜索copy),官方给出的浅复制概念为: Copying compound objects, ...

  8. 【C#】Deep copy of objects

    If you learned C++ carefully, you must have known something about the copy of object. For example, I ...

  9. NumPy学习(索引和切片,合并,分割,copy与deep copy)

    NumPy学习(索引和切片,合并,分割,copy与deep copy) 目录 索引和切片 合并 分割 copy与deep copy 索引和切片 通过索引和切片可以访问以及修改数组元素的值 一维数组 程 ...

随机推荐

  1. Mybatis映射文件sql语句注意事项

    1.插入

  2. Shiro加盐加密

    接本人的上篇文章<Shiro认证.角色.权限>,这篇文章我们来学习shiro的加盐加密实现 自定义Realm: package com.czhappy.realm; import org. ...

  3. bootstrap基础学习【导航条、分页导航】(五)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. php 的 socket简单原理及实现

    什么是socket socket:网络上的两个程序通过一个双向的通信连接实现数据的交换,连接的一端称为一个socket. 因此socket运行是置少有2个端组成,一个为服务端一个为客户端(客户端可以多 ...

  5. 学习笔记:oracle学习二:oracle11g数据库sql*plus命令之数据库交互、设置运行环境

    目录 1.SQL*PLUS与数据库的交互 2.设置sql*plus运行环境 2.1 set命令简介 2.2 使用set命令设置运行环境 2.2.1 pagesize变量 2.2.2 NEWPAGE变量 ...

  6. C++:链表(有头链表)

    介绍 把链表分为无头链表和有头链表. 无头链表:所有的节点都包含了有效数据,上一篇文章中演示代码使用的就是无头链表. 有头链表:用一个固定的头节点来指代整个链表,所有的对象都挂在这个头节点下面,而头节 ...

  7. python学习-45 模块

    模块 -----模块包括三种: ····python标准库 ····第三方模块 ····应用程序自定义模块 -------应用程序自定义模块 1.建立两个py文件,一个是定义函数用的cal.py de ...

  8. 如何用Dome4j(2.2.1)创建Xml

    XML解析器常见的有两种: 1.SAX解析器,用于xml的简单API 2.DOM解析器,文档对象模型 DOM就是利用对象来把文本模型化,但是模型实现有以下几个基本的点: 1. 用来表示.操作文档的接口 ...

  9. IDEA中通过Maven插件使用MyBatis Generator

    这样做更简单,参考: IDEA集成MyBatis Generator 插件 详解

  10. (三)Lucene之删除更新文档以及luke的基本使用

    一.demo 本例中采用单元测试,故在pom.xml中引入junit jar包 1.1 前提: public class IndexTest { /** *数据准备 */ private String ...