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.
 
 
 

50 lines
1.1 KiB

  1. package utils
  2. import (
  3. "encoding/json"
  4. "strconv"
  5. "strings"
  6. )
  7. func ConvertInterfaceToString(value interface{}) string {
  8. if value == nil {
  9. return ""
  10. }
  11. switch value.(type) {
  12. case int:
  13. return strconv.Itoa(value.(int))
  14. case string:
  15. return value.(string)
  16. case uint:
  17. return strconv.Itoa(int(value.(uint)))
  18. case int32:
  19. return strconv.Itoa(int(value.(int32)))
  20. case uint32:
  21. return strconv.Itoa(int(value.(uint32)))
  22. case int64:
  23. return strconv.FormatInt(value.(int64), 10)
  24. case uint64:
  25. return strconv.FormatUint(value.(uint64), 10)
  26. case float32:
  27. return strconv.FormatFloat(float64(value.(float32)), 'f', -1, 64)
  28. case float64:
  29. return strconv.FormatFloat(value.(float64), 'f', -1, 64)
  30. case int8:
  31. return strconv.Itoa(int(value.(int8)))
  32. case uint8:
  33. return strconv.Itoa(int(value.(uint8)))
  34. case int16:
  35. return strconv.Itoa(int(value.(int16)))
  36. case uint16:
  37. return strconv.Itoa(int(value.(uint16)))
  38. case []byte:
  39. return string(value.([]byte))
  40. default:
  41. b, err := json.Marshal(value)
  42. if err != nil {
  43. return ""
  44. }
  45. return strings.ReplaceAll(strings.Trim(string(b[:]), "[]"), "\"", "")
  46. }
  47. }