go reflect TypeOf 与ValueOf
来源:互联网 发布:淘宝 买家秀图 编辑:程序博客网 时间:2024/06/08 15:27
学习go的反射对于理解go的底层实现以及接口机制有着非常重要的价值;本篇文章从一个源程序来体验一下go反射的两个核心函数TypeOf 和ValueOf的使用方法,特别体验下怎么根据type构造type实例的办法。
package mainimport ("fmt""reflect")type Speaker interface {Speak()stringSet(string)}type Teacher struct {Name string}func (this *Teacher) Speak() string {return this.Name}func (this *Teacher) Set(name string) {this.Name = name}func TestRef(s Speaker)(t reflect.Type){ reflectVal := reflect.ValueOf(s)//t = reflect.Indirect(reflectVal).Type()t = reflectVal.Elem().Type()fmt.Printf("reflect.ValueOf(%v).Elem().Type()=%v\n",s,t)return}func TestRef2(s Speaker)(t reflect.Type){ //t = reflect.TypeOf(s).Elem()//fmt.Printf("reflect.TypeOf(%v).Elem()=%v\n",s,t) t = reflect.TypeOf(s)fmt.Printf("reflect.TypeOf(%v)=%v\n",s,t)return}func TestRef3(s *Teacher)(t reflect.Type){ t = reflect.TypeOf(s).Elem()fmt.Printf("reflect.TypeOf(%v).Elem()=%v\n",s,t)return}func main(){te := &Teacher{Name:"newyear",}fmt.Printf("source=%v\n",te)fmt.Printf("\n")var tea Speaker = te t1 := TestRef(tea)//默认是创建一个零值实例,string 就是""if m, ok := reflect.New(t1).Interface().(Speaker); ok {fmt.Printf("reflect.New(%v).Interface().(Speaker)=%v\n",t1, m)fmt.Printf("se.Speak()=%v\n",m.Speak())m.Set("2014")fmt.Printf("reflect.New(%v).Interface().(Speaker)=%v\n",t1, m)fmt.Printf("se.Speak()=%v\n",m.Speak())}fmt.Printf("Source Instance te=%v\n",te)fmt.Printf("\n")t2 := TestRef2(tea)//if m, ok := reflect.New(t2).Interface().(Speaker); ok {if m, ok := reflect.New(t2.Elem()).Interface().(Speaker); ok {fmt.Printf("reflect.New(%v.Elem()).Interface().(Speaker)=%v\n",t2, m)fmt.Printf("se.Speak()=%v\n",m.Speak()) m.Set("2015")fmt.Printf("reflect.New(%v.Elem()).Interface().(Speaker)=%v\n",t2, m)fmt.Printf("se.Speak()=%v\n",m.Speak())}fmt.Printf("Source Instance te=%v\n",te)fmt.Printf("\n")t3 := TestRef3(te)if m, ok := reflect.New(t3).Interface().(Speaker); ok {fmt.Printf("reflect.New(%v).Interface().(Speaker)=%v\n", t3, m)fmt.Printf("se.Speak()=%v\n",m.Speak())m.Set("2016")fmt.Printf("reflect.New(%v).Interface().(Speaker)=%v\n", t3, m)fmt.Printf("se.Speak()=%v\n",m.Speak())}fmt.Printf("Source Instance te=%v\n",te)fmt.Printf("\n")fmt.Printf("reflect.TypeOf(tea)=%v\n",reflect.TypeOf(tea))fmt.Printf("reflect.TypeOf(tea).Elem()=%v\n",reflect.TypeOf(tea).Elem())fmt.Printf("reflect.ValueOf(tea)=%v\n",reflect.ValueOf(tea))fmt.Printf("reflect.ValueOf(tea).Elem()=%v\n",reflect.ValueOf(tea).Elem())fmt.Printf("\n") if se, ok := reflect.ValueOf(tea).Interface().(Speaker); ok { //if se, ok := reflect.ValueOf(te).Interface().(Speaker); ok {fmt.Printf("reflect.ValueOf(%v).Interface().(Speaker)=%v\n",tea, se)fmt.Printf("se.Speak()=%v\n",se.Speak())se.Set("2017")fmt.Printf("reflect.ValueOf(%v).Interface().(Speaker)=%v\n",tea, se)fmt.Printf("se.Speak()=%v\n",se.Speak()) }fmt.Printf("Source Instance te=%v\n",te)fmt.Printf("\n") //if se, ok := reflect.New(reflect.TypeOf(te).Elem()).Interface().(Speaker); ok { if se, ok := reflect.New(reflect.TypeOf(tea).Elem()).Interface().(Speaker); ok {fmt.Printf("reflect.New(reflect.TypeOf(%v).Elem()).Interface().(Speaker)=%v\n",tea,se)fmt.Printf("se.Speak()=%v\n",se.Speak())se.Set("2018")fmt.Printf("reflect.New(reflect.TypeOf(%v).Elem()).Interface().(Speaker)=%v\n",tea,se)fmt.Printf("se.Speak()=%v\n",se.Speak()) }fmt.Printf("Source Instance te=%v\n",te)fmt.Printf("\n") if se, ok := reflect.New(reflect.TypeOf(te).Elem()).Interface().(Speaker); ok {fmt.Printf("reflect.New(reflect.TypeOf(%v).Elem()).Interface().(Speaker)=%v\n",te,se)fmt.Printf("se.Speak()=%v\n",se.Speak())se.Set("2019")fmt.Printf("se.Speak()=%v\n",se.Speak())fmt.Printf("reflect.New(reflect.TypeOf(%v).Elem()).Interface().(Speaker)=%v\n",te,se) }fmt.Printf("Source Instance te=%v\n",te)fmt.Printf("\n")}
0 0
- go reflect TypeOf 与ValueOf
- Go Reflect
- go-reflect
- go reflect
- Go Reflect
- javascript typeof,null,undefined,valueof()
- indexOf、instanceOf、typeOf、valueOf详解
- Go语言 详解reflect
- go reflect 3 rules
- go语言反射reflect
- Go reflect初探
- javascript(八)instanceof, typeof, indexOf,valueOf()
- Go的reflect机制和reflect包
- toString与String.valueOf()
- Java parseInt与valueOf
- 《The.Go.Programming.Language.2015.11》之 reflect
- typeof 详解 typeof 与 define 的区别
- Go语言学习之reflect包(The way to go)
- ubuntu 14.04 x64 安装ia32-libs(时间点为2015.1.2)
- shadow文件中密码的加密方式
- MongoDB学习笔记(三) 在MVC模式下通过Jqgrid表格操作MongoDB数据
- 用WebCollector 2.x爬取新浪微博(无需手动获取cookie)
- 2015-01-02
- go reflect TypeOf 与ValueOf
- 文本格式不变存放在数据库
- UVA201 - Squares
- c++实现Callback机制
- Java基础----UDP
- h2database源码浅析:SQL语句的执行
- 变量类型 15-1-2
- python 和 c++ 之间发消息用json
- 《gdb调试之基础篇》