<p>I'm working on a library for handling a wire protocol that sends specific messages where elements could be represented by bits in a byte, a fixed byte or word, or even a variable array of bytes. The methods in encoding/binary sounds great for my purpose except they only work with fixed length fields. It is important for my project that each unique message has a specific struct associated with it.</p>
<p>My first approach was to jimmy a struct tag and custom Marshal/Unmarshal methods that handles bit fields and slices via reflect. The prototype works, and could be generalized to a separate library, but I don't want to recreate the wheel. </p>
<p>Does anyone know of an existing library/pattern?</p>
<hr/>**评论:**<br/><br/>connor4312: <pre><p>Protocol buffers and/or msgpack sounds a lot like what you're doing here, you may be able to use those off the shelf if you have control over the other consumer of the protocol.</p></pre>fncypants: <pre><p>Thanks, but my client is the consumer of the wire protocol so I don't get to define the serialization representation. Instead, it is parsing a series of bytes as a fixed structure describing fields that may be bit flags, bytes, words, or an array of those. </p></pre>rchrome: <pre><p>You can write binaries using net/http.</p>
<p>Here is an example where I convert a struct to binary representation:</p>
<p><a href="https://github.com/amitu/rtime/blob/master/src/rtime/boltdb.go#L267-L282" rel="nofollow">https://github.com/amitu/rtime/blob/master/src/rtime/boltdb.go#L267-L282</a></p>
<p>Let me know if you have questions or example is not clear.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传