gstructs
组件用于方便获取结构体的相关信息。
这是一个偏底层组件,一般业务上很少会用到,在框架、基础库、中间件编写中用到。
使用方式:
import "github.com/gogf/gf/v2/os/gstructs"
接口文档:
https://pkg.go.dev/github.com/gogf/gf/v2/os/gstructs
Fields
将输入参数in
的 Pointer
属性的字段以Field
切片的形式返回。Fields(in FieldsInput) ([]Field, error)
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
从参数pointer
中检索tag
,并以map[string]string
的形式返回。 pointer
的类型应该是 struct/*struct
。TagMapName(pointer interface{}, priority []string) (map[string]string, error)
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",
}
备案信息: 粤ICP备15087711号-2
Copyright © 2008-2024 啊嘎哇在线工具箱 All Rights.
本站所有资料来源于网络,版权归原作者所有,仅作学习交流使用,如不慎侵犯了您的权利,请联系我们。