当前位置:网站首页>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~~
边栏推荐
- Go language learning
- How to write and read ASM file system data
- PHP curl function extension basic usage
- 短视频挺进在线音乐腹地
- Four traversals of map sets
- 解决Slf4j日志不打印问题
- 不同网络结构的特征也能进行对比学习?蚂蚁&美团&南大&阿里提出跨架构自监督视频表示学习方法CACL,性能SOTA!...
- Detailed explanation of MySQL database configuration information viewing and modification methods
- 微信视频号如何用 PC 电脑做直播?
- PHP的curl功能扩展基本用法
猜你喜欢

STM32-------外部中斷

解决Slf4j日志不打印问题

Face and lining of fresh food pre storage

Million message IM system technical points sharing

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

How to write and read ASM file system data

The sandbox week is coming!

FANUC机器人SRVO-050碰撞检测报警原因分析及处理对策(亲测可用)

Telecommuting: how to become a master of time management| Community essay solicitation

【设计】1359- Umi3 如何实现插件化架构
随机推荐
开发协同,高效管理 | 社区征文
The sandbox week is coming!
浩哥的博客之路
PHP的curl功能扩展基本用法
The fortress machine installs pytorch, mmcv, and mmclassification, and trains its own data sets
云原生流水线工具汇总
TDD开发模式流程推荐
Autofac详解
Nlog详解
巨头下场“摆摊”,大排档陷入“苦战”
短视频挺进在线音乐腹地
抖音支付十万级 TPS 流量发券实践
Data interpretation! Ideal L9 sprints to "sell more than 10000 yuan a month" to grab share from BBA
在OpenCloudOS使用snap安装.NET 6
C#/VB. Net word to text
laravel之任务队列
【Xilinx AX7103 MicroBalze学习笔记6】MicroBlaze 自定义 IP 核封装实验
The sandbox and bayz have reached cooperation to jointly drive the development of metauniverse in Brazil
Application of clock synchronization system in banking system
2022年信息安全工程师考试知识点:访问控制