package lib import ( "encoding/json" "github.com/nats-io/nats.go" "runtime" "strconv" "strings" ) var Nats *nats.Conn type JSONS struct { //必须的大写开头 Code int16 Msg string Data interface{} // 泛型 } 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 To_int(value interface{}) int { var key int if value == nil { return key } switch value.(type) { case float64: key = int(value.(float64)) case float32: key = int(value.(float32)) case int: key = int(value.(int)) case uint: key = int(value.(uint)) case int8: key = int(value.(int8)) case uint8: key = int(value.(uint8)) case int16: key = int(value.(int16)) case uint16: key = int(value.(uint16)) case int32: key = int(value.(int32)) case uint32: key = int(value.(uint32)) case int64: key = int(value.(int64)) case uint64: key = int(value.(uint64)) case string: key, _ = strconv.Atoi(value.(string)) case []byte: key, _ = strconv.Atoi(string(value.([]byte))) default: newValue, _ := json.Marshal(value) key, _ = strconv.Atoi(string(newValue)) } return key } // 取文本(字符串)中间 func GetBetweenStr(str, start, end string) string { n := strings.Index(str, start) if n == -1 { n = 0 } else { n = n + len(start) // 增加了else,不加的会把start带上 } str = string([]byte(str)[n:]) m := strings.Index(str, end) if m == -1 { m = len(str) } str = string([]byte(str)[:m]) return str } // 获取正在运行的函数名 func FuncName() string { pc := make([]uintptr, 1) runtime.Callers(2, pc) f := runtime.FuncForPC(pc[0]) return f.Name() }