This commit is contained in:
kenneth 2022-04-07 17:31:49 +08:00
parent 811536bea4
commit 68e9c75c70
3 changed files with 178 additions and 1 deletions

View File

@ -1,7 +1,69 @@
package conver
import "strconv"
import (
"fmt"
"strconv"
"strings"
)
func FloatToString(val float64) string {
return strconv.FormatFloat(val, 'f', 1, 64)
}
func SliceIntToString(num []int64) string {
result := ""
if len(num) > 0 {
for _, item := range num {
result = fmt.Sprintf("%s,%d", result, item)
}
if !strings.HasSuffix(result, ",") {
result = fmt.Sprintf("%s,", result)
}
}
return result
}
func SliceStringToString(num []string) string {
result := ""
if len(num) > 0 {
for _, item := range num {
result = fmt.Sprintf("%s,%s", result, item)
}
if !strings.HasSuffix(result, ",") {
result = fmt.Sprintf("%s,", result)
}
}
return result
}
func StringToSlice(num string) []string {
var result []string
if len(num) > 0 {
result = strings.Split(num, ",")
}
return result
}
func StringToSliceInt(num string) []int64 {
var result []int64
if len(num) > 0 {
array := strings.Split(num, ",")
if len(array) > 0 {
for _, item := range array {
i, err := strconv.Atoi(item)
if err == nil {
result = append(result, int64(i))
}
}
}
}
return result
}

View File

@ -0,0 +1,55 @@
package snowflake
import (
"errors"
"sync"
"time"
)
const (
workerBits uint8 = 10
numberBits uint8 = 12
workerMax int64 = -1 ^ (-1 << workerBits)
numberMax int64 = -1 ^ (-1 << numberBits)
timeShift uint8 = workerBits + numberBits
workerShift uint8 = numberBits
startTime int64 = 1262275200000 // 如果在程序跑了一段时间修改了epoch这个值 可能会导致生成相同的ID (开始时间-毫秒)
)
type Worker struct {
mu sync.Mutex
timestamp int64
workerId int64
number int64
}
func NewWorker(workerId int64) (*Worker, error) {
if workerId < 0 || workerId > workerMax {
return nil, errors.New("worker id excess of quantity")
}
// 生成一个新节点
return &Worker{
timestamp: 0,
workerId: workerId,
number: 0,
}, nil
}
func (w *Worker) GetId() int64 {
w.mu.Lock()
defer w.mu.Unlock()
now := time.Now().UnixNano() / 1e6
if w.timestamp == now {
w.number++
if w.number > numberMax {
for now <= w.timestamp {
now = time.Now().UnixNano() / 1e6
}
}
} else {
w.number = 0
w.timestamp = now
}
ID := int64((now-startTime)<<timeShift | (w.workerId << workerShift) | (w.number))
return ID
}

View File

@ -1,7 +1,9 @@
package st
import (
"encoding/json"
"regexp"
"strconv"
"strings"
)
@ -52,3 +54,61 @@ func RemoveHTML(str string) string {
}
return ""
}
// Strval 获取变量的字符串值
// 浮点型 3.0将会转换成字符串3, "3"
// 非数值或字符类型的变量将会被转换成JSON格式字符串
func Strval(value interface{}) string {
// 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
}