Introduction
The gstructs
component is used to conveniently obtain information about structures.
This is a low-level component, rarely used in general business, but used in framework, basic library, and middleware writing.
Usage:
import "github.com/gogf/gf/v2/os/gstructs"
API Documentation:
https://pkg.go.dev/github.com/gogf/gf/v2/os/gstructs
Common Methods
Fields
-
Description:
Fields
returns the fields of thePointer
attribute of the input parameterin
in the form of aField
slice. -
Format:
Fields(in FieldsInput) ([]Field, error)
- Example:
func main() {
type User struct {
Id int
Name string `params:"name"`
Pass string `my-tag1:"pass1" my-tag2:"pass2" params:"pass"`
}
var user *User
fields, _ := gstructs.Fields(gstructs.FieldsInput{
Pointer: user,
RecursiveOption: 0,
})
g.Dump(fields)
}
// Output:
[
{
Value: "<int Value>",
Field: {
Name: "Id",
PkgPath: "",
Type: "int",
Tag: "",
Offset: 0,
Index: [
0,
],
Anonymous: false,
},
TagValue: "",
},
{
Value: {},
Field: {
Name: "Name",
PkgPath: "",
Type: "string",
Tag: "params:\"name\"",
Offset: 8,
Index: [
1,
],
Anonymous: false,
},
TagValue: "",
},
{
Value: {},
Field: {
Name: "Pass",
PkgPath: "",
Type: "string",
Tag: "my-tag1:\"pass1\" my-tag2:\"pass2\" params:\"pass\"",
Offset: 24,
Index: [
2,
],
Anonymous: false,
},
TagValue: "",
},
]
TagMapName
-
Description:
TagMapName
retrievestag
from the parameterpointer
and returns it in the form ofmap[string]string
. -
Note:
- The type of parameter
pointer
should bestruct/*struct
. - Only exported fields (fields with an uppercase initial) will be returned.
- The type of parameter
-
Format:
TagMapName(pointer interface{}, priority []string) (map[string]string, error)
- Example:
func main() {
type User struct {
Id int
Name string `params:"name"`
Pass string `my-tag1:"pass1" my-tag2:"pass2" params:"pass"`
}
var user User
m, _ := gstructs.TagMapName(user, []string{"params"})
g.Dump(m)
}
// Output:
{
"name": "Name",
"pass": "Pass",
}