Command line client for Write.as https://write.as/apps/cli
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

README.md 2.7 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # go-multierror
  2. [![Build Status](http://img.shields.io/travis/hashicorp/go-multierror.svg?style=flat-square)][travis]
  3. [![Go Documentation](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)][godocs]
  4. [travis]: https://travis-ci.org/hashicorp/go-multierror
  5. [godocs]: https://godoc.org/github.com/hashicorp/go-multierror
  6. `go-multierror` is a package for Go that provides a mechanism for
  7. representing a list of `error` values as a single `error`.
  8. This allows a function in Go to return an `error` that might actually
  9. be a list of errors. If the caller knows this, they can unwrap the
  10. list and access the errors. If the caller doesn't know, the error
  11. formats to a nice human-readable format.
  12. `go-multierror` implements the
  13. [errwrap](https://github.com/hashicorp/errwrap) interface so that it can
  14. be used with that library, as well.
  15. ## Installation and Docs
  16. Install using `go get github.com/hashicorp/go-multierror`.
  17. Full documentation is available at
  18. http://godoc.org/github.com/hashicorp/go-multierror
  19. ## Usage
  20. go-multierror is easy to use and purposely built to be unobtrusive in
  21. existing Go applications/libraries that may not be aware of it.
  22. **Building a list of errors**
  23. The `Append` function is used to create a list of errors. This function
  24. behaves a lot like the Go built-in `append` function: it doesn't matter
  25. if the first argument is nil, a `multierror.Error`, or any other `error`,
  26. the function behaves as you would expect.
  27. ```go
  28. var result error
  29. if err := step1(); err != nil {
  30. result = multierror.Append(result, err)
  31. }
  32. if err := step2(); err != nil {
  33. result = multierror.Append(result, err)
  34. }
  35. return result
  36. ```
  37. **Customizing the formatting of the errors**
  38. By specifying a custom `ErrorFormat`, you can customize the format
  39. of the `Error() string` function:
  40. ```go
  41. var result *multierror.Error
  42. // ... accumulate errors here, maybe using Append
  43. if result != nil {
  44. result.ErrorFormat = func([]error) string {
  45. return "errors!"
  46. }
  47. }
  48. ```
  49. **Accessing the list of errors**
  50. `multierror.Error` implements `error` so if the caller doesn't know about
  51. multierror, it will work just fine. But if you're aware a multierror might
  52. be returned, you can use type switches to access the list of errors:
  53. ```go
  54. if err := something(); err != nil {
  55. if merr, ok := err.(*multierror.Error); ok {
  56. // Use merr.Errors
  57. }
  58. }
  59. ```
  60. **Returning a multierror only if there are errors**
  61. If you build a `multierror.Error`, you can use the `ErrorOrNil` function
  62. to return an `error` implementation only if there are errors to return:
  63. ```go
  64. var result *multierror.Error
  65. // ... accumulate errors here
  66. // Return the `error` only if errors were added to the multierror, otherwise
  67. // return nil since there are no errors.
  68. return result.ErrorOrNil()
  69. ```