Skip to content

Commit 0759dae

Browse files
committed
Add example to readme
1 parent 40a5f24 commit 0759dae

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,42 @@
1717
* ability to **rename keys** during conversion
1818
* ability to **omit keys** based on field name, value or tag etc.
1919
* ability to **map elements** during conversion
20+
21+
## Installation
22+
23+
```shell
24+
# Add mapify to your Go module:
25+
go get github.com/elgopher/mapify
26+
```
27+
28+
## Hello, world!
29+
30+
```go
31+
package main
32+
33+
import (
34+
"fmt"
35+
36+
"github.com/elgopher/mapify"
37+
)
38+
39+
// This example shows how to convert struct into map
40+
func main() {
41+
s := SomeStruct{Field: "value"}
42+
43+
// create Mapper instance. Here default parameters are used.
44+
mapper := mapify.Mapper{}
45+
// MapAny maps any object - this can be a struct, slice or map. The whole object is traversed in order to find
46+
// all nested structs. Each struct will be converted to map[string]interface{}
47+
result, err := mapper.MapAny(s)
48+
if err != nil {
49+
panic(err)
50+
}
51+
52+
fmt.Printf("%+v", result) // map[Field:value]
53+
}
54+
55+
type SomeStruct struct {
56+
Field string
57+
}
58+
```

_examples/hello/hello.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/elgopher/mapify"
7+
)
8+
9+
// This example shows how to convert struct into map
10+
func main() {
11+
s := SomeStruct{Field: "value"}
12+
13+
// create Mapper instance. Here default parameters are used.
14+
mapper := mapify.Mapper{}
15+
// MapAny maps any object - this can be a struct, slice or map. The whole object is traversed in order to find
16+
// all nested structs. Each struct will be converted to map[string]interface{}
17+
result, err := mapper.MapAny(s)
18+
if err != nil {
19+
panic(err)
20+
}
21+
22+
fmt.Printf("%+v", result) // map[Field:value]
23+
}
24+
25+
type SomeStruct struct {
26+
Field string
27+
}

0 commit comments

Comments
 (0)