http://play.golang.org/p/RQXB-hCq_M
type Header struct {
ByteField1 uint32 // 4 bytes
ByteField2 [32]uint8 // 32 bytes
ByteField3 [32]uint8 // 32 bytes
SkipField1 []SomethingElse
}
func main() {
var header Header
headerBytes := make([]byte, 68) // 4 + 32 + 32 == 68
headerBuf := bytes.NewBuffer(headerBytes)
err := binary.Read(headerBuf, binary.LittleEndian, &header)
if err != nil {
fmt.Println(err)
}
fmt.Println(header)
}
I don't want to read from the buffer into the header struct in chunks. I want to read into the bytefield in one step but skip non byte fields. If you run the program in the given link (http://play.golang.org/p/RQXB-hCq_M) you will find that binary.Read to throw an error: binary.Read: invalid type []main.SomethingElse
Is there a way that I can skip this field?
Update: Based on dommage's answer, I decided to embed the fields inside the struct instead like this http://play.golang.org/p/i0xfmnPx4A
You can cause a field to be skipped by prefixing it's name with _ (underscore).
But: binary.Read()
requires all fields to have a known size. If SkipField1
is of variable or unknown length then you have to leave it out of your struct.
You could then use io.Reader.Read()
to manually skip over the skip field portion of your input and then call binary.Read()
again.