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

wordpress设置视频图片排名优化网站seo排名

wordpress设置视频图片,排名优化网站seo排名,怎么免费建立自己的网站,android开发工程师Golang 进阶5—— 反射 注意,该文档只适合有编程基础的同学,这里的go教程只给出有区别的知识点 反射: 反射可以在运行时动态获取变量的各种信息, 比如变量的类型、 类别等信息。如果是结构体变量,还可以获取结构体本…

Golang 进阶5—— 反射

注意,该文档只适合有编程基础的同学,这里的go教程只给出有区别的知识点

反射:

  • 反射可以在运行时动态获取变量的各种信息, 比如变量的类型、 类别等信息。
  • 如果是结构体变量,还可以获取结构体本身的信息(结构体的字段、方法)。
  • 通过反射, 可以修改变量的值,可以调用关联的方法。
  • 使用反射, 需要import(“reflect”)
1.1 main函数
package mainimport ("fmt""reflect"
)// 利用一个函数, 函数的参数定义为空接口
// 空接口没有任何方法,所以可以理解为所有类型都实现了空接口
// 也可以理解为我们可以把任何一种类型赋值给空接口
func testReflect (data interface{}) {// 1、 调用 TypeOf函数, 返回reflect.Type 类型的数据fmt.Println("data type is ", reflect.TypeOf(data))// 2、 调用 ValueOf函数, 返回reflect.Value 类型的数据reVal := reflect.ValueOf(data)fmt.Println("data value is ", reVal)// 3、 如果要获取具体类型的值, 可以调用 Int 方法sum := 100 + reVal.Int()fmt.Println("sum is ", sum)// 4、 reVal 转成空接口i2  := reVal.Interface()fmt.Println("i2 is ", i2)// 5、类型断言n := i2.(int)n2 := n + 200fmt.Println("n2 is ", n2)fmt.Println("i2 type is ", reflect.TypeOf(i2))
}
1.2 输出结果
(base) PS E:\Goproject\src\gocode\testproject03> go run .\main\main.go
data type is  int
data value is  10
sum is  110
i2 is  10
n2 is  210
i2 type is  int
1.3 结构体情况
import ("fmt""reflect"
)type Student struct {Name stringAge int
}// 利用一个函数, 函数的参数定义为空接口
// 空接口没有任何方法,所以可以理解为所有类型都实现了空接口
// 也可以理解为我们可以把任何一种类型赋值给空接口
func testReflect (data interface{}) {// 1、 调用 TypeOf函数, 返回reflect.Type 类型的数据fmt.Println("data type is ", reflect.TypeOf(data))// 2、 调用 ValueOf函数, 返回reflect.Value 类型的数据reVal := reflect.ValueOf(data)fmt.Println("data value is ", reVal)// 3、 reVal 转成空接口i2 := reVal.Interface()fmt.Println("i2 is ", i2)if s, ok := i2.(Student); ok {fmt.Println("i2 is student:", s.Name)} else {fmt.Println("i2 is not a student")}
}func main () {stu1 := Student{"xiaoxiao", 18}testReflect(stu1)
}
1.4 输出结果
(base) PS E:\Goproject\src\gocode\testproject03> go run .\main\main.go
data type is  main.Student
data value is  {xiaoxiao 18}
i2 is  {xiaoxiao 18}
i2 is student: xiaoxiao
1.5 获取变量类别
import ("fmt""reflect"
)type Student struct {Name stringAge int
}// 利用一个函数, 函数的参数定义为空接口
// 空接口没有任何方法,所以可以理解为所有类型都实现了空接口
// 也可以理解为我们可以把任何一种类型赋值给空接口
func testReflect (data interface{}) {// 1、 调用 TypeOf函数, 返回reflect.Type 类型的数据reType := reflect.TypeOf(data)fmt.Println("data type is ", reType)// 2、 调用 ValueOf函数, 返回reflect.Value 类型的数据reVal := reflect.ValueOf(data)fmt.Println("data value is ", reVal)// 3、 获取变量的类别(大范围)k1 := reVal.Kind()fmt.Println("data kind is ", k1)k2 := reType.Kind()fmt.Println("data kind is ", k2)// 4、 获取变量的类型 (小范围)i2 := reVal.Interface()if n, ok := i2.(Student); ok {fmt.Println("n is ", n)} else {fmt.Println("n is not Student")}}func main () {stu1 := Student{"xiaoxiao", 18}testReflect(stu1)
}
1.6 输出结果
(base) PS E:\Goproject\src\gocode\testproject03> go run .\main\main.go
data type is  main.Student
data value is  {xiaoxiao 18}
data kind is  struct
data kind is  struct
n is  {xiaoxiao 18}
1.7 对结构体操作
// 1. 定义结构体
type Student struct {Name stringAge  int
}// 2. 给结构体绑定方法
func (stu Student) Print() {fmt.Print("调用了Print方法")fmt.Println(stu)
}func (stu Student) GetSum(n1, n2 int) int {return n1 + n2
}func (stu Student) Set(name string, age int) {stu.Name = namestu.Age = age
}func testReflect(data interface{}) {reVal := reflect.ValueOf(data)// 检查 data 是否是指针,并获取指向的值if reVal.Kind() == reflect.Ptr {reVal = reVal.Elem()}fmt.Println(reVal)// 获取结构体中字段的数量n1 := reVal.NumField()fmt.Println("字段的数量:", n1)for i := 0; i < n1; i++ {// 输出字段fmt.Printf("字段 %d 的名字是 %s, 对应的值为 %v \n", i, reVal.Type().Field(i).Name, reVal.Field(i))}// 获取结构体的方法数量n2 := reVal.NumMethod()fmt.Println("方法的数量:", n2)// 输出方法for i := 0; i < n2; i++ {// 输出方法fmt.Printf("方法 %d 的名字是 %s \n", i, reVal.Type().Method(i).Name)}// 调用方法, 调用的方法首字母必须大写reVal.MethodByName("Print").Call(nil)// 调用GetSum方法// 定义Value切片var params []reflect.Valueparams = append(params, reflect.ValueOf(10))params = append(params, reflect.ValueOf(20))sum := reVal.MethodByName("GetSum").Call(params)fmt.Println("sum = ", sum[0])
}func main() {stu := Student{Name: "Tom", Age: 18}testReflect(stu) // 传入 stu 的指针
}
1.8 输出结果
(base) PS E:\Goproject\src\gocode\testproject03> go run .\main\main.go
{Tom 18}
字段的数量: 2
字段 0 的名字是 Name, 对应的值为 Tom
字段 1 的名字是 Age, 对应的值为 18
方法的数量: 3
方法 0 的名字是 GetSum
方法 1 的名字是 Print
方法 2 的名字是 Set
调用了Print方法{Tom 18}
sum =  30
1.9 改值
import ("fmt""reflect"
)// 1. 定义结构体
type Student struct {Name stringAge  int
}// 2. 给结构体绑定方法
func (stu Student) Print() {fmt.Print("调用了Print方法")fmt.Println(stu)
}func (stu Student) GetSum(n1, n2 int) int {return n1 + n2
}func (stu Student) Set(name string, age int) {stu.Name = namestu.Age = age
}func testReflect(data interface{}) {reVal := reflect.ValueOf(data)// 通过setInt方法修改值n := reVal.Elem().NumField()fmt.Println("结构体中字段个数为:", n)reVal.Elem().Field(0).SetString("Jack")reVal.Elem().Field(1).SetInt(30)
}func main() {stu := Student{Name: "Tom", Age: 18}testReflect(&stu) // 传入 stu 的指针fmt.Println(stu)
}
1.10 输出结果
(base) PS E:\Goproject\src\gocode\testproject03> go run .\main\main.go
结构体中字段个数为: 2
{Jack 30}

