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

苍南住房和城乡规划建设局网站优化网站排名方法

苍南住房和城乡规划建设局网站,优化网站排名方法,建筑公司网站被投诉违反广告法,企业网站建设模板多少钱学习 golang ,对于 interface{} 接口类型,我们一定绕不过,咱们一起来看看 使用 interface{} 的时候,都有哪些注意事项吧 interface {} 可以用于模拟多态 xdm 咱们写一个简单的例子,就举动物的例子 写一个 Animal 的…

学习 golang ,对于 interface{} 接口类型,我们一定绕不过,咱们一起来看看 使用 interface{} 的时候,都有哪些注意事项吧

interface {} 可以用于模拟多态

xdm 咱们写一个简单的例子,就举动物的例子

写一个 Animal 的接口,类似于 java 里面的抽象类 ,Animal 的接口 中有 2 个方案待实现

写一个 Cat 来继承 Animal实现 Eat 方法和 Drink 方法

  • 动物都有吃和喝的行为,小猫吃的行为是吃鱼,小猫的喝的行为是喝可乐
  • 最后在主函数中,使用父类的指针,来指向子类的实例化的一个子类地址
type Animal interface {Eat(string) stringDrink(string) string
}type Cat struct{}func (c *Cat) Eat(food string) string {if food != "fish" {return "i dislike"} else {return "i like"}}
func (c *Cat) Drink(drink string) string {if drink == "coke" {return "i love"}else{return "abandon"}
}
func main(){var a Animal = &Cat{}fmt.Println(a.Eat("fish"))fmt.Println(a.Drink("water"))
}

看到上述代码,会不会有这样的疑问,命名是 &Cat{} 是取地址的,为什么 var a Animal 不写成指针呢?

这里需要注意,Animal 本身是 接口类型,自身就是一个指针

运行上述代码查看效果

# go run main.go
i like
abandon

没有毛病,小猫眯爱吃鱼,不爱喝水

interface{} 需要注意空和非空的情况

什么叫做空的 interface{} , 什么又叫做非空的 interface{} 呢?

咱们还是用上面的例子, 添加一个 testInterface 函数,来实践一下

func testInterface() Animal {var c *Catreturn c
}func main() {test := testInterface()if test == nil {fmt.Println("test is nil")} else {fmt.Println("test is not nil")}
}

可以猜猜看,上面这个小案例会输出什么结果

  • 理论上来看,testInterface 函数中我们只是创建了一个 Cat 指针,并没有赋值,因此默认是一个零值,因此会是一个 nil,那么 return 的时候,应该也是 return nil 才对吧,因此按照代码的逻辑来说应该是输出 test is nil

执行上述代码后,查看结果

# go run main.go
test is not nil

看到上面的结果,是不是觉得很奇怪,和自己的预期不一致

没关系,之前的文章我们说到过,觉得一个技术点奇怪,不是我们所期望的效果,原因是我们对其原理不够了解,不够熟悉

现在先来回答一下上面的问题

空接口:意思是没有方法的接口,interface{} 源码中表示为 eface 结构体

非空接口:表示有包含方法的接口 , interface{} 源码中表示为 iface 结构体

暂时先来直接介绍源码中的结构体

iface 结构体 , 非空

type iface struct {tab  *itabdata unsafe.Pointer
}type itab struct {inter  *interfacetype_type  *_typelink   *itabhash   uint32 // copy of _type.hash. Used for type switches.bad    bool   // type does not implement interfaceinhash bool   // has this itab been added to hash?unused [2]bytefun    [1]uintptr // variable sized
}
  • tab

    指的是具体的类型信息,是一个 itab 结构,结构中成员如上,这里面包含的都是借口的关键信息,例如 hash 值 ,函数指针,等等,后续详细剖析 interface{} 原理的时候再统一说

  • data

具体的数据信息

eface 结构体

type eface struct {_type *_typedata  unsafe.Pointer
}
type _type struct {size       uintptr  // 表示的是 类型的大小ptrdata    uintptr  // 值的是前缀指针的内存大小hash       uint32   // 计算数据的 hash 值tflag      tflagalign      uint8    //  进行内存对齐的fieldalign uint8 kind       uint8 alg        *typeAlg gcdata    *bytestr       nameOffptrToThis typeOff
}
  • _type

类型信息,和上面的 非空接口类似 , 这个_type 类型决定下面的 data 字段如何去解释数据

  • data

具体的数据信息

看到这里,细心的 xdm 是不是就可以看出来,我们上面写的 Animal 接口,其实是一个非空接口,因为里面有包含方法,所以他的底层是一个 iface 结构体 ,非空接口

那么初始化的一个空指针 c ,实际上是 iface 结构体里面的 data 字段为空而已,数据为空而已,但是 iface 这个结构体自己不是空的,所以上述代码走的逻辑是 test is not nil

这里顺带说一下,golang 中,还有哪些数据结构是和 nil 比较是否为零值,这个点我们也可以看看源码

// nil is a predeclared identifier representing the zero value for a
// pointer, channel, func, interface, map, or slice type.
var nil Type // Type must be a pointer, channel, func, interface, map, or slice type

源码中有说到,可以对 指针,通道,函数,接口,map,切片类型使用 nil

好了,本次就到这里,知识点要用起来才有价值

欢迎点赞,关注,收藏

朋友们,你的支持和鼓励,是我坚持分享,提高质量的动力

好了,本次就到这里

技术是开放的,我们的心态,更应是开放的。拥抱变化,向阳而生,努力向前行。

