Skip to content

Instantly share code, notes, and snippets.

@abursavich
Created November 29, 2020 02:49
Show Gist options
  • Save abursavich/a6f257fdaa5f87ac6c998c13135953a0 to your computer and use it in GitHub Desktop.
Save abursavich/a6f257fdaa5f87ac6c998c13135953a0 to your computer and use it in GitHub Desktop.
native endianness
package foo
import (
"encoding/binary"
"unsafe"
)
var NativeEndianV1 = func() binary.ByteOrder {
switch *(*uint32)(unsafe.Pointer(&[4]byte{1, 2, 3, 4})) {
case 0x01020304:
return binary.BigEndian
case 0x04030201:
return binary.LittleEndian
default:
panic("unknown endianness")
}
}()
var NativeEndianV2 = [2]binary.ByteOrder{binary.LittleEndian, binary.BigEndian}[*(*uint16)(unsafe.Pointer(&[2]byte{0, 1}))&1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment