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: Write() struct int type no output. #13156

Closed
codinl opened this issue Nov 5, 2015 · 2 comments
Closed

encoding/binary: Write() struct int type no output. #13156

codinl opened this issue Nov 5, 2015 · 2 comments

Comments

@codinl
Copy link

codinl commented Nov 5, 2015

// right:int32 is ok
type T struct {
    A int32
}

// wrong:int has no output,and no error
type T struct {
    A int
}

func main() {
    buf := new(bytes.Buffer)
    t := T{1}
    err := binary.Write(buf, binary.BigEndian, t)
    if err != nil {
        panic(err)
    }
    fmt.Println("buf.Bytes()=", buf.Bytes())   // if t.A is "int" but not "int32" type, here no out put.
}
@ianlancetaylor ianlancetaylor changed the title binary.Write() struct int type no output. encoding/binary: Write() struct int type no output. Nov 5, 2015
@ianlancetaylor
Copy link
Contributor

I get an error with this program: http://play.golang.org/p/1ssllTW1H- .

If you still think there is a bug, please provide a complete standalone program and show the output you get.

@dominikh
Copy link
Member

dominikh commented Nov 5, 2015

What version of Go are you on? Here (1.5.1), it does return an error: binary.Write: invalid type main.T. It's invalid because encoding/binary is for fixed size data, and int isn't fixed size: it can be 32 or 64 bits wide, depending on the architecture.

See also http://play.golang.org/p/BoCMunGSHA for the error.

@golang golang locked and limited conversation to collaborators Nov 4, 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

4 participants