Bit fields / binary parsing.

2,787 views
Skip to first unread message

Alexey Veselovsky

unread,
May 26, 2010, 4:38:35 PM5/26/10
to golang-nuts
For easy "parsing" binary protocols in C we can use structs with
bitfields. It is very easy and obviously. (if we can tell compiler
about data alignment).

In erlang there is binary's and bit syntax
http://erlang.org/doc/reference_manual/expressions.html#bit_syntax
http://erlang.org/doc/reference_manual/data_types.html#id2257844 with
pattern patching and so on. This is even better then bitfields in C.

What we have in Go? There are any plans about it?

Ian Lance Taylor

unread,
May 26, 2010, 6:11:25 PM5/26/10
to Alexey Veselovsky, golang-nuts
Alexey Veselovsky <alexey.v...@gmail.com> writes:

> For easy "parsing" binary protocols in C we can use structs with
> bitfields. It is very easy and obviously. (if we can tell compiler
> about data alignment).

But note that such code is not portable, because on different
processors the bitfields start at different ends of the byte. Also
different ABIs have different rules as to how bitfields which cross
byte or word boundaries are handled.


> In erlang there is binary's and bit syntax
> http://erlang.org/doc/reference_manual/expressions.html#bit_syntax
> http://erlang.org/doc/reference_manual/data_types.html#id2257844 with
> pattern patching and so on. This is even better then bitfields in C.
>
> What we have in Go? There are any plans about it?

There are no current plans for struct bitfields in Go.

Ian

Reply all
Reply to author
Forward
0 new messages