7.2 Go type assertion
7.2 Go type assertion
类型断言是使用在接口值上的操作。
语法x.(T)被称为类型断言,x代表接口的类型,T代表一个类型检查。
类型断言检查它操作对象的动态类型是否和断言类型匹配。
类型断言快速入门
package main import (
"fmt"
) type Point struct {
x int
y int
} func main() { var a interface{}
var point Point = Point{1, 2}
//任何类型都实现了空接口
a = point
fmt.Printf("类型:%T 值:%v\n", a, a) /*
想要将a赋值给b变量,可以直接这么玩吗?
var b Point
b = a
报错
cannot use a (type interface {}) as type Point in assignment: need type assertion
提示需要类型断言type assertion
*/
var b Point
b, ok := a.(Point)
if ok {
fmt.Printf("类型:%T 值:%v\n", b, b)
}
}
1.1. 类型断言介绍
在类型断言时,如果类型不匹配,程序会直接panic异常退出,因此要确保类型断言,空接口指向的就是断言的类型,或者加上检查机制,防止程序panic退出。
package main import (
"fmt"
) //test函数接收一个空接口,可以接收任意的数据类型
func test(a interface{}) { //带有检查机制的类型断言,ok是布尔值,返回true或false
s, ok := a.(int)
if ok {
fmt.Println(s)
//手动return结束这个类型检查
return
} str, ok := a.(string)
if ok {
fmt.Println(str)
return
} f, ok := a.(float32)
if ok {
fmt.Println(f)
return
} fmt.Println("can not define the type of a")
} //测试test函数类型检查
func testInterface1() {
var a int = 100
test(a) var b string = "hello"
test(b)
} //使用分支判断,检测类型断言
func testSwitch(a interface{}) {
//直接switch跟着类型断言表达式
switch a.(type) {
case string:
fmt.Printf("a is string, value:%v\n", a.(string))
case int:
fmt.Printf("a is int, value:%v\n", a.(int))
case int32:
fmt.Printf("a is int, value:%v\n", a.(int))
default:
fmt.Println("not support type\n")
}
} func testSwitch2(a interface{}) {
//将类型断言结果赋值给变量
switch v := a.(type) {
case string:
fmt.Printf("a is string, value:%v\n", v)
case int:
fmt.Printf("a is int, value:%v\n", v)
case int32:
fmt.Printf("a is int, value:%v\n", v)
default:
fmt.Println("not support type\n")
}
} func testInterface2() {
var a int = 123456
testSwitch(a)
fmt.Println("-------------")
var b string = "hello"
testSwitch(b)
} func testInterface3() {
var a int = 100
testSwitch2(a)
var b string = "hello"
testSwitch2(b)
} func main() {
//testInterface1()
//testInterface2()
testInterface3()
}
7.2 Go type assertion的更多相关文章
- [golang] go的typeswitch guard(类型区别)语法和type assertion(类型断言)语法
最近在实现golang,看到个go的特性语法: typeswitch guard. typeswitch guard语法如下: package main import "fmt" ...
- TypeScript & as & Type Assertion
TypeScript & as & Type Assertion Type Assertion (as) That is not vanilla JavaScript, it is T ...
- Go 的类型断言type assertion
Go语言中的类型断言,语法上是这样的: x.(T) 其中,x是interface接口的表达式,T是类型,称为被断言类型. 补充一下,接口有接口值的概念,其包括动态类型和动态值两部分. 类型断言根据T的 ...
- Go语言中cannot convert adminname (type interface {}) to type *: need type assertion的解决办法
解决的办法是把string(adminname)替换为adminname.(string).其它类型也是类似.
- go 报 need type assertion
responese_total := m["responses"].([]interface{})[0].(map[string]interface{})["hits&q ...
- [TypeScript] Work with DOM Elements in TypeScript using Type Assertions
The DOM can be a bit tricky when it comes to typing. You never really know exactly what you're going ...
- (type interface {}) to type string
go 语言开发中,经常会在函数中碰到使用 insterface{} 作为接收任意参数,但是我们接收的数据经常是需要做类型转换,由于是初学者,因此,初次转换我是直接就 func New(paramete ...
- [TypeScript] Type check JavaScript files using JSDoc and Typescript 2.5
Typescript 2.5 adds JSDoc type assertion support for javascript file via ts-check service. First of ...
- [TypeScript] Using Assertion to Convert Types in TypeScript
Sometimes the compiler needs help figuring out a type. In this lesson we learn how to help out the c ...
随机推荐
- 笔记-VUE滚动加载更多数据
来源:https://blog.csdn.net/qq_17281881/article/details/87342403 VUE滚动加载更多数据 data() { return { loading: ...
- PHP实现MySQL并发查询
一般的,一个看似很简单的页面,一次http请求后,到达服务端,穿过Cache层,落到后台后,实际可能会有很多很多的数据查询逻辑!而这些查询实际是不相互依赖的,也即可以同时查询.比如各种用户信息,用户的 ...
- Vsftp用户限制
背景 Oracle全库备份,异地备份 在实现异地备份后,由第三方人员登录服务器拉取dmp文件. 为了确保安全,创建一个特定ftp账号用于第三方人员使用 要求 1.可以登录服务器 2.可以拉取dmp文件 ...
- 文件包含漏洞(pikachu)
文件包含漏洞 在web后台开发中,程序员往往为了提高效率以及让代码看起来更加简洁,会使用'包含'函数功能,比如把一系列功能函数都写进function.php中,之后当某个文件需要调用的时候,就直接在文 ...
- jenkins及Maven介绍
一.环境介绍 随着软件开发需求及复杂度的不断提高,团队开发成员之间如何更好地协同工作以确保软件开发的质量已经慢慢成为开发过程中不可回避的问题.Jenkins自动化部署可以解决集成.测试.部署等重复性的 ...
- 《C Primer Plus(第6版)中文版》一1.12 复习题
本节书摘来自异步社区<C Primer Plus(第6版)中文版>一书中的第1章,第1.12节,作者 傅道坤,更多章节内容可以访问云栖社区"异步社区"公众号查看. 1. ...
- 为物联网而生:高性能时间序列数据库HiTSDB商业化首发!
为什么80%的码农都做不了架构师?>>> 摘要: 近日,阿里云宣布高性能时间序列数据库 (High-Performance Time Series Database , 简称 H ...
- #Week4 Logistic Regression
一.Classification 主要讨论二元分类. 线性回归处理分类问题显然不靠谱,所以采用逻辑回归. 二.Hypothesis Representation 假设函数变为\(h_\theta(x) ...
- CodeForces-259B]Little Elephant and Magic Square
Little Elephant loves magic squares very much. A magic square is a 3 × 3 table, each cell contains ...
- 解决Vue中文本输入框v-model双向绑定后数据不显示的问题
前言 项目中遇到一个问题就是在Vue中双向绑定对象属性时,手动赋值属性后输入框的数据不实时更新的问题. <FormItem label="地址" prop="eve ...