当前位置:网站首页>Golang 类型断言
Golang 类型断言
2022-06-23 22:12:00 【别抢我的辣条~】
一,如何检测和转换接口变量的类型
在Go语言的interface中可以是任何类型,所以Go给出了类型断言来判断某一时刻接口中所含有的类型,例如现在给出一个接口,名为InterfaceText:
x,err:=interfaceText.(T)//T是某一种类型
上式是接口断言的一般形式,因为此方法不一定每次都可以完好运行,所以err的作用就是判断是否出错。所以一般接口断言常用以下写法:
if v,err:=InterfaceText.(T);err {//T是一种类型
possess(v)//处理v
return
}如果转换合法,则v为InterfaceText转换为类型T的值,err为ture,反之err为false。
值得注意的是:InterfaceText必须是接口类型!!!
有些时候若是想仅判断是否含有类型T,可以写为:
if _,err:=InterfaceText.(T);err{
//..
return
}下面给出一个具体的例子帮助理解:
package main
import (
"fmt"
"math"
)
type Square struct{
slide float32
}
type Circle struct{
radius float32
}
type Figure interface{
Area() float32
}
func main(){
var fi Figure
sq:=new(Square)
sq.slide=5
fi=sq
if v,err:=fi.(*Square);err {
fmt.Printf("fi contain a variable of type : %v\n",v)
}else {
fmt.Println("fi does not contain a variable of Square")
}
if v2,ok:=fi.(*Circle);ok {
fmt.Printf("fi contain a variable of type : %v\n",v2)
}else {
fmt.Println("fi does not contain a variable of Circle")
}
}
func (s *Square) Area() float32{
return s.slide*s.slide
}
func (c *Circle) Area() float32{
return c.radius*c.radius*math.Pi
}运行结果:

二,类型判断:type-switch
这是另一种类型判断的方法,此方法和switch很相似。直接看代码:
switch x:=InterfaceText.(type) {
case *Square:
fmt.Printf("text:%v",i)
case *Circle:
//..
case nil:
//..
default:
//..
//..and so forth
}理解思路和switch很相似,如果InterfaceText中有*Square,*Circle,nil三种类型,就会执行对应的代码,若都没有,便会执行default里的代码。
如果仅判断,而不使用值的话可以写为:
switch InterfaceText.(type) {
case *Square:
fmt.Printf("text:%v",i)
case *Circle:
//..
case nil:
//..
default:
//..
//..and so forth
}有时为了方便,我们可以把它打包成一个函数来判断一些未知类型:
func classify(items...interface{}){
for i,x:=range items {
switch x.(type) {
case bool:
fmt.Printf("text:%v",i)
case int:
//..
case float32:
//..
default:
//..
//..and so forth
}
}
}可以这样调用此方法:classifier(13, -14.3, false) 。
当然也可以加入其他类型,这个看具体情况而定。
ending~~
边栏推荐
- [Xilinx ax7103 microbalze Learning Notes 6] MicroBlaze custom IP core packaging experiment
- PHP timestamp
- The sandbox week is coming!
- 2022 cloud consulting technology series storage & CDN special sharing meeting
- Nlog详解
- Go language learning
- 根据先序遍历和中序遍历生成后序遍历
- Which securities dealers recommend? Is online account opening safe?
- C WinForm custom progress bar ProgressBar
- C # read the occupied size of memory module and hard disk
猜你喜欢

图像分割-数据标注

The national post office and other three departments: strengthen the security management of personal information related to postal express delivery, and promote the de identification technology of per

HDLBits-> Circuits-> Arithmetic Circuitd-> 3-bit binary adder

【HackTheBox】 meow

【设计】1359- Umi3 如何实现插件化架构

短视频挺进在线音乐腹地

The Sandbox 与 BAYZ 达成合作,共同带动巴西的元宇宙发展

【HackTheBox】Fawn

抖音支付十万级 TPS 流量发券实践

Short video enters the hinterland of online music
随机推荐
评估和选择最佳学习模型的一些指标总结
Fabric. JS manual bold text iText
Androidkotlin comprehensive and detailed class usage grammar learning guide
PLC数据操作系列之构造不同应用场景的缓存栈FIFO(算法详解)
腾讯会议号设计的几种猜测
MySQL事务隔离
[design] 1359- how umi3 implements plug-in architecture
Aicon2021 | AI technology helps content security and promotes the healthy development of Internet Environment
根据先序遍历和中序遍历生成后序遍历
How to index websites in Google
光大期货安全吗?开户需要什么东西?
Telecommuting: how to become a master of time management| Community essay solicitation
How to connect the fortress machine to the new server? What can I do if there is a problem with the fortress machine?
保障特殊困难群体安全,广州民政全力做好三防工作
Face and lining of fresh food pre storage
HAOGE's blog Road
AIX系统月维护查什么(二)
数据解读!理想L9冲刺「月销过万」,从BBA手中抢份额
什么是免疫组织化学实验? 免疫组织化学实验
“山大地纬杯”第十二届山东省ICPC大学生程序设计竞赛