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

..03-May-2022-

.gitignoreH A D14-Sep-2018259

.travis.ymlH A D14-Sep-2018302

LICENSEH A D14-Sep-20181.1 KiB

README.mdH A D14-Sep-20184.4 KiB

field.goH A D14-Sep-20183.8 KiB

field_test.goH A D14-Sep-20187.4 KiB

structs.goH A D14-Sep-201814.9 KiB

structs_example_test.goH A D14-Sep-20186.4 KiB

structs_test.goH A D14-Sep-201824.7 KiB

tags.goH A D14-Sep-2018695

tags_test.goH A D14-Sep-2018842

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 uninitialized
85z := s.IsZero()           // Check if all fields are uninitialized
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
144s := structs.New(server)
145
146for _, f := range s.Fields() {
147	fmt.Printf("field name: %+v\n", f.Name())
148
149	if f.IsExported() {
150		fmt.Printf("value   : %+v\n", f.Value())
151		fmt.Printf("is zero : %+v\n", f.IsZero())
152	}
153}
154```
155
156## Credits
157
158 * [Fatih Arslan](https://github.com/fatih)
159 * [Cihangir Savas](https://github.com/cihangir)
160
161## License
162
163The MIT License (MIT) - see LICENSE.md for more details
164