Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encoding/binary: can't compare NativeEndian with LittleEndian/BigEndian #63611

Open
dolmen opened this issue Oct 18, 2023 · 3 comments
Open

encoding/binary: can't compare NativeEndian with LittleEndian/BigEndian #63611

dolmen opened this issue Oct 18, 2023 · 3 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@dolmen
Copy link
Contributor

dolmen commented Oct 18, 2023

TL;DR: binary.NativeEndian == binary.BigEndian doesn't work.

Follow-up on #57237.

What version of Go are you using (go version)?

$ go version
go version go1.21.3 darwin/amd64

Does this issue reproduce with the latest release?

Yes.

What did you do?

https://go.dev/play/p/PIdcmG89Rp-

package main

import (
	"encoding/binary"
	"fmt"
)

func main() {
	fmt.Println(binary.NativeEndian == binary.BigEndian)
}

What did you expect to see?

I expect this program to compile and to allow me to simply determine if the platform is big endian or little endian as a boolean value.

As the author of package github.com/dolmen-go/endian which provided Native long before and has this feature, I miss that.

What did you see instead?

./prog.go:9:37: invalid operation: binary.NativeEndian == binary.BigEndian (mismatched types binary.nativeEndian and binary.bigEndian)
@bcmills
Copy link
Contributor

bcmills commented Oct 18, 2023

Programs generally shouldn't need to care which endianness is native, especially if they are already using encoding/binary. But for the rare ones that do, how about golang.org/x/sys/cpu.IsBigEndian?

@rittneje
Copy link

From what I recall, this is an artifact of the fact that binary.LittleEndian and binary.BigEndian are not comparable with each other. So if for example binary.NativeEndian were an alias of binary.LittleEndian on a little endian system, then binary.NativeEndian == binary.LittleEndian would return true on little endian and fail to compile on big endian. The only way around that would have been to make binary.NativeEndian an interface, preventing some compile-time optimizations.

I'll note your library has the compilation issue I mentioned, so I don't think it can be used in the way you intended. https://go.dev/play/p/NuQV5ijf5PG

@cagedmantis cagedmantis added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Oct 20, 2023
@cagedmantis cagedmantis added this to the Backlog milestone Oct 20, 2023
@cagedmantis
Copy link
Contributor

cc @griesemer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants