当前位置:网站首页>【愚公系列】2022年08月 Go教学课程 036-类型断言
【愚公系列】2022年08月 Go教学课程 036-类型断言
2022-08-11 03:14:00 【愚公搬代码】
一、类型断言
1,类型断言的定义
GO中的类型断言用于检查接口类型变量所持有的值是否实现了期望的接口或者具体的类型。
类型断言的语法格式如下:
value, ok := x.(T)
其中,x 表示一个接口的类型的实际值,T 表示一个具体的类型(也可为接口类型)。
该断言表达式会返回 value(x的实际值)和ok(x的值是否等于T的类型),可根据该布尔值判断 x 是否为 T 类型:
- 如果 T 是具体某个类型,类型断言会检查 x 的动态类型是否等于具体类型 T。如果检查成功,类型断言返回的结果是 x 的动态值,其类型是 T。
- 如果 T 是接口类型,类型断言会检查 x 的动态类型是否满足 T。如果检查成功,x 的动态值不会被提取,返回值是一个类型为 T 的接口值。
- 无论 T 是什么类型,如果 x 是 nil 接口值,类型断言都会失败。
2.类型断言的使用
package main
import "fmt"
func main() {
var i interface{
}
i="abc"
value,ok:=i.(int)
if ok{
fmt.Println(value+10)
}else{
fmt.Println("类型推断错误")
}
}
3.案例
package main
import "fmt"
// 加法类
type Add struct {
Object
}
func (a *Add) GetResult() int {
// 方法的实现要和接口中方法的声明保持一致
return a.numA + a.numB
}
func (a *Add) SetData(data ...interface{
}) bool {
// 1: 对数据的个数进行校验。
var b bool = true
if len(data) > 2 {
fmt.Println("参数个数错误!!")
b = false
}
value, ok := data[0].(int)
if !ok {
fmt.Println("第一个数类型错误")
b = false
}
value1, ok1 := data[1].(int)
if !ok1 {
fmt.Println("第二个数据类型错误")
b = false
}
a.numA = value
a.numB = value1
// 2: 类型进行校验。
return b
}
// 减法类
type Sub struct {
Object
}
func (s *Sub) SetData(data ...interface{
}) bool {
// 1: 对数据的个数进行校验。
var b bool = true
if len(data) > 2 {
fmt.Println("参数个数错误!!")
b = false
}
value, ok := data[0].(int)
if !ok {
fmt.Println("第一个数类型错误")
b = false
}
value1, ok1 := data[1].(int)
if !ok1 {
fmt.Println("第二个数据类型错误")
b = false
}
s.numA = value
s.numB = value1
// 2: 类型进行校验。
return b
}
func (s *Sub) GetResult() int {
return s.numA - s.numB
}
type Object struct {
numA int
numB int
}
type Resulter interface {
GetResult() int
SetData(data ...interface{
}) bool // 完成参数运算的数据的类型校验。
}
// 对象问题
// 1: 定义一个新的类
type OperatorFactory struct {
}
// 2: 创建一个方法,在该方法中完成对象的创建
func (o *OperatorFactory) CreateOperator(op string) Resulter {
switch op {
case "+":
add := new(Add)
return add
case "-":
sub := new(Sub)
return sub
default:
return nil
}
}
func OperatorWho(h Resulter) int {
return h.GetResult()
}
func main() {
var operator OperatorFactory
obj := operator.CreateOperator("-")
b := obj.SetData(30, 10)
if b {
num := OperatorWho(obj)
fmt.Println(num)
}
}
边栏推荐
- Vim and copy and paste from the outside (don't need to install the plugin)
- [BX] and loop
- 音频编解码,利用FAAC来实现AAC编码
- 分布式和集群的区别和联系
- CC0 vs. commercial IP: which model is better for NFTs?
- Add support for Textbundle
- 没想到MySQL还会问这些...
- Economic Misunderstandings in the Crypto World: Is Cash a Savings?Scarcity creates value?
- 轮转数组问题:如何实现数组“整体逆序,内部有序”?“三步转换法”妙转数组
- pathman_config、pathman_config_params 删除后,如何重建?
猜你喜欢
Salesforce解散中国团队,什么样的CRM产品更适合中国人
正式发布丨VS Code 1.70
互换性与测量技术——表面粗糙度选取和标注方法
JS-DOM element object
"Life Is Like First Seen" is ill-fated, full of characters, and the contrast of Zhu Yawen's characters is too surprising
深度学习-第二次
leetcode:358. K 距离间隔重排字符串
flink The object probably contains or references non serializable fields.
ES进阶 数组功能语法新特性详解
最倒霉与最幸运
随机推荐
创业的第125天——随记
CTO说MySQL单表行数不要超过2000w,为啥?
DOM-DOM tree, a DOM tree has three types of nodes
获取链表长度
【LeetCode】Day112-重复的DNA序列
MSP430如何给板子下载程序?(IAR MSPFET CCS)
IDE编译报错:Dangling metacharacter
聊聊对RPC的理解
添加用户报错useradd: cannot open /etc/passwd
多线程之ThreadPoolExecutor
C语言之自定义类型------结构体
构建程序化交易系统需要注意什么问题?
CC0 vs. commercial IP: which model is better for NFTs?
JS-DOM element object
Goodbye Guangzhou paper invoices!The issuance of electronic invoices for accommodation fees will completely replace the invoices of hotels, restaurants and gas stations
(CVPR-2017)在身体和潜在部位学习深度上下文感知特征以进行行人重识别
【idea 报错】 无效的目标发行版:17 的解决参考
Goodbye Chongqing paper invoices!The issuance of electronic invoices for accommodation expenses will soon completely replace the invoices of hotels, catering and gas stations
基于FPGA状态机的自动售货机功能实现
夫妻一方婚内向异性大额转款,怎么判