Skip to content

Instantly share code, notes, and snippets.

@607011
Created January 28, 2019 09:25
Show Gist options
  • Save 607011/c21d2df896e8504c491b27d62534f646 to your computer and use it in GitHub Desktop.
Save 607011/c21d2df896e8504c491b27d62534f646 to your computer and use it in GitHub Desktop.
Get Endianness
func nativeEndianness() binary.ByteOrder {
buf := [2]byte{}
*(*uint16)(unsafe.Pointer(&buf[0])) = uint16(0xabcd)
switch buf {
case [2]byte{0xcd, 0xab}:
return binary.LittleEndian
case [2]byte{0xab, 0xcd}:
return binary.BigEndian
default:
panic("Could not determine native endianness.")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment