1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
package main import "fmt" import ( "reflect" //"net" //"bufio" ) type StructTest struct { name string } func (this *StructTest) GetFunc() string { return this.name } type IStruct interface{ GetFunc() string } func main() { s := "this is string" fmt.Printf("1: %v\n",reflect.TypeOf(s)) //string a := new(StructTest) a.name = "testname" typ := reflect.TypeOf(a) aValue := reflect.ValueOf(a) var a1 StructTest a.name = "testname1" typ1 := reflect.TypeOf(a1) fmt.Printf("2: %v----%v\n",typ,typ1) //*main.MyStruct main.MyStruct fmt.Printf("3: %v--%v\n",typ.Elem(),aValue.Elem()) //main.MyStruct {testname1}返回指针的真实值 fmt.Printf("4: %v\n",typ.NumMethod()) //1 返回方法数目 fmt.Printf("5: %v\n",typ.Method(0)) //{GetName func(*main.MyStruct) string <func(*main.MyStruct) string Value> 0} 返回方法0 的调用 fmt.Printf("6: %s\n",typ.Name()) // "" fmt.Printf("7: %v\n",typ.PkgPath()) //"" fmt.Printf("8: %v\n",typ.Size()) //8 获取类型的值在分配内存时的大小,功能和 unsafe.SizeOf 一样。 fmt.Printf("9: %v\n",typ.Elem().String()) //main.MyStruct 获取字符串描述 fmt.Printf("10: %v\n",typ.Elem().FieldByIndex([]int{0})) //{name main string 0 [0] false} fieldByName ,status:= typ.Elem().FieldByName("name") //根据名称获取结构体的字段 fmt.Printf("11: %v--%v\n",fieldByName,status) //{name main string 0 [0] false}--true fmt.Printf("12: %v\n",typ.Kind() == reflect.Ptr) //true 判断是否为指针类型 fmt.Printf("13: %v\n",typ.Elem().Kind() == reflect.Struct) //true 判断是否为结构体类型 fmt.Printf("14: %v\n",typ.Implements(reflect.TypeOf((*IStruct)(nil)).Elem())) //true 判断是否实现了接口类型 fmt.Printf("15: %v\n",reflect.TypeOf(12.12).Bits()) //64 占位 cha := make(chan int) fmt.Printf("16: %v\n",reflect.TypeOf(cha).ChanDir()) //chan 网上说是获取通道的方向 var fun func(x int,y...float64) string var fun2 func(x int,t float64) string fmt.Printf("17: %v\n",reflect.TypeOf(fun).IsVariadic()) //true 返回函数的类型最后一个输入参数是否是“...”参数。 fmt.Printf("18: %v\n",reflect.TypeOf(fun2).IsVariadic()) //false fmt.Printf("19: %v\n",reflect.TypeOf(fun).In(0)) //int 返回函数输入参数的第i个类型 Type fmt.Printf("20: %v\n",reflect.TypeOf(fun).In(1)) //[]float64 fmt.Printf("21: %v\n",reflect.TypeOf(fun).NumIn()) //2 函数输入参数总数量 fmt.Printf("22: %v\n",reflect.TypeOf(fun).NumOut()) // 1 函数输出参数总数量 fmt.Printf("23: %v\n",reflect.TypeOf(fun).Out(0)) //string 输出参数的第一个参数的类型 mp := make(map[string]int) mp["rest1"] = 1 fmt.Printf("24: %v\n",reflect.TypeOf(mp).Key()) //stirng 返回 Map 键Key的类型 arr := [1]string{"test"} fmt.Printf("25: %v\n",reflect.TypeOf(arr).Len()) //1 返回数组的长度 fmt.Printf("26: %v\n",typ.Elem().NumField()) //1 字段总数量,在struct结构中 b := reflect.ValueOf(a).MethodByName("GetFunc").Call([]reflect.Value{}) fmt.Printf("27: %v\n", reflect.ValueOf(a).MethodByName("GetFunc")) //0x4aa490 这个我也不太明白,网上说是:使用“字符串”函数名称返回函数的 Value 类型,在struct结构中 fmt.Printf("28: %v\n",[]reflect.Value{}) //[] 空数组 fmt.Printf("29: %v\n",b) //[testname1] 返回调用的结果数组 fmt.Printf("30: %v\n", b[0]) //testname1 取返回结果数组的第一个值 //l,_ := net.Listen("tcp","0.0.0.0::80") //conn,_ :=l.Accept() //bufio.NewWriter(conn) } |