util.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package utils
  2. import "time"
  3. func CostTime(start time.Time) int64 {
  4. duration := time.Now().UnixNano() - start.UnixNano()
  5. return duration / 1e6
  6. }
  7. type FeatureInfo struct {
  8. Value interface{} `json "value"`
  9. Type string `json "type"`
  10. }
  11. func ConvertFeatures(features map[string]interface{}) map[string]*FeatureInfo {
  12. ret := make(map[string]*FeatureInfo, len(features))
  13. for name, value := range features {
  14. info := &FeatureInfo{
  15. Value: value,
  16. Type: GetTypeOf(value),
  17. }
  18. ret[name] = info
  19. }
  20. return ret
  21. }
  22. func GetTypeOf(value interface{}) string {
  23. switch value.(type) {
  24. case int:
  25. return "int"
  26. case int32:
  27. return "int"
  28. case int64:
  29. return "bigint"
  30. case string:
  31. return "string"
  32. case float64:
  33. return "float64"
  34. default:
  35. return "none"
  36. }
  37. }
  38. func GetValueByType(value interface{}, vtype string) interface{} {
  39. switch vtype {
  40. case "int":
  41. return ToInt(value, 0)
  42. case "string":
  43. return ToString(value, "")
  44. case "float64":
  45. return ToFloat(value, float64(0))
  46. case "int64":
  47. return ToInt64(value, 0)
  48. case "bigint":
  49. return ToInt64(value, 0)
  50. default:
  51. return value
  52. }
  53. }
  54. func IndexOf(a []string, e string) int {
  55. n := len(a)
  56. var i = 0
  57. for ; i < n; i++ {
  58. if e == a[i] {
  59. return i
  60. }
  61. }
  62. return -1
  63. }