Foreword: I optimized and improved the below solution, and released it as a library here: github.com/icza/dyno.


The cleanest way would be to create predefined types (structures struct) that model your JSON, and unmarshal to a value of that type, and you can simply refer to elements using Selectors (for struct types) and Index expressions (for maps and slices).

However if your input is not of a predefined structure, I suggest you the following 2 helper functions: get() and set(). The first one accesses (returns) an arbitrary element specified by an arbitrary path (list of string map keys and/or int slice indices), the second changes (sets) the value specified by an arbitrary path (implementations of these helper functions are at the end of the answer).

You only have to include these 2 functions once in your project/app.

And now using these helpers, the tasks you want to do becomes this simple (just like the python solution):

fmt.Println(get(d, "key3", 0, "c2key1", "c3key1"))
set("NEWVALUE", d, "key3", 0, "c2key1", "c3key1")
fmt.Println(get(d, "key3", 0, "c2key1", "c3key1"))

Output:

change1
NEWVALUE

Try your modified app on the Go Playground.

Note - Further Simplification:

You can even save the path in a variable and reuse it to simplify the above code further:

path := []interface{}{"key3", 0, "c2key1", "c3key1"}

fmt.Println(get(d, path...))
set("NEWVALUE", d, path...)
fmt.Println(get(d, path...))

And the implementations of get() and set() are below. Note: checks whether the path is valid is omitted. This implementation uses Type switches:

func get(m interface{}, path ...interface{}) interface{} {
for _, p := range path {
switch idx := p.(type) {
case string:
m = m.(map[string]interface{})[idx]
case int:
m = m.([]interface{})[idx]
}
}
return m
} func set(v interface{}, m interface{}, path ...interface{}) {
for i, p := range path {
last := i == len(path)-1
switch idx := p.(type) {
case string:
if last {
m.(map[string]interface{})[idx] = v
} else {
m = m.(map[string]interface{})[idx]
}
case int:
if last {
m.([]interface{})[idx] = v
} else {
m = m.([]interface{})[idx]
}
}
}
}

golang depth read map的更多相关文章

  1. 记一次坑爹的golang 二维map判断问题

    记一次坑爹的golang 二维map判断问题 2018年10月18日 23:16:21 yinnnnnnn 阅读数:32更多 个人分类: golang   版权声明:本文为博主原创文章,未经博主允许不 ...

  2. Golang基础教程——map使用篇

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是golang专题的第7篇文章,我们来聊聊golang当中map的用法. map这个数据结构我们经常使用,存储的是key-value的键 ...

  3. Golang,用map写个单词统计器

    Golang中也有实用的泛型编程模板.如map.据Go官方团队称,其实现为Hash表,而非类似cpp或Java的红黑树.所以理论上速度更能快上几个等级(Hash与红黑树的效率对比可以看我的文章C++中 ...

  4. Golang 入门 : 映射(map)

    映射是一种数据结构,用于存储一系列无序的键值对,它基于键来存储值.映射的特点是能够基于键快速检索数据.键就像是数组的索引一样,指向与键关联的值.与 C++.Java 等编程语言不同,在 Golang ...

  5. Golang教程:Map

    什么是 map? Map 是 Go 中的内置类型,它将键与值绑定到一起.可以通过键获取相应的值. 如何创建 map? 可以通过将键和值的类型传递给内置函数 make 来创建一个 map.语法为:mak ...

  6. golang struct 转map 及 map[string]*Struct 初始化和遍历

    package main import ( "encoding/json" "errors" "fmt" "reflect&quo ...

  7. 深入理解golang:sync.map

    疑惑开篇 有了map为什么还要搞个sync.map 呢?它们之间有什么区别? 答:重要的一点是,map并发不是安全的. 在Go 1.6之前, 内置的map类型是部分goroutine安全的,并发的读没 ...

  8. golang struct转map

    struct转map package main import ( "fmt" "reflect" "time" ) type User st ...

  9. golang中,map作为函数参数是如何传递的

    当你声明一个map的时候: m := make(map[int]int) 编译器会调用 runtime.makemap: // makemap implements a Go map creation ...

随机推荐

  1. 解决Android无法正常https://dl.google.com/dl/android/maven2/com/的办法

    最近需要进行移动开发,在安装Android Studio时,遇到了很纠结的问题,安装一直很不顺利.经过2天的百度搜索终于是找到解决的办法. 问题花了2天的时间才从茫茫大海中找到确切的答案.所以必须开个 ...

  2. js数据持久化本地数据存储-JSON.parse和JSON.stringify的区别

    JSON.stringify()的作用是将 JavaScript 值转换为 JSON 字符串, 而JSON.parse()可以将JSON字符串转为一个对象. 简单点说,它们的作用是相对的,我用JSON ...

  3. Windows系统下载地址

    地址: https://msdn.itellyou.cn/ 里面给出的是迅雷下载链接,请提前安装好迅雷

  4. idea设置数据库连接所有项目都能看到

    首先打来配置好数据库的项目,打开database视图,点击配置. 点击 Project Data Soures 下配置好的数据库,然后点击 Make Global ,然后就可以在其他项目看到该数据库连 ...

  5. 【一起学源码-微服务】Nexflix Eureka 源码二:EurekaServer启动之配置文件加载以及面向接口的配置项读取

    前言 上篇文章已经介绍了 为何要读netflix eureka源码了,这里就不再概述,下面开始正式源码解读的内容. 如若转载 请标明来源:一枝花算不算浪漫 代码总览 还记得上文中,我们通过web.xm ...

  6. webpack4(4.41.2) 打包出现 TypeError this.getResolve is not a function

    报错问题: webpack 打包出现 TypeError: this.getResolve is not a function 环境: nodejs 12.13.0 npm 6.12.0 webpac ...

  7. vue-cli 4.0.5 配置环境变量样例

    在项目根目录下建 .env 文件,环境变量无论运行何种模式均可获取其设置值. vue 中主要有三种模式: development.test.production,在 package.json 中目前三 ...

  8. 微信小程序_(组件)swiper轮播图

    微信小程序swiper轮播图组件官方文档 传送门 Learn: swiper组件 一.swiper组件 indicator-dots:是否显示面板指示点[默认值false] autoplay:是否自动 ...

  9. hbase hbck

    Number of Tables: 7Number of live region servers: 3Number of dead region servers: 0Number of empty R ...

  10. QTableWidget懒加载(动态加载)实现

    在QTableWidget中加入一行行的数据,如果数据数量过多,滚动起来就会卡顿,插入数据时也会影响性能,为了解决这个问题,采用懒加载的方式,只在界面上显示10几条数据,填满界面.同时增加而外的竖直滚 ...