当前位置:网站首页>【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)
}
}
边栏推荐
- (CVPR-2017)在身体和潜在部位学习深度上下文感知特征以进行行人重识别
- this question in js
- "Life Is Like First Seen" is ill-fated, full of characters, and the contrast of Zhu Yawen's characters is too surprising
- UNI-APP_iphone苹果手机底部安全区域
- 构建程序化交易系统需要注意什么问题?
- A practice arrangement about map GIS (below) GIS practice of Redis
- MongoDB 基础了解(二)
- Ten Advanced Concepts of SQL Development
- Multi-threaded ThreadPoolExecutor
- oracle的基数会影响到查询速度吗?
猜你喜欢
C语言之自定义类型------结构体
Unity2D animation (1) introduction to Unity scheme - animation system composition and the function of use
Multi-merchant mall system function disassembly 26 lectures - platform-side distribution settings
Official release丨VS Code 1.70
Vim and copy and paste from the outside (don't need to install the plugin)
入职数字ic设计后的一些工作心得
JS-DOM元素对象
Detailed explanation of new features of ES advanced function syntax
AI+医疗:使用神经网络进行医学影像识别分析
flink The object probably contains or references non serializable fields.
随机推荐
Detailed explanation of new features of ES advanced function syntax
JS-DOM element object
What does the sanction of the mixer Tornado mean for the DeFi market?
The practice of alibaba data synchronization component canal
Typescript学习笔记 | 字节青训营笔记
Idea (preferred) cherry-pick operation
oracle的基数会影响到查询速度吗?
正式发布丨VS Code 1.70
ES6 advanced string processing new features
轮转数组问题:如何实现数组“整体逆序,内部有序”?“三步转换法”妙转数组
元素的BFC属性
JS-DOM元素对象
CSAPP Data Lab
OpenCV founder: Open source must not be completely free!
解决vim与外界的复制粘贴(不用安装插件)
The 125th day of starting a business - a note
Docker 链接sqlserver时出现en-us is an invalid culture错误解决方案
js中的this问题
Google search skills - programmer is recommended
音视频开发,为什么要学习FFmpeg?应该怎么入手FFmpeg学习?