本文介绍了类型接口{}不支持golang中的索引编制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的地图:
Map := make(map[string]interface{})
该映射应该包含从字符串到对象数组的映射.数组可以是不同的类型,例如[]Users
或[]Hosts
.我填充了这个数组:
This map is supposed to contain mapping from string to array of objects. Arrays can be of different types, like []Users
or []Hosts
. I populated this array:
TopologyMap["Users"] = Users_Array
TopologyMap["Hosts"] = Hosts_Array
但是当我尝试从中获取元素时:
but when I try to get an elements from it:
Map["Users"][0]
它给出一个错误:(type interface {} does not support indexing)
我该如何克服?
推荐答案
您必须将接口{}显式转换为所需类型的切片才能实现.像这样 https://play.golang.org/p/yZmniZwFar
You have to explicitly convert your interface{} to a slice of the expected type to achieve it. Something like thishttps://play.golang.org/p/yZmniZwFar
这篇关于类型接口{}不支持golang中的索引编制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!