As @David said, use the binary.ByteOrder type
package main
import (
"encoding/binary"
"fmt"
)
func main() {
value := make([]byte, 4)
// need to know the byte ordering ahead of time
binary.LittleEndian.PutUint32(value, 0x12345678)
fmt.Printf("%#v
", value)
fmt.Printf("Big Endian Representation, 0x%X
", binary.BigEndian.Uint32(value))
fmt.Printf("Little Endian Representation, 0x%X
", binary.LittleEndian.Uint32(value))
}
Playground link
This will output:
[]byte{0x78, 0x56, 0x34, 0x12}
Big Endian Representation, 0x78563412
Little Endian Representation, 0x12345678
These are the big-endian and little-endian representations on a little-endian server.