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.

75 lines
2.9 KiB

  1. cast
  2. ====
  3. [![GoDoc](https://godoc.org/github.com/spf13/cast?status.svg)](https://godoc.org/github.com/spf13/cast)
  4. [![Build Status](https://api.travis-ci.org/spf13/cast.svg?branch=master)](https://travis-ci.org/spf13/cast)
  5. [![Go Report Card](https://goreportcard.com/badge/github.com/spf13/cast)](https://goreportcard.com/report/github.com/spf13/cast)
  6. Easy and safe casting from one type to another in Go
  7. Don’t Panic! ... Cast
  8. ## What is Cast?
  9. Cast is a library to convert between different go types in a consistent and easy way.
  10. Cast provides simple functions to easily convert a number to a string, an
  11. interface into a bool, etc. Cast does this intelligently when an obvious
  12. conversion is possible. It doesn’t make any attempts to guess what you meant,
  13. for example you can only convert a string to an int when it is a string
  14. representation of an int such as “8”. Cast was developed for use in
  15. [Hugo](http://hugo.spf13.com), a website engine which uses YAML, TOML or JSON
  16. for meta data.
  17. ## Why use Cast?
  18. When working with dynamic data in Go you often need to cast or convert the data
  19. from one type into another. Cast goes beyond just using type assertion (though
  20. it uses that when possible) to provide a very straightforward and convenient
  21. library.
  22. If you are working with interfaces to handle things like dynamic content
  23. you’ll need an easy way to convert an interface into a given type. This
  24. is the library for you.
  25. If you are taking in data from YAML, TOML or JSON or other formats which lack
  26. full types, then Cast is the library for you.
  27. ## Usage
  28. Cast provides a handful of To_____ methods. These methods will always return
  29. the desired type. **If input is provided that will not convert to that type, the
  30. 0 or nil value for that type will be returned**.
  31. Cast also provides identical methods To_____E. These return the same result as
  32. the To_____ methods, plus an additional error which tells you if it successfully
  33. converted. Using these methods you can tell the difference between when the
  34. input matched the zero value or when the conversion failed and the zero value
  35. was returned.
  36. The following examples are merely a sample of what is available. Please review
  37. the code for a complete set.
  38. ### Example ‘ToString’:
  39. cast.ToString("mayonegg") // "mayonegg"
  40. cast.ToString(8) // "8"
  41. cast.ToString(8.31) // "8.31"
  42. cast.ToString([]byte("one time")) // "one time"
  43. cast.ToString(nil) // ""
  44. var foo interface{} = "one more time"
  45. cast.ToString(foo) // "one more time"
  46. ### Example ‘ToInt’:
  47. cast.ToInt(8) // 8
  48. cast.ToInt(8.31) // 8
  49. cast.ToInt("8") // 8
  50. cast.ToInt(true) // 1
  51. cast.ToInt(false) // 0
  52. var eight interface{} = 8
  53. cast.ToInt(eight) // 8
  54. cast.ToInt(nil) // 0