golang 的 rabbitmq 消费项目
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.
 
 
 

97 lines
2.2 KiB

  1. package utils
  2. import (
  3. "fmt"
  4. "reflect"
  5. "strings"
  6. )
  7. func Implode(glue string, args ...interface{}) string {
  8. data := make([]string, len(args))
  9. for i, s := range args {
  10. data[i] = fmt.Sprint(s)
  11. }
  12. return strings.Join(data, glue)
  13. }
  14. func ImplodeByKey(glue string, args ...interface{}) string {
  15. data := make([]string, len(args))
  16. for i, _ := range args {
  17. data[i] = fmt.Sprint(i)
  18. }
  19. return strings.Join(data, glue)
  20. }
  21. //字符串是否在数组里
  22. func InArr(target string, str_array []string) bool {
  23. for _, element := range str_array {
  24. if target == element {
  25. return true
  26. }
  27. }
  28. return false
  29. }
  30. //把数组的值放到key里
  31. func ArrayColumn(array interface{}, key string) (result map[string]interface{}, err error) {
  32. result = make(map[string]interface{})
  33. t := reflect.TypeOf(array)
  34. v := reflect.ValueOf(array)
  35. if t.Kind() != reflect.Slice {
  36. return nil, nil
  37. }
  38. if v.Len() == 0 {
  39. return nil, nil
  40. }
  41. for i := 0; i < v.Len(); i++ {
  42. indexv := v.Index(i)
  43. if indexv.Type().Kind() != reflect.Struct {
  44. return nil, nil
  45. }
  46. mapKeyInterface := indexv.FieldByName(key)
  47. if mapKeyInterface.Kind() == reflect.Invalid {
  48. return nil, nil
  49. }
  50. mapKeyString, err := InterfaceToString(mapKeyInterface.Interface())
  51. if err != nil {
  52. return nil, err
  53. }
  54. result[mapKeyString] = indexv.Interface()
  55. }
  56. return result, err
  57. }
  58. //转string
  59. func InterfaceToString(v interface{}) (result string, err error) {
  60. switch reflect.TypeOf(v).Kind() {
  61. case reflect.Int64, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32:
  62. result = fmt.Sprintf("%v", v)
  63. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  64. result = fmt.Sprintf("%v", v)
  65. case reflect.String:
  66. result = v.(string)
  67. default:
  68. err = nil
  69. }
  70. return result, err
  71. }
  72. func HideTrueName(name string) string {
  73. res := "**"
  74. if name != "" {
  75. runs := []rune(name)
  76. leng := len(runs)
  77. if leng <= 3 {
  78. res = string(runs[0:1]) + res
  79. } else if leng < 5 {
  80. res = string(runs[0:2]) + res
  81. } else if leng < 10 {
  82. res = string(runs[0:2]) + "***" + string(runs[leng-2:leng])
  83. } else if leng < 16 {
  84. res = string(runs[0:3]) + "****" + string(runs[leng-3:leng])
  85. } else {
  86. res = string(runs[0:4]) + "*****" + string(runs[leng-4:leng])
  87. }
  88. }
  89. return res
  90. }