用于 Go 结构的工具
This project is not maintained anymore and is archived. Feel free to fork and make your own changes if needed. For more detail read my blog post: Taking an indefinite sabbatical from my projects
Thanks to everyone for their valuable feedback and contributions.
Structs contains various utilities to work with Go (Golang) structs. It was
initially used by me to convert a struct into a map[string]interface{}. With
time I've added other utilities for structs. It's basically a high level
package based on primitives from the reflect package. Feel free to add new
functions or improve the existing code.
go get github.com/fatih/structs
Just like the standard lib strings, bytes and co packages, structs has
many global functions to manipulate or organize your struct data. Lets define
and declare a struct:
type Server struct {
Name string `json:"name,omitempty"`
ID int
Enabled bool
users []string // not exported
http.Server // embedded
}
server := &Server{
Name: "gopher",
ID: 123456,
Enabled: true,
}
…
The structs functions can be also used as independent methods by creating a new
*structs.Struct. This is handy if you want to have more control over the
structs (such as retrieving a single Field).
// Create a new struct type:
s := structs.New(server)
m := s.Map() // Get a map[string]interface{}
v := s.Values() // Get a []interface{}
f := s.Fields() // Get a []*Field
n := s.Names() // Get a []string
f := s.Field(name) // Get a *Field based on the given field name
f, ok := s.FieldOk(name) // Get a *Field based on the given field name
n := s.Name() // Get the struct name
h := s.HasZero() // Check if any field is uninitialized
z := s.IsZero() // Check if all fields are uninitialized
We can easily examine a single Field for more detail. Below you can see how we get and interact with various field methods:
…
Nested structs are supported too:
addrField := s.Field("Server").Field("Addr")
// Get the value for addr
a := addrField.Value().(string)
// Or get all fields
httpServer := s.Field("Server").Fields()
We can also get a slice of Fields from the Struct type to iterate over all fields. This is handy if you wish to examine all fields:
s := structs.New(server)
for _, f := range s.Fields() {
fmt.Printf("field name: %+v\n", f.Name())
if f.IsExported() {
fmt.Printf("value : %+v\n", f.Value())
fmt.Printf("is zero : %+v\n", f.IsZero())
}
}
The MIT License (MIT) - see LICENSE.md for more details
暂无开放 Issues,或尚未同步最近议题。