使用方式:
int64(a) // 类型转换
v, ok := a.(typeName) //类型断言
总结:
相似:都是右边有括号。
区别:断言有.
类型转换demo:
package main
import (
"github.com/davecgh/go-spew/spew"
)
func main() {
var age int
age = 18
// int转int64
ageInt64 := int64(age)
spew.Dump(ageInt64)
}
// 输出
(int64) 18
断言demo:
package main
import (
"github.com/davecgh/go-spew/spew"
)
type XiaoYi struct {
age int
}
func main() {
var xy interface{}
xy = XiaoYi{
age: 18,
}
originType, ok := xy.(XiaoYi)
spew.Dump(originType)
spew.Dump(ok)
}
// 输出
(main.XiaoYi) {
age: (int) 18
}
(bool) true
网友评论