binary_386.gobinary_amd64.gobinary_arm.go
package binaryvar NativeEndian littleEndian
Yes, I would also like this.
> I have encountered the need for this multiple times. The only other
> suggestion as been to use unsafe.Unreflect.
I'm curious how that would work. gosndfile has:
func isLittleEndian() bool {
var i int32 = 0x01020304
u := unsafe.Pointer(&i)
pb := (*byte)(u)
b := *pb
return (b == 0x04)
}
--
matt kane's brain
http://hydrogenproject.com
Russ
People keep suggesting this, and always for talking
to the local kernel. Isn't that what data structures are for?
The reality is there are good programs and bad programs.
Go doesn't have to make it easy to write bad programs.
> It is sad Go cannot handle the third case without unsafe.
Not true. Nothing is stopping you from writing, in *your* code,
var NativeEndian = binary.LittleEndian
We are only objecting to putting that in the standard library.
Russ
The reality is there are good programs and bad programs.Go doesn't have to make it easy to write bad programs.
> It is sad Go cannot handle the third case without unsafe.Not true. Nothing is stopping you from writing, in *your* code,
var NativeEndian = binary.LittleEndian
We are only objecting to putting that in the standard library.
var NativeEndian = binary.LittleEndian
I'd like to see go standard pkg provides the NativeEndian information of the machine the program runs currently, at runtime. Do not require programmers hack this in a "unsafe" way.
--
Aram Hăvărneanu
> But I choose to use Go rather than C. Are you suggesting that my work is different from yours and the OS is important I should not use Go?
>
> Adding the ability to know your native byte order does not mean you must use it. You might never need it, but it will enable Go to be a better and broader systems programming language without damaging the language or the libraries.
I'm torn.
On the one hand, I want to see you able to use Go for your work.
On the other hand, almost every time I hear someone asking what the native byte order is, it's a mistake.
-rob
I am not suggesting anything, I merely enjoy the silence :-).
> Adding the ability to know your native byte order does not mean you must use
> it. You might never need it, but it will enable Go to be a better and
> broader systems programming language without damaging the language or the
> libraries.
But you do, just like in C++ you need to use templates and turing
complete exceptions and every other feature it has. Your code doesn't
live in a vacuum, it interacts with other code. If the code uses
templates and exceptions you have to do the same. You can't stick to
a reasonable C++ subset. If anyone chooses NativeEndian, it forces
any consumer of that code to do the same.
This is exactly what's happening right now, you want NativeEndian for
interacting with some code other people wrote! I believe NativeEndian
is a mistake. For anyone that wants it, using cgo to determine host
endianess is a non issue.
--
Aram Hăvărneanu
var hbo = binary.LittleEndian // hack - we want host byte order!
Put that in a file named byteorder_amd64.go and
it stops being a hack. It need not be in the standard
library.
Russ
As C networking hardware developer in High Performance Computing, I'm torn, too. Go has so brilliantly addressed so many of C/C++'s shortcomings that I face daily that I'm left longing to use it… but I'm stuck with fixed binary interfaces to hardware and the network. (We are fond of OS-bypass networking for the utmost distributed performance, so we see these binary interfaces even in user-space at the application level.)
Based on my experience with binary interfaces, I long for a simple 'Portable Struct' with portable fields and platform-independent memory layout. A 'Portable Struct would have the following properties:
0) only naturally-aligned fixed-sized integer and Portable Struct fields are permitted,
1) the length of the struct is a multiple of its largest field,
2) implicit padding is prohibited by the compiler,
3) the Endianness is specified per struct or per field,
4) the compiler does implicit Endian conversions on read or write.
Such structs are platform-independent on modern byte-addressed machines. They are a formalization of pragmatic tricks used for C structure portability.
The other missing piece for me in Go (without 'unsafe') is the ability to recast data into these structs. Since Portable Structs contain no pointers, they can be constructed arbitrarily without circumventing security. So, a language can safely allow recasting arbitrary pointers to pointers to Portable Structs as long as the memory backing the new Portable Struct did not originally contain any pointer or private field. So, Go could conceivably allow received data in slices to be dynamically reinterpreted as a Portable Struct without a copy, and without resorting to 'unsafe'.
I do respect the neatness of serialization for portable communication in Go (and Plan 9), but for our CPU-bound network-intensive applications, only binary zero-copy interfaces are competitive.
For what it's worth,
--Glenn
I believe converting the endianess was not a performance issue even in
1985, much less today. Unspecified endianess is a mistake. In my
last endeavor I worked on a file system that not only swapped
endianess all the time, but also hashed and compressed all the data,
in real time.
We used to advertise this file system as particularly fast.
--
Aram Hăvărneanu
Not true. Nothing is stopping you from writing, in *your* code,
var NativeEndian = binary.LittleEndian
We are only objecting to putting that in the standard library.So you are suggesting writing non-portable programs? My proposal was not to add the non-portable statement:var NativeEndian = binary.LittleEndian
I think Russ meant to include such declaration in one's foo_thisArch.go andvar NativeEndian = binary.LittleEndian
i think encoding/binary could use some work - the signature
of binary.Read means it can never avoid an allocation, for example.