Skip to content

Commit

Permalink
feat: optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
withchao committed Mar 14, 2024
1 parent e348055 commit 7cc62a7
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ package utils

import (
"encoding/json"
"math/rand"
"reflect"
"sort"
"strconv"
)

// SliceSub a中存在,b中不存在 (a-b)
Expand Down Expand Up @@ -605,3 +607,35 @@ func InitMap[K comparable, V any](val *map[K]V) {
*val = map[K]V{}
}
}

func GetSwitchFromOptions(Options map[string]bool, key string) (result bool) {
if Options == nil {
return true
}
if flag, ok := Options[key]; !ok || flag {
return true
}
return false
}

func SetSwitchFromOptions(options map[string]bool, key string, value bool) {
if options == nil {
options = make(map[string]bool, 5)
}
options[key] = value
}

func StructToJsonString(param any) string {
dataType, _ := json.Marshal(param)
dataString := string(dataType)
return dataString
}

func GetMsgID(sendID string) string {
t := int64ToString(GetCurrentTimestampByNano())
return Md5(t + sendID + int64ToString(rand.Int63n(GetCurrentTimestampByNano())))
}

func int64ToString(i int64) string {
return strconv.FormatInt(i, 10)
}

0 comments on commit 7cc62a7

Please sign in to comment.