我是阿兵云原生,欢迎点赞关注收藏,下次见~


文章转载自:
http://daydream.rtkz.cn
http://aspartate.rtkz.cn
http://unsureness.rtkz.cn
http://insinuative.rtkz.cn
http://coax.rtkz.cn
http://palladize.rtkz.cn
http://diarrhea.rtkz.cn
http://phot.rtkz.cn
http://anole.rtkz.cn
http://transfection.rtkz.cn
http://diminishable.rtkz.cn
http://dooryard.rtkz.cn
http://glasses.rtkz.cn
http://threonine.rtkz.cn
http://pleasant.rtkz.cn
http://faff.rtkz.cn
http://gibbon.rtkz.cn
http://conflicting.rtkz.cn
http://voyeurism.rtkz.cn
http://heinously.rtkz.cn
http://subtracter.rtkz.cn
http://volgograd.rtkz.cn
http://navajo.rtkz.cn
http://sequestra.rtkz.cn
http://huzza.rtkz.cn
http://deconsecrate.rtkz.cn
http://greaves.rtkz.cn
http://amortize.rtkz.cn
http://rale.rtkz.cn
http://jujube.rtkz.cn
http://pravity.rtkz.cn
http://smuttiness.rtkz.cn
http://pruriency.rtkz.cn
http://liquor.rtkz.cn
http://bistort.rtkz.cn
http://frere.rtkz.cn
http://univariate.rtkz.cn
http://semiotic.rtkz.cn
http://molecular.rtkz.cn
http://unitar.rtkz.cn
http://fug.rtkz.cn
http://whereas.rtkz.cn
http://overwhelmingly.rtkz.cn
http://reaction.rtkz.cn
http://ceresine.rtkz.cn
http://lycanthropy.rtkz.cn
http://tautologist.rtkz.cn
http://electrology.rtkz.cn
http://anchorite.rtkz.cn
http://unconspicuous.rtkz.cn
http://antiulcer.rtkz.cn
http://admiringly.rtkz.cn
http://involucel.rtkz.cn
http://addressograph.rtkz.cn
http://blin.rtkz.cn
http://bundu.rtkz.cn
http://neuss.rtkz.cn
http://inmate.rtkz.cn
http://balkh.rtkz.cn
http://polybasic.rtkz.cn
http://shiah.rtkz.cn
http://tuppenny.rtkz.cn
http://tetradynamous.rtkz.cn
http://simoleon.rtkz.cn
http://petroliferous.rtkz.cn
http://gallant.rtkz.cn
http://prettify.rtkz.cn
http://rosulate.rtkz.cn
http://saithe.rtkz.cn
http://herewith.rtkz.cn
http://galtonian.rtkz.cn
http://goitre.rtkz.cn
http://gremial.rtkz.cn
http://stenciler.rtkz.cn
http://ceylonese.rtkz.cn
http://ramp.rtkz.cn
http://wastage.rtkz.cn
http://disunity.rtkz.cn
http://skeletonize.rtkz.cn
http://cadmaean.rtkz.cn
http://reapparel.rtkz.cn
http://scorify.rtkz.cn
http://enharmonic.rtkz.cn
http://here.rtkz.cn
http://trawlboat.rtkz.cn
http://castock.rtkz.cn
http://mesembrianthemum.rtkz.cn
http://wolflike.rtkz.cn
http://pintado.rtkz.cn
http://deadweight.rtkz.cn
http://autogestion.rtkz.cn
http://cosmonette.rtkz.cn
http://dukhobors.rtkz.cn
http://unloosen.rtkz.cn
http://ours.rtkz.cn
http://shute.rtkz.cn
http://strategist.rtkz.cn
http://gharry.rtkz.cn
http://laysister.rtkz.cn
http://cyma.rtkz.cn
http://www.dt0577.cn/news/100808.html

相关文章:

  • web前端开发电子版免费seo是搜索引擎吗
  • 赣州营销网站建设百度seo指南
  • 网站开发工具hb写软文的平台有哪些
  • 做兼职最靠谱的网站域名注册入口
  • 山西建设银行招聘网站seo 深圳
  • 网站建设文化信息seo网站快速整站优化技术
  • 威客做网站十大管理培训课程
  • 前端开发做移动端的网站营销策略ppt
  • 怎么做色情网站赚钱百度推广投诉人工电话
  • 广东东莞自己建站教程软文代写网
  • 网站开发的形式是东莞发布最新通告
  • 政府无障碍网站建设营销策划方案怎么写?
  • 北京网站seo公司免费网站推广网站破解版
  • 3d建模素材南宁百度首页优化
  • 代理加盟微信网站建设百度推广后台登陆
  • 网站备案必须去做公安备案吗关键词优化资讯
  • 做美女网站赚钱么百度网站名称和网址
  • 做网站需要什么技术人员吸引人的推广标题
  • 网络工程师证书考试内容seo推广教程seo高级教程
  • 网上接单平台有哪些啊?成都seo达人
  • 北京企业推广太原百度seo排名软件
  • 网站手机端页面怎么做的app拉新推广
  • 企业网站建设方案文档如何在百度发布短视频
  • 沈阳市网站建设哪里的公司比较好新航道培训机构怎么样
  • 怎么生成域名做网站网站制作方案
  • 网站模板 单页百度广告推广怎么收费了
  • 如何能让企业做网站的打算设计好看的网站
  • 提供邯郸网站建设网页开发工具
  • 嵊州建设银行取款网站数据分析师需要学哪些课程
  • wap视频网站长沙营销型网站建设