structs

package
v2.3.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 1, 2025 License: MIT Imports: 5 Imported by: 4

Documentation

Overview

Package structs provide several high level functions to manipulate struct, tag, and field.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToMap

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

ToMap convert struct to map, only convert exported struct field map key is specified same as struct field tag `json` value. Play: https://go.dev/play/p/qQbLySBgerZ

Types

type Field

type Field struct {
	Struct
	// contains filtered or unexported fields
}

Field is abstract struct field for provide several high level functions

func (*Field) IsEmbedded

func (f *Field) IsEmbedded() bool

IsEmbedded returns true if the given field is an embedded field. Play: https://go.dev/play/p/wV2PrbYm3Ec

func (*Field) IsExported

func (f *Field) IsExported() bool

IsExported returns true if the given field is exported. Play: https://go.dev/play/p/csK4AXYaNbJ

func (*Field) IsNil added in v2.2.9

func (f *Field) IsNil() bool

IsNil returns true if the given field is nil value.

func (*Field) IsSlice

func (f *Field) IsSlice() bool

IsSlice check if a struct field type is slice or not Play: https://go.dev/play/p/MKz4CgBIUrU

func (*Field) IsTargetType added in v2.2.9

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

IsTargetType check if a struct field type is target type or not Play: https://go.dev/play/p/Ig75P-agN39

func (*Field) IsZero

func (f *Field) IsZero() bool

IsZero returns true if the given field is zero value. Play: https://go.dev/play/p/RzqpGISf87r

func (*Field) Kind

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

Kind returns the field's kind Play: https://go.dev/play/p/wg4NlcUNG5o

func (*Field) Name

func (f *Field) Name() string

Name returns the name of the given field Play: https://go.dev/play/p/zfIGlqsatee

func (*Field) Tag

func (f *Field) Tag() *Tag

Tag returns the value that the key in the tag string. Play: https://go.dev/play/p/DVrx5HvvUJr

func (*Field) Value

func (f *Field) Value() any

Value returns the underlying value of the field. Play: https://go.dev/play/p/qufYEU2o4Oi

type Struct

type Struct struct {
	TagName string
	// contains filtered or unexported fields
}

Struct is abstract struct for provide several high level functions

func New

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

New returns a new *Struct Play: https://go.dev/play/p/O29l8kk-Z17

func (*Struct) Field

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

Field returns a Field if the given field name was found Play: https://go.dev/play/p/KocZMSYarza

func (*Struct) Fields

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

Fields returns all the struct fields within a slice Play: https://go.dev/play/p/w3Kk_CyVY7D

func (*Struct) IsStruct

func (s *Struct) IsStruct() bool

IsStruct returns true if the given rvalue is a struct Play: https://go.dev/play/p/bU2FSdkbK1C

func (*Struct) ToMap

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

ToMap converts the given struct to a map[string]any, where the keys of the keys are the field names and the values of the map are the values of the fields. The default map key is the struct field name, but you can change it. The `json` key is the default tag key. Example:

	// default
	Name string `json:"name"`

	// ignore the field
	Name string							// no tag
 	Age string `json:"-"`				// json ignore tag
	sex string							// unexported field
 	Goal int   `json:"goal,omitempty"`  // omitempty if the field is zero value

 	// custom map key
 	Name string `json:"myName"`

ToMap convert the exported fields of a struct to map. Play: https://go.dev/play/p/qQbLySBgerZ

func (*Struct) TypeName added in v2.3.8

func (s *Struct) TypeName() string

TypeName return struct type name Play: https://go.dev/play/p/todo

type Tag

type Tag struct {
	Name    string
	Options []string
}

Tag is abstract struct field tag

func (*Tag) HasOption

func (t *Tag) HasOption(opt string) bool

HasOption check if a struct field tag has option setting.

func (*Tag) IsEmpty

func (t *Tag) IsEmpty() bool

IsEmpty check if a struct field has tag setting.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL