GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — lancet/docs/en/api/packages/struct.md at main · duke-git/lancet

Expand file tree

/

struct.md

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
621 lines (448 loc) · 10.2 KB
/

struct.md

Copy path

File metadata and controls

621 lines (448 loc) · 10.2 KB
Raw
Copy raw file
Download raw file
Outline
Edit and raw actions

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/field.go

Usage:

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

Index:

  • New
  • ToMap
  • Fields
  • Field
  • IsStruct
  • Tag
  • Name
  • TypeName
  • Value
  • Kind
  • IsEmbedded
  • IsExported
  • IsZero
  • IsSlice
  • IsTargetType

Documentation:

New

The constructor function of the `Struct`

Signature:

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

Example:Run

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.ToMap())

    // Output:
    // map[name:11] <nil>
}

ToMap

convert a valid struct to a map

Signature:

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

In addition, provided a convenient static function ToMap

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

Example:Run

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

Fields

Get all fields of a given struct, that the fields are abstract struct field

Signature:

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

Example:Run

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

Get an abstract field of a struct by given field name

Signature:

func (s *Struct) Field(name string) (*Field, bool)

Example:Run

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, found := s.Field("Name")

    fmt.Println(f.Value())
    fmt.Println(found)

    // Output:
    // 11
    // true
}

IsStruct

Check if the struct is valid

Signature:

func (s *Struct) IsStruct() bool

Example:Run

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

Get a `Tag` of the `Field`, `Tag` is a abstract struct field tag

Signature:

func (f *Field) Tag() *Tag

Example:Run

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

Get the `Field` underlying value

Signature:

func (f *Field) Value() any

Example:Run

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

Check if the field is an embedded field

Signature:

func (f *Field) IsEmbedded() bool

Example:Run

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

Check if the field is exported

Signature:

func (f *Field) IsExported() bool

Example:Run

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

Check if the field is zero value

Signature:

func (f *Field) IsZero() bool

Example:Run

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

Get the field name

Signature:

func (f *Field) Name() string

Example:Run

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

Get the field's kind

Signature:

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

Example:Run

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
}

TypeName

Return struct type name.

Signature:

func (s *Struct) TypeName() string

Example:Run

package main

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

func main() {
    type Parent struct {
        Name string
        Age  int
    }

    p := &Parent{Age: 11}
    s := structs.New(p)

    fmt.Println(s.TypeName())

    // Output:
    // Parent
}

IsSlice

Check if the field is a slice

Signature:

func (f *Field) IsSlice() bool

Example:Run

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

check if a struct field type is target type or not

Signature:

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

Example:Run

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
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.