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. 前端添加视频流rtmp格式

    要求:rtmp格式, 在线直播 url地址 效果: 代码:初次打开时间较长, <!DOCTYPE html> <html> <head> <script ty ...

  2. eclipse设置html js css自动提示

    eclipse也可以像Visual Studio 2008那样完全智能提示HTML/JS/CSS代码,使用eclipse自带的插件,无需另外安装插件,具体步骤如下 1.打开eclipse→Window ...

  3. cocos creator 倒计时code

    let countDownNode = this.viewNode.getChildByName('countDownNode').getComponent(cc.Label); countDownN ...

  4. Seq2Seq模型与注意力机制

    Seq2Seq模型 基本原理 核心思想:将一个作为输入的序列映射为一个作为输出的序列 编码输入 解码输出 解码第一步,解码器进入编码器的最终状态,生成第一个输出 以后解码器读入上一步的输出,生成当前步 ...

  5. 第一周----常量和final

    符号常量   final 字面常量:1 2 int  a=3;   a是变量   3是常量        

  6. BS

  7. git简易指南

    目录 工作流 初始化仓库 查看分支 添加和提交 推送改动 分支 重命名分支 更新与合并 替换本地改动 回滚 远程仓库 有用的贴士 GIT vs SVN 工作流 你的本地仓库由 git 维护的三棵&qu ...

  8. Python 中文件操作

    上代码: import os import os.path rootdir = "d:/code/su/data" # 指明被遍历的文件夹 for parent,dirnames, ...

  9. 将mdf文件copy到docker对应的目录下

    将mdf文件copy到docker对应的目录下: (<Docker-Container ID> 需要整体替换) docker cp /Users/Jay/Works/db/MyPost.m ...

  10. centos从安装到环境配置

    1.安装centos6.5    http://jingyan.baidu.com/article/25648fc1a235c99191fd0008.html 2.配置网络ip   http://ji ...