Update README.md
This commit is contained in:
parent
716932739a
commit
e10d13000f
22
README.md
22
README.md
@ -9,9 +9,9 @@ go get github.com/fatih/structure
|
|||||||
```
|
```
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
Below is an example which converts a **struct** to a **map**
|
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
// Lets define and declare a struct
|
||||||
type Server struct {
|
type Server struct {
|
||||||
Name string
|
Name string
|
||||||
ID int32
|
ID int32
|
||||||
@ -23,31 +23,21 @@ s := &Server{
|
|||||||
ID: 123456,
|
ID: 123456,
|
||||||
Enabled: true,
|
Enabled: true,
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```go
|
||||||
|
// convert it to a map[string]interface{}
|
||||||
m, err := structure.ToMap(s)
|
m, err := structure.ToMap(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("%#v", m)
|
fmt.Printf("%#v", m)
|
||||||
// Output: map[string]interface {}{"Name":"Arslan", "ID":123456, "Enabled":true}
|
// prints: map[string]interface {}{"Name":"Arslan", "ID":123456, "Enabled":true}
|
||||||
```
|
```
|
||||||
|
|
||||||
Test if the given variable is a struct or not
|
|
||||||
|
|
||||||
```go
|
```go
|
||||||
type Server struct {
|
// check if it's a struct or a pointer to struct
|
||||||
Name string
|
|
||||||
ID int32
|
|
||||||
Enabled bool
|
|
||||||
}
|
|
||||||
|
|
||||||
s := &Server{
|
|
||||||
Name: "Arslan",
|
|
||||||
ID: 123456,
|
|
||||||
Enabled: true,
|
|
||||||
}
|
|
||||||
|
|
||||||
if structure.IsStruct(s) {
|
if structure.IsStruct(s) {
|
||||||
fmt.Println("s is a struct")
|
fmt.Println("s is a struct")
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user