Browse Source

增加MAP合并方法

master
guzeng 1 year ago
parent
commit
c824e5a723
4 changed files with 56 additions and 4 deletions
  1. +30
    -4
      map.go
  2. +9
    -0
      map_test.go
  3. +5
    -0
      time_test.go
  4. +12
    -0
      uuid_test.go

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

+ 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