package rpc import "bytes" type Var struct { Key string Value string } func (v *Var) MarshalBinary() ([]byte, error) { var buf bytes.Buffer buf.WriteString(v.Key) buf.WriteByte(byte(0)) buf.Write(v.Len()[0:4]) buf.WriteString(v.Value) buf.WriteByte(byte(0)) return buf.Bytes(), nil } func (v *Var) UnmarshalBinary(data []byte) error { return nil } func (v *Var) Len() (l []byte) { /* Using a slice instead of [4]byte the wasted 2 bytes are better than the more difficult to read read/write calls, and the extra copy operations that would be needed anyway, which waste 6 bytes at a time */ l = make([]byte, 4) l[0] = byte((len(v.Value) / 0x1) % 0x100) l[1] = byte((len(v.Value) / 0x100) % 0x100) l[2] = byte((len(v.Value) / 0x10000) % 0x100) l[3] = byte((len(v.Value) / 0x1000000) % 0x100) return l }