https://github.com/mitchellh/mapstructure
Go library for decoding generic map values into native Go structures and vice versa.
https://github.com/mitchellh/mapstructure
Last synced: about 2 months ago
JSON representation
Go library for decoding generic map values into native Go structures and vice versa.
- Host: GitHub
- URL: https://github.com/mitchellh/mapstructure
- Owner: mitchellh
- License: mit
- Archived: true
- Created: 2013-05-20T05:24:34.000Z (over 12 years ago)
- Default Branch: main
- Last Pushed: 2024-06-25T15:17:23.000Z (over 1 year ago)
- Last Synced: 2025-01-19T19:50:50.716Z (10 months ago)
- Language: Go
- Homepage: https://gist.github.com/mitchellh/90029601268e59a29e64e55bab1c5bdc
- Size: 357 KB
- Stars: 7,950
- Watchers: 66
- Forks: 683
- Open Issues: 84
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - mapstructure - Go library for decoding generic map values into native Go structures. (Serialization / HTTP Clients)
- awesome-go - mitchellh/mapstructure
- awesome-ccamel - mitchellh/mapstructure - Go library for decoding generic map values into native Go structures and vice versa. (Go)
- stars - mitchellh/mapstructure
- go-awesome - mapstructure - 将 map 值转换到结构体中 (开源类库 / 开发辅助包)
- awesome-go - mapstructure - Go library for decoding generic map values into native Go structures. (Serialization / HTTP Clients)
- awesome-go-cn - mapstructure
- go-awesome - mapstructure - convert map value to structure (Open source library / Development Aid Package)
- awesome-go - mapstructure - | - | - | (Serialization / HTTP Clients)
- awesome-go - mapstructure - Go library for decoding generic map values into native Go structures. (Serialization / HTTP Clients)
- awesome-go-plus - mapstructure - Go library for decoding generic map values into native Go structures.  (Serialization / HTTP Clients)
- fucking-awesome-go - :octocat: mapstructure - Go library for decoding generic map values into native Go structures. :star: 607 :fork_and_knife: 75 (Serialization / Advanced Console UIs)
- awesome-go-extra - mapstructure - 05-20T05:24:34Z|2022-08-10T10:59:34Z| (Serialization / HTTP Clients)
- awesome-go - mapstructure - Go library for decoding generic map values into native Go structures. - :arrow_down:4445 - :star:676 (Serialization / HTTP Clients)
- awesome-go - mapstructure - Go library for decoding generic map values into native Go structures. - ★ 1790 (Serialization)
- awesome-go - mapstructure - Go library for decoding generic map values into native Go structures. Stars:`8.0K`. (Serialization / HTTP Clients)
- awesome-go-cn - mapstructure
- awesome-go-zh - mapstructure
- awesome-go - mapstructure - Go library for decoding generic map values into native Go structures. (<span id="序列化-serialization">序列化 Serialization</span> / <span id="高级控制台用户界面-advanced-console-uis">高级控制台用户界面 Advanced Console UIs</span>)
- awesome-go-cn - mapstructure
- awesome-go - mapstructure - Go library for decoding generic map values into native Go structures. (Serialization / Advanced Console UIs)
- zero-alloc-awesome-go - mapstructure - Go library for decoding generic map values into native Go structures. (Serialization / HTTP Clients)
- awesome-go - mapstructure - Go library for decoding generic map values into native Go structures. (Serialization / HTTP Clients)
- awesome-Char - mapstructure - Go library for decoding generic map values into native Go structures. (Serialization / HTTP Clients)
- awesome-go-cn - mapstructure
- awesome-go - mapstructure - Go library for decoding generic map values into native Go structures. (Serialization / HTTP Clients)
README
# mapstructure [](https://godoc.org/github.com/mitchellh/mapstructure)
mapstructure is a Go library for decoding generic map values to structures
and vice versa, while providing helpful error handling.
This library is most useful when decoding values from some data stream (JSON,
Gob, etc.) where you don't _quite_ know the structure of the underlying data
until you read a part of it. You can therefore read a `map[string]interface{}`
and use this library to decode it into the proper underlying native Go
structure.
## Installation
Standard `go get`:
```
$ go get github.com/mitchellh/mapstructure
```
## Usage & Example
For usage and examples see the [Godoc](http://godoc.org/github.com/mitchellh/mapstructure).
The `Decode` function has examples associated with it there.
## But Why?!
Go offers fantastic standard libraries for decoding formats such as JSON.
The standard method is to have a struct pre-created, and populate that struct
from the bytes of the encoded format. This is great, but the problem is if
you have configuration or an encoding that changes slightly depending on
specific fields. For example, consider this JSON:
```json
{
"type": "person",
"name": "Mitchell"
}
```
Perhaps we can't populate a specific structure without first reading
the "type" field from the JSON. We could always do two passes over the
decoding of the JSON (reading the "type" first, and the rest later).
However, it is much simpler to just decode this into a `map[string]interface{}`
structure, read the "type" key, then use something like this library
to decode it into the proper structure.