Involved Source Filesfield.go
Package structs contains various utilities functions to work with structs.
tags.go
Package-Level Type Names (total 3, in which 2 are exported)
/* sort exporteds by: | */
Field represents a single struct field that encapsulates high level
functions around the field.
defaultTagstringfieldreflect.StructFieldvaluereflect.Value
Field returns the field from a nested struct. It panics if the nested struct
is not exported or if the field was not found.
FieldOk returns the field from a nested struct. The boolean returns whether
the field was found (true) or not (false).
Fields returns a slice of Fields. This is particular handy to get the fields
of a nested struct . A struct tag with the content of "-" ignores the
checking of that particular field. Example:
// Field is ignored by this package.
Field *http.Request `structs:"-"`
It panics if field is not exported or if field's kind is not struct
IsEmbedded returns true if the given field is an anonymous field (embedded)
IsExported returns true if the given field is exported.
IsZero returns true if the given field is not initialized (has a zero value).
It panics if the field is not exported.
Kind returns the fields kind, such as "string", "map", "bool", etc ..
Name returns the name of the given field
Set sets the field to given value v. It returns an error if the field is not
settable (not addressable or not exported) or if the given value's type
doesn't match the fields type.
Tag returns the value associated with key in the tag string. If there is no
such key in the tag, Tag returns the empty string.
Value returns the underlying value of the field. It panics if the field
is not exported.
Zero sets the field to its zero value. It returns an error if the field is not
settable (not addressable or not exported).
*T : gopkg.in/yaml.v2.IsZeroer
*T : gopkg.in/yaml.v3.IsZeroer
func Fields(s interface{}) []*Field
func (*Field).Field(name string) *Field
func (*Field).FieldOk(name string) (*Field, bool)
func (*Field).Fields() []*Field
func (*Struct).Field(name string) *Field
func (*Struct).FieldOk(name string) (*Field, bool)
func (*Struct).Fields() []*Field
func getFields(v reflect.Value, tagName string) []*Field
Struct encapsulates a struct type to provide several high level functions
around the struct.
TagNamestringrawinterface{}valuereflect.Value
Field returns a new Field struct that provides several high level functions
around a single struct field entity. It panics if the field is not found.
FieldOk returns a new Field struct that provides several high level functions
around a single struct field entity. The boolean returns true if the field
was found.
Fields returns a slice of Fields. A struct tag with the content of "-"
ignores the checking of that particular field. Example:
// Field is ignored by this package.
Field bool `structs:"-"`
It panics if s's kind is not struct.
FillMap is the same as Map. Instead of returning the output, it fills the
given map.
HasZero returns true if a field in a struct is not initialized (zero value).
A struct tag with the content of "-" ignores the checking of that particular
field. Example:
// Field is ignored by this package.
Field bool `structs:"-"`
A value with the option of "omitnested" stops iterating further if the type
is a struct. Example:
// Field is not processed further by this package.
Field time.Time `structs:"myName,omitnested"`
Field *http.Request `structs:",omitnested"`
Note that only exported fields of a struct can be accessed, non exported
fields will be neglected. It panics if s's kind is not struct.
IsZero returns true if all fields in a struct is a zero value (not
initialized) A struct tag with the content of "-" ignores the checking of
that particular field. Example:
// Field is ignored by this package.
Field bool `structs:"-"`
A value with the option of "omitnested" stops iterating further if the type
is a struct. Example:
// Field is not processed further by this package.
Field time.Time `structs:"myName,omitnested"`
Field *http.Request `structs:",omitnested"`
Note that only exported fields of a struct can be accessed, non exported
fields will be neglected. It panics if s's kind is not struct.
Map converts the given struct to a map[string]interface{}, where the keys
of the map are the field names and the values of the map the associated
values of the fields. The default key string is the struct field name but
can be changed in the struct field's tag value. The "structs" key in the
struct's field tag value is the key name. Example:
// Field appears in map as key "myName".
Name string `structs:"myName"`
A tag value with the content of "-" ignores that particular field. Example:
// Field is ignored by this package.
Field bool `structs:"-"`
A tag value with the content of "string" uses the stringer to get the value. Example:
// The value will be output of Animal's String() func.
// Map will panic if Animal does not implement String().
Field *Animal `structs:"field,string"`
A tag value with the option of "flatten" used in a struct field is to flatten its fields
in the output map. Example:
// The FieldStruct's fields will be flattened into the output map.
FieldStruct time.Time `structs:",flatten"`
A tag value with the option of "omitnested" stops iterating further if the type
is a struct. Example:
// Field is not processed further by this package.
Field time.Time `structs:"myName,omitnested"`
Field *http.Request `structs:",omitnested"`
A tag value with the option of "omitempty" ignores that particular field if
the field value is empty. Example:
// Field appears in map as key "myName", but the field is
// skipped if empty.
Field string `structs:"myName,omitempty"`
// Field appears in map as key "Field" (the default), but
// the field is skipped if empty.
Field string `structs:",omitempty"`
Note that only exported fields of a struct can be accessed, non exported
fields will be neglected.
Name returns the structs's type name within its package. For more info refer
to Name() function.
Names returns a slice of field names. A struct tag with the content of "-"
ignores the checking of that particular field. Example:
// Field is ignored by this package.
Field bool `structs:"-"`
It panics if s's kind is not struct.
Values converts the given s struct's field values to a []interface{}. A
struct tag with the content of "-" ignores the that particular field.
Example:
// Field is ignored by this package.
Field int `structs:"-"`
A value with the option of "omitnested" stops iterating further if the type
is a struct. Example:
// Fields is not processed further by this package.
Field time.Time `structs:",omitnested"`
Field *http.Request `structs:",omitnested"`
A tag value with the option of "omitempty" ignores that particular field and
is not added to the values if the field value is empty. Example:
// Field is skipped if empty
Field string `structs:",omitempty"`
Note that only exported fields of a struct can be accessed, non exported
fields will be neglected.
nested retrieves recursively all types for the given value and returns the
nested value.
structFields returns the exported struct fields for a given s struct. This
is a convenient helper method to avoid duplicate code in some of the
functions.
*T : gopkg.in/yaml.v2.IsZeroer
*T : gopkg.in/yaml.v3.IsZeroer
func New(s interface{}) *Struct
tagOptions contains a slice of tag options
Has returns true if the given option is available in tagOptions
func parseTag(tag string) (string, tagOptions)
Package-Level Functions (total 13, in which 10 are exported)
Fields returns a slice of *Field. For more info refer to Struct types
Fields() method. It panics if s's kind is not struct.
FillMap is the same as Map. Instead of returning the output, it fills the
given map.
HasZero returns true if any field is equal to a zero value. For more info
refer to Struct types HasZero() method. It panics if s's kind is not struct.
IsStruct returns true if the given variable is a struct or a pointer to
struct.
IsZero returns true if all fields is equal to a zero value. For more info
refer to Struct types IsZero() method. It panics if s's kind is not struct.
Map converts the given struct to a map[string]interface{}. For more info
refer to Struct types Map() method. It panics if s's kind is not struct.
Name returns the structs's type name within its package. It returns an
empty string for unnamed types. It panics if s's kind is not struct.
Names returns a slice of field names. For more info refer to Struct types
Names() method. It panics if s's kind is not struct.
New returns a new *Struct with the struct s. It panics if the s's kind is
not struct.
Values converts the given struct to a []interface{}. For more info refer to
Struct types Values() method. It panics if s's kind is not struct.
parseTag splits a struct field's tag into its name and a list of options
which comes after a name. A tag is in the form of: "name,option1,option2".
The name can be neglectected.
Package-Level Variables (total 3, in which 1 are exported)
DefaultTagName is the default tag name for struct fields which provides
a more granular to tweak certain structs. Lookup the necessary functions
for more info.
The pages are generated with Goldsv0.3.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds.