You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.2, but preserves some behavior
  12. from 1.1 for backwards compatibility.
  13. Specifically, as of v3 of the yaml package:
  14. - YAML 1.1 bools (_yes/no, on/off_) are supported as long as they are being
  15. decoded into a typed bool value. Otherwise they behave as a string. Booleans
  16. in YAML 1.2 are _true/false_ only.
  17. - Octals encode and decode as _0777_ per YAML 1.1, rather than _0o777_
  18. as specified in YAML 1.2, because most parsers still use the old format.
  19. Octals in the _0o777_ format are supported though, so new files work.
  20. - Does not support base-60 floats. These are gone from YAML 1.2, and were
  21. actually never supported by this package as it's clearly a poor choice.
  22. and offers backwards
  23. compatibility with YAML 1.1 in some cases.
  24. 1.2, including support for
  25. anchors, tags, map merging, etc. Multi-document unmarshalling is not yet
  26. implemented, and base-60 floats from YAML 1.1 are purposefully not
  27. supported since they're a poor design and are gone in YAML 1.2.
  28. Installation and usage
  29. ----------------------
  30. The import path for the package is *gopkg.in/yaml.v3*.
  31. To install it, run:
  32. go get gopkg.in/yaml.v3
  33. API documentation
  34. -----------------
  35. If opened in a browser, the import path itself leads to the API documentation:
  36. - [https://gopkg.in/yaml.v3](https://gopkg.in/yaml.v3)
  37. API stability
  38. -------------
  39. The package API for yaml v3 will remain stable as described in [gopkg.in](https://gopkg.in).
  40. License
  41. -------
  42. The yaml package is licensed under the MIT and Apache License 2.0 licenses.
  43. Please see the LICENSE file for details.
  44. Example
  45. -------
  46. ```Go
  47. package main
  48. import (
  49. "fmt"
  50. "log"
  51. "gopkg.in/yaml.v3"
  52. )
  53. var data = `
  54. a: Easy!
  55. b:
  56. c: 2
  57. d: [3, 4]
  58. `
  59. // Note: struct fields must be public in order for unmarshal to
  60. // correctly populate the data.
  61. type T struct {
  62. A string
  63. B struct {
  64. RenamedC int `yaml:"c"`
  65. D []int `yaml:",flow"`
  66. }
  67. }
  68. func main() {
  69. t := T{}
  70. err := yaml.Unmarshal([]byte(data), &t)
  71. if err != nil {
  72. log.Fatalf("error: %v", err)
  73. }
  74. fmt.Printf("--- t:\n%v\n\n", t)
  75. d, err := yaml.Marshal(&t)
  76. if err != nil {
  77. log.Fatalf("error: %v", err)
  78. }
  79. fmt.Printf("--- t dump:\n%s\n\n", string(d))
  80. m := make(map[interface{}]interface{})
  81. err = yaml.Unmarshal([]byte(data), &m)
  82. if err != nil {
  83. log.Fatalf("error: %v", err)
  84. }
  85. fmt.Printf("--- m:\n%v\n\n", m)
  86. d, err = yaml.Marshal(&m)
  87. if err != nil {
  88. log.Fatalf("error: %v", err)
  89. }
  90. fmt.Printf("--- m dump:\n%s\n\n", string(d))
  91. }
  92. ```
  93. This example will generate the following output:
  94. ```
  95. --- t:
  96. {Easy! {2 [3 4]}}
  97. --- t dump:
  98. a: Easy!
  99. b:
  100. c: 2
  101. d: [3, 4]
  102. --- m:
  103. map[a:Easy! b:map[c:2 d:[3 4]]]
  104. --- m dump:
  105. a: Easy!
  106. b:
  107. c: 2
  108. d:
  109. - 3
  110. - 4
  111. ```