Created
November 29, 2020 02:49
-
-
Save abursavich/a6f257fdaa5f87ac6c998c13135953a0 to your computer and use it in GitHub Desktop.
native endianness
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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