Slide 13
Slide 13 text
Minimal serialization of a struct
Given the struct type Particle:
type Particle struct {
X int16 // bytes: 0, 1
Y int16 // bytes: 2, 3
Z int16 // bytes: 4, 5
RGB [3]byte // bytes: 6, 7, 8
}
Stored in this buffer:
buf := []byte{1, 0, 2, 0, 3, 0, 128, 0, 192}
Get the X value:
func GetX(buf []byte) (n int16) {
data := buf[:2]
n = *(*int16)(unsafe.Pointer(&data[0]))
return
}
Minimal serialization of a struct
type Particle struct {
X int16 // bytes: 0, 1
Y int16 // bytes: 2, 3
Z int16 // bytes: 4, 5
RGB [3]byte // bytes: 6, 7, 8
}
buf := []byte{1, 0, 2, 0, 3, 0, 128, 0, 192}
func GetY(buf []byte) (n int16) {
data := buf[2:4]
n = *(*int16)(unsafe.Pointer(&data[0]))
return
}