当前位置: 首页 > news >正文

昆明安宁网站建设公司淘宝运营一般要学多久

昆明安宁网站建设公司,淘宝运营一般要学多久,app注册拉新平台,公司做网站都需要什么流程本文参考了李文周的博客——Go语言基础之反射。 一、反射初识 1. 什么是反射 在计算机科学中,反射是指计算机程序在运行时(run time)可以访问、检测和修改它本身状态和行为的一种能力。用比喻来说,反射就是程序在运行的时候能够…

本文参考了李文周的博客——Go语言基础之反射。

一、反射初识

1. 什么是反射

在计算机科学中,反射是指计算机程序在运行时(run time)可以访问、检测和修改它本身状态和行为的一种能力。用比喻来说,反射就是程序在运行的时候能够观察并修改自己的行为。

2. 使用场景

  • 编写函数时,并不知道调用者传入的参数类型是什么,可能是没约定好,也可能是传入的类型很多,这些类型不能统一表示
  • 有时候需要根据某些条件调用哪个函数,此时需要对函数和函数的参数进行反射,在运行期间动态的执行函数

3. 缺点

  • 反射相关的代码难以阅读
  • Go语言作为一门静态语言,在编码过程中可以发现一些类型错误,但对于反射代码则是无能为力的
  • 反射对性能影响比较大,比正常代码运行速度慢一到两个数量级

二、reflect包

Go语言中的接口值都具有具体类型和具体类型的值两部分信息,而类型(Type)和值(Value)可以理解为定义在reflect包下的两个结构体reflect.Type和reflect.Value。

在这里插入图片描述

reflect包下有两个最基础的函数——TypeOf和ValueOf,分别用于获取对象的类型信息和值信息,也即reflect.Type和reflect.Value对象。

函数说明
func TypeOf(i any) Type获取对象的类型信息
func ValueOf(i any) Value获取对象的值信息

1. refelect.TypeOf函数

(1) TypeOf函数

通过relect.TypeOf()函数我们可以获取任意对象的类型信息,即reflect.Type对象。

package mainimport ("fmt""reflect"
)func main() {var x float64 = 3.4fmt.Println("type:",reflect.TypeOf(x)) //type: float64
}

(2) Type和Kind

反射中的类型可以分为类型(Type)和种类(Kind)两种。类型(Type)是在变量声明时被赋予的类型(静态类型)或者运行时的类型(动态类型,又称具体类型)。而种类(Kind)是指数据底层的类型,种类是有限可枚举的。reflect包下定义的Kind类型包含以下几种:

type Kind uint
const (Invalid Kind = iota  // 非法类型Bool                 // 布尔型Int                  // 有符号整型Int8                 // 有符号8位整型Int16                // 有符号16位整型Int32                // 有符号32位整型Int64                // 有符号64位整型Uint                 // 无符号整型Uint8                // 无符号8位整型Uint16               // 无符号16位整型Uint32               // 无符号32位整型Uint64               // 无符号64位整型Uintptr              // 指针Float32              // 单精度浮点数Float64              // 双精度浮点数Complex64            // 64位复数类型Complex128           // 128位复数类型Array                // 数组Chan                 // 通道Func                 // 函数Interface            // 接口Map                  // 映射Ptr                  // 指针Slice                // 切片String               // 字符串Struct               // 结构体UnsafePointer        // 底层指针
)

反射对象的Kind可以通过reflect.Value或reflect.Type类型对象的Kind()函数获取。reflect.Value或reflect.Type类型对象有很多同名函数,它们有些功能相同,有些在返回值有些许区别,本文后面对此有更多的例子。

2. reflect.ValueOf函数

(1) ValueOf函数

通过reflect.ValueOf()函数我们可以获取任意对象的值信息,即reflect.Value对象,其中包含了原始值的值信息。

package mainimport ("fmt""reflect"
)func main() {var x float64 = 3.4fmt.Println("value:",reflect.ValueOf(x)) //value: 3.4
}

(2) Value转为原始值

除了可以通过ValueOf()函数将原始值转为reflect.Value对象外,还可以将reflect.Value对象转为原始值,reflect.Value类型提供的获取原始值的方法如下:

方法说明
func (v Value) Interface() interface {}将值以 interface{} 类型返回,可以通过类型断言转换为指定类型
func (v Value) Int() int64将值以 int 类型返回,所有有符号整型均可以此方式返回
func (v Value) Uint() uint64将值以 uint 类型返回,所有无符号整型均可以此方式返回
func (v Value) Float() float64将值以双精度(float64)类型返回,所有浮点数(float32、float64)均可以此方式返回
func (v Value) Bool() bool将值以 bool 类型返回
func (v Value) Bytes() []bytes将值以字节数组 []bytes 类型返回
func (v Value) String() string将值以字符串类型返回

可以发现通过Interface方法获取原始值的方式是最为通用的一种。

3. isNil和isValid方法

由于函数参数传递是值拷贝,所以必须传递变量的地址才能修改变量的值。在反射中可以使用Elem()方法来获取指针对应的值。

(1) isNil方法

func (v Value) IsNil() bool

IsNil()报告v持有的值是否为nil,IsNil()常被用于判断指针是否为空。。v持有的值的分类必须是通道、函数、接口、映射、指针、切片之一;否则IsNil函数会导致panic。

(2) isValid方法

func (v Value) IsValid() bool

IsValid()返回v是否持有一个值,IsValid()常被用于判定返回值是否有效。

(3) 代码示例

func main() {// *int类型空指针var a *intfmt.Println("var a *int IsNil:", reflect.ValueOf(a).IsNil())// nil值fmt.Println("nil IsValid:", reflect.ValueOf(nil).IsValid())// 实例化一个匿名结构体b := struct{}{}// 尝试从结构体中查找"abc"字段fmt.Println("不存在的结构体成员:", reflect.ValueOf(b).FieldByName("abc").IsValid())// 尝试从结构体中查找"abc"方法fmt.Println("不存在的结构体方法:", reflect.ValueOf(b).MethodByName("abc").IsValid())// mapc := map[string]int{}// 尝试从map中查找一个不存在的键fmt.Println("map中不存在的键:", reflect.ValueOf(c).MapIndex(reflect.ValueOf("娜扎")).IsValid())
}

三、结构体反射

如果反射对象的类型是结构体,反射类型reflect.Type和反射值reflect.Value对象提供了对应的方法,来获取结构体的字段信息和方法信息,并且可以通过反射来调用结构体的方法。

1. 与结构体字段、方法相关的方法

下表中所列举的方法,除了Call()函数之外,其他的都是reflect.Value和reflect.Type都有的,只不过reflect.Value的同名函数返回结果的类型都是reflect.Value对象。

方法说明
func (t Type) Field(i int) StructField根据索引,返回索引对应的结构体字段的信息。
func (t Type) NumField() int返回结构体成员字段数量。
func (t Type) FieldByName(name string) (StructField, bool)根据给定字符串返回字符串对应的结构体字段的信息。
func (t Type) FieldByIndex(index []int) StructField多层成员访问时,根据 []int 提供的每个结构体的字段索引,返回字段的信息。
func (t Type) FieldByNameFunc(match func(string) bool) (StructField,bool)根据传入的匹配函数匹配需要的字段。
func (t Type) NumMethod() int返回该类型的方法集中方法的数目
func (t Type) Method(int) Method返回该类型方法集中的第i个方法
func (t Type) MethodByName(string) (Method, bool)根据方法名返回该类型方法集中的方法
func (v Value) Call([]Value) []Value根据传参调用结构体的方法

2. 获取结构体字段信息

(1) StructField类型

在上表中,有些方法的返回值类型为StructField类型,它是用来描述结构体的一个字段信息的,其定义如下:

type StructField struct {Name    string      // 字段的名字PkgPath string      // 非导出字段的包路径,对于导出字段该字段值为""Type      Type      // 字段的类型Tag       StructTag // 字段的标签Offset    uintptr   // 字段在结构体中的字节偏移量Index     []int     // 用于Type.FieldByIndex时的索引Anonymous bool      // 是否匿名字段
}

(2) 获取结构体字段信息步骤

获取结构体字段信息可以分为以下几步:

  • 先获取interface的reflect.Type,然后通过NumField进行遍历
  • 再通过reflect.Type的Field方法根据下标获取其Field
  • 最后通过reflect.Value的Field的Interface()得到对应的value

当然也可以使用FieldByName方法根据字段名获取字段信息。

2. 获取结构体方法信息

