我想知道是否可以从该结构内部使用的自定义类型访问结构标记集。

type Out struct {
    C Custom `format:"asd"`
}

type Custom struct {
}

func (c Custom) GetTag() string {
    // somehow get access to `format:"asd"`
}

我的目标是能够为取消/编码定义时间格式,并处理由structtag参数化的实际时间/编码时间。

谢谢

最佳答案

那不可能标签属于结构域,而不是类型。因此,C类型无法知道使用了哪个标签。另外,如果发生以下情况,它将如何工作:

type A struct {
  C Custom `tag1`
}
type B struct {
  C Custom `tag2`
}

关于go - 如何从Golang的字段类型内部访问struct标记,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57895092/

10-14 14:43
查看更多