func (w *Writer) getType(obj interface{}, length int) []byte {
	w.checkLength(length)
	var tmp = make([]byte, 8)
	switch objType := obj.(type) {
	case int8:
		tmp[0] = byte(objType)
	case uint8:
		tmp[0] = byte(objType)
	case int16:
		tmp[0] = byte(objType)
		tmp[1] = byte(objType >> 8)
	case uint16:
		tmp[0] = byte(objType)
		tmp[1] = byte(objType >> 8)
	case int32:
		tmp[0] = byte(objType)
		tmp[1] = byte(objType >> 8)
		tmp[2] = byte(objType >> 16)
		tmp[3] = byte(objType >> 24)
	case uint32:
		tmp[0] = byte(objType)
		tmp[1] = byte(objType >> 8)
		tmp[2] = byte(objType >> 16)
		tmp[3] = byte(objType >> 24)
	case int:
		tmp[0] = byte(objType)
		tmp[1] = byte(objType >> 8)
		tmp[2] = byte(objType >> 16)
		tmp[3] = byte(objType >> 24)
	case int64:
		tmp[0] = byte(objType)
		tmp[1] = byte(objType >> 8)
		tmp[2] = byte(objType >> 16)
		tmp[3] = byte(objType >> 24)
		tmp[4] = byte(objType >> 32)
		tmp[5] = byte(objType >> 40)
		tmp[6] = byte(objType >> 48)
		tmp[7] = byte(objType >> 56)
	case uint64:
		tmp[0] = byte(objType)
		tmp[1] = byte(objType >> 8)
		tmp[2] = byte(objType >> 16)
		tmp[3] = byte(objType >> 24)
		tmp[4] = byte(objType >> 32)
		tmp[5] = byte(objType >> 40)
		tmp[6] = byte(objType >> 48)
		tmp[7] = byte(objType >> 56)
	default:
		log.Error("Unknown data type:", reflect.TypeOf(obj))
		return nil
	}
	return tmp[:length]
}
Attempts to read specified interface type and serializes it into byte array.
It has a length parameter, which tells the required size of interface type.
If interface type length is smaller or higher than required, the correct
length will be returned
package network
import (
	"reflect"
	"share/encryption"
	"share/log"
)
const DEFAULT_BUFFER_SIZE = 1024
type Writer struct {
	buffer []byte
	index  int
	Type int
}
func NewWriter(code uint16, magic ...uint16) *Writer {
	var w = &Writer{}
	w.buffer = make([]byte, DEFAULT_BUFFER_SIZE)
	w.index = 0
	if magic == nil {
		w.WriteUint16(encryption.MagicKey)
	} else {
		w.WriteUint16(magic[0])
	}
	w.WriteUint16(0x00)  
	w.WriteUint16(code)  
	w.Type = int(code)
	return w
}
func (w *Writer) checkLength(length int) {
	if len(w.buffer) < w.index+length {
		var tmp = make([]byte, len(w.buffer)+DEFAULT_BUFFER_SIZE)
		copy(tmp[:len(w.buffer)], w.buffer)
		w.buffer = tmp
		w.checkLength(length)
	}
}