【发布时间】:2014-04-29 01:01:34
【问题描述】:
两个密切相关的问题:
如果T2 具有T1 的基础类型,为什么Go Specification 不允许您将[]T1 转换为[]T2?
使用unsafe 包进行转换有什么负面影响?
示例:
package main
import (
"fmt"
"unsafe"
)
type T1 struct {
Val int
}
// T2 has the underlying type of T1
type T2 T1
func main() {
a := []T1{T1{12}}
// cannot convert a (type []T1) to type []T2
//b := ([]T2)(a)
// But with some unsafe we can do it.
// So, why doesn't Go allow it? And what unforeseen consequence might it have?
b := *(*[]T2)(unsafe.Pointer(&a))
b[0].Val = 42
fmt.Println(a[0].Val) // 42
}
游乐场: http://play.golang.org/p/x2tBRKuRF1
使用示例:
如果 T1 实现了某个接口,例如 json.Marshaler,并且您想以不同的方式对类型进行 JSON 编码,您可以创建一个新的 type T2 T1,并使用它自己的 json.Marshaler 实现。
它在封送单个值时可以正常工作,但是当您获得 []T1 切片时,您必须将其复制到 []T2 切片或使用自己的 MarshalJSON() 方法创建一个新的 type ST1 []T1。做一个简单的转换而不是转向unsafe 会很好,因为它可能会导致运行时错误而不是编译时间。
【问题讨论】:
标签: go type-conversion