golang reflect demo讲解

2024-05-27 21:48
文章标签 讲解 golang demo reflect

本文主要是介绍golang reflect demo讲解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Demo

这里引用了第三方包

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

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

package mainimport ("fmt""reflect""github.com/influxdata/toml"
)type testInterface interface {
}type testStruT struct {TestFieldA stringTestFieldB 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,这里会PanicrvType := 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"] = testStruct2testMap["testKey3"] = testStruct3testPorcMap(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

这篇关于golang reflect demo讲解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1008683

相关文章

SQL Server中行转列方法详细讲解

《SQLServer中行转列方法详细讲解》SQL行转列、列转行可以帮助我们更方便地处理数据,生成需要的报表和结果集,:本文主要介绍SQLServer中行转列方法的相关资料,需要的朋友可以参考下... 目录前言一、为什么需要行转列二、行转列的基本概念三、使用PIVOT运算符进行行转列1.创建示例数据表并插入数

C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解

《C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解》:本文主要介绍C++,C#,Rust,Go,Java,Python,JavaScript性能对比全面... 目录编程语言性能对比、核心优势与最佳使用场景性能对比表格C++C#RustGoJavapythonjav

golang实现nacos获取配置和服务注册-支持集群详解

《golang实现nacos获取配置和服务注册-支持集群详解》文章介绍了如何在Go语言中使用Nacos获取配置和服务注册,支持集群初始化,客户端结构体中的IpAddresses可以配置多个地址,新客户... 目录golang nacos获取配置和服务注册-支持集群初始化客户端可选参数配置new一个客户端 支

VS Code中的Python代码格式化插件示例讲解

《VSCode中的Python代码格式化插件示例讲解》在Java开发过程中,代码的规范性和可读性至关重要,一个团队中如果每个开发者的代码风格各异,会给代码的维护、审查和协作带来极大的困难,这篇文章主... 目录前言如何安装与配置使用建议与技巧如何选择总结前言在 VS Code 中,有几款非常出色的 pyt

Java中实现对象的拷贝案例讲解

《Java中实现对象的拷贝案例讲解》Java对象拷贝分为浅拷贝(复制值及引用地址)和深拷贝(递归复制所有引用对象),常用方法包括Object.clone()、序列化及JSON转换,需处理循环引用问题,... 目录对象的拷贝简介浅拷贝和深拷贝浅拷贝深拷贝深拷贝和循环引用总结对象的拷贝简介对象的拷贝,把一个

解决升级JDK报错:module java.base does not“opens java.lang.reflect“to unnamed module问题

《解决升级JDK报错:modulejava.basedoesnot“opensjava.lang.reflect“tounnamedmodule问题》SpringBoot启动错误源于Jav... 目录问题描述原因分析解决方案总结问题描述启动sprintboot时报以下错误原因分析编程异js常是由Ja

Unity新手入门学习殿堂级知识详细讲解(图文)

《Unity新手入门学习殿堂级知识详细讲解(图文)》Unity是一款跨平台游戏引擎,支持2D/3D及VR/AR开发,核心功能模块包括图形、音频、物理等,通过可视化编辑器与脚本扩展实现开发,项目结构含A... 目录入门概述什么是 UnityUnity引擎基础认知编辑器核心操作Unity 编辑器项目模式分类工程

MySQL连表查询之笛卡尔积查询的详细过程讲解

《MySQL连表查询之笛卡尔积查询的详细过程讲解》在使用MySQL或任何关系型数据库进行多表查询时,如果连接条件设置不当,就可能发生所谓的笛卡尔积现象,:本文主要介绍MySQL连表查询之笛卡尔积查... 目录一、笛卡尔积的数学本质二、mysql中的实现机制1. 显式语法2. 隐式语法3. 执行原理(以Nes

全面解析Golang 中的 Gorilla CORS 中间件正确用法

《全面解析Golang中的GorillaCORS中间件正确用法》Golang中使用gorilla/mux路由器配合rs/cors中间件库可以优雅地解决这个问题,然而,很多人刚开始使用时会遇到配... 目录如何让 golang 中的 Gorilla CORS 中间件正确工作一、基础依赖二、错误用法(很多人一开

RabbitMQ消费端单线程与多线程案例讲解

《RabbitMQ消费端单线程与多线程案例讲解》文章解析RabbitMQ消费端单线程与多线程处理机制,说明concurrency控制消费者数量,max-concurrency控制最大线程数,prefe... 目录 一、基础概念详细解释:举个例子:✅ 单消费者 + 单线程消费❌ 单消费者 + 多线程消费❌ 多