...

Text file src/github.com/mitchellh/mapstructure/README.md

Documentation: github.com/mitchellh/mapstructure

     1# mapstructure [![Godoc](https://godoc.org/github.com/mitchellh/mapstructure?status.svg)](https://godoc.org/github.com/mitchellh/mapstructure)
     2
     3mapstructure is a Go library for decoding generic map values to structures
     4and vice versa, while providing helpful error handling.
     5
     6This library is most useful when decoding values from some data stream (JSON,
     7Gob, etc.) where you don't _quite_ know the structure of the underlying data
     8until you read a part of it. You can therefore read a `map[string]interface{}`
     9and use this library to decode it into the proper underlying native Go
    10structure.
    11
    12## Installation
    13
    14Standard `go get`:
    15
    16```
    17$ go get github.com/mitchellh/mapstructure
    18```
    19
    20## Usage & Example
    21
    22For usage and examples see the [Godoc](http://godoc.org/github.com/mitchellh/mapstructure).
    23
    24The `Decode` function has examples associated with it there.
    25
    26## But Why?!
    27
    28Go offers fantastic standard libraries for decoding formats such as JSON.
    29The standard method is to have a struct pre-created, and populate that struct
    30from the bytes of the encoded format. This is great, but the problem is if
    31you have configuration or an encoding that changes slightly depending on
    32specific fields. For example, consider this JSON:
    33
    34```json
    35{
    36  "type": "person",
    37  "name": "Mitchell"
    38}
    39```
    40
    41Perhaps we can't populate a specific structure without first reading
    42the "type" field from the JSON. We could always do two passes over the
    43decoding of the JSON (reading the "type" first, and the rest later).
    44However, it is much simpler to just decode this into a `map[string]interface{}`
    45structure, read the "type" key, then use something like this library
    46to decode it into the proper structure.

View as plain text