golang interface 转 int string slice struct 类型
在golang中,interface{}
允许接纳任意值,int
, string
, struct
,slice
等,因此我可以很简单的将值传递到interface{}
package main
import ("fmt"
)
type User struct{Name string
}
func main() {any := User{Name: "fidding",}test(any)any2 := "fidding"test(any2)any3 := int32(123)test(any3)any4 := int64(123)test(any4)any5 := []int{1, 2, 3, 4, 5}test(any5)
}// value 允许为任意值
func test(value interface{}) {...
}
但是当我们将任意类型传入到test函数中转为interface后,经常需要进行一系列操作interface不具备的方法(即传入的User结构体
,interface本身也没有所谓的Name
属性),此时就需要用到interface特性type assertions
和type switches
,来将其转换为回原本传入的类型
package main
import ("fmt"
)
type User struct{Name string
}
func main() {any := User{Name: "fidding",}test(any)any2 := "fidding"test(any2)any3 := int32(123)test(any3)any4 := int64(123)test(any4)any5 := []int{1, 2, 3, 4, 5}test(any5)
}
func test(value interface{}) {switch value.(type) {case string:// 将interface转为string字符串类型op, ok := value.(string)fmt.Println(op, ok)case int32:// 将interface转为int32类型op, ok := value.(int32)fmt.Println(op, ok)case int64:// 将interface转为int64类型op, ok := value.(int64)fmt.Println(op, ok)case User:// 将interface转为User struct类型,并使用其Name对象op, ok := value.(User)fmt.Println(op.Name, ok)case []int:// 将interface转为切片类型op := make([]int, 0) //[]op = value.([]int)fmt.Println(op)default:fmt.Println("unknown")}
}
输出:
fidding truefidding true123 true123 true[][1 2 3 4 5]
可以看到我们可以对interface使用.()
并在括号中传入想要解析的任何类型,形如
// 如果转换失败ok=false,转换成功ok=true
res, ok := anyInterface.(someType)
不确定interface类型时候,使用anyInterface.(type)
结合switch case
来做判断。
golang interface 转 int string slice struct 类型相关推荐
- golang interface 转 string,int,float64
inter 是interface类型,转化为string类型是: str := inter.(string) 转为其他类型也类似 testInt := inter.(int) testFloat := ...
- golang 获取struct类型_聊聊golang的类型断言
序 本文主要研究一下golang的类型断言 类型断言 x.(T) 断言x不为nil且x为T类型 如果T不是接口类型,则该断言x为T类型 如果T类接口类型,则该断言x实现了T接口 实例1 func ma ...
- java中三种转string的方法_java中int,char,string三种类型的相互转换
如何将字串 String 转换成整数 int? int i = Integer.valueOf(my_str).intValue(); int i=Integer.parseInt(str); 如何将 ...
- golang 字符串转整形 string 转 int ,go string 转 int
golang 字符串转整形 string 转 int ,go string 转 int 初 代码: 效果 初 很多时候,我们会遇到需要使用 字符串 转 数字 的功能,我们可以用 strconv.Ato ...
- golang 结构体struct、数组、结构体数组 定义与赋值 、json string转struct
文章目录 1.结构体struct 1.1 声明结构体 1.2 初始化结构体 以及 传参 2. 结构体数组.append 3. 接口返回 含数组的结构体 常用写法 json string转struct ...
- go interface转int_Go 中 slice 的 In 功能实现探索
之前在知乎看到一个问题:为什么 Golang 没有像 Python 中 in 一样的功能?于是,搜了下这个问题,发现还是有不少人有这样的疑问. 今天来谈谈这个话题. in 是一个很常用的功能,有些语言 ...
- golang interface to string_Golang 反射
原文作者:OhBonsai 来源:简书 基本了解 在Go语言中,大多数时候值/类型/函数非常直接,要的话,定义一个.你想要个Struct 1type Foo struct {2 A int 3 B s ...
- Golang interface 接口详细原理和使用技巧
文章目录 Golang interface 接口详细原理和使用技巧 一.Go interface 介绍 interface 在 Go 中的重要性说明 interface 的特性 interface 接 ...
- Golang黑科技之——string与[]byte转换
我们知道,相对于C语言,golang是类型安全的语言.但是安全的代价就是性能的妥协. 下面我们通过Golang中的"黑科技"来一窥Golang不想让我们看到的"秘密&qu ...
最新文章
- Benchmarking of long-read correction methods长期校正方法的基准测试
- 将jpg图像转换成yuv420p格式
- CNCF宣布containerd毕业
- android黑窗口获取md5_Android获取文件的MD5
- 紫书 团队队列(队列)
- 小记安装python的MySQLdb模块
- 【华为云技术分享】看得见的安心,一手掌握华为云DRS迁移进度
- java用cookie最新浏览商品_jQuery.cookie.js实现记录最近浏览过的商品功能示例
- 2x麦克劳林公式_极限求解-泰勒公式理解
- Linux下php-fpm进程过多导致内存耗尽问题解决
- 移除mac下 .Ds_Store的解决办法
- 《我的祖国》正确英译应该是《The Evercountry, Mine》
- JavaScript事件
- ruoyi(若依)框架使用说明(前后端分离)
- 2011年八大赚钱爆发点
- linux 3t 硬盘无法识别,希捷(3T)硬盘故障处理备忘
- redmibook pro 14 arch linux alsamixer 检测不到声卡
- 漫威漫画公司(Marvel Comics)简介
- 微信小程序开发者代码管理中,不想要的项目怎么删除
- 独家 | 被大佬纷纷唱衰的AI行业,2018路在何方?
热门文章
- node.js安装模式 的区别_Nodejs入门篇(一)
- 5G NR — 关键技术
- C 语言面试题 — int i [ 4 ]={ i [ 2 ]=2 }
- 用 C 语言开发一门编程语言 — S-表达式
- 5G NGC — AF 的 Service information
- 51单片机中将变量、数组、函数设置在固定位置,定位到绝对地址
- Altium Designer隐藏电气连接线
- 颜色传感器TCS230的使用
- 我为什么看好IPFS
- [svc]java初步