Skip to content

Commit

Permalink
Add more runnable examples
Browse files Browse the repository at this point in the history
Add ExampleLittleEqual.
Add examples which run on amd64.
  • Loading branch information
dolmen committed Apr 24, 2024
1 parent bce09fa commit 70a2f8d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
15 changes: 14 additions & 1 deletion example_amd64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
package endian_test

import (
"encoding/binary"
"fmt"
"runtime"

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

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

const n = 0xDeadBeef
Expand All @@ -21,3 +22,15 @@ func Example() {
// GOARCH=amd64: LittleEndian
// 0xdeadbeef => [ef be ad de]
}

func ExampleLittleEqual() {
for _, bo := range []binary.ByteOrder{
binary.BigEndian,
binary.LittleEndian,
} {
fmt.Println("endian.Native ==", bo, "=>", endian.Native == bo)
}
// Output:
// endian.Native == BigEndian => false
// endian.Native == LittleEndian => true
}
36 changes: 36 additions & 0 deletions example_arm64_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//go:build arm64 && !generate
// +build arm64,!generate

package endian_test

import (
"encoding/binary"
"fmt"
"runtime"

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

func ExampleLittle() {
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=arm64: LittleEndian
// 0xdeadbeef => [ef be ad de]
}

func ExampleLittleEqual() {
for _, bo := range []binary.ByteOrder{
binary.BigEndian,
binary.LittleEndian,
} {
fmt.Println("endian.Native ==", bo, "=>", endian.Native == bo)
}
// Output:
// endian.Native == BigEndian => false
// endian.Native == LittleEndian => true
}

0 comments on commit 70a2f8d

Please sign in to comment.