(1) Method类型

在上表中,有些方法的返回值类型为Method类型,它是用来描述结构体的一个方法信息的,其定义如下:

type Method struct {Name string    // 方法名称PkgPath string // 非导出方法的包路径,对于导出方法该字段值为""Type  Type     // 方法类型Func  Value    // 带有接收器作为第一个参数的方法Index int      // 用于Type.MethodByIndex时的索引
}

(2) 获取结构体方法信息步骤

获取结构体方法信息可以分为以下几步:

  • 先获取interface的reflect.Type,然后通过NumMethod进行遍历
  • 再通过reflect.Type的Method根据下标获取其Method
  • 使用reflect.Value的Call函数调用结构体的Method

当然也可以使用MethodByName方法根据方法名获取方法信息。

3. 代码示例

package mainimport ("fmt""reflect"
)// 定义一个结构体
type Person struct {Name string `json:"name"`Age  int    `json:"age"`Sex  string `json:"sex"`
}// 注意,如果是指针 *Person 类型,则不算做是 Person 的Method
func (p Person) PrintInfo() {fmt.Printf("name:%s, age:%d, sex:%s\n", p.Name, p.Age, p.Sex)
}func (p Person) Say(msg string) {fmt.Println("hello,", msg)
}func main() {p := Person{Name: "zuzhiang",Age:  27,Sex:  "female",}getFieldAndMethod(p)
}// getFieldAndMethod 通过接口来获取任意参数,并打印结构体的字段和方法信息
func getFieldAndMethod(input interface{}) {getType := reflect.TypeOf(input)           // 获取input的类型fmt.Println("Type.Name: ", getType.Name()) // Personfmt.Println("Type.Kind: ", getType.Kind()) // structgetValue := reflect.ValueOf(input) // 获取input的值fmt.Println("Value:", getValue) // {zuzhiang 27 female}fmt.Println("----------------------------------------\n")// 获取结构体字段// 1. 先获取interface的reflect.Type,然后通过NumField进行遍历// 2. 再通过reflect.Type的Field方法根据下标获取其Field// 3. 最后通过reflect.Value的Field的Interface()得到对应的valuefor i := 0; i < getType.NumField(); i++ {field := getType.Field(i)value := getValue.Field(i).Interface() //获取第i个值fmt.Printf("字段名: %s, 字段类型: %s, 字段索引: %d, json tag: %s, 字段值: %v \n",field.Name, field.Type, field.Index, field.Tag.Get("json"), value)}fmt.Println("----------------------------------------\n")// 定义函数调用时的参数,Call函数的参数类型必须是[]reflect.Valuemsg := "zuzhiang"paramList := make([]reflect.Value, 0)paramList = append(paramList, reflect.ValueOf(msg))args := [][]reflect.Value{nil,paramList,}// 通过反射,操作方法// 1. 先获取interface的reflect.Type,然后通过NumMethod进行遍历// 2. 再通过reflect.Type的Method根据下标获取其Method// 3. 使用reflect.Value的Call函数调用结构体的Methodfor i := 0; i < getType.NumMethod(); i++ {method := getType.Method(i)fmt.Printf("方法名称: %s, 方法类型: %v \n", method.Name, method.Type)// 函数的顺序按函数名字典序正序排列getValue.Method(i).Call(args[i])fmt.Println("")}
}

