package lib import ( "encoding/json" "github.com/nats-io/nats.go" "github.com/thinkeridea/go-extend/exunicode/exutf8" "runtime" "strconv" "unicode/utf8" ) var Nats *nats.Conn func To_int64(value interface{}) int64 { var key int64 if value == nil { return key } switch value.(type) { case float64: key = int64(value.(float64)) case float32: key = int64(value.(float32)) case int: key = int64(value.(int)) case uint: key = int64(value.(uint)) case int8: key = int64(value.(int8)) case uint8: key = int64(value.(uint8)) case int16: key = int64(value.(int16)) case uint16: key = int64(value.(uint16)) case int32: key = int64(value.(int32)) case uint32: key = int64(value.(uint32)) case int64: key = int64(value.(int64)) case uint64: key = int64(value.(uint64)) case string: key, _ = strconv.ParseInt(value.(string), 10, 64) case []byte: key, _ = strconv.ParseInt(string(value.([]byte)), 10, 64) default: newValue, _ := json.Marshal(value) key, _ = strconv.ParseInt(string(newValue), 10, 64) } return key } func To_string(value interface{}) string { var key string if value == nil { return key } switch value.(type) { case float64: ft := value.(float64) key = strconv.FormatFloat(ft, 'f', -1, 64) case float32: ft := value.(float32) key = strconv.FormatFloat(float64(ft), 'f', -1, 64) case int: it := value.(int) key = strconv.Itoa(it) case uint: it := value.(uint) key = strconv.Itoa(int(it)) case int8: it := value.(int8) key = strconv.Itoa(int(it)) case uint8: it := value.(uint8) key = strconv.Itoa(int(it)) case int16: it := value.(int16) key = strconv.Itoa(int(it)) case uint16: it := value.(uint16) key = strconv.Itoa(int(it)) case int32: it := value.(int32) key = strconv.Itoa(int(it)) case uint32: it := value.(uint32) key = strconv.Itoa(int(it)) case int64: it := value.(int64) key = strconv.FormatInt(it, 10) case uint64: it := value.(uint64) key = strconv.FormatUint(it, 10) case string: key = value.(string) case []byte: key = string(value.([]byte)) default: newValue, _ := json.Marshal(value) key = string(newValue) } return key } // 获取正在运行的函数名 func FuncName() string { pc := make([]uintptr, 1) runtime.Callers(2, pc) f := runtime.FuncForPC(pc[0]) return f.Name() } func Limit_len(str string,lenx int) string { if utf8.RuneCountInString(str) > lenx { return exutf8.RuneSubString(str, 0, lenx-3)+"..." } return exutf8.RuneSubString(str, 0, lenx) }