Marshal/Unmarshal struct with bit fields and variable length fields

blov · · 434 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;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&#39;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&#39;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&#39;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

434 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传