首页 > 其他分享 >Go - Defining Metadata for Struct Fields

Go - Defining Metadata for Struct Fields

时间:2023-10-07 22:44:39浏览次数:29  
标签:struct tags Fields package json Go Defining string name

Problem: You want to define metadata to describe the struct fields.


Solution: Use struct tags to define metadata and the reflect package to access the tags.

 

One very common place you find this is in the json package:

type   Person   struct   { 
      Id           int      `json:"id"` 
      GivenName    string   `json:"given_name"` 
      FamilyName   string   `json:"family_name"` 
      Email        string   `json:"email"` 
}

Many packages that process structs use struct tags. For example, xml package, the protobuf package, and the sqlx package all use struct tags. You can also roll out your support for struct tags using the reflect package.

Struct tags are defined in name - value pairs within the string literal. In the preceding example, where the string literal is json:"given_name" , the name is json and the value is a string "given_name".

Here’s how you can extract the values:

person   :=   Person { 
      Id :           1 , 
      GivenName :    "Sau  Sheong" , 
      FamilyName :   "Chang" , 
      Email :        "[email protected]" , 
} 

p   :=   reflect . TypeOf ( person ) 
for   i   :=   0 ;   i   <   p . NumField ();   i ++   { 
      field   :=   p . Field ( i ) 
      fmt . Println ( field . Tag . Get ( "json" )) 
}

To get the values from the struct tags, you use the reflect package. Start with getting the Type of the person variable, which is a struct. The NumField method on the Type tells you how many fields are in this Type , which is 4. Then you call the Field method on the Type with a given index to get the StructField . Call the Tag method on the StructField to get the StructTag , and finally call the Get method with the name to get the value.

Each struct tag can contain more than a single name - value pair, so you can call Get on different names to get to the corresponding values.

 

标签:struct,tags,Fields,package,json,Go,Defining,string,name
From: https://www.cnblogs.com/zhangzhihui/p/17747670.html

相关文章

  • CF1856B Good Arrays
    题意简述:给定一个序列\(a\),我们定义一个序列\(b\)是好的当且仅当对于\(1\dotsn\)内的每一个\(i\),\(a_i\neqb_i\)且\(\sum_{i=1}^na_i=\sum_{i=1}^nb_i\)(\(a_i\),\(b_i\)均为正整数)。现在有\(T\)组数据,每组数据给定\(n\)和序列\(a\),判断是否存在一个合法的序......
  • Go 项目代码布局
    Go项目代码布局目录Go项目代码布局一、Go语言“创世项目”结构1.1src目录结构三个特点二、Go项目布局演进2.1演进一:Go1.4版本删除pkg这一中间层目录并引入internal目录2.2演进二:Go1.6版本增加vendor目录2.3演进三:Go1.13版本引入go.mod和go.sum三、现在Go......
  • Go 基础之基本数据类型
    Go基础之基本数据类型目录Go基础之基本数据类型一、整型1.1平台无关整型1.1.1基本概念1.1.2分类有符号整型(int8~int64)无符号整型(uint8~uint64)1.2平台相关整型1.2.1基本概念1.2.2注意点1.2.3获取三个类型在目标运行平台上的长度1.3整型的溢出问题1.3.1什么是整形溢出?1.......
  • GO语言基础之基本运算符
    GO语言基础之基本运算符目录GO语言基础之基本运算符一、运算符内置运算符:二、算术运算符三、关系运算符四、逻辑运算符五、位运算符六、赋值运算符一、运算符作用:运算符用于在程序运行时执行数学或逻辑运算。内置运算符:Go语言内置的运算符有:算术运算符关系运算符逻辑运......
  • Go基础之指针
    Go语言中的指针目录Go语言中的指针一、Go语言中的指针介绍1.1指针介绍1.2基本语法1.3声明和初始化1.4Go指针的3个重要概念1.4.1指针地址(PointerAddress)1.4.2指针类型(PointerType)1.4.3指针取值(PointerDereferencing)1.5获取指针的地址和解引用1.6传递指针给函数1.7指......
  • Go 复合数据类型之结构体与自定义类型
    Go复合数据类型之结构体与自定义类型目录Go复合数据类型之结构体与自定义类型一、类型别名和自定义类型1.1类型定义(TypeDefinition)简单示例1.2类型别名简单示例1.3类型定义和类型别名的区别二、结构体2.1结构体介绍2.2结构体的定义2.3定义一个空结构体2.3.1空结构体介......
  • Go基础之变量和常量
    Go基础之变量和常量目录Go基础之变量和常量一.标识符、关键字、内置类型和函数1.1标识符1.2关键字1.3保留字1.4内置类型1.4.1值类型:1.4.2引用类型:(指针类型)1.5内置函数1.6内置接口error二.Go变量命名规范2.1采用驼峰体命名2.2简单、短小为首要原则2.3变量名字中不要......
  • Django实战项目-学习任务系统-用户登录
    第一步:先创建一个Django应用程序框架代码1,先创建一个Django项目django-adminstartprojectmysite将创建一个目录,其布局如下:mysite/manage.pymysite/__init__.pysettings.pyurls.pyasgi.pywsgi.py2,再创建一个Dja......
  • GoLang context包
    初始化一个context如果确定是开头则用Background,如果不确定则用ToDocontext包核心API有四个1.context.WithValue设置键值对,并且返回一个新的context实例2.context.WithCancel3.context.WithDeadline4.context.WithTimeout三者都返回一个可取消的context实例和取消函数,WithTi......
  • golang实现一个简单的文件浏览下载功能代码示例
    想省事用Claude(一个依托chatgpt的AI)生成一段golang的文件浏览下载示例,结果给生成的代码大概是这样的(省去了无关部分,主要部分如下):http.HandleFunc("/*",downloadFile)http.HandleFunc("/",showFileList)测试之后,结果发现每次都会走到“/”下去,无论如何都不会......