当前位置:网站首页>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~~
边栏推荐
- 有哪些劵商推荐?在线开户安全么?
- Fabric.js 手动加粗文本iText
- How PostgreSQL creates partition tables
- STM32-------外部中断
- 评估和选择最佳学习模型的一些指标总结
- C# 读取内存条占用大小,硬盘占用大小
- Eight models of data analysis: detailed PEST model
- Unknown character set index for field ‘255‘ received from server.
- [Xilinx ax7103 microbalze Learning Notes 6] MicroBlaze custom IP core packaging experiment
- 3D打印和激光切割流程的初步了解
猜你喜欢
随机推荐
MySQL事务隔离
PLC数据操作系列之构造不同应用场景的缓存栈FIFO(算法详解)
Come on, touch and write a hook
MySQL索引底层为什么用B+树?看完这篇文章,轻松应对面试。
国内外最好的12款项目管理系统优劣势分析
腾讯会议号设计的几种猜测
Unknown character set index for field ‘255‘ received from server.
Unknown character set index for field ‘255‘ received from server.
C# 读取内存条占用大小,硬盘占用大小
TDD development mode process recommendation
PyQt5_QTableWidget分页单选右键菜单控件
How to index websites in Google
Autofac details
Is the geTx status management in the flutter really so good to use?
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
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
Cause analysis and Countermeasures for FANUC robot srvo-050 collision detection alarm (available for personal test)
微信视频号如何用 PC 电脑做直播?
The fortress machine installs pytorch, mmcv, and mmclassification, and trains its own data sets
【Try to Hack】masscan