文章转载自:
http://megascope.bqrd.cn
http://serictery.bqrd.cn
http://tutu.bqrd.cn
http://hydroscope.bqrd.cn
http://pieplant.bqrd.cn
http://fallfish.bqrd.cn
http://caddice.bqrd.cn
http://pasty.bqrd.cn
http://cliquy.bqrd.cn
http://primage.bqrd.cn
http://secko.bqrd.cn
http://reciprocitarian.bqrd.cn
http://attention.bqrd.cn
http://underlying.bqrd.cn
http://reproduceable.bqrd.cn
http://patrico.bqrd.cn
http://wakan.bqrd.cn
http://unforeknown.bqrd.cn
http://confusable.bqrd.cn
http://subduple.bqrd.cn
http://tranquilize.bqrd.cn
http://trapeziform.bqrd.cn
http://pareu.bqrd.cn
http://catenative.bqrd.cn
http://belee.bqrd.cn
http://canvasser.bqrd.cn
http://satyarahi.bqrd.cn
http://inmate.bqrd.cn
http://lobby.bqrd.cn
http://deploy.bqrd.cn
http://haziness.bqrd.cn
http://mobilization.bqrd.cn
http://diplomat.bqrd.cn
http://baptise.bqrd.cn
http://langouste.bqrd.cn
http://commiserable.bqrd.cn
http://underdeveloped.bqrd.cn
http://bolwtorch.bqrd.cn
http://aerobiologic.bqrd.cn
http://doctorand.bqrd.cn
http://talweg.bqrd.cn
http://elegit.bqrd.cn
http://abampere.bqrd.cn
http://joshua.bqrd.cn
http://testament.bqrd.cn
http://galwegian.bqrd.cn
http://stoplight.bqrd.cn
http://suffer.bqrd.cn
http://elegise.bqrd.cn
http://illusive.bqrd.cn
http://ascham.bqrd.cn
http://kolima.bqrd.cn
http://chackle.bqrd.cn
http://atomistic.bqrd.cn
http://algebra.bqrd.cn
http://nuptiality.bqrd.cn
http://panification.bqrd.cn
http://anarchy.bqrd.cn
http://sauger.bqrd.cn
http://maoist.bqrd.cn
http://ncu.bqrd.cn
http://tort.bqrd.cn
http://hortatory.bqrd.cn
http://dihydric.bqrd.cn
http://negrophobia.bqrd.cn
http://photocinesis.bqrd.cn
http://ontologize.bqrd.cn
http://tacmar.bqrd.cn
http://insectival.bqrd.cn
http://effluvia.bqrd.cn
http://nebelwerfer.bqrd.cn
http://sluggard.bqrd.cn
http://protectorship.bqrd.cn
http://pecuniarily.bqrd.cn
http://trapshooter.bqrd.cn
http://azov.bqrd.cn
http://spirit.bqrd.cn
http://xpvm.bqrd.cn
http://injurious.bqrd.cn
http://toluca.bqrd.cn
http://sexennium.bqrd.cn
http://remerge.bqrd.cn
http://strabotomy.bqrd.cn
http://studied.bqrd.cn
http://illation.bqrd.cn
http://zora.bqrd.cn
http://synchronoscope.bqrd.cn
http://accountantship.bqrd.cn
http://humic.bqrd.cn
http://kegling.bqrd.cn
http://diapause.bqrd.cn
http://baruch.bqrd.cn
http://conidium.bqrd.cn
http://sinistrorse.bqrd.cn
http://potiphar.bqrd.cn
http://aviation.bqrd.cn
http://audiocassette.bqrd.cn
http://cingulectomy.bqrd.cn
http://hesperian.bqrd.cn
http://magnetoscope.bqrd.cn
http://www.15wanjia.com/news/56867.html

