Skip to content

Structs

structs 包封装了一个抽象的Struct结构体,提供了操作struct的相关函数

源码:

用法:

go
import (
    "github.com/duke-git/lancet/v2/structs"
)

目录:

API 文档:

New

`Struct`结构体的构造函数

函数签名:

go
func New(value any, tagName ...string) *Struct

示例:

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
}

ToMap

将一个合法的struct对象转换为map[string]any

函数签名:

go
func (s *Struct) ToMap() (map[string]any, error)

除此之外,提供一个便捷的静态方法 ToMap

go
func ToMap(v any) (map[string]any, error)

示例:

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"}
    s1 := structs.New(p1)
    m1, _ := s1.ToMap()

    fmt.Println(m1)

    // 如果不需要Struct更多的方法,可以直接使用ToMap
    m2, _ := structs.ToMap(p1)

    fmt.Println(m2)

    // Output:
    // map[name:11]
    // map[name:11]
}

Fields

获取一个struct对象的属性列表

函数签名:

go
func (s *Struct) Fields() []*Field

示例:

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
}

Field

根据属性名获取一个struct对象的属性

函数签名:

go
func (s *Struct) Field(name string) *Field

示例:

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
}

IsStruct

判断是否为一个合法的struct对象

函数签名:

go
func (s *Struct) IsStruct() bool

示例:

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
}

Tag

获取`Field`的`Tag`,默认的tag key是json

函数签名:

go
func (f *Field) Tag() *Tag

示例:

go
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
}

Value

获取`Field`属性的值

函数签名:

go
func (f *Field) Value() any

示例:

go
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
}

IsEmbedded

判断属性是否为嵌入

函数签名:

go
func (f *Field) IsEmbedded() bool

示例:

go
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
}

IsExported

判断属性是否导出

函数签名:

go
func (f *Field) IsExported() bool

示例:

go
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
}

IsZero

判断属性是否为零值

函数签名:

go
func (f *Field) IsZero() bool

示例:

go
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
}

Name

获取属性名

函数签名:

go
func (f *Field) Name() string

示例:

go
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
}

Kind

获取属性Kind

函数签名:

go
func (f *Field) Kind() reflect.Kind

示例:

go
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
}

IsSlice

判断属性是否是切片

函数签名:

go
func (f *Field) IsSlice() bool

示例:

go
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
}

IsTargetType

判断属性是否是目标类型

函数签名:

go
func (f *Field) IsTargetType(targetType reflect.Kind) bool

示例:

go
package main

import (
    "fmt"
    "reflect"
    "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)
    n, _ := s.Field("Name")
	a, _ := s.Field("arr")
    
    fmt.Println(n.IsTargetType(reflect.String))
    fmt.Println(a.IsTargetType(reflect.Slice))
    
    // Output: 
    // true
    // true
}