<p>I recently ran into the <a href="https://github.com/google/gopacket/issues/84" rel="nofollow">problem of needing to convert between network byte order and host byte order</a>. Searching around I found <a href="https://groups.google.com/forum/#!topic/golang-nuts/3GEzwKfRRQw" rel="nofollow">this mailing list post</a> from 2011 proposing that Go have the concept of 'native' endianess. That seems like a pretty reasonable suggestion to me, but met with some pretty stiff resistance back then. What do people think in 2015?</p>
<hr/>**评论:**<br/><br/>pappogeomys: <pre><p>No. <a href="http://commandcenter.blogspot.com/2012/04/byte-order-fallacy.html" rel="nofollow">http://commandcenter.blogspot.com/2012/04/byte-order-fallacy.html</a></p></pre>TheMerovius: <pre><p>I always agreed with everything in the article, but it still doesn't make a very good argument.</p>
<blockquote>
<p>And if the native byte order really does matter to the execution of the
program, it's almost certain to be dealing with some external software that
is either wrong or misguided</p>
</blockquote>
<p>While that is certainly true, I might still want to be able to deal with external software, that is either wrong or misguided. For example the i3 IPC interface left the byte order intentionally unspecified. Of course that was a mistake, of course it was misguided, but that's how it is and I still want to communicate with i3 using go.</p>
<p>That being said, a native byte order can easily be implemented as a separate package, with build-flags for different architectures. But I still find it crazy, that this is not simply provided by the binary package.</p></pre>[deleted]: <pre><p>What is missing from the binary encoding package? </p></pre>TheMerovius: <pre><p>I am confused by the question. I'd go with "A NativeEndian variable". But then, that's the title of the discussion. So yes, the question confuses me.</p>
<p>I hope we can agree, that encoding/binary gives you no way to read "A uint32 in the byte order of the machine, the code is running on" from a socket (or a file, or whatever). So that is, what is missing.</p>
<p>The question was: Should there be such a way. My answer is "yes, go needs such a way. Though admittedly, that doesn't have to be in the stdlib, necessarily".You can agree or disagree with my reasoning, but… meh. The question confuses me :)</p></pre>[deleted]: <pre><p>encoding/binary gives you big endian and little endian encoders/decoders, use a built tag to choose which one you need.</p></pre>TheMerovius: <pre><p>That's what I mean by "it can easily be implemented as a separate package". I don't think every programmer should need to concern themselves with what architectures need what endianness (especially as there are architectures that don't have a compile-time-known endianness).</p></pre>binaryblade: <pre><p>The encoding package sorts this out for you.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传