假设我有一个像这样的结构:
type MyType struct {
A, B string
}
我可以在这些字段上添加标签吗?显然,我可以将每个字段放在自己的行中。我只是好奇是否可以以原始格式标记字段。
最佳答案
是的,您可以,但您不能标记各个字段。来自Language Reference :
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
所以你可以说
type MyType struct {
A, B string `my_tag`
}
但这最终会导致 A
和 B
都被标记为 my_tag
。对于必须常见的用例(JSON、Protobuf 等),这不是您想要的。
关于go - Golang 中逗号分隔字段的结构标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48793302/