# Structs | Lancet
一个强大的Go语言工具函数库

Skip to content
本页内容

# Structs ​

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

## 源码: ​

- https://github.com/duke-git/lancet/blob/main/structs/struct.go

- https://github.com/duke-git/lancet/blob/main/structs/field.go

## 用法: ​

go

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

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

## 目录: ​

- New
- ToMap
- Fields
- Field
- IsStruct
- Tag
- Name
- Value
- Kind
- IsEmbedded
- IsExported
- IsZero
- IsSlice

## API 文档: ​

### New ​

`Struct`结构体的构造函数

函数签名:

go

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

    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
    }

    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)

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

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

go

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

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

    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

    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
    }

    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

    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
    }

    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

    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
    }

    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

    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
    }

    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

    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
    }

    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

    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
    }

    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

    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
    }

    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

    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
    }

    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

    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
    }

    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

    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
    }

    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

    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
    }

    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
    }