本站源代码
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.

48 lines
1.2KB

  1. // Copyright (c) 2015, Emir Pasic. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package utils provides common utility functions.
  5. //
  6. // Provided functionalities:
  7. // - sorting
  8. // - comparators
  9. package utils
  10. import (
  11. "fmt"
  12. "strconv"
  13. )
  14. // ToString converts a value to string.
  15. func ToString(value interface{}) string {
  16. switch value.(type) {
  17. case string:
  18. return value.(string)
  19. case int8:
  20. return strconv.FormatInt(int64(value.(int8)), 10)
  21. case int16:
  22. return strconv.FormatInt(int64(value.(int16)), 10)
  23. case int32:
  24. return strconv.FormatInt(int64(value.(int32)), 10)
  25. case int64:
  26. return strconv.FormatInt(int64(value.(int64)), 10)
  27. case uint8:
  28. return strconv.FormatUint(uint64(value.(uint8)), 10)
  29. case uint16:
  30. return strconv.FormatUint(uint64(value.(uint16)), 10)
  31. case uint32:
  32. return strconv.FormatUint(uint64(value.(uint32)), 10)
  33. case uint64:
  34. return strconv.FormatUint(uint64(value.(uint64)), 10)
  35. case float32:
  36. return strconv.FormatFloat(float64(value.(float32)), 'g', -1, 64)
  37. case float64:
  38. return strconv.FormatFloat(float64(value.(float64)), 'g', -1, 64)
  39. case bool:
  40. return strconv.FormatBool(value.(bool))
  41. default:
  42. return fmt.Sprintf("%+v", value)
  43. }
  44. }
上海开阖软件有限公司 沪ICP备12045867号-1