当前位置:网站首页>【Yugong Series】August 2022 Go Teaching Course 036-Type Assertion
【Yugong Series】August 2022 Go Teaching Course 036-Type Assertion
2022-08-11 03:19:00 【Yugong move code】
一、类型断言
1,Definition of Type Assertion
GOThe type assertion in is used to check whether the value held by the interface type variable implements the expected interface or concrete type.
The syntax of a type assertion is as follows:
value, ok := x.(T)
其中,x Actual value representing the type of an interface,T 表示一个具体的类型(也可为接口类型).
该断言表达式会返回 value(x的实际值)和ok(x的值是否等于T的类型),可根据该布尔值判断 x 是否为 T 类型:
- 如果 T 是具体某个类型,类型断言会检查 x 的动态类型是否等于具体类型 T.如果检查成功,类型断言返回的结果是 x 的动态值,其类型是 T.
- 如果 T 是接口类型,类型断言会检查 x 的动态类型是否满足 T.如果检查成功,x 的动态值不会被提取,返回值是一个类型为 T 的接口值.
- 无论 T 是什么类型,如果 x 是 nil 接口值,类型断言都会失败.
2.Use of type assertions
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)
}
}
边栏推荐
猜你喜欢
Summary of Logstash log data write exception troubleshooting
DOM-DOM tree, a DOM tree has three types of nodes
Multi-threaded ThreadPoolExecutor
The problem that Merge will be lost again after code Revert has been solved
Qnet弱网测试工具操作指南
EasyCVR接入海康大华设备选择其它集群服务器时,通道ServerID错误该如何解决?
你不知道的 console.log 替代品
【DB运营管理/开发解决方案】上海道宁为您提供提高工作便利性的集成开发工具——Orange
[BX] and loop
EasyCVR接入GB28181设备时,设备接入正常但视频无法播放是什么原因?
随机推荐
flink The object probably contains or references non serializable fields.
df和df -lh的意思
The 125th day of starting a business - a note
电商项目——商城限时秒杀功能系统
js中的this问题
没想到MySQL还会问这些...
KingbaseES有什么办法,默认不读取sys_catalog下的系统视图?
AI+医疗:使用神经网络进行医学影像识别分析
元素的BFC属性
DNS分离解析和智能解析
程序化交易的策略类型可以分为哪几种?
STC8H开发(十五): GPIO驱动Ci24R1无线模块
浮点数在内存中的存储方式
Economic Misunderstandings in the Crypto World: Is Cash a Savings?Scarcity creates value?
EasyCVR接入GB28181设备时,设备接入正常但视频无法播放是什么原因?
C语言之自定义类型------结构体
AI+医疗:使用神经网络进行医学影像识别分析
A surviving spouse of the opposite sex within large turn paragraph, what for
Idea (preferred) cherry-pick operation
LeetCode热题(12.买卖股票的最佳时机)