我正在编写一个小程序来使用 Go 检查字节序:
var i int = 0x0100
ptr := unsafe.Pointer(&i)
if 0x01 == *(*byte)(ptr) {
fmt.Println("Big Endian")
} else if 0x00 == *(*byte)(ptr) {
fmt.Println("Little Endian")
} else {
// ...
}
Run Code Online (Sandbox Code Playgroud)
我import "unsafe"打包转换*int为*byte. 但正如https://golang.org/pkg/unsafe/ 中提到的:
包 unsafe 包含绕过 Go 程序类型安全的操作。
导入不安全的包可能是不可移植的,不受 Go 1 兼容性指南的保护。
有没有更好的方法来确定字节序,还是我必须使用不安全的包?