endian

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 6, 2022 License: Apache-2.0 Imports: 1 Imported by: 1

README

endian.Native - A single constant that you should not use

GoDoc Travis-CI Go Report Card

WARNING!

You should probably mot use this package!

Read this article by Rob Pike before using this package:

https://commandcenter.blogspot.fr/2012/04/byte-order-fallacy.html

You should probably first try to fix the broken code that generates data dependent on the architecture on which it is compiled. Fix it so it always generates code using a fixed byte order. If you can't, see if you can detect the byte order of the data from the data itself.

And if you ask yourself why I created this package if no one should use it, this is because I wanted to play with build tags and go:generate.

Usage

This package only exports a single variable containing the byte order of GOARCH.

See the encoding/binary package for how to use it to read/write a binary data stream.

package endian

import "encoding/binary"

var Native binary.ByteOrder

The implementation is ultra lightweight because it relies on build tags.

License

Copyright 2016-2017 Olivier Mengué

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Documentation

Overview

Package endian exposes the native byte order of GOARCH.

You should not use this package before reading and understanding this article by Rob Pike: https://commandcenter.blogspot.fr/2012/04/byte-order-fallacy.html

This package could be useful ONLY to workaround bugs in other software.

Note: reading godoc on one particular platform may be misleading. Check this:

GOARCH=amd64   godoc github.com/dolmen-go/endian
GOARCH=mips64  godoc github.com/dolmen-go/endian
GOARCH=unknown godoc github.com/dolmen-go/endian
Example
package main

import (
	"fmt"
	"runtime"

	"github.com/dolmen-go/endian"
)

func main() {
	fmt.Printf("GOARCH=%s: %s\n", runtime.GOARCH, endian.Native)

	const n = 0xDeadBeef
	var b [4]byte
	endian.Native.PutUint32(b[:], n)
	fmt.Printf("0x%x => [% x]\n", n, b)
}
Output:

GOARCH=amd64: LittleEndian
0xdeadbeef => [ef be ad de]

Index

Examples

Constants

This section is empty.

Variables

Native is the byte order of GOARCH.

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL