# Structs Struct is abstract struct for provide several high level functions
## Source: - [https://github.com/duke-git/lancet/blob/main/structs/struct.go](https://github.com/duke-git/lancet/blob/main/structs/struct.go) ## Usage: ```go import ( "github.com/duke-git/lancet/v2/structs" ) ``` ## Index: - [New](#New) - [ToMap](#ToMap) - [Fields](#Fields) - [Field](#Field) - [IsStruct](#IsStruct) ## Documentation: ### NewThe constructor function of the `Struct`
Signature: ```go func New(value any, tagName ...string) *Struct ``` Example: ```go package main import ( "github.com/duke-git/lancet/v2/structs" ) func main() { type People struct { Name string `json:"name"` } p1 := &People{Name: "11"} s := structs.New(p1) // to do something } ``` ### ToMapconvert a valid struct to a map
Signature: ```go func (s *Struct) ToMap() (map[string]any, error) ``` > In addition, provided a convenient static function ToMap ```go func ToMap(v any) (map[string]any, error) ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/structs" ) func main() { type People struct { Name string `json:"name"` } p1 := &People{Name: "11"} // use constructor function s1 := structs.New(p1) m1, _ := s1.ToMap() fmt.Println(m1) // use static function m2, _ := structs.ToMap(p1) fmt.Println(m2) // Output: // map[name:11] // map[name:11] } ``` ### FieldsGet all fields of a given struct, that the fields are abstract struct field
Signature: ```go func (s *Struct) Fields() []*Field ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/structs" ) func main() { type People struct { Name string `json:"name"` } p1 := &People{Name: "11"} s := structs.New(p1) fields := s.Fields() fmt.Println(len(fields)) // Output: // 1 } ``` ### FieldGet an abstract field of a struct by given field name
Signature: ```go func (s *Struct) Field(name string) *Field ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/structs" ) func main() { type People struct { Name string `json:"name"` } p1 := &People{Name: "11"} s := structs.New(p1) f := s.Field("Name") fmt.Println(f.Value()) // Output: // 11 } ``` ### IsStructCheck if the struct is valid
Signature: ```go func (s *Struct) IsStruct() bool ``` Example: ```go package main import ( "fmt" "github.com/duke-git/lancet/v2/structs" ) func main() { type People struct { Name string `json:"name"` } p1 := &People{Name: "11"} s := structs.New(p1) fmt.Println(s.IsStruct()) // Output: // true } ```