且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

得到无效的操作:尝试为地图建立索引时,mymap [“ title”](类型接口{}不支持索引)

更新时间:2021-10-17 23:18:29

此处的数据类型是关键。 mdi 实际上不是地图,而是 interface {} ,它可以是任何东西-地图,字符串,一个int。您需要首先将其声明为具有预期键/值类型的映射,或者执行 JSON和Go

The data type here was the key. mdi was not actually a map, but an interface{}, which could be anything - a map, a string, an int. You need to assert it to a map with expected key/value types first, or do the awkward case switch outlined in JSON and Go.

mdi, err := page.Metadata()
md, ok := mdi.(map[string]interface{})
fmt.Println(md["title"])