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
上一篇:基于Floyd算法的校园导航系统(Python版)


下一篇:【每日一题见微知著】BFS和DP——力扣第70场双周赛