package rpc import "bytes" type Vars map[string]string func NewVars() Vars { return make(Vars) } func (vs Vars) MarshalBinary() ([]byte, error) { var buf bytes.Buffer for k, v := range vs { buf.WriteString(k) buf.WriteByte(byte(0)) buf.Write(varLen(v)) buf.WriteString(v) buf.WriteByte(byte(0)) } return buf.Bytes(), nil } func (vs *Vars) UnmarshalBinary(data []byte) error { return nil } func varLen(v string) (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 */ sl := len(v) l = make([]byte, 4) l[0] = byte((sl / 0x1) % 0x100) l[1] = byte((sl / 0x100) % 0x100) l[2] = byte((sl / 0x10000) % 0x100) l[3] = byte((sl / 0x1000000) % 0x100) return l }