Marshal/Unmarshal struct with bit fields and variable length fields

blov · 2017-02-06 13:00:10 · 623 次点击    
这是一个分享于 2017-02-06 13:00:10 的资源,其中的信息可能已经有所发展或是发生改变。

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.

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.

Does anyone know of an existing library/pattern?


评论:

connor4312:

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.

fncypants:

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.

rchrome:

You can write binaries using net/http.

Here is an example where I convert a struct to binary representation:

https://github.com/amitu/rtime/blob/master/src/rtime/boltdb.go#L267-L282

Let me know if you have questions or example is not clear.


入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

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