Esempio n. 1
0
//对数据进行编码
func (this *Client) encoding(value interface{}, hasArray bool) string {
	switch t := value.(type) {
	case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, complex64, complex128:
		return to.String(t)
	case string: //byte==uint8
		return t
	case []byte:
		return string(t)
	case bool:
		if t {
			return "1"
		} else {
			return "0"
		}
	case nil:
		return ""
	case []bool, []string, []int, []int8, []int16, []int32, []int64, []uint, []uint16, []uint32, []uint64, []float32, []float64, []interface{}:
		if hasArray && Encoding {
			if bs, err := json.Marshal(value); err == nil {
				return string(bs)
			}
		}
		return "can not support slice,please open the Encoding options"
	default:
		if Encoding {
			if bs, err := json.Marshal(value); err == nil {
				return string(bs)
			}
		}
		return "not open Encoding options"
	}
}
Esempio n. 2
0
// NewIDWorkder 构建一个新的 IDWorker,worker ID 必须是千位的数值
//
func NewIDWorkder(id int) (*IDWorkder, error) {
	if id < 1000 || id > 9999 {
		return nil, errors.New("worker ID 超出范围")
	}

	worker := &IDWorkder{
		workerID:      to.String(id),
		lastTimestamp: 0,
		sequence:      1,
	}
	return worker, nil
}
Esempio n. 3
0
func formatUnixTimestamp(ts int64) string {
	sec := ts / 1000
	mspart := to.String(ts - sec*1000)
	return time.Unix(sec, 0).Format("20060102150405") + strings.Repeat("0", 3-len(mspart)) + mspart
}
Esempio n. 4
0
// formatSequence 格式化序号,长度不够的,补零
//
func formatSequence(num int64) string {
	numStr := to.String(num)
	return strings.Repeat("0", _SEQUENCE_STR_SIZE-len(numStr)) + numStr
}