附近小店
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

87 righe
1.4 KiB

  1. package cache
  2. import (
  3. "fmt"
  4. "strconv"
  5. )
  6. // GetString convert interface to string.
  7. func GetString(v interface{}) string {
  8. switch result := v.(type) {
  9. case string:
  10. return result
  11. case []byte:
  12. return string(result)
  13. default:
  14. if v != nil {
  15. return fmt.Sprint(result)
  16. }
  17. }
  18. return ""
  19. }
  20. // GetInt convert interface to int.
  21. func GetInt(v interface{}) int {
  22. switch result := v.(type) {
  23. case int:
  24. return result
  25. case int32:
  26. return int(result)
  27. case int64:
  28. return int(result)
  29. default:
  30. if d := GetString(v); d != "" {
  31. value, _ := strconv.Atoi(d)
  32. return value
  33. }
  34. }
  35. return 0
  36. }
  37. // GetInt64 convert interface to int64.
  38. func GetInt64(v interface{}) int64 {
  39. switch result := v.(type) {
  40. case int:
  41. return int64(result)
  42. case int32:
  43. return int64(result)
  44. case int64:
  45. return result
  46. default:
  47. if d := GetString(v); d != "" {
  48. value, _ := strconv.ParseInt(d, 10, 64)
  49. return value
  50. }
  51. }
  52. return 0
  53. }
  54. // GetFloat64 convert interface to float64.
  55. func GetFloat64(v interface{}) float64 {
  56. switch result := v.(type) {
  57. case float64:
  58. return result
  59. default:
  60. if d := GetString(v); d != "" {
  61. value, _ := strconv.ParseFloat(d, 64)
  62. return value
  63. }
  64. }
  65. return 0
  66. }
  67. // GetBool convert interface to bool.
  68. func GetBool(v interface{}) bool {
  69. switch result := v.(type) {
  70. case bool:
  71. return result
  72. default:
  73. if d := GetString(v); d != "" {
  74. value, _ := strconv.ParseBool(d)
  75. return value
  76. }
  77. }
  78. return false
  79. }