import{_ as l,o as p,c as o,k as s,a,X as n}from"./chunks/framework.6e839c56.js";const k=JSON.parse('{"title":"Structs","description":"","frontmatter":{},"headers":[],"relativePath":"en/api/packages/struct.md","filePath":"en/api/packages/struct.md"}'),e={name:"en/api/packages/struct.md"},t=s("h1",{id:"Structs",tabindex:"-1"},[a("Structs "),s("a",{class:"header-anchor",href:"#Structs","aria-label":'Permalink to "Structs"'},"")],-1),c=s("p",null,"Struct is abstract struct for provide several high level functions",-1),r=s("div",{STYLE:"page-break-after: always;"},null,-1),y=s("h2",{id:"Source-",tabindex:"-1"},[a("Source: "),s("a",{class:"header-anchor",href:"#Source-","aria-label":'Permalink to "Source:"'},"")],-1),F=s("ul",null,[s("li",null,[s("p",null,[s("a",{href:"https://github.com/duke-git/lancet/blob/main/structs/struct.go",target:"_blank",rel:"noreferrer"},"https://github.com/duke-git/lancet/blob/main/structs/struct.go")])]),s("li",null,[s("p",null,[s("a",{href:"https://github.com/duke-git/lancet/blob/main/structs/field.go",target:"_blank",rel:"noreferrer"},"https://github.com/duke-git/lancet/blob/main/structs/field.go")])])],-1),i=s("div",{STYLE:"page-break-after: always;"},null,-1),A=n(`
import (
"github.com/duke-git/lancet/v2/structs"
)import (
"github.com/duke-git/lancet/v2/structs"
)The constructor function of the \`Struct\`
Signature:
func New(value any, tagName ...string) *Structfunc New(value any, tagName ...string) *StructExample:
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
}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
}convert a valid struct to a map
Signature:
func (s *Struct) ToMap() (map[string]any, error)func (s *Struct) ToMap() (map[string]any, error)In addition, provided a convenient static function ToMap
func ToMap(v any) (map[string]any, error)func ToMap(v any) (map[string]any, error)Example:
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]
}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]
}Get all fields of a given struct, that the fields are abstract struct field
Signature:
func (s *Struct) Fields() []*Fieldfunc (s *Struct) Fields() []*FieldExample:
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
}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
}Get an abstract field of a struct by given field name
Signature:
func (s *Struct) Field(name string) *Fieldfunc (s *Struct) Field(name string) *FieldExample:
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
}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
}Check if the struct is valid
Signature:
func (s *Struct) IsStruct() boolfunc (s *Struct) IsStruct() boolExample:
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
}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
}Get a \`Tag\` of the \`Field\`, \`Tag\` is a abstract struct field tag
Signature:
func (f *Field) Tag() *Tagfunc (f *Field) Tag() *TagExample:
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/structs"
)
func main() {
type Parent struct {
Name string \`json:"name,omitempty"\`
}
p1 := &Parent{"111"}
s := structs.New(p1)
n, _ := s.Field("Name")
tag := n.Tag()
fmt.Println(tag.Name)
// Output:
// name
}package main
import (
"fmt"
"github.com/duke-git/lancet/v2/structs"
)
func main() {
type Parent struct {
Name string \`json:"name,omitempty"\`
}
p1 := &Parent{"111"}
s := structs.New(p1)
n, _ := s.Field("Name")
tag := n.Tag()
fmt.Println(tag.Name)
// Output:
// name
}Get the \`Field\` underlying value
Signature:
func (f *Field) Value() anyfunc (f *Field) Value() anyExample:
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/structs"
)
func main() {
type Parent struct {
Name string \`json:"name,omitempty"\`
}
p1 := &Parent{"111"}
s := structs.New(p1)
n, _ := s.Field("Name")
fmt.Println(n.Value())
// Output:
// 111
}package main
import (
"fmt"
"github.com/duke-git/lancet/v2/structs"
)
func main() {
type Parent struct {
Name string \`json:"name,omitempty"\`
}
p1 := &Parent{"111"}
s := structs.New(p1)
n, _ := s.Field("Name")
fmt.Println(n.Value())
// Output:
// 111
}Check if the field is an embedded field
Signature:
func (f *Field) IsEmbedded() boolfunc (f *Field) IsEmbedded() boolExample:
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/structs"
)
func main() {
type Parent struct {
Name string
}
type Child struct {
Parent
Age int
}
c1 := &Child{}
c1.Name = "111"
c1.Age = 11
s := structs.New(c1)
n, _ := s.Field("Name")
a, _ := s.Field("Age")
fmt.Println(n.IsEmbedded())
fmt.Println(a.IsEmbedded())
// Output:
// true
// false
}package main
import (
"fmt"
"github.com/duke-git/lancet/v2/structs"
)
func main() {
type Parent struct {
Name string
}
type Child struct {
Parent
Age int
}
c1 := &Child{}
c1.Name = "111"
c1.Age = 11
s := structs.New(c1)
n, _ := s.Field("Name")
a, _ := s.Field("Age")
fmt.Println(n.IsEmbedded())
fmt.Println(a.IsEmbedded())
// Output:
// true
// false
}Check if the field is exported
Signature:
func (f *Field) IsExported() boolfunc (f *Field) IsExported() boolExample:
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/structs"
)
func main() {
type Parent struct {
Name string
age int
}
p1 := &Parent{Name: "11", age: 11}
s := structs.New(p1)
n, _ := s.Field("Name")
a, _ := s.Field("age")
fmt.Println(n.IsExported())
fmt.Println(a.IsExported())
// Output:
// true
// false
}package main
import (
"fmt"
"github.com/duke-git/lancet/v2/structs"
)
func main() {
type Parent struct {
Name string
age int
}
p1 := &Parent{Name: "11", age: 11}
s := structs.New(p1)
n, _ := s.Field("Name")
a, _ := s.Field("age")
fmt.Println(n.IsExported())
fmt.Println(a.IsExported())
// Output:
// true
// false
}Check if the field is zero value
Signature:
func (f *Field) IsZero() boolfunc (f *Field) IsZero() boolExample:
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/structs"
)
func main() {
type Parent struct {
Name string
Age int
}
p1 := &Parent{Age: 11}
s := structs.New(p1)
n, _ := s.Field("Name")
a, _ := s.Field("Age")
fmt.Println(n.IsZero())
fmt.Println(a.IsZero())
// Output:
// true
// false
}package main
import (
"fmt"
"github.com/duke-git/lancet/v2/structs"
)
func main() {
type Parent struct {
Name string
Age int
}
p1 := &Parent{Age: 11}
s := structs.New(p1)
n, _ := s.Field("Name")
a, _ := s.Field("Age")
fmt.Println(n.IsZero())
fmt.Println(a.IsZero())
// Output:
// true
// false
}Get the field name
Signature:
func (f *Field) Name() stringfunc (f *Field) Name() stringExample:
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/structs"
)
func main() {
type Parent struct {
Name string
Age int
}
p1 := &Parent{Age: 11}
s := structs.New(p1)
n, _ := s.Field("Name")
a, _ := s.Field("Age")
fmt.Println(n.Name())
fmt.Println(a.Name())
// Output:
// Name
// Age
}package main
import (
"fmt"
"github.com/duke-git/lancet/v2/structs"
)
func main() {
type Parent struct {
Name string
Age int
}
p1 := &Parent{Age: 11}
s := structs.New(p1)
n, _ := s.Field("Name")
a, _ := s.Field("Age")
fmt.Println(n.Name())
fmt.Println(a.Name())
// Output:
// Name
// Age
}Get the field's kind
Signature:
func (f *Field) Kind() reflect.Kindfunc (f *Field) Kind() reflect.KindExample:
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/structs"
)
func main() {
type Parent struct {
Name string
Age int
}
p1 := &Parent{Age: 11}
s := structs.New(p1)
n, _ := s.Field("Name")
a, _ := s.Field("Age")
fmt.Println(n.Kind())
fmt.Println(a.Kind())
// Output:
// string
// int
}package main
import (
"fmt"
"github.com/duke-git/lancet/v2/structs"
)
func main() {
type Parent struct {
Name string
Age int
}
p1 := &Parent{Age: 11}
s := structs.New(p1)
n, _ := s.Field("Name")
a, _ := s.Field("Age")
fmt.Println(n.Kind())
fmt.Println(a.Kind())
// Output:
// string
// int
}Check if the field is a slice
Signature:
func (f *Field) IsSlice() boolfunc (f *Field) IsSlice() boolExample:
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/structs"
)
func main() {
type Parent struct {
Name string
arr []int
}
p1 := &Parent{arr: []int{1, 2, 3}}
s := structs.New(p1)
a, _ := s.Field("arr")
fmt.Println(a.IsSlice())
// Output:
// true
}package main
import (
"fmt"
"github.com/duke-git/lancet/v2/structs"
)
func main() {
type Parent struct {
Name string
arr []int
}
p1 := &Parent{arr: []int{1, 2, 3}}
s := structs.New(p1)
a, _ := s.Field("arr")
fmt.Println(a.IsSlice())
// Output:
// true
}