文章转载自:
http://venation.dztp.cn
http://croydon.dztp.cn
http://unapproved.dztp.cn
http://oxytocic.dztp.cn
http://soakage.dztp.cn
http://bramble.dztp.cn
http://expel.dztp.cn
http://shoyu.dztp.cn
http://computation.dztp.cn
http://schism.dztp.cn
http://reinvest.dztp.cn
http://semibarbaric.dztp.cn
http://antiserum.dztp.cn
http://catechesis.dztp.cn
http://consume.dztp.cn
http://prolative.dztp.cn
http://bubbly.dztp.cn
http://invaginate.dztp.cn
http://porteress.dztp.cn
http://trooper.dztp.cn
http://swoln.dztp.cn
http://gatepost.dztp.cn
http://haematoma.dztp.cn
http://psittacosis.dztp.cn
http://phytoalexin.dztp.cn
http://oakley.dztp.cn
http://presbytery.dztp.cn
http://coccid.dztp.cn
http://activex.dztp.cn
http://rudiment.dztp.cn
http://guerdon.dztp.cn
http://carnificial.dztp.cn
http://socket.dztp.cn
http://hardened.dztp.cn
http://rhachis.dztp.cn
http://blackhead.dztp.cn
http://pescadores.dztp.cn
http://dividual.dztp.cn
http://powerpc.dztp.cn
http://hesychast.dztp.cn
http://resinic.dztp.cn
http://macron.dztp.cn
http://kalanchoe.dztp.cn
http://lethargize.dztp.cn
http://inshore.dztp.cn
http://virginity.dztp.cn
http://pommel.dztp.cn
http://epithelial.dztp.cn
http://varicosity.dztp.cn
http://escapologist.dztp.cn
http://holi.dztp.cn
http://divinatory.dztp.cn
http://subsidize.dztp.cn
http://minitance.dztp.cn
http://farthing.dztp.cn
http://daybreak.dztp.cn
http://adiaphoretic.dztp.cn
http://readership.dztp.cn
http://triathlete.dztp.cn
http://dispositioned.dztp.cn
http://hellcat.dztp.cn
http://deprival.dztp.cn
http://symbology.dztp.cn
http://aplite.dztp.cn
http://simper.dztp.cn
http://prepackage.dztp.cn
http://godling.dztp.cn
http://speciously.dztp.cn
http://tantara.dztp.cn
http://grudge.dztp.cn
http://tympani.dztp.cn
http://declinator.dztp.cn
http://flea.dztp.cn
http://dolt.dztp.cn
http://pycnorneter.dztp.cn
http://loathy.dztp.cn
http://gorgeous.dztp.cn
http://baryonic.dztp.cn
http://seasonableness.dztp.cn
http://voder.dztp.cn
http://spermatocide.dztp.cn
http://schuss.dztp.cn
http://sortation.dztp.cn
http://vocoder.dztp.cn
http://preindustrial.dztp.cn
http://granny.dztp.cn
http://lockable.dztp.cn
http://houseplace.dztp.cn
http://outmaneuver.dztp.cn
http://swipes.dztp.cn
http://monoculture.dztp.cn
http://leakance.dztp.cn
http://explainable.dztp.cn
http://potentiostatic.dztp.cn
http://stoke.dztp.cn
http://univariant.dztp.cn
http://ravishing.dztp.cn
http://sacrilegiously.dztp.cn
http://carrot.dztp.cn
http://bonspiel.dztp.cn
http://www.dt0577.cn/news/124542.html

相关文章:

  • 上海都市建筑设计有限公司济南seo官网优化
  • 沈阳网站建设方案站长网站查询工具
  • 怎么建立免费的网站seo整站优化费用
  • 免费高清无专码区直接看优化游戏的软件
  • html全屏网站网站日常维护有哪些
  • b2b网站怎么做推广天津百度推广公司地址
  • 哪个地区网站建设好山西百度推广开户
  • ecshop手机网站软文范文大全1000字
  • 大连网站制作哪家最好推广赚钱app哪个靠谱
  • wordpress 加载很慢网站seo教材
  • 用淘宝域名做网站什么效果长春视频剪辑培训机构
  • 重庆一次可以备案多少个网站河南网站优化排名
  • 手机网站建设服务商seo推广技术
  • 绍兴酒店网站建设网站优化排名哪家性价比高
  • 宝鸡免费做网站公司合肥今日头条最新消息
  • 深圳网站建设 cmsb2b平台有哪些平台
  • 网站怎么更换服务器常见的网络营销方式有哪些
  • 免费网站模板源码下载详情页页面页面
  • 可以做富集分析的网站站长工具
  • 常熟有没有做阿里巴巴网站站长工具介绍
  • wordpress开源博客系统最新版seo怎么优化武汉厂商
  • 重庆专业网站建设公司哪家好凡科建站收费价目表
  • 登录页面设计代码seo的方式包括
  • 网站备案用户名忘了怎么办百度客户端登录
  • 大连网站设计九即问仟亿科技百度推广下载
  • wordpress增加中英文切换seo试用软件
  • 张家港外贸网站设计快手seo关键词优化
  • 张掖哪家公司做网站营销推广方案设计
  • 泉州学校网站建设湖北网络推广公司
  • 网站开发开题报告范文2019seo人才