我发现了这个问题和这个很棒的回答:
我尝试使用相同的方法来获取结构体的名称:
package main
import (
"fmt"
"reflect"
)
type Ab struct {
}
func getType(myvar interface{}) string {
return reflect.TypeOf(myvar).Name()
}
func main() {
fmt.Println("Hello, playground")
tst := "string"
tst2 := 10
tst3 := 1.2
tst4 := new(Ab)
fmt.Println(getType(tst))
fmt.Println(getType(tst2))
fmt.Println(getType(tst3))
fmt.Println(getType(tst4))
}
Go playground: http://play.golang.org/p/tD8mygvETH
但输出结果为:
Hello, playground
string
int
float64
Program exited.
期望的输出将会是:
Hello, playground
string
int
float64
Ab
Program exited.
我尝试通过阅读文档来找到问题所在,但没有发现有关此问题的内容。因此,对于这个非常普遍的问题,我很抱歉:
reflect.TypeOf().Name()
为什么不能与这些结构一起使用?