import (
"fmt"
"os"
"encoding/binary"
)
type oinfo struct {
a int
b byte
c int
}
type pa [9]byte
func (p pa) Read(b []byte) (n int, err os.Error) {
for i:=byte(0); i<9; i++ {
p[i] = i
}
fmt.Println(p)
n = copy(b, p[:])
return n, nil
}
func main() {
fmt.Printf("hello world\n")
var p pa
var v oinfo
err := binary.Read(p, binary.LittleEndian, &v)
if err != nil {
panic(err)
}
fmt.Println(v)
}--
-Daniel
From the pkg documentation: "Data must be a pointer to a fixed-size
value or a slice of fixed-size values. A fixed-size value is either a
fixed-size arithmetic type (int8, uint8, int16, float32, complex64,
...) or an array or struct containing only fixed-size values."
int is not a fixed size value, so oinfo is not a valid data argument.
The program should work if you define a and c as int32. However, you
will also need to export the fields or reflect will panic. If you use
this oinfo struct the program finishes correctly:
type oinfo struct {
A int32
B byte
C int32
}
It should probably be mentioned in the documentation that, if data is
a struct, its fields needs to pass the reflect.CanSet test or the
program will panic.
--
- yiyus || JGL .