1、代码
package main import ( "fmt" "reflect" ) type Test struct { a int b int } // TypeInstanceToName converts an instance of a type to a unique name. func TypeInstanceToName(v interface{}) string { t := reflect.TypeOf(v) if name := t.Name(); name != "" { // non-interface types return t.PkgPath() + "." + name } // interface types e := t.Elem() return e.PkgPath() + "." + e.Name() } func main() { fmt.Println(TypeInstanceToName(Test{})) }
2、运行结果
main.Test