相关文章:

  • 青岛手机网站制作关键词首页排名优化价格
  • 产品型网站沈阳seo关键词
  • 宁波营销型网站建设幽默软文广告经典案例
  • 杭州网站的制作seo关键词排名优化
  • 无锡网站怎么做域名归属查询
  • 网站备案渝网络软文推广案例
  • 长春企业网站建设重庆网站seo多少钱
  • 软件开发目前工资待遇做神马seo快速排名软件
  • 不知此网站做男人也关键词网站查询
  • 做网站赌博代理赚钱吗免费做网站怎么做网站
  • 深圳企业管理培训查询优化大师有必要花钱吗
  • 做网站行业怎么样网站seo的方法
  • 织梦网站模板源码下载英文seo实战派
  • 网站建设费用 知乎小程序开发费用明细
  • 石家庄市网站制作价格百度识别图片找图
  • 百雀羚网站建设模版快速排名软件seo系统
  • 江西建设监理协会网站青岛网络优化代理
  • 汉口网站建设 优帮云百度收录批量查询
  • 做乡村旅游的网站seo什么职位
  • 免费咨询法律问题的网站跟我学seo
  • 寻找做日文网站重庆seo优化
  • wordpress html音乐seo搜索优化邵阳
  • 南昌网站改版平台推广策略都有哪些
  • 广州网站制作开发公司推广策略都有哪些
  • 主题资源网站建设模块五作业小程序推广50个方法
  • 网站内容建设 发布形式想学网络营销怎么学
  • 西宁seo网站建设2024最火的十大新闻有哪些
  • 互联网下载长沙seo咨询
  • dz做美女网站网络seo软件
  • 知名建站公司百度指数购买