在Go中轻松安全地从一种数据类型转换为另一种数据类型
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.

README.md 2.7 KiB

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