无法在[]界面中访问映射中的键 [英] Having trouble accessing a key in a map inside an []interface
问题描述
包主
导入fmt
func main(){
example_container:= [] interface {} {
map [string] string {
name:bob,
id:1,
},
map [string] string {
name:jim,
id:2 ,
},
}
fmt.Printf(%v \ n,example_container)
fmt.Printf(%v \ n,example_container [0] )
fmt.Printf(%v \ n,example_container [0] [name])
}
有问题的行:
fmt.Printf( %v \ n,example_container [0] [name])
错误:
无效操作:example_container [0] [name](type interface {}不支持索引)
问题:
我如何访问在这个接口中的键,然后?
我必须定义一个更复杂的接口和一个方法来完成这个吗?
接口{}
。类型 interface {}
的值不能被索引。 但是,由于您将类型 map [string] string
,你可以使用类型断言获得该映射类型的值,您可以正确编制索引:
fmt.Printf(%v \\\
,example_container [0]。(map [string] string)[name])
输出(在去游乐场试试):
[map [name:bob id:1] map [name:jim id:2]]
map [name:bob id:1]
bob
如果您知道您将始终存储类型 map [string]字符串
在你的 example_container
切片中,最好的办法就是像这样定义它:
example_container:= [] map [string] string {
map [string] string {
name:bob,
id:1,
},
map [string] string {
name:jim,
id:2 ,
},
}
然后你不需要输入断言来访问名称:
$ $ $ $ $ $ $ $ $ $ $'$'$,$'',example_container [0] [name ])
请在 Go Playground 。
另外请注意,在用于初始化 example_container
slice,您甚至可以在列出元素时省略地图类型: example_container: = [] map [string] string {
{
name:bob,
id:1,
},
{
name:jim,
id:2,
},
}
Example code:
package main
import "fmt"
func main() {
example_container := []interface{}{
map[string]string{
"name": "bob",
"id": "1",
},
map[string]string{
"name": "jim",
"id": "2",
},
}
fmt.Printf("%v\n", example_container)
fmt.Printf("%v\n", example_container[0])
fmt.Printf("%v\n", example_container[0]["name"])
}
Problematic line:
fmt.Printf("%v\n", example_container[0]["name"])
Error:
invalid operation: example_container[0]["name"] (type interface {} does not support indexing)
Question:
How do I access the keys inside this interface, then?
Do I have to define a more elaborate interface with a method set to accomplish this?
Since your slice type is []interface{}
, indexing this slice will give you elements of type interface{}
. Values of type interface{}
cannot be indexed.
But since you put values of type map[string]string
into it, you may use type assertion to obtain a value of that map type, which you can index properly:
fmt.Printf("%v\n", example_container[0].(map[string]string)["name"])
Output (try it on the Go Playground):
[map[name:bob id:1] map[name:jim id:2]]
map[name:bob id:1]
bob
If you know you will always store values of type map[string]string
in your example_container
slice, best would be to define it like this:
example_container := []map[string]string{
map[string]string{
"name": "bob",
"id": "1",
},
map[string]string{
"name": "jim",
"id": "2",
},
}
And then you don't need type assertion to access the name:
fmt.Printf("%v\n", example_container[0]["name"])
Try this one on the Go Playground.
Also note that in the composite literal you use to initialize your example_container
slice, you can even omit the map type when listing the elements:
example_container := []map[string]string{
{
"name": "bob",
"id": "1",
},
{
"name": "jim",
"id": "2",
},
}
这篇关于无法在[]界面中访问映射中的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!