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
)放置在Member
的Age
字段中,转换为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/json
和encoding/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上 ,并且非常全面:
json
- used by theencoding/json
package, detailed atjson.Marshal()
json
-由所使用的encoding/json
包,在详述json.Marshal()
xml
- used by theencoding/xml
package, detailed atxml.Marshal()
xml
-由所使用的encoding/xml
包,在详述xml.Marshal()
bson
- used by gobson, detailed atbson.Marshal()
bson
-通过使用gobson ,在详述bson.Marshal()
protobuf
- used bygithub.com/golang/protobuf/proto
, detailed in the package docprotobuf
由github.com/golang/protobuf/proto
,在包doc中有详细说明yaml
- used by thegopkg.in/yaml.v2
package, detailed atyaml.Marshal()
yaml
-由所使用的gopkg.in/yaml.v2
包,在详述yaml.Marshal()
db
- used by thegithub.com/jmoiron/sqlx
packagedb
-使用的github.com/jmoiron/sqlx
包orm
- used by thegithub.com/astaxie/beego/orm
package, detailed at Models – Beego ORMorm
-由所使用的github.com/astaxie/beego/orm
包,在详细的模型- Beego ORMgorm
- used by thegithub.com/jinzhu/gorm
package, examples can be found in their doc: Modelsgorm
由github.com/jinzhu/gorm
包使用,示例可在其doc中找到: 模型datastore
- used byappengine/datastore
(Google App Engine platform, Datastore service), detailed at Propertiesdatastore
-由appengine/datastore
(Google App Engine平台,数据存储区服务),在“ 属性”中有详细说明schema
- used bygithub.com/gorilla/schema
to fill astruct
with HTML form values, detailed in the package docschema
-由github.com/gorilla/schema
使用,以HTML表单值填充struct
,详细信息在包doc中asn
- used by theencoding/asn1
package, detailed atasn1.Marshal()
andasn1.Unmarshal()
asn
-由所使用的encoding/asn1
包,在详述asn1.Marshal()
和asn1.Unmarshal()
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"))
}
will print age
, thanks to adhering to the key:"value"
format for our tag.
由于我们的代码坚持使用key:"value"
格式,因此将打印age
。
hive行标签转列标签