Skip to content
Snippets Groups Projects
Commit 4b3726e9 authored by cuiweixie's avatar cuiweixie Committed by Gopher Robot
Browse files

encoding/binary: add var NativeEndian

Updates #57237

Change-Id: I149c8b7eeac91b87b5810250f96d48ca87135807
Reviewed-on: https://go-review.googlesource.com/c/go/+/463218


Reviewed-by: default avatarIan Lance Taylor <iant@google.com>
Reviewed-by: default avatarDmitri Shuralyov <dmitshur@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Run-TryBot: xie cui <523516579@qq.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
parent 09267142
No related branches found
No related tags found
No related merge requests found
pkg encoding/binary, var NativeEndian nativeEndian #57237
...@@ -13,6 +13,7 @@ import ( ...@@ -13,6 +13,7 @@ import (
"strings" "strings"
"sync" "sync"
"testing" "testing"
"unsafe"
) )
type Struct struct { type Struct struct {
...@@ -831,3 +832,12 @@ func BenchmarkWriteSlice1000Uint8s(b *testing.B) { ...@@ -831,3 +832,12 @@ func BenchmarkWriteSlice1000Uint8s(b *testing.B) {
Write(w, BigEndian, slice) Write(w, BigEndian, slice)
} }
} }
func TestNativeEndian(t *testing.T) {
const val = 0x12345678
i := uint32(val)
s := unsafe.Slice((*byte)(unsafe.Pointer(&i)), unsafe.Sizeof(i))
if v := NativeEndian.Uint32(s); v != val {
t.Errorf("NativeEndian.Uint32 returned %#x, expected %#x", v, val)
}
}
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build armbe || arm64be || m68k || mips || mips64 || mips64p32 || ppc || ppc64 || s390 || s390x || shbe || sparc || sparc64
package binary
type nativeEndian struct {
bigEndian
}
// NativeEndian is the native-endian implementation of ByteOrder and AppendByteOrder.
var NativeEndian nativeEndian
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build 386 || amd64 || amd64p32 || alpha || arm || arm64 || loong64 || mipsle || mips64le || mips64p32le || nios2 || ppc64le || riscv || riscv64 || sh || wasm
package binary
type nativeEndian struct {
littleEndian
}
// NativeEndian is the native-endian implementation of ByteOrder and AppendByteOrder.
var NativeEndian nativeEndian
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment