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 4.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. TOML stands for Tom's Obvious, Minimal Language. This Go package provides a
  2. reflection interface similar to Go's standard library `json` and `xml`
  3. packages.
  4. Compatible with TOML version [v1.0.0](https://toml.io/en/v1.0.0).
  5. Documentation: https://godocs.io/github.com/BurntSushi/toml
  6. See the [releases page](https://github.com/BurntSushi/toml/releases) for a
  7. changelog; this information is also in the git tag annotations (e.g. `git show
  8. v0.4.0`).
  9. This library requires Go 1.13 or newer; install it with:
  10. % go get github.com/BurntSushi/toml@latest
  11. It also comes with a TOML validator CLI tool:
  12. % go install github.com/BurntSushi/toml/cmd/tomlv@latest
  13. % tomlv some-toml-file.toml
  14. ### Testing
  15. This package passes all tests in [toml-test] for both the decoder and the
  16. encoder.
  17. [toml-test]: https://github.com/BurntSushi/toml-test
  18. ### Examples
  19. This package works similar to how the Go standard library handles XML and JSON.
  20. Namely, data is loaded into Go values via reflection.
  21. For the simplest example, consider some TOML file as just a list of keys and
  22. values:
  23. ```toml
  24. Age = 25
  25. Cats = [ "Cauchy", "Plato" ]
  26. Pi = 3.14
  27. Perfection = [ 6, 28, 496, 8128 ]
  28. DOB = 1987-07-05T05:45:00Z
  29. ```
  30. Which could be defined in Go as:
  31. ```go
  32. type Config struct {
  33. Age int
  34. Cats []string
  35. Pi float64
  36. Perfection []int
  37. DOB time.Time // requires `import time`
  38. }
  39. ```
  40. And then decoded with:
  41. ```go
  42. var conf Config
  43. err := toml.Decode(tomlData, &conf)
  44. // handle error
  45. ```
  46. You can also use struct tags if your struct field name doesn't map to a TOML
  47. key value directly:
  48. ```toml
  49. some_key_NAME = "wat"
  50. ```
  51. ```go
  52. type TOML struct {
  53. ObscureKey string `toml:"some_key_NAME"`
  54. }
  55. ```
  56. Beware that like other most other decoders **only exported fields** are
  57. considered when encoding and decoding; private fields are silently ignored.
  58. ### Using the `Marshaler` and `encoding.TextUnmarshaler` interfaces
  59. Here's an example that automatically parses duration strings into
  60. `time.Duration` values:
  61. ```toml
  62. [[song]]
  63. name = "Thunder Road"
  64. duration = "4m49s"
  65. [[song]]
  66. name = "Stairway to Heaven"
  67. duration = "8m03s"
  68. ```
  69. Which can be decoded with:
  70. ```go
  71. type song struct {
  72. Name string
  73. Duration duration
  74. }
  75. type songs struct {
  76. Song []song
  77. }
  78. var favorites songs
  79. if _, err := toml.Decode(blob, &favorites); err != nil {
  80. log.Fatal(err)
  81. }
  82. for _, s := range favorites.Song {
  83. fmt.Printf("%s (%s)\n", s.Name, s.Duration)
  84. }
  85. ```
  86. And you'll also need a `duration` type that satisfies the
  87. `encoding.TextUnmarshaler` interface:
  88. ```go
  89. type duration struct {
  90. time.Duration
  91. }
  92. func (d *duration) UnmarshalText(text []byte) error {
  93. var err error
  94. d.Duration, err = time.ParseDuration(string(text))
  95. return err
  96. }
  97. ```
  98. To target TOML specifically you can implement `UnmarshalTOML` TOML interface in
  99. a similar way.
  100. ### More complex usage
  101. Here's an example of how to load the example from the official spec page:
  102. ```toml
  103. # This is a TOML document. Boom.
  104. title = "TOML Example"
  105. [owner]
  106. name = "Tom Preston-Werner"
  107. organization = "GitHub"
  108. bio = "GitHub Cofounder & CEO\nLikes tater tots and beer."
  109. dob = 1979-05-27T07:32:00Z # First class dates? Why not?
  110. [database]
  111. server = "192.168.1.1"
  112. ports = [ 8001, 8001, 8002 ]
  113. connection_max = 5000
  114. enabled = true
  115. [servers]
  116. # You can indent as you please. Tabs or spaces. TOML don't care.
  117. [servers.alpha]
  118. ip = "10.0.0.1"
  119. dc = "eqdc10"
  120. [servers.beta]
  121. ip = "10.0.0.2"
  122. dc = "eqdc10"
  123. [clients]
  124. data = [ ["gamma", "delta"], [1, 2] ] # just an update to make sure parsers support it
  125. # Line breaks are OK when inside arrays
  126. hosts = [
  127. "alpha",
  128. "omega"
  129. ]
  130. ```
  131. And the corresponding Go types are:
  132. ```go
  133. type tomlConfig struct {
  134. Title string
  135. Owner ownerInfo
  136. DB database `toml:"database"`
  137. Servers map[string]server
  138. Clients clients
  139. }
  140. type ownerInfo struct {
  141. Name string
  142. Org string `toml:"organization"`
  143. Bio string
  144. DOB time.Time
  145. }
  146. type database struct {
  147. Server string
  148. Ports []int
  149. ConnMax int `toml:"connection_max"`
  150. Enabled bool
  151. }
  152. type server struct {
  153. IP string
  154. DC string
  155. }
  156. type clients struct {
  157. Data [][]interface{}
  158. Hosts []string
  159. }
  160. ```
  161. Note that a case insensitive match will be tried if an exact match can't be
  162. found.
  163. A working example of the above can be found in `_example/example.{go,toml}`.