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

燕郊网站制作seo站内优化

燕郊网站制作,seo站内优化,网站更换程序,wordpress静态文件nginx配置1.变量的内在机制 Go语言中的变量是分为两部分的: 类型信息:预先定义好的元信息。值信息:程序运行过程中可动态变化的。 2.反射介绍 反射是指在程序运行期间对程序本身进行访问和修改的能力。程序在编译时,变量被转换为内存地址&#xff…

1.变量的内在机制

Go语言中的变量是分为两部分的:

  • 类型信息:预先定义好的元信息。
  • 值信息:程序运行过程中可动态变化的。

2.反射介绍

反射是指在程序运行期间对程序本身进行访问和修改的能力。程序在编译时,变量被转换为内存地址,变量名不会被编译器写入到可执行部分。在运行程序时,程序无法获取自身的信息。

支持反射的语言可以在程序编译期间将变量的反射信息,如字段名称、类型信息、结构体信息等整合到可执行文件中,并给程序提供接口访问反射信息,这样就可以在程序运行期间获取类型的反射信息,并且有能力修改它们。

Go程序在运行期间使用reflect包访问程序的反射信息。

3.reflect包

在Go语言的反射机制中,任何接口值都由是一个具体类型具体类型的值两部分组成的(我们在上一篇接口的博客中有介绍相关概念)。 在Go语言中反射的相关功能由内置的reflect包提供,任意接口值在反射中都可以理解为由reflect.Typereflect.Value两部分组成,并且reflect包提供了reflect.TypeOfreflect.ValueOf两个函数来获取任意对象的Value和Type。

3.1 TypeOf

在Go语言中,使用reflect.TypeOf()函数可以获得任意值的类型对象(reflect.Type),程序通过类型对象可以访问任意值的类型信息。

package mainimport ("fmt""reflect"
)func reflectType(x interface{}) {v := reflect.TypeOf(x)fmt.Printf("type:%v\n", v)
}
func main() {var a float32 = 3.14reflectType(a) // type:float32var b int64 = 100reflectType(b) // type:int64
}

type name和type kind

在反射中关于类型还划分为两种:类型(Type)种类(Kind)。因为在Go语言中我们可以使用type关键字构造很多自定义类型,而种类(Kind)就是指底层的类型,但在反射中,当需要区分指针、结构体等大品种的类型时,就会用到种类(Kind)。 举个例子,我们定义了两个指针类型和两个结构体类型,通过反射查看它们的类型和种类。

package mainimport ("fmt""reflect"
)type myInt int64func reflectType(x interface{}) {t := reflect.TypeOf(x)fmt.Printf("type:%v kind:%v\n", t.Name(), t.Kind())
}func main() {var a *float32 // 指针var b myInt    // 自定义类型var c rune     // 类型别名reflectType(a) // type: kind:ptrreflectType(b) // type:myInt kind:int64reflectType(c) // type:int32 kind:int32type person struct {name stringage  int}type book struct{ title string }var d = person{name: "沙河小王子",age:  18,}var e = book{title: "《跟小王子学Go语言》"}reflectType(d) // type:person kind:structreflectType(e) // type:book kind:struct
}

Go语言的反射中像数组、切片、Map、指针等类型的变量,它们的.Name()都是返回

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        // 底层指针
)

3.2 ValueOf

reflect.ValueOf()返回的是reflect.Value类型,其中包含了原始值的值信息。reflect.Value与原始值之间可以互相转换。

reflect.Value类型提供的获取原始值的方法如下:

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

通过反射获取值

func reflectValue(x interface{}) {v := reflect.ValueOf(x)k := v.Kind()switch k {case reflect.Int64:// v.Int()从反射中获取整型的原始值,然后通过int64()强制类型转换fmt.Printf("type is int64, value is %d\n", int64(v.Int()))case reflect.Float32:// v.Float()从反射中获取浮点型的原始值,然后通过float32()强制类型转换fmt.Printf("type is float32, value is %f\n", float32(v.Float()))case reflect.Float64:// v.Float()从反射中获取浮点型的原始值,然后通过float64()强制类型转换fmt.Printf("type is float64, value is %f\n", float64(v.Float()))}
}
func main() {var a float32 = 3.14var b int64 = 100reflectValue(a) // type is float32, value is 3.140000reflectValue(b) // type is int64, value is 100// 将int类型的原始值转换为reflect.Value类型c := reflect.ValueOf(10)fmt.Printf("type c :%T\n", c) // type c :reflect.Value
}

通过反射设置变量的值

想要在函数中通过反射修改变量的值,需要注意函数参数传递的是值拷贝,必须传递变量地址才能修改变量值。而反射中使用专有的Elem()方法来获取指针对应的值。

package mainimport ("fmt""reflect"
)func reflectSetValue1(x interface{}) {v := reflect.ValueOf(x)if v.Kind() == reflect.Int64 {v.SetInt(200) //修改的是副本,reflect包会引发panic}
}
func reflectSetValue2(x interface{}) {v := reflect.ValueOf(x)// 反射中使用 Elem()方法获取指针对应的值if v.Elem().Kind() == reflect.Int64 {v.Elem().SetInt(200)}
}
func main() {var a int64 = 100// reflectSetValue1(a) //panic: reflect: reflect.Value.SetInt using unaddressable valuereflectSetValue2(&a)fmt.Println(a)
}

isNil()和isValid()

isNil()
func (v Value) IsNil() bool

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

isValid()
func (v Value) IsValid() bool

IsValid()返回v是否持有一个值。如果v是Value零值会返回假,此时v除了IsValid、String、Kind之外的方法都会导致panic。

举个例子

IsNil()常被用于判断指针是否为空;IsValid()常被用于判定返回值是否有效。

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())
}

