map[string]string
map[string]interface{}
我现在的代码:
package main
import (
"fmt"
"reflect"
)
type Foo struct {
FirstName string `tag_name:"tag 1"`
LastName string `tag_name:"tag 2"`
Age int `tag_name:"tag 3"`
}
func inspect(f interface{}) map[string]string {
m := make(map[string]string)
val := reflect.ValueOf(f).Elem()
for i := 0; i < val.NumField(); i++ {
valueField := val.Field(i)
typeField := val.Type().Field(i)
f := valueField.Interface()
val := reflect.ValueOf(f)
m[typeField.Name] = val.String()
}
return m
}
func dump(m map[string]string) {
for k, v := range m {
fmt.Printf("%s : %s\n", k, v)
}
}
func main() {
f := &Foo{
FirstName: "Drew",
LastName: "Olson",
Age: 30,
}
a := inspect(f)
dump(a)
}
运行代码的输出:
FirstName : Drew
LastName : Olson
Age : <int Value>
根据我的理解,FirstName和LastName的输出是实际的reflect.Value对象,但对于字符串,值上的String()方法只输出底层的String . 我想要获取int并将其更改为字符串,但是从relfect包文档中我不会立即看到它是如何完成的 .
Soo ....如何从golang中的reflect.Value中获取基础值?