54 lines
1.1 KiB
Markdown
54 lines
1.1 KiB
Markdown
# Structure [](http://godoc.org/github.com/fatih/structure) [](https://travis-ci.org/fatih/structure)
|
|
|
|
Structure contains various utilitis to work with Go (Golang) structs.
|
|
|
|
## Install
|
|
|
|
```bash
|
|
go get github.com/fatih/structure
|
|
```
|
|
|
|
## Usage and Examples
|
|
|
|
Lets define and declare a struct
|
|
|
|
```go
|
|
type Server struct {
|
|
Name string
|
|
ID int32
|
|
Enabled bool
|
|
}
|
|
|
|
s := &Server{
|
|
Name: "gopher",
|
|
ID: 123456,
|
|
Enabled: true,
|
|
}
|
|
```
|
|
|
|
```go
|
|
// Convert a struct to a map[string]interface{}
|
|
// => {"Name":"gopher", "ID":123456, "Enabled":true}
|
|
m := structure.Map(s)
|
|
|
|
// Convert the values of a struct to a []interface{}
|
|
// => [true, 123456, "gopher"]
|
|
v := structure.Values(s)
|
|
|
|
// Convert the fields of a struct to a []string.
|
|
// => ["Enabled", "ID", "Name"]
|
|
f := structure.Fields(s)
|
|
|
|
// Check if the fields of a struct is initialized or not.
|
|
if structure.IsValid(s) {
|
|
fmt.Println("s is initialized")
|
|
}
|
|
|
|
// Check if it's a struct or a pointer to struct
|
|
if structure.IsStruct(s) {
|
|
fmt.Println("s is a struct")
|
|
}
|
|
|
|
```
|
|
|