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.

133 lines
2.6 KiB

  1. # YAML support for the Go language
  2. Introduction
  3. ------------
  4. The yaml package enables Go programs to comfortably encode and decode YAML
  5. values. It was developed within [Canonical](https://www.canonical.com) as
  6. part of the [juju](https://juju.ubuntu.com) project, and is based on a
  7. pure Go port of the well-known [libyaml](http://pyyaml.org/wiki/LibYAML)
  8. C library to parse and generate YAML data quickly and reliably.
  9. Compatibility
  10. -------------
  11. The yaml package supports most of YAML 1.1 and 1.2, including support for
  12. anchors, tags, map merging, etc. Multi-document unmarshalling is not yet
  13. implemented, and base-60 floats from YAML 1.1 are purposefully not
  14. supported since they're a poor design and are gone in YAML 1.2.
  15. Installation and usage
  16. ----------------------
  17. The import path for the package is *gopkg.in/yaml.v2*.
  18. To install it, run:
  19. go get gopkg.in/yaml.v2
  20. API documentation
  21. -----------------
  22. If opened in a browser, the import path itself leads to the API documentation:
  23. * [https://gopkg.in/yaml.v2](https://gopkg.in/yaml.v2)
  24. API stability
  25. -------------
  26. The package API for yaml v2 will remain stable as described in [gopkg.in](https://gopkg.in).
  27. License
  28. -------
  29. The yaml package is licensed under the Apache License 2.0. Please see the LICENSE file for details.
  30. Example
  31. -------
  32. ```Go
  33. package main
  34. import (
  35. "fmt"
  36. "log"
  37. "gopkg.in/yaml.v2"
  38. )
  39. var data = `
  40. a: Easy!
  41. b:
  42. c: 2
  43. d: [3, 4]
  44. `
  45. // Note: struct fields must be public in order for unmarshal to
  46. // correctly populate the data.
  47. type T struct {
  48. A string
  49. B struct {
  50. RenamedC int `yaml:"c"`
  51. D []int `yaml:",flow"`
  52. }
  53. }
  54. func main() {
  55. t := T{}
  56. err := yaml.Unmarshal([]byte(data), &t)
  57. if err != nil {
  58. log.Fatalf("error: %v", err)
  59. }
  60. fmt.Printf("--- t:\n%v\n\n", t)
  61. d, err := yaml.Marshal(&t)
  62. if err != nil {
  63. log.Fatalf("error: %v", err)
  64. }
  65. fmt.Printf("--- t dump:\n%s\n\n", string(d))
  66. m := make(map[interface{}]interface{})
  67. err = yaml.Unmarshal([]byte(data), &m)
  68. if err != nil {
  69. log.Fatalf("error: %v", err)
  70. }
  71. fmt.Printf("--- m:\n%v\n\n", m)
  72. d, err = yaml.Marshal(&m)
  73. if err != nil {
  74. log.Fatalf("error: %v", err)
  75. }
  76. fmt.Printf("--- m dump:\n%s\n\n", string(d))
  77. }
  78. ```
  79. This example will generate the following output:
  80. ```
  81. --- t:
  82. {Easy! {2 [3 4]}}
  83. --- t dump:
  84. a: Easy!
  85. b:
  86. c: 2
  87. d: [3, 4]
  88. --- m:
  89. map[a:Easy! b:map[c:2 d:[3 4]]]
  90. --- m dump:
  91. a: Easy!
  92. b:
  93. c: 2
  94. d:
  95. - 3
  96. - 4
  97. ```