当前位置:网站首页>【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)
}
}

边栏推荐
- 阿里低代码框架 lowcode-engine 之自定义物料篇
- ESP32的环境配置(arduino arduino2.0 VScode platform哪个好用?)
- watch监听
- MSP430如何给板子下载程序?(IAR MSPFET CCS)
- 树莓派入门(5)系统备份
- 程序化交易与主观交易对盈利曲线的影响!
- 输入起始位置,终止位置截取链表
- Unity2D animation (1) introduction to Unity scheme - animation system composition and the function of use
- “京台高铁”亮相百度地图,真能在2035年建成吗?
- "Beijing-Taiwan high-speed rail" debuted on Baidu map, can it really be built in 2035?
猜你喜欢
随机推荐
OpenCV founder: Open source must not be completely free!
图解LeetCode——640. 求解方程(难度:中等)
ES6 advanced string processing new features
flink The object probably contains or references non serializable fields.
rac备库双节点查询到的表最后更新时间不一致
Summary of debugging skills
用户如何克服程序化交易中的情绪问题?
The most unlucky and the luckiest
输入起始位置,终止位置截取链表
Deep Learning - Second Time
The negative semantic transformation layer
Unity2D animation (1) introduction to Unity scheme - animation system composition and the function of use
LeetCode Hot Questions (12. The Best Time to Buy and Sell Stocks)
MongoDB 基础了解(二)
电商项目——商城限时秒杀功能系统
入职数字ic设计后的一些工作心得
Idea (优选)cherry-pick操作
[Pdf generated automatically bookmarks]
JS-DOM element object
21天学习挑战赛第一周总结









