• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..08-Aug-2017-

.gitignoreH A D08-Aug-2017259 2418

.travis.ymlH A D08-Aug-2017420 1211

LICENSEH A D08-Aug-20171.1 KiB2117

README.mdH A D08-Aug-20174.5 KiB165121

field.goH A D08-Aug-20173.2 KiB12774

structs.goH A D08-Aug-201711.7 KiB450217

tags.goH A D08-Aug-2017696 3315

README.md

1# Structs [![GoDoc](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)](http://godoc.org/github.com/fatih/structs) [![Build Status](http://img.shields.io/travis/fatih/structs.svg?style=flat-square)](https://travis-ci.org/fatih/structs) [![Coverage Status](http://img.shields.io/coveralls/fatih/structs.svg?style=flat-square)](https://coveralls.io/r/fatih/structs)
2
3Structs contains various utilities to work with Go (Golang) structs. It was
4initially used by me to convert a struct into a `map[string]interface{}`. With
5time I've added other utilities for structs.  It's basically a high level
6package based on primitives from the reflect package. Feel free to add new
7functions or improve the existing code.
8
9## Install
10
11```bash
12go get github.com/fatih/structs
13```
14
15## Usage and Examples
16
17Just like the standard lib `strings`, `bytes` and co packages, `structs` has
18many global functions to manipulate or organize your struct data. Lets define
19and declare a struct:
20
21```go
22type Server struct {
23	Name        string `json:"name,omitempty"`
24	ID          int
25	Enabled     bool
26	users       []string // not exported
27	http.Server          // embedded
28}
29
30server := &Server{
31	Name:    "gopher",
32	ID:      123456,
33	Enabled: true,
34}
35```
36
37```go
38// Convert a struct to a map[string]interface{}
39// => {"Name":"gopher", "ID":123456, "Enabled":true}
40m := structs.Map(server)
41
42// Convert the values of a struct to a []interface{}
43// => ["gopher", 123456, true]
44v := structs.Values(server)
45
46// Convert the names of a struct to a []string
47// (see "Names methods" for more info about fields)
48n := structs.Names(server)
49
50// Convert the values of a struct to a []*Field
51// (see "Field methods" for more info about fields)
52f := structs.Fields(server)
53
54// Return the struct name => "Server"
55n := structs.Name(server)
56
57// Check if any field of a struct is initialized or not.
58h := structs.HasZero(server)
59
60// Check if all fields of a struct is initialized or not.
61z := structs.IsZero(server)
62
63// Check if server is a struct or a pointer to struct
64i := structs.IsStruct(server)
65```
66
67### Struct methods
68
69The structs functions can be also used as independent methods by creating a new
70`*structs.Struct`. This is handy if you want to have more control over the
71structs (such as retrieving a single Field).
72
73```go
74// Create a new struct type:
75s := structs.New(server)
76
77m := s.Map()              // Get a map[string]interface{}
78v := s.Values()           // Get a []interface{}
79f := s.Fields()           // Get a []*Field
80n := s.Names()            // Get a []string
81f := s.Field(name)        // Get a *Field based on the given field name
82f, ok := s.FieldOk(name)  // Get a *Field based on the given field name
83n := s.Name()             // Get the struct name
84h := s.HasZero()          // Check if any field is initialized
85z := s.IsZero()           // Check if all fields are initialized
86```
87
88### Field methods
89
90We can easily examine a single Field for more detail. Below you can see how we
91get and interact with various field methods:
92
93
94```go
95s := structs.New(server)
96
97// Get the Field struct for the "Name" field
98name := s.Field("Name")
99
100// Get the underlying value,  value => "gopher"
101value := name.Value().(string)
102
103// Set the field's value
104name.Set("another gopher")
105
106// Get the field's kind, kind =>  "string"
107name.Kind()
108
109// Check if the field is exported or not
110if name.IsExported() {
111	fmt.Println("Name field is exported")
112}
113
114// Check if the value is a zero value, such as "" for string, 0 for int
115if !name.IsZero() {
116	fmt.Println("Name is initialized")
117}
118
119// Check if the field is an anonymous (embedded) field
120if !name.IsEmbedded() {
121	fmt.Println("Name is not an embedded field")
122}
123
124// Get the Field's tag value for tag name "json", tag value => "name,omitempty"
125tagValue := name.Tag("json")
126```
127
128Nested structs are supported too:
129
130```go
131addrField := s.Field("Server").Field("Addr")
132
133// Get the value for addr
134a := addrField.Value().(string)
135
136// Or get all fields
137httpServer := s.Field("Server").Fields()
138```
139
140We can also get a slice of Fields from the Struct type to iterate over all
141fields. This is handy if you wish to examine all fields:
142
143```go
144// Convert the fields of a struct to a []*Field
145fields := s.Fields()
146
147for _, f := range fields {
148	fmt.Printf("field name: %+v\n", f.Name())
149
150	if f.IsExported() {
151		fmt.Printf("value   : %+v\n", f.Value())
152		fmt.Printf("is zero : %+v\n", f.IsZero())
153	}
154}
155```
156
157## Credits
158
159 * [Fatih Arslan](https://github.com/fatih)
160 * [Cihangir Savas](https://github.com/cihangir)
161
162## License
163
164The MIT License (MIT) - see LICENSE.md for more details
165