6 Commits

15 changed files with 308 additions and 84 deletions
Split View
  1. +1
    -1
      header.go
  2. +45
    -4
      map.go
  3. +15
    -0
      map_test.go
  4. +12
    -1
      math.go
  5. +14
    -5
      math_test.go
  6. +90
    -63
      slice.go
  7. +8
    -0
      slice_test.go
  8. +62
    -0
      snowflake.go
  9. +16
    -0
      snowflake_test.go
  10. +7
    -7
      string.go
  11. +13
    -0
      string_test.go
  12. +0
    -0
      system.go
  13. +0
    -0
      time.go
  14. +13
    -3
      time_test.go
  15. +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


+ 45
- 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)
@ -277,3 +277,44 @@ func MapPage(pageNum, pageSize int, MapDate []map[string]interface{}) []map[stri
}
return MapDate[stat:end]
}
/**
* MAP索引转大驼峰
* gz
*/
func SetMapFirstToUpper(info map[string]interface{}) map[string]interface{} {
var ret map[string]interface{}
if len(info) > 0 {
ret = make(map[string]interface{})
for k, v := range info {
ret[StrFirstToUpper(k)] = v
}
}
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
}

+ 15
- 0
map_test.go View File

@ -22,3 +22,18 @@ func Test_HttpBuildQuery(t *testing.T) {
t.Log(ret)
}
func Test_SetMapFirstToUpper(t *testing.T) {
a := map[string]interface{}{"id": "11", "name": "8", "a_c": 99, "price": "2.3", "cc_dd_ee": 22}
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)
}

+ 12
- 1
math.go View File

@ -134,7 +134,7 @@ func FloatQuo(str ...interface{}) float64 {
/**
* 十进制转十六进制
*/
func TenToHex(ten int) string {
func DecToHex(ten int) string {
m := 0
hex := make([]int, 0)
for {
@ -156,3 +156,14 @@ func TenToHex(ten int) string {
}
return strings.Join(hexStr, "")
}
/**
* 十六进制转十进制
*/
func HexToDec(val string) (int, error) {
n, err := strconv.ParseUint(val, 16, 64)
if err != nil {
return 0, err
}
return int(n), nil
}

+ 14
- 5
math_test.go View File

@ -1,6 +1,7 @@
package helper
import (
"math"
"strings"
"testing"
)
@ -41,10 +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_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)
// }

+ 90
- 63
slice.go View File

@ -4,85 +4,112 @@
package helper
import (
"errors"
"fmt"
"errors"
"fmt"
"sort"
)
// 求差集
func Diff(slice1, slice2 []string) []string {
m := make(map[string]int)
nn := make([]string, 0)
inter := Intersect(slice1, slice2)
for _, v := range inter {
m[v]++
}
m := make(map[string]int)
nn := make([]string, 0)
inter := Intersect(slice1, slice2)
for _, v := range inter {
m[v]++
}
for _, value := range slice1 {
times, _ := m[value]
if times == 0 {
nn = append(nn, value)
}
}
return nn
for _, value := range slice1 {
times, _ := m[value]
if times == 0 {
nn = append(nn, value)
}
}
return nn
}
// 求交集
func Intersect(slice1, slice2 []string) []string {
m := make(map[string]int)
nn := make([]string, 0)
for _, v := range slice1 {
m[v]++
}
m := make(map[string]int)
nn := make([]string, 0)
for _, v := range slice1 {
m[v]++
}
for _, v := range slice2 {
times, _ := m[v]
if times == 1 {
nn = append(nn, v)
}
}
return nn
for _, v := range slice2 {
times, _ := m[v]
if times == 1 {
nn = append(nn, v)
}
}
return nn
}
// 求并集
func Union(slice1, slice2 []string) []string {
m := make(map[string]int)
for _, v := range slice1 {
m[v]++
}
m := make(map[string]int)
for _, v := range slice1 {
m[v]++
}
for _, v := range slice2 {
times, _ := m[v]
if times == 0 {
slice1 = append(slice1, v)
}
}
return slice1
for _, v := range slice2 {
times, _ := m[v]
if times == 0 {
slice1 = append(slice1, v)
}
}
return slice1
}
func Unique(originals interface{}) (interface{}, error) {
temp := map[string]struct{}{}
switch slice := originals.(type) {
case []string:
result := make([]string, 0, len(originals.([]string)))
for _, item := range slice {
key := fmt.Sprint(item)
if _, ok := temp[key]; !ok {
temp[key] = struct{}{}
result = append(result, item)
}
}
return result, nil
case []int64:
result := make([]int64, 0, len(originals.([]int64)))
for _, item := range slice {
key := fmt.Sprint(item)
if _, ok := temp[key]; !ok {
temp[key] = struct{}{}
result = append(result, item)
}
}
return result, nil
default:
return nil, errors.New(fmt.Sprintf("Unknown type: %T", slice))
}
temp := map[string]struct{}{}
switch slice := originals.(type) {
case []string:
result := make([]string, 0, len(originals.([]string)))
for _, item := range slice {
key := fmt.Sprint(item)
if _, ok := temp[key]; !ok {
temp[key] = struct{}{}
result = append(result, item)
}
}
return result, nil
case []int64:
result := make([]int64, 0, len(originals.([]int64)))
for _, item := range slice {
key := fmt.Sprint(item)
if _, ok := temp[key]; !ok {
temp[key] = struct{}{}
result = append(result, item)
}
}
return result, nil
default:
return nil, errors.New(fmt.Sprintf("Unknown type: %T", slice))
}
}
// 将切片字符串(数字型)排序
func StringSliceSort(str []string, sort_type string) ([]string, error) {
int_arr := []int{}
string_arr := []string{}
if len(str) < 1 {
return []string{}, nil
}
for _, val := range str {
int_arr = append(int_arr, ToInt(val))
}
if sort_type == "desc" {
sort.Sort(sort.Reverse(sort.IntSlice(int_arr)))
}else{
sort.Ints(int_arr)
}
for _, val_int := range int_arr {
string_arr = append(string_arr, ToStr(val_int))
}
return string_arr, nil
}

+ 8
- 0
slice_test.go View File

@ -31,3 +31,11 @@ func Test_Unique(t *testing.T) {
ret, err := Unique(str)
fmt.Println(ret, err)
}
func Test_StringSliceSort(t *testing.T) {
var str = []string{"10", "2", "5"}
ret, err := StringSliceSort(str,"asc")
fmt.Println(ret, err)
ret2, err := StringSliceSort(str,"desc")
fmt.Println(ret2, 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"
}

+ 13
- 0
string_test.go View File

@ -2,6 +2,7 @@ package helper
import (
"fmt"
"strings"
"testing"
)
@ -47,3 +48,15 @@ func Test_StrFirstToUpper(t *testing.T) {
ret := StrFirstToUpper(str)
t.Log(ret)
}
func Test_ToInt(t *testing.T) {
var str string = "sd23455667,123456789aq,qq123456789"
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