常用类型及数据操作方法
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.0 KiB

3 years ago
  1. package helper
  2. import (
  3. "errors"
  4. )
  5. func MergeStringMap(x, y map[string]string) map[string]string {
  6. n := make(map[string]string)
  7. for i, v := range x {
  8. for j, w := range y {
  9. if i == j {
  10. n[i] = w
  11. } else {
  12. if _, ok := n[i]; !ok {
  13. n[i] = v
  14. }
  15. if _, ok := n[j]; !ok {
  16. n[j] = w
  17. }
  18. }
  19. }
  20. }
  21. return n
  22. }
  23. /**
  24. * 转换为map string数组
  25. * 2021/1/5
  26. * gz
  27. */
  28. func ToMapStringArray(x interface{}) ([]map[string]string, error) {
  29. list, ok := x.([]interface{})
  30. if !ok {
  31. return []map[string]string{}, errors.New("type error")
  32. }
  33. if len(list) < 1 {
  34. return []map[string]string{}, nil
  35. }
  36. var ret []map[string]string = make([]map[string]string, len(list))
  37. var tmp map[string]interface{}
  38. var map_tmp map[string]string
  39. var err error
  40. for key, item := range list {
  41. if tmp, ok = item.(map[string]interface{}); ok {
  42. map_tmp = make(map[string]string)
  43. for k, v := range tmp {
  44. map_tmp[k] = ToStr(v)
  45. }
  46. ret[key] = map_tmp
  47. } else {
  48. err = errors.New("data type error")
  49. break
  50. }
  51. }
  52. return ret, err
  53. }