4 Commits

13 changed files with 170 additions and 25 deletions
Split View
  1. +1
    -1
      header.go
  2. +30
    -4
      map.go
  3. +9
    -0
      map_test.go
  4. +0
    -0
      math.go
  5. +13
    -10
      math_test.go
  6. +62
    -0
      snowflake.go
  7. +16
    -0
      snowflake_test.go
  8. +7
    -7
      string.go
  9. +7
    -0
      string_test.go
  10. +0
    -0
      system.go
  11. +0
    -0
      time.go
  12. +13
    -3
      time_test.go
  13. +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)
}

+ 0
- 0
math.go View File


+ 13
- 10
math_test.go View File

@ -1,6 +1,7 @@
package helper
import (
"math"
"strings"
"testing"
)
@ -41,16 +42,18 @@ func Test_FloatQuo(t *testing.T) {
str2 := "7"
ret := FloatQuo(str1, str2, 3)
t.Log(ret)
}
func Test_TenToHex(t *testing.T) {
str1 := 10664
ret := TenToHex(str1)
ret = math.Ceil(FloatQuo(4500, 3600))
t.Log(ret)
}
func Test_HexToTen(t *testing.T) {
str := "29B79F0D"
ret, err := HexToTen(str)
t.Log(ret, err)
}
// func Test_TenToHex(t *testing.T) {
// str1 := 10664
// ret := TenToHex(str1)
// t.Log(ret)
// }
// func Test_HexToTen(t *testing.T) {
// str := "29B79F0D"
// ret, err := HexToTen(str)
// t.Log(ret, err)
// }

+ 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)
}
}

+ 7
- 7
string.go View File

@ -31,6 +31,8 @@ func ToString(v interface{}) string {
value = string(v.([]uint8))
// case []byte:
// value = string(v.([]byte))
case time.Time:
value = v.(time.Time).Format("2006-01-02 15:04:05")
case interface{}:
value = v.(string)
case nil:
@ -141,7 +143,7 @@ func ToInt64(inter interface{}) int64 {
return value
}
//生成随机字符串
// 生成随机字符串
func GetRandomString(length int) string {
str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
bytes := []byte(str)
@ -153,7 +155,7 @@ func GetRandomString(length int) string {
return string(result)
}
//生成随机数字
// 生成随机数字
func GetRandomNumber(length int) string {
str := "0123456789"
bytes := []byte(str)
@ -272,7 +274,6 @@ func UrlJoin(host, url string) string {
return StringJoin(host, url)
}
/**
* 去除字符串的html标签
* @2021/10/20
@ -296,12 +297,11 @@ func TrimHtml(src string) string {
return strings.TrimSpace(src)
}
/**
* 腾讯云图片压缩
* @2021/11/19
* @linsen
*/
func TencentCloudImageCompress(imgUrl,ratio string)string{
return imgUrl + "?imageMogr2/thumbnail/"+ ratio +"x/interlace/1"
}
func TencentCloudImageCompress(imgUrl, ratio string) string {
return imgUrl + "?imageMogr2/thumbnail/" + ratio + "x/interlace/1"
}

+ 7
- 0
string_test.go View File

@ -2,6 +2,7 @@ package helper
import (
"fmt"
"strings"
"testing"
)
@ -53,3 +54,9 @@ func Test_ToInt(t *testing.T) {
ret := ToInt(str)
t.Log(ret)
}
func Test_IsInStringArray(t *testing.T) {
var s string = "1,19,37,220,351,466,585,655,801,820,933,1046,1168,1263,1375,1532,1709,1827,1965,1977,1988,2003,2011,2017,2025,2035,2041,2050,2056,2065,2070,2077,2082,2091,2123,2146,2150,2156,2162,2291,2323,2367,2572,2670,2816,2898,3022,3126,3178,3206,3325,3716,3738"
arr := strings.Split(s, ",")
t.Log(IsInStringArray(arr, "351"))
}

+ 0
- 0
system.go View File


+ 0
- 0
time.go View File


+ 13
- 3
time_test.go View File

@ -2,6 +2,7 @@ package helper
import (
"testing"
"time"
// "time"
)
@ -29,9 +30,18 @@ import (
func Test_GetTodayStartTimeStamp(t *testing.T) {
for i := 0; i < 1000; i++ {
ret := GetTodayEndTimeStamp()
t.Log(ret)
reserve_date := "2023-05-15"
reserveTime, err := time.ParseInLocation("2006-01-02", reserve_date, time.Local)
if err != nil {
t.Log("err:", err)
}
// 今天0点的时间戳
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