当他们的标的类型(underlying type) 共享同一个标的类型的时候,他们就可以相互转换。
例如:
package main
type MyInt int64
type Ta *int64
type Tb *MyInt
func main() {
var a Ta
var b Tb
//a = Ta(b) // error: direct conversion is not allowed
// But indirect conversion is possible.
y := (*MyInt)(b)
x := (*int64)(y)
a = x // <=> the next line
a = (*int64)(y) // <=> the next line
a = (*int64)((*MyInt)(b))
_ = a
}
网友评论