hive行标签转列标签_转到标签说明

hive行标签转列标签

Tags are a way to attach additional information to a struct field.

标签是一种将附加信息附加到结构字段的方法。

The Go spec in the Struct types definition defines tags as

Struct类型定义中的Go规范将标签定义为

A field declaration may be followed by an optional string literal tag, which becomes an attribute for all the fields in the corresponding field declaration. An empty tag string is equivalent to an absent tag. The tags are made visible through a reflection interface and take part in type identity for structs but are otherwise ignored.

字段声明后可以跟一个可选的字符串文字标签,该标签成为相应字段声明中所有字段的属性。 空标签字符串等效于缺少标签。 这些标记通过反射接口可见,并参与结构的类型标识,但在其他情况下将被忽略。

and provides an example:

并提供一个示例:

struct {
    x, y float64 ""  // an empty tag string is like an absent tag
    name string  "any string is permitted as a tag"
    _    [4]byte "ceci n'est pas un champ de structure"
}

// A struct corresponding to a TimeStamp protocol buffer.
// The tag strings define the protocol buffer field numbers;
// they follow the convention outlined by the reflect package.
struct {
    microsec  uint64 `protobuf:"1"`
    serverIP6 uint64 `protobuf:"2"`
}

一个真实的例子 (A real-world example)

A common use-case for this is when unmarshaling JSON, as I explain in JSON processing with Go.

一个常见的用例是在解组JSON时,正如我在Go的JSON处理中所解释的那样。

In that post, the example

在那个帖子中,例子

type Member struct {
    Age       int `json:"age,string"`
}

tells json.Unmarshal() to put the age JSON property, a string, and put it in the Age field of Member, translating to an int. In this example, we pass json two informations, separated with a comma.

告诉json.Unmarshal()age JSON属性(一个string )放置在MemberAge字段中,转换为int 。 在此示例中,我们将两个信息传递给json ,并以逗号分隔。

标签格式 (Format of tags)

Tags use the key:"value" format. It’s not a strict rule, but a convention, which provides built-in parsing. In that example,we have only one key-value pair, but we could have more than one:

标签使用key:"value"格式。 这不是严格的规则,而是提供内置解析的约定。 在该示例中,我们只有一对键值对,但可以有多个:

type Member struct {
    Age       int `json:"age,string" xml:"the_age,string"`
}

The key by convention is the name of the package we want to target. In the above example, encoding/json and encoding/xml.

按照惯例,键是我们要定位的程序包的名称。 在上面的示例中, encoding/jsonencoding/xml

But tags can be used also by ourselves in our own libraries, they are not just reserved for the standard library.

但是标签也可以由我们自己在我们自己的库中使用,它们不仅是为标准库保留的。

标签有什么用? (What are tags used for?)

Different packages use tags for different reasons.

不同的软件包出于不同的原因使用标签。

You’ll see them used in encoding libs, like json, xml, bson, yaml, but also in ORM / database libs, and even filling a struct with form data.

您会看到它们用于编码库(如json,xml,bson,yaml),还用于ORM /数据库库,甚至用表单数据填充结构

The following list was posted on SO and is quite comprehensive:

以下列表发布在SO上 ,并且非常全面:

There are many other usages in the wild. For example mcuadros/go-defaults use s it to set default values for struct fields, and asaskevich/govalidator allows to add tags that determine validation (that’s just one possible way of validating, alternatives exist).

在野外还有许多其他用法。 例如, mcuadros / go-defaults使用它设置结构字段的默认值,而asaskevich / govalidator允许添加确定验证的标签(这只是一种可能的验证方式, 存在替代方法 )。

fatih/gomodifytags allows to edit tags at runtime,

fatih / gomodifytags允许在运行时编辑标签,

在代码中使用标签 (Using tags in your code)

To use tags in your own code, you can use the reflect package.

要在自己的代码中使用标签,可以使用reflect

Let’s try getting age in a Member struct. The following code

让我们尝试增加Member结构的age 。 以下代码

package main

import (
    "fmt"
    "reflect"
)

type Member struct {
    Age int `something:"age"`
}

func main() {
    member := Member{34}
    t := reflect.TypeOf(member)
    field := t.Field(0)
    //field, _ := t.FieldByName("Age") //alternative
    fmt.Print(field.Tag.Get("something"))
}

play

will print age, thanks to adhering to the key:"value" format for our tag.

由于我们的代码坚持使用key:"value"格式,因此将打印age

翻译自: https://flaviocopes.com/go-tags/

hive行标签转列标签

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值