Documentation
¶
Overview ¶
Package field provides some functions about the struct field.
Index ¶
- func GetAllFields(stype reflect.Type) []reflect.StructField
- func GetAllFieldsWithTag(stype reflect.Type, tag string) map[string]Field
- func GetTag(sf reflect.StructField, tag string) (value, arg string)
- func GetValueByName(structValue interface{}, fieldName string) (fieldValue reflect.Value, ok bool)
- func LookupTag(sf reflect.StructField, tag string) (value, arg string, ok bool)
- type Field
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetAllFields ¶
func GetAllFields(stype reflect.Type) []reflect.StructField
GetAllFields returns all the field types of the struct.
Example ¶
type Uint uint
var s struct {
string
Uint
str string
Int int
}
fields := GetAllFields(reflect.TypeOf(s))
for _, field := range fields {
fmt.Printf("name=%s, isexported=%v, anonymous=%v\n",
field.Name, field.IsExported(), field.Anonymous)
}
Output: name=string, isexported=false, anonymous=true name=Uint, isexported=true, anonymous=true name=str, isexported=false, anonymous=false name=Int, isexported=true, anonymous=false
func GetAllFieldsWithTag ¶
GetAllFieldsWithTag returns all the field types of the struct with the tag, which will filter the fields that the tag value contains "-".
Example ¶
type Uint uint
var s struct {
string
Uint
str string
Int int64 `json:"int64"`
int `json:"-"`
Ignore int `json:"-"`
}
fields := GetAllFieldsWithTag(reflect.TypeOf(s), "json")
for name, field := range fields {
fmt.Printf("name=%s, isexported=%v, anonymous=%v\n",
name, field.IsExported(), field.Anonymous)
}
Output: name=string, isexported=false, anonymous=true name=Uint, isexported=true, anonymous=true name=str, isexported=false, anonymous=false name=int64, isexported=true, anonymous=false
func GetTag ¶
func GetTag(sf reflect.StructField, tag string) (value, arg string)
GetTag returns the value and arg about the tag from the struct field.
Example ¶
type T struct {
Int int
Int8 int8 `key:""`
Int16 int16 `key:"int16"`
Int32 int32 `key:",arg"`
Int64 int64 `key:"int64,arg"`
Bool bool `key:"-"`
}
stype := reflect.TypeOf(T{})
for i := 0; i < stype.NumField(); i++ {
field := stype.Field(i)
value, arg := GetTag(field, "key")
fmt.Printf("fieldname=%s, tagvalue=%s, tagarg=%s\n", field.Name, value, arg)
}
Output: fieldname=Int, tagvalue=, tagarg= fieldname=Int8, tagvalue=, tagarg= fieldname=Int16, tagvalue=int16, tagarg= fieldname=Int32, tagvalue=, tagarg=arg fieldname=Int64, tagvalue=int64, tagarg=arg fieldname=Bool, tagvalue=-, tagarg=
func GetValueByName ¶
GetValueByName returns the struct field value by the name.
fieldName maybe starts with ".".
Example ¶
print := func(v interface{}, name string) {
if vf, ok := GetValueByName(v, name); ok {
fmt.Printf("%s: %v\n", name, vf.Interface())
} else {
fmt.Printf("no the field named \"%s\"\n", name)
}
}
var v struct {
Field1 int
Field2 string
Field3 struct {
Field4 int
Field5 string
Field6 struct {
Field7 int
Field8 string
}
}
}
v.Field1 = 123
v.Field2 = "abc"
v.Field3.Field4 = 456
v.Field3.Field5 = "rst"
v.Field3.Field6.Field7 = 789
v.Field3.Field6.Field8 = "xyz"
fmt.Println("--- Struct ---")
print(v, ".Field1")
print(v, "Field2")
print(v, "Field4")
print(v, "Field3.Field4")
print(v, "Field3.Field7")
print(v, "Field3.Field6.Field7")
print(v, "Field3.Field6.Field9")
fmt.Println()
fmt.Println("--- Pointer to Struct ---")
print(&v, ".Field1")
print(&v, "Field2")
print(&v, "Field4")
print(&v, "Field3.Field4")
print(&v, "Field3.Field7")
print(&v, "Field3.Field6.Field7")
print(&v, "Field3.Field6.Field9")
fmt.Println()
fmt.Println("--- reflect.Value of Struct ---")
print(reflect.ValueOf(v), ".Field1")
print(reflect.ValueOf(v), "Field2")
print(reflect.ValueOf(v), "Field4")
print(reflect.ValueOf(v), "Field3.Field4")
print(reflect.ValueOf(v), "Field3.Field7")
print(reflect.ValueOf(v), "Field3.Field6.Field7")
print(reflect.ValueOf(v), "Field3.Field6.Field9")
fmt.Println()
fmt.Println("--- reflect.Value of Pointer to Struct ---")
print(reflect.ValueOf(&v), ".Field1")
print(reflect.ValueOf(&v), "Field2")
print(reflect.ValueOf(&v), "Field4")
print(reflect.ValueOf(&v), "Field3.Field4")
print(reflect.ValueOf(&v), "Field3.Field7")
print(reflect.ValueOf(&v), "Field3.Field6.Field7")
print(reflect.ValueOf(&v), "Field3.Field6.Field9")
Output: --- Struct --- .Field1: 123 Field2: abc no the field named "Field4" Field3.Field4: 456 no the field named "Field3.Field7" Field3.Field6.Field7: 789 no the field named "Field3.Field6.Field9" --- Pointer to Struct --- .Field1: 123 Field2: abc no the field named "Field4" Field3.Field4: 456 no the field named "Field3.Field7" Field3.Field6.Field7: 789 no the field named "Field3.Field6.Field9" --- reflect.Value of Struct --- .Field1: 123 Field2: abc no the field named "Field4" Field3.Field4: 456 no the field named "Field3.Field7" Field3.Field6.Field7: 789 no the field named "Field3.Field6.Field9" --- reflect.Value of Pointer to Struct --- .Field1: 123 Field2: abc no the field named "Field4" Field3.Field4: 456 no the field named "Field3.Field7" Field3.Field6.Field7: 789 no the field named "Field3.Field6.Field9"
Types ¶
Click to show internal directories.
Click to hide internal directories.