2014-08-02 01:02:04 +03:00
2014-07-26 14:08:52 +03:00
2014-07-26 14:08:52 +03:00
2014-07-30 23:19:49 +03:00
2014-08-02 01:02:04 +03:00
2014-08-02 01:02:04 +03:00
2014-08-02 01:02:04 +03:00

Structure GoDoc Build Status

Structure contains various utilities 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:    "gopher",
	ID:      123456,
	Enabled: true,
}
// 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)

// Return the struct name
// => "Server"
n := structure.Name(s)

// Check if field name exists
// => true
n := structure.Has(s, "Enabled")

// Check if the fields of a struct is initialized or not.
if !structure.IsZero(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")
}

Description
Utilities for Go structs
Readme MIT 200 KiB
Languages
Go 100%