structs/README.md
2014-07-29 23:07:59 +03:00

74 lines
1.3 KiB
Markdown

# Structure [![GoDoc](https://godoc.org/github.com/fatih/structure?status.svg)](http://godoc.org/github.com/fatih/structure) [![Build Status](https://travis-ci.org/fatih/structure.svg)](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: "Arslan",
ID: 123456,
Enabled: true,
}
```
#### Map()
Convert a struct to a `map[string]interface{}`
```go
m := structure.Map(s)
// prints: {"Name":"Arslan", "ID":123456, "Enabled":true}
fmt.Printf("%#v", m)
```
#### Values()
Convert the values of a struct to a `[]interface{}`. Slice values are
**sorted** by default according to the field names.
```go
m := structure.Values(s)
// prints: [true, 123456, "Arslan"]
fmt.Printf("%#v", m)
```
#### Fields()
Convert the fields of a struct to a `[]string`. Slice values are **sorted** by
default according to the field names.
```go
m := structure.Fields(s)
// prints: ["Enabled", "ID", "Name"]
fmt.Printf("%#v", m)
```
#### IsStruct()
Check if it's a struct or a pointer to struct
```go
if structure.IsStruct(s) {
fmt.Println("s is a struct")
}
```