Go反射四讲---第一讲:什么是反射,反射常用的API,反射三原则以及注意事项

2024-08-25 10:28

本文主要是介绍Go反射四讲---第一讲:什么是反射,反射常用的API,反射三原则以及注意事项,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

反射-引入

这是我们反射四讲的第一讲,本次主要向大家介绍一下反射的基础概念以及相关的API。

在计算机科学中,反射是指计算机程序能够在运行时可以访问,检测和修改本身状态或者行为的一种能力。

简单的来说,反射就是程序在运行时能够动地查看自己的状态,并允许修改自身的行为。

首先,要明白对于一个类型变量。它有两层含义,一是类型是什么,二是存储的值是什么。类型决定了变量的存储方式,支持的操作和方法等。值无外乎是读与写,在存储是以0,1格式存放,并使用类型加以约束。类型和值不是孤立存在的。

反射的相关API都在 reflect 包中,最核心的就是reflect.Valuereflect.Type
其中,reflect.Value用于操作值,部分值是可以被反射修改的。reflect.Type用于操作类型信息。类型信息是只能读取的。

注意:reflect.Type可以通过reflect.Value得到,但是反过来不行。

reflect.Type

func TypeOf(i any) Type
//type any = interface{}

形参是一个空接口类型,返回值是一个 Type 接口类型。

注意:reflect包有一个很强的的假设,我们必须我们要操作的是什么Kind

Kind是一个枚举值,用来判断操作的对应类型。例如是否是指针,是否是数组,是否是切片等。
如果使用不当就会引起 panic。so,在调用API之前,我们一定要先读注释,确认什么情况下可以使用!!!

接下来,讲解一下 Type 接口提供的主要通用方法。

类型的通用方法

// Name returns the type's name within its package for a defined type.
Name() string// Kind returns the specific kind of this type.
Kind() Kind// Implements reports whether the type implements the interface type u.
Implements(u Type) bool// AssignableTo reports whether a value of the type is assignable to type u.
AssignableTo(u Type) bool// ConvertibleTo reports whether a value of the type is convertible to type u.
ConvertibleTo(u Type) bool// Comparable reports whether values of this type are comparable.
Comparable() bool// NumMethod returns the number of methods accessible using Method.
NumMethod() int// Method returns the i'th method in the type's method set.
Method(int) Method// MethodByName returns the method with that name in the type's
// method set and a boolean indicating if the method was found.
MethodByName(string) (Method, bool)// PkgPath returns a defined type's package path, that is, the import path
PkgPath() string// Size returns the number of bytes needed to store
Size() uintptr

类型的专有方法

type Type interface {
// Elem returns a type's element type.
// It panics if the type's Kind is not Array, Chan, Map, Pointer, or Slice.
Elem() Type// Bits returns the size of the type in bits.
Bits() int// Len returns an array type's length.
// It panics if the type's Kind is not Array.
Len() int// struct 专有的方法// NumField returns a struct type's field count.
// It panics if the type's Kind is not Struct.
NumField() int// Field returns a struct type's i'th field.
// It panics if the type's Kind is not Struct.
// It panics if i is not in the range [0, NumField()).
Field(i int) StructField// FieldByIndex returns the nested field corresponding
// to the index sequence. It is equivalent to calling Field
// successively for each index i.
// It panics if the type's Kind is not Struct.
FieldByIndex(index []int) StructField// FieldByName returns the struct field with the given name
// and a boolean indicating if the field was found.
// If the returned field is promoted from an embedded struct,
// then Offset in the returned StructField is the offset in
// the embedded struct.
FieldByName(name string) (StructField, bool)// FieldByNameFunc returns the struct field with a name
// that satisfies the match function and a boolean indicating if
// the field was found.
FieldByNameFunc(match func(string) bool) (StructField, bool)// func 专有的方法// IsVariadic reports whether a function type's final input parameter
// IsVariadic panics if the type's Kind is not Func.
IsVariadic() bool// In returns the type of a function type's i'th input parameter.
// It panics if the type's Kind is not Func.
// It panics if i is not in the range [0, NumIn()).
In(i int) Type// NumIn returns a function type's input parameter count.
// It panics if the type's Kind is not Func.
NumIn() int// NumOut returns a function type's output parameter count.
// It panics if the type's Kind is not Func.
NumOut() int// map 专有的方法// Key returns a map type's key type.
// It panics if the type's Kind is not Map.
Key() Type
}