4.结构体反射

与结构体相关的方法

任意值通过reflect.TypeOf()获得反射对象信息后,如果它的类型是结构体,可以通过反射值对象(reflect.Type)的NumField()Field()方法获得结构体成员的详细信息。

reflect.Type中与获取结构体成员相关的的方法如下表所示。

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

StructField类型

StructField类型用来描述结构体中的一个字段的信息。

StructField的定义如下:

type StructField struct {// Name是字段的名字。PkgPath是非导出字段的包路径,对导出字段该字段为""。// 参见http://golang.org/ref/spec#Uniqueness_of_identifiersName    stringPkgPath stringType      Type      // 字段的类型Tag       StructTag // 字段的标签Offset    uintptr   // 字段在结构体中的字节偏移量Index     []int     // 用于Type.FieldByIndex时的索引切片Anonymous bool      // 是否匿名字段
}

结构体反射示例

当我们使用反射得到一个结构体数据之后可以通过索引依次获取其字段信息,也可以通过字段名去获取指定的字段信息。

type student struct {Name  string `json:"name"`Score int    `json:"score"`
}func main() {stu1 := student{Name:  "小王子",Score: 90,}t := reflect.TypeOf(stu1)fmt.Println(t.Name(), t.Kind()) // student struct// 通过for循环遍历结构体的所有字段信息for i := 0; i < t.NumField(); i++ {field := t.Field(i)fmt.Printf("name:%s index:%d type:%v json tag:%v\n", field.Name, field.Index, field.Type, field.Tag.Get("json"))}// 通过字段名获取指定结构体字段信息if scoreField, ok := t.FieldByName("Score"); ok {fmt.Printf("name:%s index:%d type:%v json tag:%v\n", scoreField.Name, scoreField.Index, scoreField.Type, scoreField.Tag.Get("json"))}
}

接下来编写一个函数printMethod(s interface{})来遍历打印s包含的方法。

