I want to read and write binary data to file and my data is simply slice. The encoding part is working, but my decoding via binary.Read gives zero result. What did I do wrong?
data := []int16{1, 2, 3}
buf := new(bytes.Buffer)
err := binary.Write(buf, binary.LittleEndian, data)
if err != nil {
fmt.Println("binary.Write failed:", err)
}
fmt.Println(buf.Bytes())
// working up to this point
r := bytes.NewReader(buf.Bytes())
got := []int16{}
if err := binary.Read(r, binary.LittleEndian, &got); err != nil {
fmt.Println("binary.Read failed:")
}
fmt.Println("got:", got)
Running this code gives
[1 0 2 0 3 0]
got: []
The playground link is here: https://go.dev/play/p/yZOkwXj8BNv