reflect.Value

reflect.Value 表示的是实例的值信息。reflect.Value是一个 struct,并且提供了一系列的 method

func ValueOf(i any) Value
//type Value struct {
//	typ_ *abi.Type  //值的指针类型typ
//	ptr unsafe.Pointer // 指向值的指针ptr
//	flag //标记字段
//}

注意:这里一定要搞清与Type的区别,Type 直接返回接口,Value 返回的是结构体,而在结构体上绑定了一些方法。

下面我们介绍几个常用的在 Value 结构体上绑定的方法。

// CanAddr reports whether the value's address can be obtained with [Value.Addr].
func (v Value) CanAddr() bool {}// Addr returns a pointer value representing the address of v.
func (v Value) Addr() Value {}// CanSet reports whether the value of v can be changed.
func (v Value) CanSet() bool {}// Elem returns the value that the interface v contains
// or that the pointer v points to.
func (v Value) Elem() Value {}// Field returns the i'th field of the struct v.
func (v Value) Field(i int) Value {}// Bool returns v's underlying value.
func (v Value) Bool() bool {}// Bytes returns v's underlying value.
func (v Value) Bytes() []byte {} // Index returns v's i'th element.
func (v Value) Index(i int) Value {}

这里方法我们没有讲解全面,如果想了解源码内容,可以去 reflect 包下的 value.go 下查看。

反射的原则

下图展示了实例,Type与Value三者之间的关系。

API使用总结:

  1. 实例到 Value。使用实例获取对象的 Value,直接使用 reflect.ValueOf() 函数。

  2. 实例到 Type。使用实例获取对象的 Type, 直接使用 reflect.TypeOf()函数。

  3. TypeValue。无法直接从一个 Type 接口变量获取实例的 Value。一般先使用 Type 构建一个新的实例Value

    第一种,使用func New(typ Type) Value创建一个ValueValueType是一个指定的typ的指针类型。

    第二种,使用func Zero(typ Type) Value创建一个Value,注意此时的Value不能寻址,意味着值不可以变。

    第三种,如果知道一个值的底层存放地址,可以使用func NewAt(typ Type, p unsafe.Pointer) Value来恢复Value。此处的typ是实例类型,p是实例的地址。

  4. ValueType。直接可以调用Value结构体上绑定的func (v Value) Type() Type {}方法。

  5. Value 到实例。可以使用通用方法func (v Value) Interface() (i any) {}直接返回实例。

  6. Value指针到值。使用func (v Value) Elem() Value {}返回值。注意:如果类型是接口,则 Elem()返回接口绑定的实例的 Value ,如果类型是指针,则返回指针值的 Value ,否则引起 panic

  7. Type指针到Type值。使用Elem() Type返回子元素的Type

  8. Type值到Type指针。使用func PtrTo(t Type) Type {},返回指向t的指针类型Type

  9. 值的可修改性。可以使用func (v Value) CanSet() bool {}去判断一个值是否可以修改,如果可以修改返回true。接着可以使用func (v Value) Set(x Value)去设置一个值。

总结

本次,带领大家了解反射的基本概念,如何使用等。待熟悉相关的API之后,下一次,我们将展示给您如何在实战中使用reflect包。

参考

  • 《Go语言核心编程》-- 李文塔
  • https://pkg.go.dev/reflect

这篇关于Go反射四讲---第一讲:什么是反射,反射常用的API,反射三原则以及注意事项的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Go 语言中的select语句详解及工作原理

