go中的map[Interface{}]Interface{}理解
map里面的k,v支持很多的类型。对于go来说也是,go中有个接口的概念,任何对象都实现了一个空接口。那么我们把map里面的k,v都用interface去定义,当我们在使用这个map的时候,我们可以把任何类型的参数传入到,这个map中。真的可以吗,下面我们来看下代码。
package main import (
"fmt"
) func main() { mapInterface := make(map[interface{}]interface{})
mapString := make(map[string]string) mapInterface["k1"] = 1
mapInterface[3] = "hello"
mapInterface["world"] = 1.05
mapInterface["rt"] = true for key, value := range mapInterface {
strKey := fmt.Sprintf("%v", key)
strValue := fmt.Sprintf("%v", value) mapString[strKey] = strValue
} fmt.Printf("%#v", mapString)
}
输出结果

我们可以看到,不管int类型,bool类型,都可以传值到map中。
我们最后只需要把他装换成我们需要的类型就可以了
go中的map[Interface{}]Interface{}理解的更多相关文章
- Java中abstract class 和 interface 的解释和他们的异同点(转)
(一)概述 在Java语言中, abstract class 和interface 是支持抽象类定义的两种机制.正是由于这两种机制的存 在,才赋予了Java强大的 面向对象能力.abstract ...
- go中的数据结构接口-interface
1. 接口的基本使用 golang中的interface本身也是一种类型,它代表的是一个方法的集合.任何类型只要实现了接口中声明的所有方法,那么该类就实现了该接口.与其他语言不同,golang并不需要 ...
- GO学习-(38) Go语言结构体转map[string]interface{}的若干方法
结构体转map[string]interface{}的若干方法 本文介绍了Go语言中将结构体转成map[string]interface{}时你需要了解的"坑",也有你需要知道的若 ...
- 面向对象编程语言中的接口(Interface)
在大多面向对象的编程语言中都提供了Interface(接口)的概念.如果你事先学过这个概念,那么在谈到“接口测试”时,会不会想起这个概念来!?本篇文章简单介绍一下面向对象编程语言中的Interface ...
- is not valid JSON: json: cannot unmarshal string into Go value of type map[string]interface | mongodb在windows和Linux导出出错
执行mongoexport命令的时候 mongoexport --csv -f externalSeqNum,paymentId --host 127.0.0.1:27017 -d liveX -c ...
- go语言解析 map[string]interface{} 数据格式
原文:https://blog.csdn.net/Nick_666/article/details/79801914 map记得分配内存 解析出来的int类型会变成float64类型 注意判断不为ni ...
- STL 中的map 与 hash_map的理解
可以参考侯捷编著的<STL源码剖析> STL 中的map 与 hash_map的理解 1.STL的map底层是用红黑树存储的,查找时间复杂度是log(n)级别: 2.STL的hash_ma ...
- 【转】GO语言map类型interface{}转换踩坑小记
原文:https://www.az1314.cn/art/69 ------------------------------------------ mapA := make([string]inte ...
- This sample is for changing from “float64” to “int” for values did unmarshal using map[string]interface{}. When it did unmarshal using map[string]interface{}, a number with “int” was changed to “floa
This sample is for changing from “float64” to “int” for values did unmarshal using map[string]interf ...
随机推荐
- hdu3605 Escape 二分图多重匹配/最大流
2012 If this is the end of the world how to do? I do not know how. But now scientists have found tha ...
- python------模块定义、导入、优化 ------->Yaml, l模块
一. yaml模块 用来做配置文件. 需要pip安装该包. 二. ConfigParser模块 用来生成和修改常见配置文件,在python3.x版本中更名为configparser. (什么是配置文件 ...
- 直面Java 第003期
. 问:什么是平台无关性,Java是如何做到平台无关的? 解: 大家说的都很对,看来大家对这个概念掌握的很可以.我简单总结一下. 跨平台指的是一种语言在计算机上的运行不受平台的约束,一次编译,到处执行 ...
- PHP用curl发送get post put delete patch请求
function getUrl($url){ $headerArray = array("Content-type:application/json;", "Accept ...
- 线性代数及其应用 (David C.Lay, Steven R.Lay 著)
第1章 线性代数中的线性方程组 (已看) 介绍性实例 经济学与工程中的线性模型 1.1 线性方程组 1.2 行化简与阶梯形矩阵 1.3 向量方程 1.4 矩阵方程Ax=b 1.5 线性方程组的解集 1 ...
- Linq中left join之多表查询
using System; using System.Collections; using System.Collections.Generic; using System.Data; using S ...
- Redis 可视化界面工具:Fastoredis
下载地址:https://sourceforge.net/projects/fastoredis/
- linux 自总结常用命令(centos系统)
查看apache2的命令 httpd -V 其中HTTPD_ROOT和SERVER_CONFIG_FILE 就可以确定httpd.conf(Apache配置文件)的路径了 apache启动.停止.重 ...
- Spark代码Eclipse远程调试
我们在编写Spark Application或者是阅读源码的时候,我们很想知道代码的运行情况,比如参数设置的是否正确等等.用Logging方式来调试是一个可以选择的方式,但是,logging方式调试代 ...
- kafka_2.11-0.8.2.1生产者producer的Java实现
转载自:http://blog.csdn.net/ch717828/article/details/50818261 1. 开启Kafka Consumer 首先选择集群的一台机器,打开kafka c ...