From c824e5a7236d6abfc8ea65d7657f10ace311ac2e Mon Sep 17 00:00:00 2001 From: guzeng Date: Mon, 13 Nov 2023 18:04:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0MAP=E5=90=88=E5=B9=B6?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- map.go | 34 ++++++++++++++++++++++++++++++---- map_test.go | 9 +++++++++ time_test.go | 5 +++++ uuid_test.go | 12 ++++++++++++ 4 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 uuid_test.go diff --git a/map.go b/map.go index cfaa53d..68df776 100644 --- a/map.go +++ b/map.go @@ -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 +} diff --git a/map_test.go b/map_test.go index 8cd4ccb..2124d9b 100644 --- a/map_test.go +++ b/map_test.go @@ -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) +} diff --git a/time_test.go b/time_test.go index 06ae872..4363e4c 100644 --- a/time_test.go +++ b/time_test.go @@ -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)) +} diff --git a/uuid_test.go b/uuid_test.go new file mode 100644 index 0000000..d02f3e5 --- /dev/null +++ b/uuid_test.go @@ -0,0 +1,12 @@ +package helper + +import ( + "testing" +) + +func Test_GenerateUUID(t *testing.T) { + uuid, err := GenerateUUID() + + t.Log(uuid) + t.Log(err) +}