Go语言里面有一个语法,可以直接判断是否是该类型的变量: value, ok = element.(T),这里value就是变量的值,ok是一个bool类型,element是interface变量,T是断言的类型。
如果element里面确实存储了T类型的数值,那么ok返回true,否则返回false。
package main import ( "fmt" ) type Order struct { ordId int customerId int callback func() } func main() { var i interface{} i = Order{ ordId: 456, customerId: 56, } value, ok := i.(Order) if !ok { fmt.Println("It's not ok for type Order") return } fmt.Println("The value is ", value) }
输出:
The value is {456 56 <nil>}
常见的还有用switch来断言:
package main import ( "fmt" ) type Order struct { ordId int customerId int callback func() } func main() { var i interface{} i = Order{ ordId: 456, customerId: 56, } switch value := i.(type) { case int: fmt.Printf("It is an int and its value is %d\n", value) case string: fmt.Printf("It is a string and its value is %s\n", value) case Order: fmt.Printf("It is a Order and its value is %v\n", value) default: fmt.Println("It is of a different type") } }
输出:
It is a Order and its value is {456 56 <nil>}
golang的语言中提供了断言的功能。golang中的所有程序都实现了interface{}的接口,这意味着,所有的类型如string,int,int64甚至是自定义的struct类型都就此拥有了interface{}的接口,这种做法和java中的Object类型比较类似。那么在一个数据通过func funcName(interface{})的方式传进来的时候,也就意味着这个参数被自动的转为interface{}的类型。
如以下的代码:
func funcName(a interface{}) string { return string(a) }
编译器将会返回:
cannot convert a (type interface{}) to type string: need type assertion
此时,意味着整个转化的过程需要类型断言。类型断言有以下几种形式:
直接断言使用
var a interface{} fmt.Println("Where are you,Jonny?", a.(string))
但是如果断言失败一般会导致panic的发生。所以为了防止panic的发生,我们需要在断言前进行一定的判断。
value, ok := a.(string)
如果断言失败,那么ok的值将会是false,但是如果断言成功ok的值将会是true,同时value将会得到所期待的正确的值。示例:
value, ok := a.(string) if !ok { fmt.Println("It's not ok for type string") return } fmt.Println("The value is ", value)
另外也可以配合switch语句进行判断:
var t interface{} t = functionOfSomeType() switch t := t.(type) { default: fmt.Printf("unexpected type %T", t) // %T prints whatever type t has case bool: fmt.Printf("boolean %t\n", t) // t has type bool case int: fmt.Printf("integer %d\n", t) // t has type int case *bool: fmt.Printf("pointer to boolean %t\n", *t) // t has type *bool case *int: fmt.Printf("pointer to integer %d\n", *t) // t has type *int }
另外补充几个go语言编程的小tips:
(1)如果不符合要求可以尽快的return(return as fast as you can),而减少else语句的使用,这样可以更加直观一些。
(2)转换类型的时候如果是string可以不用断言,使用fmt.Sprint()函数可以达到想要的效果。
(3)变量的定义和申明可以用组的方式,如:
var ( a string b int c int64 ... ) import ( "fmt" "strings" "net/http" ... )
(4)函数逻辑比较复杂,可以把一些逻辑封装成一个函数,提高可读性。
(5)使用net/http包和net/url包的函数,可能会带有url encode功能,需要注意。
PHP中文网,有大量免费的Golang入门教程,欢迎大家学习!
以上是golang 断言是什么的详细内容。更多信息请关注PHP中文网其他相关文章!

Go语言的核心特性包括垃圾回收、静态链接和并发支持。1.Go语言的并发模型通过goroutine和channel实现高效并发编程。2.接口和多态性通过实现接口方法,使得不同类型可以统一处理。3.基本用法展示了函数定义和调用的高效性。4.高级用法中,切片提供了动态调整大小的强大功能。5.常见错误如竞态条件可以通过gotest-race检测并解决。6.性能优化通过sync.Pool重用对象,减少垃圾回收压力。

Go语言在构建高效且可扩展的系统中表现出色,其优势包括:1.高性能:编译成机器码,运行速度快;2.并发编程:通过goroutines和channels简化多任务处理;3.简洁性:语法简洁,降低学习和维护成本;4.跨平台:支持跨平台编译,方便部署。

关于SQL查询结果排序的疑惑学习SQL的过程中,常常会遇到一些令人困惑的问题。最近,笔者在阅读《MICK-SQL基础�...

golang ...

Go语言中如何对比并处理三个结构体在Go语言编程中,有时需要对比两个结构体的差异,并将这些差异应用到第�...

GoLand中自定义结构体标签不显示怎么办?在使用GoLand进行Go语言开发时,很多开发者会遇到自定义结构体标签在�...


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

Atom编辑器mac版下载
最流行的的开源编辑器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

禅工作室 13.0.1
功能强大的PHP集成开发环境

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境