Go入门笔记-15 Go 使用反射

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  

本博客是个人工作中记录,遇到问题可以互相探讨,没有遇到的问题可能没有时间去特意研究,勿扰。
另外建了几个QQ技术群:
2、全栈技术群:616945527,加群口令abc123
2、硬件嵌入式开发: 75764412
3、Go语言交流群:9924600

闲置域名www.nsxz.com出售(等宽等高字符四字域名)。
原文地址:https://www.cnblogs.com/zhaogaojian/p/15136351.html