《Go语言中的select语句详解及工作原理》在Go语言中,select语句是用于处理多个通道(channel)操作的一种控制结构,它类似于switch语句,本文给大家介绍Go语言中的select语... 目录Go 语言中的 select 是做什么的基本功能语法工作原理示例示例 1:监听多个通道示例 2:带

Java String字符串的常用使用方法

《JavaString字符串的常用使用方法》String是JDK提供的一个类,是引用类型,并不是基本的数据类型,String用于字符串操作,在之前学习c语言的时候,对于一些字符串,会初始化字符数组表... 目录一、什么是String二、如何定义一个String1. 用双引号定义2. 通过构造函数定义三、St

Go标准库常见错误分析和解决办法

《Go标准库常见错误分析和解决办法》Go语言的标准库为开发者提供了丰富且高效的工具,涵盖了从网络编程到文件操作等各个方面,然而,标准库虽好,使用不当却可能适得其反,正所谓工欲善其事,必先利其器,本文将... 目录1. 使用了错误的time.Duration2. time.After导致的内存泄漏3. jsO

go中空接口的具体使用

《go中空接口的具体使用》空接口是一种特殊的接口类型,它不包含任何方法,本文主要介绍了go中空接口的具体使用,具有一定的参考价值,感兴趣的可以了解一下... 目录接口-空接口1. 什么是空接口?2. 如何使用空接口?第一,第二,第三,3. 空接口几个要注意的坑坑1:坑2:坑3:接口-空接口1. 什么是空接

java中反射(Reflection)机制举例详解

《java中反射(Reflection)机制举例详解》Java中的反射机制是指Java程序在运行期间可以获取到一个对象的全部信息,:本文主要介绍java中反射(Reflection)机制的相关资料... 目录一、什么是反射?二、反射的用途三、获取Class对象四、Class类型的对象使用场景1五、Class

基于Flask框架添加多个AI模型的API并进行交互

《基于Flask框架添加多个AI模型的API并进行交互》:本文主要介绍如何基于Flask框架开发AI模型API管理系统,允许用户添加、删除不同AI模型的API密钥,感兴趣的可以了解下... 目录1. 概述2. 后端代码说明2.1 依赖库导入2.2 应用初始化2.3 API 存储字典2.4 路由函数2.5 应

Linux上设置Ollama服务配置(常用环境变量)

《Linux上设置Ollama服务配置(常用环境变量)》本文主要介绍了Linux上设置Ollama服务配置(常用环境变量),Ollama提供了多种环境变量供配置,如调试模式、模型目录等,下面就来介绍一... 目录在 linux 上设置环境变量配置 OllamPOgxSRJfa手动安装安装特定版本查看日志在

利用Go语言开发文件操作工具轻松处理所有文件

《利用Go语言开发文件操作工具轻松处理所有文件》在后端开发中,文件操作是一个非常常见但又容易出错的场景,本文小编要向大家介绍一个强大的Go语言文件操作工具库,它能帮你轻松处理各种文件操作场景... 目录为什么需要这个工具?核心功能详解1. 文件/目录存javascript在性检查2. 批量创建目录3. 文件

Java常用注解扩展对比举例详解

《Java常用注解扩展对比举例详解》:本文主要介绍Java常用注解扩展对比的相关资料,提供了丰富的代码示例,并总结了最佳实践建议,帮助开发者更好地理解和应用这些注解,需要的朋友可以参考下... 目录一、@Controller 与 @RestController 对比二、使用 @Data 与 不使用 @Dat

Mysql中深分页的五种常用方法整理

《Mysql中深分页的五种常用方法整理》在数据量非常大的情况下,深分页查询则变得很常见,这篇文章为大家整理了5个常用的方法,文中的示例代码讲解详细,大家可以根据自己的需求进行选择... 目录方案一:延迟关联 (Deferred Join)方案二:有序唯一键分页 (Cursor-based Paginatio