GoFrame 对象信息-gstructs

基本介绍

gstructs​组件用于方便获取结构体的相关信息。

这是一个偏底层组件,一般业务上很少会用到,在框架、基础库、中间件编写中用到。

使用方式:

import "github.com/gogf/gf/v2/os/gstructs"

接口文档:

https://pkg.go.dev/github.com/gogf/gf/v2/os/gstructs

常用方法

Fields

  • 说明:​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

  • 说明:​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",
}

作者:唐伯虎点蚊香,如若转载,请注明出处:https://www.web176.com/goframe/20923.html

(0)
打赏 支付宝 支付宝 微信 微信
唐伯虎点蚊香的头像唐伯虎点蚊香
上一篇 2023年5月18日
下一篇 2023年5月18日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注


Fatal error: Uncaught JSMin_UnterminatedStringException: JSMin: Unterminated String at byte 529: ", in /www/wwwroot/web176.cc/wp-content/plugins/autoptimize/classes/external/php/jsmin.php:214 Stack trace: #0 /www/wwwroot/web176.cc/wp-content/plugins/autoptimize/classes/external/php/jsmin.php(152): JSMin->action() #1 /www/wwwroot/web176.cc/wp-content/plugins/autoptimize/classes/external/php/jsmin.php(86): JSMin->min() #2 /www/wwwroot/web176.cc/wp-content/plugins/autoptimize/classes/external/php/ao-minify-html.php(257): JSMin::minify() #3 [internal function]: AO_Minify_HTML->_removeScriptCB() #4 /www/wwwroot/web176.cc/wp-content/plugins/autoptimize/classes/external/php/ao-minify-html.php(108): preg_replace_callback() #5 /www/wwwroot/web176.cc/wp-content/plugins/autoptimize/classes/external/php/ao-minify-html.php(47): AO_Minify_HTML->process() #6 /www/wwwroot/web176.cc/wp-content/plugins/autoptimize/classes/autoptimizeHTML.php(105): AO_Minify_HTML::minify() #7 /www/wwwroot/web176.cc/wp-content/plugins/autoptimize/classes/aut in /www/wwwroot/web176.cc/wp-content/plugins/autoptimize/classes/external/php/jsmin.php on line 214