123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- 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()
- }
|