convert.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package runtime
  2. import (
  3. "strconv"
  4. )
  5. // String just returns the given string.
  6. // It is just for compatibility to other types.
  7. func String(val string) (string, error) {
  8. return val, nil
  9. }
  10. // Bool converts the given string representation of a boolean value into bool.
  11. func Bool(val string) (bool, error) {
  12. return strconv.ParseBool(val)
  13. }
  14. // Float64 converts the given string representation into representation of a floating point number into float64.
  15. func Float64(val string) (float64, error) {
  16. return strconv.ParseFloat(val, 64)
  17. }
  18. // Float32 converts the given string representation of a floating point number into float32.
  19. func Float32(val string) (float32, error) {
  20. f, err := strconv.ParseFloat(val, 32)
  21. if err != nil {
  22. return 0, err
  23. }
  24. return float32(f), nil
  25. }
  26. // Int64 converts the given string representation of an integer into int64.
  27. func Int64(val string) (int64, error) {
  28. return strconv.ParseInt(val, 0, 64)
  29. }
  30. // Int32 converts the given string representation of an integer into int32.
  31. func Int32(val string) (int32, error) {
  32. i, err := strconv.ParseInt(val, 0, 32)
  33. if err != nil {
  34. return 0, err
  35. }
  36. return int32(i), nil
  37. }
  38. // Uint64 converts the given string representation of an integer into uint64.
  39. func Uint64(val string) (uint64, error) {
  40. return strconv.ParseUint(val, 0, 64)
  41. }
  42. // Uint32 converts the given string representation of an integer into uint32.
  43. func Uint32(val string) (uint32, error) {
  44. i, err := strconv.ParseUint(val, 0, 32)
  45. if err != nil {
  46. return 0, err
  47. }
  48. return uint32(i), nil
  49. }