golang reflect demo讲解

Demo

这里引用了第三方包

go get github.com/influxdata/toml
go install github.com/influxdata/toml

就可以直接使用第三方代码了,可以修改,也可以打断点。

package main

import (
	"fmt"
	"reflect"

	"github.com/influxdata/toml"
)

type testInterface interface {
}

type testStruT struct {
	TestFieldA string
	TestFieldB string `testFieldB:"b"`
	TestFieldC string `testFieldC:"ccc"`
}

func testProcStruct(v interface{}) {
	fmt.Printf("1 v =                       %v\n", v)
	rv := reflect.ValueOf(v)
	fmt.Printf("2 rv = reflect.ValueOf(v) = %v\n", rv)
	fmt.Printf("3 rv.Kind() =               %v\n", rv.Kind())
	elem := rv.Elem()
	fmt.Printf("4 elem = rv.Elem() =        %v\n", elem)
	fmt.Printf("5 elem.Kind() =             %v\n", elem.Kind())

	/*判断改结构体是否有名为TestFieldA、TESTFIELDA的字段,若找不到found为false.在FindField会将传入的字符串格式化判断(可以看源码)*/
	fv, fieldName, found := toml.FindField(elem, "testFieldA")
	fmt.Printf("6 fv = %v, fieldName = %v, found = %v\n", fv, fieldName, found)
}

func testPorcMap(v interface{}) {
	fmt.Printf("7 v =                       %v\n", v)
	rv := reflect.ValueOf(v)
	fmt.Printf("8 rv = reflect.ValueOf(v) = %v\n", rv)
	fmt.Printf("9 rv.Kind() =               %v\n", rv.Kind())
	//elem := rv.Elem()   若Kind是Map,这里会Panic

	rvType := rv.Type()
	fmt.Printf("10 rvType =                 %v\n", rvType)
	fmt.Printf("11 rvType.Elem() =          %v\n", rvType.Elem())
	mv := reflect.New(rvType.Elem())
	fmt.Printf("12 mv =                     %v\n", mv)
	elem := mv.Elem()
	fmt.Printf("13 elem =                   %v\n", elem)
	fmt.Printf("14 elem.Type() =            %v\n", elem.Type())
	fmt.Printf("15 elem.Kind() =            %v\n", elem.Kind())

	fmt.Printf("16 v =                      %v\n", v)
	rv.SetMapIndex(reflect.ValueOf("testKey2"), elem) //  修改键值
	fmt.Printf("17 v =                      %v\n", v)
}

func main() {
	//  匿名函数
	creator := func() testInterface {
		return &testStruT{
			TestFieldA: "testValue1",
		}
	}
	testInter := creator()
	testProcStruct(testInter)

	testStruct2 := testStruT{
		TestFieldC: "testValue2",
	}
	testStruct3 := testStruT{
		TestFieldB: "testValue3",
	}
	testMap := map[string]testStruT{}
	testMap["testKey2"] = testStruct2
	testMap["testKey3"] = testStruct3
	testPorcMap(testMap)
}


运行结果
[Running] go run "c:\Code\Go\src\study\main.go"
1 v =                       &{testValue1  }
2 rv = reflect.ValueOf(v) = &{testValue1  }
3 rv.Kind() =               ptr
4 elem = rv.Elem() =        {testValue1  }
5 elem.Kind() =             struct
6 fv = testValue1, fieldName = TestFieldA, found = true
7 v =                       map[testKey2:{  testValue2} testKey3:{ testValue3 }]
8 rv = reflect.ValueOf(v) = map[testKey2:{  testValue2} testKey3:{ testValue3 }]
9 rv.Kind() =               map
10 rvType =                 map[string]main.testStruT
11 rvType.Elem() =          main.testStruT
12 mv =                     &{  }
13 elem =                   {  }
14 elem.Type() =            main.testStruT
14 v =                      map[testKey2:{  testValue2} testKey3:{ testValue3 }]
15 v =                      map[testKey2:{  } testKey3:{ testValue3 }]

[Done] exited with code=0 in 3.769 seconds
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值