同一Go结构成员上的多个标记

时间:2012-12-04 01:08:18

标签: json reflection tags go

我觉得这应该是一个小问题,但我已经尝试了我能想到的每一种模式,而且我没有运气。我有一个需要encoding/jsongithub.com/zeebo/bencode包编码的结构。它碰巧包含一个通道,无法通过任何一个包进行编码。因此,它需要携带标记"-",以便跳过该字段。

type Index struct {
    Data data
    Queue chan string `json:"-"`
}

这在json包编码时有效,但在bencode包中失败。

type Index struct {
    Data data
    Queue chan string `bencode:"-"`
}

这个区块当然有免费问题。我尝试过标记语法,例如json:"-",bencode:"-"*:"-""-"-。有解决方案吗?

谢谢大家。

1 个答案:

答案 0 :(得分:19)

当用于编码提示时,空格似乎是struct标记之间的分隔符。

示例:

type TaggedStructExample struct {
    ...
    J int `datastore:",noindex" json:"j"`
}

来自:https://developers.google.com/appengine/docs/go/datastore/reference#Properties

在您的情况下,请尝试:

type Index struct {
    Data data
    Queue chan string `bencode:"-" json:"-"`
}