OK, thanks to everyone for your help. Today, what I am doing (and
which works) is:
import ("bytes"; "encoding/binary";)
...
type ProtocolHeader struct {
Id uint16;
Misc uint16;
Foo, Bar uint16;
}
...
var (
packet ProtocolHeader;
)
message := make([]byte, 1024);
n, remaddr, error := listener.ReadFrom(data);
buf := bytes.NewBuffer(data[0:n]);
binary.Read(buf, binary.BigEndian, &packet);
fmt.Printf("ID is %d\n", packet.Id);
and it works, the struct of type ProtocolHeader is correctly filled
in.
Is there a way in Go to have fields which are not multiple of a byte,
for instance 1-bit or 4bits and to use encoding/binary with them? It
does not seem so, I believe I'll have to read in a uint16 then play
with >> and && to extract my fields.