我正在尝试创建一个可以接受关注的函数
*struct
[]*struct
map[string]*struct
这里的结构可以是任何结构,而不仅仅是一个特定的结构。
将接口(interface)转换为 *struct
或 []*struct
工作正常。
但是给 map 报错。
反射(reflect)后显示它是 map[] 但尝试迭代范围时出错。
这里是代码
package main
import (
"fmt"
"reflect"
)
type Book struct {
ID int
Title string
Year int
}
func process(in interface{}, isSlice bool, isMap bool) {
v := reflect.ValueOf(in)
if isSlice {
for i := 0; i < v.Len(); i++ {
strct := v.Index(i).Interface()
//... proccess struct
}
return
}
if isMap {
fmt.Printf("Type: %v\n", v) // map[]
for _, s := range v { // Error: cannot range over v (type reflect.Value)
fmt.Printf("Value: %v\n", s.Interface())
}
}
}
func main() {
b := Book{}
b.Title = "Learn Go Language"
b.Year = 2014
m := make(map[string]*Book)
m["1"] = &b
process(m, false, true)
}
有没有办法将 interface{}
转换为映射和迭代或获取它的元素。
最佳答案
如果 map 值可以是任何类型,则使用反射来遍历 map :
if v.Kind() == reflect.Map {
for _, key := range v.MapKeys() {
strct := v.MapIndex(key)
fmt.Println(key.Interface(), strct.Interface())
}
}
如果有一组已知的结构类型,那么 type switch可以使用:
func process(in interface{}) {
switch v := in.(type) {
case map[string]*Book:
for s, b := range v {
// b has type *Book
fmt.Printf("%s: book=%v\n" s, b)
}
case map[string]*Author:
for s, a := range v {
// a has type *Author
fmt.Printf("%s: author=%v\n" s, a)
}
case []*Book:
for i, b := range v {
fmt.Printf("%d: book=%v\n" i, b)
}
case []*Author:
for i, a := range v {
fmt.Printf("%d: author=%v\n" i, a)
}
case *Book:
fmt.Ptintf("book=%v\n", v)
case *Author:
fmt.Printf("author=%v\n", v)
default:
// handle unknown type
}
}
关于dictionary - 如何将 interface{} 转换为 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38185916/