golang 结构体标签_如何在Go中使用结构标签

golang 结构体标签

介绍 (Introduction)

Structures, or structs, are used to collect multiple pieces of information together in one unit. These collections of information are used to describe higher-level concepts, such as an Address composed of a Street, City, State, and PostalCode. When you read this information from systems such as databases, or APIs, you can use struct tags to control how this information is assigned to the fields of a struct. Struct tags are small pieces of metadata attached to fields of a struct that provide instructions to other Go code that works with the struct.

结构或结构用于在一个单元中收集多条信息。 这些信息收集用于描述更高级别的概念,例如由StreetCityStatePostalCode组成的Address 。 从数据库或API等系统读取此信息时,可以使用struct标记来控制如何将此信息分配给struct的字段。 结构标签是附加到结构字段的一小段元数据,这些元数据为与该结构一起使用的其他Go代码提供了指令。

结构标签是什么样的? (What Does a Struct Tag Look Like?)

Go struct tags are annotations that appear after the type in a Go struct declaration. Each tag is composed of short strings associated with some corresponding value.

Go struct标签是在Go struct声明中的类型之后出现的注释。 每个标签由与一些相应值关联的短字符串组成。

A struct tag looks like this, with the tag offset with backtick ` characters:

struct标签看起来像这样,标签的偏移量是反引号`字符:

type User struct {
    Name string `example:"name"`
}

Other Go code is then capable of examining these structs and extracting the values assigned to specific keys it requests. Struct tags have no effect on the operation of your code without some other code that examines them.

然后其他Go代码能够检查这些结构并提取分配给它请求的特定键的值。 如果没有其他检查代码的结构标记,它们对代码的操作将没有任何影响。

Try this example to see what struct tags look like, and that without code from another package, they will have no effect.

尝试此示例以查看struct标记的外观,如果没有其他包中的代码,则它们将无效。

package main

import "fmt"

type User struct {
    Name string `example:"name"`
}

func (u *User) String() string {
    return fmt.Sprintf("Hi! My name is %s", u.Name)
}

func main() {
    u := &User{
        Name: "Sammy",
    }

    fmt.Println(u)
}

This will output:

这将输出:


   
   
   
Output
Hi! My name is Sammy

This example defines a User type with a Name field. The Name field has been given a struct tag of example:"name". We would refer to this specific tag in conversation as the “example struct tag” because it uses the word “example” as its key. The example struct tag has the value "name" for the Name field. On the User type, we also define the String() method required by the fmt.Stringer interface. This will be called automatically when we pass the type to fmt.Println and gives us a chance to produce a nicely formatted version of our struct.

本示例使用Name字段定义User类型。 “ Name字段已获得example:"name"的struct标签example:"name" 。 我们将会话中的这个特定标记称为“ example struct tag”,因为它使用单词“ example”作为其键。 该example结构的标签有值"name"Name字段。 在User类型上,我们还定义了fmt.Stringer接口所需的String()方法。 当我们将类型传递给fmt.Println时,它将被自动fmt.Println ,这使我们有机会生成结构良好的格式化版本。

Within the body of main, we create a new instance of our User type and pass it to fmt.Println. Even though the struct had a struct tag present, we see that it has no effect on the operation of this Go code. It will behave exactly the same if the struct tag were not

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值