Fatih Arslan fbcdb369b6 Merge branch 'master' of https://github.com/fatih/structure
Conflicts:
	structure_example_test.go
2014-07-29 13:14:31 +03:00
2014-07-26 14:08:52 +03:00
2014-07-26 14:08:52 +03:00

Structure GoDoc Build Status

Structure contains various utilitis to work with Go (Golang) structs.

Install

go get github.com/fatih/structure

Usage and Examples

Lets define and declare a struct

type Server struct {
	Name    string
	ID      int32
	Enabled bool
}

s := &Server{
	Name:    "Arslan",
	ID:      123456,
	Enabled: true,
}

ToMap()

Convert a struct to a map[string]interface{}

m := structure.ToMap(s)

// prints: map[string]interface {}{"Name":"Arslan", "ID":123456, "Enabled":true}
fmt.Printf("%#v", m)

ToSlice()

Convert the values of a struct to a []interface{}. Slice values are sorted by default according to the field names.

m := structure.ToSlice(s)

// prints: []interface {}{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.

m := structure.Fields(s)

// prints: []string{"Enabled", "ID", "Name"}
fmt.Printf("%#v", m)

IsStruct()

Check if it's a struct or a pointer to struct

if structure.IsStruct(s) {
    fmt.Println("s is a struct") 
}
Description
Utilities for Go structs
Readme MIT 200 KiB
Languages
Go 100%