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

边栏推荐
- 构建程序化交易系统需要注意什么问题?
- 用户如何克服程序化交易中的情绪问题?
- 调试技巧总结
- Goodbye Chongqing paper invoices!The issuance of electronic invoices for accommodation expenses will soon completely replace the invoices of hotels, catering and gas stations
- 程序化交易改变了什么?
- google搜索技巧——程序员推荐
- 多商户商城系统功能拆解26讲-平台端分销设置
- Traversal of DOM tree-----modify styles, select elements, create and delete nodes
- Salesforce disbands the Chinese team, which CRM product is more suitable for the Chinese
- DOM-DOM树,一个DOM树有三种类型的节点
猜你喜欢

浮点数在内存中的存储方式

A Practical Arrangement of Map GIS Development Matters (Part 1)

言简意赅,说说 @Transactional 在项目中的使用

The problem that Merge will be lost again after code Revert has been solved

关于地图GIS开发事项的一次实践整理(上)

Logstash日志数据写入异常排查问题总结

常用认证机制

【DB运营管理/开发解决方案】上海道宁为您提供提高工作便利性的集成开发工具——Orange

MongoDB 基础了解(二)

Vim and copy and paste from the outside (don't need to install the plugin)
随机推荐
Official release丨VS Code 1.70
Multi-threaded ThreadPoolExecutor
没想到MySQL还会问这些...
[DB operation management/development solution] Shanghai Daoning provides you with an integrated development tool to improve the convenience of work - Orange
How does MSP430 download programs to the board?(IAR MSPFET CCS)
元素的BFC属性
A surviving spouse of the opposite sex within large turn paragraph, what for
leetcode:358. K 距离间隔重排字符串
音视频开发,为什么要学习FFmpeg?应该怎么入手FFmpeg学习?
rac备库双节点查询到的表最后更新时间不一致
Logstash日志数据写入异常排查问题总结
Idea (preferred) cherry-pick operation
入职数字ic设计后的一些工作心得
[BX] and loop
代码 Revert 后再次 Merge 会丢失的问题,已解决
VIT 源码详解
否定语义转化层
什么是三方支付?
"How to kick a bad habit to read notes?
用户如何克服程序化交易中的情绪问题?