在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 true
fidding true
123 true
123 true
[1 2 3 4 5]
可以看到我们可以对interface使用.()并在括号中传入想要解析的任何类型,形如
// 如果转换失败ok=false,转换成功ok=true res, ok := anyInterface.(someType)
并且我们并不确定interface类型时候,使用anyInterface.(type)结合switch case来做判断。
现在再回过头看上面的栗子,是不是更清楚了呢
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
内蒙古资源网 Copyright www.nmgbbs.com
暂无“golang语言如何将interface转为int, string,slice,struct等类型”评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。