Skip to content

Instantly share code, notes, and snippets.

@raspi
Created March 21, 2021 03:12
Show Gist options
  • Save raspi/360811bd32eba0ac40e834d85ab1c0f3 to your computer and use it in GitHub Desktop.
Save raspi/360811bd32eba0ac40e834d85ab1c0f3 to your computer and use it in GitHub Desktop.
var nativeEndian binary.ByteOrder
func init() {
buf := [2]byte{}
*(*uint16)(unsafe.Pointer(&buf[0])) = uint16(0xABCD)
switch buf {
case [2]byte{0xCD, 0xAB}:
nativeEndian = binary.LittleEndian
case [2]byte{0xAB, 0xCD}:
nativeEndian = 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