3 Commits

Author SHA1 Message Date
  gz 1451ae0148 修改GetDomain 2 weeks ago
  guzeng c824e5a723 增加MAP合并方法 5 months ago
  guzeng 814bdf7d29 增加雪花算法实现方法 8 months ago
7 changed files with 135 additions and 5 deletions
Split View
  1. +1
    -1
      header.go
  2. +30
    -4
      map.go
  3. +9
    -0
      map_test.go
  4. +62
    -0
      snowflake.go
  5. +16
    -0
      snowflake_test.go
  6. +5
    -0
      time_test.go
  7. +12
    -0
      uuid_test.go

+ 1
- 1
header.go View File

@ -44,7 +44,7 @@ func GetDomain(req *http.Request) string {
var host string = GetHost(req)
hosts := strings.Split(host, ":")
host = hosts[0]
if hosts[1] == "443" {
if len(hosts) > 1 && hosts[1] == "443" {
scheme = "https://"
}
var w strings.Builder


+ 30
- 4
map.go View File

@ -95,7 +95,7 @@ func MapStringToInterface(src map[string]string) map[string]interface{} {
return target
}
//将interface转成map[string]interface
// 将interface转成map[string]interface
func InterfaceToMapInterface(data interface{}) (map[string]interface{}, error) {
dataJson, err := json.Marshal(data)
@ -109,7 +109,7 @@ func InterfaceToMapInterface(data interface{}) (map[string]interface{}, error) {
return returnData, nil
}
//将interface转成map[string]string
// 将interface转成map[string]string
func InterfaceToMapString(data interface{}) (map[string]string, error) {
dataJson, err := json.Marshal(data)
@ -123,7 +123,7 @@ func InterfaceToMapString(data interface{}) (map[string]string, error) {
return returnData, nil
}
//将interface转成[]map[string]interface
// 将interface转成[]map[string]interface
func InterfaceToMapInterfaceArr(data interface{}) ([]map[string]interface{}, error) {
dataJson, err := json.Marshal(data)
@ -137,7 +137,7 @@ func InterfaceToMapInterfaceArr(data interface{}) ([]map[string]interface{}, err
return returnData, nil
}
//将interface转成[]map[string]string
// 将interface转成[]map[string]string
func InterfaceToMapStringArr(data interface{}) ([]map[string]string, error) {
dataJson, err := json.Marshal(data)
@ -292,3 +292,29 @@ func SetMapFirstToUpper(info map[string]interface{}) map[string]interface{} {
}
return ret
}
/**
* MAP合并相同KEY以最后一个为准
* gz 2023/11/13
*/
func MergeMaps(map1 map[string]string, map2 ...map[string]string) map[string]string {
mergedMap := make(map[string]string)
if len(map1) > 0 {
for key, value := range map1 {
mergedMap[key] = value
}
}
if len(map2) > 0 {
for _, arr := range map2 {
if len(arr) > 0 {
for key, value := range arr {
mergedMap[key] = value //以后个为准
}
}
}
}
return mergedMap
}

+ 9
- 0
map_test.go View File

@ -28,3 +28,12 @@ func Test_SetMapFirstToUpper(t *testing.T) {
ret := SetMapFirstToUpper(a)
t.Log(ret)
}
func Test_MergeMaps(t *testing.T) {
a := map[string]string{"a": "1", "b": "2", "c": "3"}
b := map[string]string{"e": "5", "f": "6", "g": "7"}
// c := map[string]string{"a": "0", "h": "8", "f": "10"}
ret := MergeMaps(a, b)
t.Log(ret)
}

+ 62
- 0
snowflake.go View File

@ -0,0 +1,62 @@
package helper
import (
"sync"
"time"
)
// Snowflake 结构体
type Snowflake struct {
mu sync.Mutex
startTime int64 // 起始时间戳,可以根据实际需求设置
machineID int64 // 机器ID
sequenceNum int64 // 序列号
}
// NewSnowflake 创建一个Snowflake实例
func NewSnowflake(machineID int64) *Snowflake {
return &Snowflake{
startTime: getTimeStamp(),
machineID: machineID,
sequenceNum: 0,
}
}
// Generate 生成一个唯一ID
func (s *Snowflake) Generate() int64 {
s.mu.Lock()
defer s.mu.Unlock()
currentTime := getTimeStamp()
// 如果当前时间小于上一次生成ID的时间,说明时钟回拨,需要等待
if currentTime < s.startTime {
time.Sleep(time.Duration(s.startTime - currentTime))
currentTime = getTimeStamp()
}
// 如果是同一毫秒内生成的ID,需要增加序列号
if currentTime == s.startTime {
s.sequenceNum++
} else {
s.sequenceNum = 0
s.startTime = currentTime
}
// 如果序列号超过了12位的最大值,等待下一毫秒再生成ID
if s.sequenceNum >= 1<<12 {
time.Sleep(time.Millisecond)
s.startTime = getTimeStamp()
s.sequenceNum = 0
}
// 生成ID
id := (currentTime-s.startTime)<<22 | (s.machineID << 12) | s.sequenceNum
return id
}
// 获取当前时间戳(毫秒级)
func getTimeStamp() int64 {
return time.Now().UnixNano() / int64(time.Millisecond)
}

+ 16
- 0
snowflake_test.go View File

@ -0,0 +1,16 @@
package helper
import (
"testing"
)
func Test_snowflake(t *testing.T) {
// 创建一个Snowflake实例
snowflake := NewSnowflake(169379266710)
// 生成10个唯一ID并输出
for i := 0; i < 10; i++ {
id := snowflake.Generate()
t.Log(id)
}
}

+ 5
- 0
time_test.go View File

@ -40,3 +40,8 @@ func Test_GetTodayStartTimeStamp(t *testing.T) {
t.Log(reserveTime.Unix())
}
func Test_DatetimeToUnix(t *testing.T) {
date := "2023-09-01 00:00:00"
t.Log(DatetimeToUnix(date))
}

+ 12
- 0
uuid_test.go View File

@ -0,0 +1,12 @@
package helper
import (
"testing"
)
func Test_GenerateUUID(t *testing.T) {
uuid, err := GenerateUUID()
t.Log(uuid)
t.Log(err)
}

Loading…
Cancel
Save