Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encoding/binary: can Read []byte in struct directly but cant Read whole struct #10633

Closed
analogic opened this issue Apr 30, 2015 · 1 comment
Closed

Comments

@analogic
Copy link

Hi
i have this code (simplified):

type Section4 struct {
    LocalUse []byte
}

func readSection4(f *os.File, len uint32) (section Section4, err error) {   
   section.LocalUse = make([]byte, len)

   // this will work OK
    err = binary.Read(f, binary.BigEndian, &section.LocalUse)

    // this will end with ERROR: binary.Read: invalid type []uint8
    err = binary.Read(f, binary.BigEndian, &section)
}
@ianlancetaylor
Copy link
Contributor

This is behaving as documented. "Data must be a pointer to a fixed-size value or a slice of fixed-size values." Passing &section.LocalUse is a pointer to a slice of fixed-size values. Passing &section is not a pointer to a fixed-size value, because the struct contains a slice.

@golang golang locked and limited conversation to collaborators Jun 25, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants