📕
innohub
  • KEEP LEARNING
  • WebInfo
    • JS 部分运算符操作
    • javascript 中的object array
    • 错误处理以及异常捕获
    • JavaScript Bases
    • LaoZi & Confucius
  • PyInfo
    • Python3 输入与输出
    • Python3OS
    • python 修饰器的使用
    • python3 与mysql数据库连接使用实例
    • Format-specifier
    • CGI初学踩坑
    • Django 项目测试
    • Assert-info
    • 使用ngnix和uWSGI配置Django
    • write|SVN​
    • Matplotlib 基本使用
    • 重读 Python 官方文档
    • Python3 Base
    • python3 多线程
    • Python3 函数解析
    • python3 str 对象的基本操作
    • protocl buffers
    • Iterator-Generator
    • Django base
    • Decorator 2
    • Export to wheel or egg-info
    • 三. 运算、数据及逻辑
  • GoInfo
    • Info
      • Select 语句使用
      • First class function
      • Work Pools
      • StructTag
      • Go struct
      • 常用函数
      • Strings操作
      • Golang Bases
      • 数组以及切片
      • 文件操作
      • Golang 中的指针类型
      • Golang Map 类型
      • Reflection
      • 函数闭包
      • 接口
      • Panic and Recover
      • Go中的错误处理
      • 并发
      • defer usage
      • Method in golang
      • Object-oriented Programming
      • Goalng 包管理机制
  • RustInfo
    • Info
      • 包、crate、模块
      • Rust 中的错误处理
      • 智能指针
      • 泛型 generics
      • 数据布局与内存对齐
      • Functions and methods
      • 无畏并发
      • Actix-web 基本使用
      • Got from guessing game
      • 结构体的使用
      • Rust 中的函数式语言功能
      • 集合类型
      • 枚举以及模式匹配
      • Slice 类型
      • 生命周期
      • 测试
      • Rust 中的所有权
      • trait
      • 不安全 Rust
      • Format_print
      • Rust 通用编程概念
      • Macro
  • OS
    • info
      • 内存屏障 -- Part 1
      • 内存屏障 -- Part 2
      • CPU 上下文切换
      • 文件读写与零拷贝
      • ELF 文件
  • MySql
    • info
      • MySql 架构与历史
      • 02-key
  • kubernetes
    • 第二章 k8s 基本概念
    • 第一章 Kubernetes -- 伟大舵手
  • Redis
    • info
      • Redis 总览
      • 02-underline-ds
  • Shell&CInfo
    • GCC 与make的使用
    • C++ 中 malloc 与 new
    • 位运算符操作
    • Base of C macro
    • C 语言中 extern 关键字的用法
  • Distributed
    • info
      • 分布式理论概述
  • Java
    • info
      • Java 内存模型
  • Prometheus
    • Prometheus -- 不灭之火
Powered by GitBook
On this page
  • 1. 结构体标记
  • 2. StructTag 操作
  • 3. JSON tag

Was this helpful?

  1. GoInfo
  2. Info

StructTag

PreviousWork PoolsNextGo struct

Last updated 5 years ago

Was this helpful?

1. 结构体标记

按照惯例,StructTag字符串是一个可选的,在类型后由空格分隔的键值对。StructTag类型是string的别名,一般使用反引号。Go的字符串是一个任意字节的字符串常量,双引号以及反引号都可以创建一个常量的字符串,区别在于:

  • 双引号创建可解析的字符串字面量,支持转义,不可以引用多行

  • 反引号,用以表示原生字符串,通常用于多行、正则表达式、结构体标记

  • 单引号用于表示rune,码点字面量

Go对于结构体的字段标记可以通过反射机制得到,所以通常用于在对struct的编码转换过程中提供一些规则转换信息,例如常用的JSON转换。可以用于存储所需的所有元信息。

2. StructTag 操作

  • func (tag StructTag) Get(key string) string

    get 方法得到一个结构体标记内的key对应的value,如果对应的key,没有定义,会得到一个 undefined错误

  • func (tag StructTag) Lookup(key string) (value string, ok bool)

    在tag中寻找与key关联的value,value可以为空,ok返回tag中是否有该键值

3. JSON tag

这里着重举出 JSON 的 Struct Tag(1. JSON 输出很常见; 2. 可以以此类推其他如 XML’s Tag)。 我想知道 JSON 的 tag 有哪些,去哪找?去官网 函数文档中找。

The encoding of each struct field can be customized by the format string stored under the "json" key in the struct field's tag. The format string gives the name of the field, possibly followed by a comma-separated list of options. The name may be empty in order to specify options without overriding the default field name. 我们会发现,在 JSON 编码过程中会去获取每一个 Struct field 的标记,从中拿取 key 为 json 的值,然后进行相应处理。

注意解析规则:value 的第一个字符串一定表示覆盖后的新字段名,后面如果有解析选项,则以英文逗号分隔。

比如 Name string json:"name,omitempty",第一个字符串 name 表示在编码后 Name 属性名就变成了 name。然后紧跟逗号分隔符,接着是 omitempty 选项。

  1. 如果我不想覆盖,只想加选项怎么办?Name string json:",omitempty",直接英文逗号打头。

  2. 极端一点,如果我的字段名就叫 Omitempty 呢?Omitempty string json:"omitempty,omitempty",记住第一个字符串表示的是新变量名,而不是选项,所以重名就重名好了,不怕🤪。

思考一下:- string json:"-," 和 - string json:",-" 有什么区别🧐?

  1. omitempty:如果字段的值为空(Defined as false, 0, a nil pointer, a nil interface value, and any empty array, slice, map, or string),那么在编码过程中就忽略掉这个字段。

  2. -:二话不说直接忽略该字段。

  3. string:将字段值在编码过程中转换成 JSON 中的字符串类型,只有当字段类型是 string, floating point, integer, or boolean 的情况下才会转换。

JSON.Marshal