当前位置:网站首页>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~~
边栏推荐
- How to index websites in Google
- Face and lining of fresh food pre storage
- PHP timestamp
- Short video enters the hinterland of online music
- 什么是免疫组织化学实验? 免疫组织化学实验
- Several cases of index invalidation caused by MySQL
- Analysis of Alibaba cloud Tianchi competition -- prediction of o2o coupon
- STM32-------定时器
- 浩哥的博客之路
- What is the development prospect of face recognition technology?
猜你喜欢

Nlog详解

Analysis on the advantages and disadvantages of the best 12 project management systems at home and abroad

7、STM32——LCD

什么是免疫组织化学实验? 免疫组织化学实验

kubernetes之常用核心资源对象

【观察】戴尔科技+英特尔傲腾技术:以“纳秒之速”领跑存储创新

巨头下场“摆摊”,大排档陷入“苦战”

Can the characteristics of different network structures be compared? Ant & meituan & NTU & Ali proposed a cross architecture self supervised video representation learning method CaCl, performance SOTA

Is the geTx status management in the flutter really so good to use?

抖音支付十万级 TPS 流量发券实践
随机推荐
Desai wisdom number - histogram (basic histogram): the way to celebrate father's day in 2022
The Sandbox 与 BAYZ 达成合作,共同带动巴西的元宇宙发展
网站如何在Google建立索引
STM32-------外部中斷
Bitmap load memory analysis
[observation] Dell technology + Intel aoteng Technology: leading storage innovation with "nanosecond speed"
微信视频号如何用 PC 电脑做直播?
Sorry, your USB cable may be wrong!
Go language core 36 lectures (go language practice and application 23) -- learning notes
E: 无法获得锁 /var/lib/dpkg/lock
【HackTheBox】Fawn
The sandbox and bayz have reached cooperation to jointly drive the development of metauniverse in Brazil
Isolement des transactions MySQL
ASM文件系统 数据如何写和读数据
C # read the occupied size of memory module and hard disk
Nlog详解
巨头下场“摆摊”,大排档陷入“苦战”
FANUC机器人SRVO-050碰撞检测报警原因分析及处理对策(亲测可用)
The Sandbox 归属周来啦!
MySQL索引底层为什么用B+树?看完这篇文章,轻松应对面试。