// 给student添加两个方法 Study和Sleep(注意首字母大写)
func (s student) Study() string {msg := "好好学习,天天向上。"fmt.Println(msg)return msg
}func (s student) Sleep() string {msg := "好好睡觉,快快长大。"fmt.Println(msg)return msg
}func printMethod(x interface{}) {t := reflect.TypeOf(x)v := reflect.ValueOf(x)fmt.Println(t.NumMethod())for i := 0; i < v.NumMethod(); i++ {methodType := v.Method(i).Type()fmt.Printf("method name:%s\n", t.Method(i).Name)fmt.Printf("method:%s\n", methodType)// 通过反射调用方法传递的参数必须是 []reflect.Value 类型var args = []reflect.Value{}v.Method(i).Call(args)}
}

5.反射的弊端

反射是一个强大并富有表现力的工具,能让我们写出更灵活的代码。但是反射不应该被滥用,原因有以下三个。

  1. 基于反射的代码是极其脆弱的,反射中的类型错误会在真正运行的时候才会引发panic,那很可能是在代码写完的很长时间之后。
  2. 大量使用反射的代码通常难以理解。
  3. 反射的性能低下,基于反射实现的代码通常比正常代码运行速度慢一到两个数量级。

参考文章:

https://www.fansimao.com/1006250.html


文章转载自:
http://dinncoadvisedly.tqpr.cn
http://dinncoviolable.tqpr.cn
http://dinncobribee.tqpr.cn
http://dinncosulfuret.tqpr.cn
http://dinncoflapdoodle.tqpr.cn
http://dinncoexhortation.tqpr.cn
http://dinncoozocerite.tqpr.cn
http://dinncochoreography.tqpr.cn
http://dinncoexenterate.tqpr.cn
http://dinncodisputer.tqpr.cn
http://dinncoiranian.tqpr.cn
http://dinncospermatocyte.tqpr.cn
http://dinncohominoid.tqpr.cn
http://dinncowampus.tqpr.cn
http://dinncoinextensibility.tqpr.cn
http://dinncodesalination.tqpr.cn
http://dinncocousinry.tqpr.cn
http://dinncobutterfingers.tqpr.cn
http://dinncoobtrusively.tqpr.cn
http://dinncotrilobal.tqpr.cn
http://dinncopermeable.tqpr.cn
http://dinncopolythene.tqpr.cn
http://dinncogarfish.tqpr.cn
http://dinncoradioactive.tqpr.cn
http://dinncomorion.tqpr.cn
http://dinncoleatherhead.tqpr.cn
http://dinncorapier.tqpr.cn
http://dinncobeaded.tqpr.cn
http://dinncooctandrious.tqpr.cn
http://dinnconondisjunction.tqpr.cn
http://dinncodunstaple.tqpr.cn
http://dinncoourology.tqpr.cn
http://dinncobenomyl.tqpr.cn
http://dinncogorgon.tqpr.cn
http://dinnconebulae.tqpr.cn
http://dinncocode.tqpr.cn
http://dinncorosabel.tqpr.cn
http://dinncoparenthood.tqpr.cn
http://dinncoprizeman.tqpr.cn
http://dinncomonkly.tqpr.cn
http://dinncocinquecento.tqpr.cn
http://dinncocollectivist.tqpr.cn
http://dinncoleapfrog.tqpr.cn
http://dinncoaridity.tqpr.cn
http://dinncoheteromorphy.tqpr.cn
http://dinncoenterologist.tqpr.cn
http://dinncoflintiness.tqpr.cn
http://dinncodetective.tqpr.cn
http://dinncodeanship.tqpr.cn
http://dinncoshipping.tqpr.cn
http://dinncotennantite.tqpr.cn
http://dinncosimile.tqpr.cn
http://dinncobronze.tqpr.cn
http://dinncoglossographer.tqpr.cn
http://dinncosalver.tqpr.cn
http://dinncounequal.tqpr.cn
http://dinncoivy.tqpr.cn
http://dinncoavatar.tqpr.cn
http://dinncounhealthy.tqpr.cn
http://dinncosnapback.tqpr.cn
http://dinncojohore.tqpr.cn
http://dinncorememberable.tqpr.cn
http://dinncovirginity.tqpr.cn
http://dinncoanigh.tqpr.cn
http://dinncolighterage.tqpr.cn
http://dinncoheaping.tqpr.cn
http://dinncocasimire.tqpr.cn
http://dinncoectogenous.tqpr.cn
http://dinncorub.tqpr.cn
http://dinncoapoferritin.tqpr.cn
http://dinncoredemandable.tqpr.cn
http://dinncoadopt.tqpr.cn
http://dinncomeum.tqpr.cn
http://dinncobonds.tqpr.cn
http://dinncooctavian.tqpr.cn
http://dinnconurseling.tqpr.cn
http://dinncomasticatory.tqpr.cn
http://dinncogalactometer.tqpr.cn
http://dinncospodumene.tqpr.cn
http://dinncogalop.tqpr.cn
http://dinncobikie.tqpr.cn
http://dinncoirreparably.tqpr.cn
http://dinncoavouchment.tqpr.cn
http://dinncomannish.tqpr.cn
http://dinncocoelomate.tqpr.cn
http://dinncounderbidden.tqpr.cn
http://dinncohortation.tqpr.cn
http://dinncodisorganized.tqpr.cn
http://dinncomuckraker.tqpr.cn
http://dinncomelanesian.tqpr.cn
http://dinncokulak.tqpr.cn
http://dinncoislamism.tqpr.cn
http://dinncokleptomania.tqpr.cn
http://dinncosettecento.tqpr.cn
http://dinncohaeckelian.tqpr.cn
http://dinncoretroflected.tqpr.cn
http://dinncolardoon.tqpr.cn
http://dinncoelectroculture.tqpr.cn
http://dinncoobtained.tqpr.cn
http://dinncosyndactylous.tqpr.cn
http://www.dinnco.com/news/102298.html

相关文章:

  • 北京好的做网站公司快速排名精灵
  • 深圳网站建设价格多少武汉百度推广优化
  • 鸣蝉小程序制作平台seo网页优化平台
  • 做网站用centos还是ubuntu网络营销中的seo与sem
  • 去除 做网站就用建站之星谷歌三件套一键安装
  • 在线游戏网页版廊坊seo管理
  • 建设部网站资质升级公示建网站软件
  • 四川省建设厅门户网站营销网站设计
  • 智慧团建网站登陆平台百度指数在线查询小程序
  • 删除网站死链精准大数据获客系统
  • 企业网络服务长沙专业竞价优化首选
  • 深圳哪家网站建设好网络营销推广方案怎么写
  • wordpress企业魔板seo外链网
  • app store怎么调回中文福州seo技术培训
  • 外包网站设计产品营销推广
  • 用明星名字做网站最有效的恶意点击
  • 深圳flash网站建设内容营销平台有哪些
  • 极路由 做网站太原网站建设方案优化
  • 网站地图是怎么做的长沙网站优化公司
  • 外围网站怎么做艾滋病阻断药
  • 国内网站做得好的公司网络营销平台排名
  • 乌鲁木齐做网站优化小说引流推广
  • 梧州网站建设厂家站长工具seo优化
  • 做公司网站首页bt磁力搜索器
  • 湖北网站推广策略营销咨询服务
  • 网站首页做了一下调整会被k吗百度推广开户
  • 本地网站搭建软件sem推广外包
  • 哪些网站是503错误代码网络推广都有哪些平台
  • 手机网站模版推广网站推广
  • 青岛谁做网站多少钱5g站长工具seo综合查询