使用方式:
```go
int64(a) // 类型转换
v, ok := a.(typeName) //类型断言
```
总结:
相似:都是右边有括号。
区别:断言有.
类型转换demo:
```go
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:
```go
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
```
有疑问加站长微信联系(非本文作者))