Golang中允许使用元数据或属性吗?

huangapple go评论78阅读模式
英文:

Is metadata or attributes allowed in Golang?

问题

这些不同的验证库是如何将元数据添加到类似以下结构的结构体中的:

type Post struct {
    Title    string `valid:"alphanum,required"`
    Message  string `valid:"duck,ascii"`
    AuthorIP string `valid:"ipv4"`
    Date     string `valid:"-"`
}

我有点困惑,属性是Title,类型是string。除此之外,你是如何添加valid:"alphanum,required"的?这是使用反射吗?

这是否类似于其他语言中的属性?

[Required]
public int Title { get;set; }
英文:

How are these various validation libraries adding this meta data to structs like:

type Post struct {
    Title    string `valid:"alphanum,required"`
    Message  string `valid:"duck,ascii"`
    AuthorIP string `valid:"ipv4"`
    Date     string `valid:"-"`
}

I'm confused, the property is Title, the type is string. Beyond that how can you just add valid:"alphanum,required" Is this using reflection?

Is this like attributes in other languages?

[Required]
public int Title { get;set; }

答案1

得分: 5

Go语言在一般意义上没有属性。结构体中的字符串是结构体标签

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

// 与 TimeStamp 协议缓冲区对应的结构体。
// 标签字符串定义了协议缓冲区字段的编号。
struct {
microsec uint64 "field 1"
serverIP6 uint64 "field 2"
process string "field 3"
}

你不能添加或更改它们,但可以使用reflect包访问它们。

另一件类似属性的事情是"魔术注释",例如:

// +build amd64

或者

//go:noinline

这些是特定于编译器的,据我所知,它们不是语言规范的一部分。

英文:

Go doesn't have attributes in general sense. The strings in the struct are struct tags:

>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. The tags are made visible through a reflection interface and take part in type identity for structs but are otherwise ignored.
>
// A struct corresponding to the TimeStamp protocol buffer.
// The tag strings define the protocol buffer field numbers.
struct {
microsec uint64 "field 1"
serverIP6 uint64 "field 2"
process string "field 3"
}

You can't add or change them, but you can access them using the reflect package.

Another thing that kinda looks like attributes are "magic comments" like

// +build amd64

or

//go:noinline

These are compiler-specific and IIRC are not a part of the language specification.

答案2

得分: 2

这些是标签。根据语言规范(在结构类型的末尾),一个字段声明可以后跟一个可选的字符串字面值标签,该标签成为相应字段声明中所有字段的属性。标签通过反射接口可见,并参与结构体的类型标识,但在其他情况下会被忽略。

根据上述引用,您可以在反射中使用它们。

此外,根据反射包的文档:

结构体字段中的标签字符串是StructTag。

按照惯例,标签字符串是可选的以空格分隔的key:"value"对的串联。每个key是一个非空字符串,由非控制字符组成,除了空格(U+0020 ' ')、引号(U+0022 '"')和冒号(U+003A ':')。每个value使用U+0022 '"'字符和Go字符串字面值语法进行引用。

上面的链接中有一个很好的可执行示例。

标签还广泛用于JSON。根据JSON包文档

对象的默认键字符串是结构体字段名,但可以在结构体字段的标签值中指定。结构体字段的标签值中的"json"键是键名,后面可以跟一个可选的逗号和选项。

该页面上还有大量其他数据。

这个早期的问题也可能会有所帮助。

英文:

Those are tags. From the language spec (towards the end of Struct types):

> 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. The tags are made visible through a reflection
> interface and take part in type identity for structs but are otherwise
> ignored.

As per the quote above, you can use them with reflection.

ALso , from the reflect package doc:

> A StructTag is the tag string in a struct field.
>
> By convention, tag strings are a concatenation of optionally
> space-separated key:"value" pairs. Each key is a non-empty string
> consisting of non-control characters other than space (U+0020 ' '),
> quote (U+0022 '"'), and colon (U+003A ':'). Each value is quoted using
> U+0022 '"' characters and Go string literal syntax.

There is an excellent executable example at the link above.

Tags are also used extensively with json. From the json package docs:

> The object's default key string is the struct field name but can be
> specified in the struct field's tag value. The "json" key in the
> struct field's tag value is the key name, followed by an optional
> comma and options.

Loads of additional data on that page.

This earlier question may also be helpful.

huangapple
  • 本文由 发表于 2016年2月4日 23:46:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/35205421.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定