我觉得这应该是个小问题,但我已经try 了我能想到的所有模式,但我没有任何运气.我有一个 struct ,需要同时编码的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:"-"`
}

当然,这个街区也有问题.我try 过标记语法,比如json:"-",bencode:"-"*:"-""-"-.有解决办法吗?

谢谢大家.

推荐答案

用于编码提示时,空格似乎是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:"-"`
}

Go相关问答推荐

GORM Find(&;Room)操作使用空数据而不是实际数据填充 struct

带有一个新变量的Go冒号等于运算符

Go中的Slice[:1][0]与Slice[0]

go aws-lambda 与 terraform 中的 exec 格式错误

Go 1.20 中如何计算连接错误?

从 wincrypt API 到 Go 的 RC2 解密

Kusto Go API 从多个表查询

在多个 struct 体中重用 Go 中的函数

Github Actions Go lambda 项目不同的 sha256sums

Go 中的 Azure JWT 验证不起作用

使用模拟在 golang 中测试我的界面.专门测试调用同级函数的 1 个函数

使用自定义处理程序 nats golang 保留订阅方法

将 big.Int 转换为 [2]int64,反之亦然和二进制补码

在恒等函数中将类型 T 转换为类型 U

自定义指标未显示在 prometheus web ui 中,grafana 中也是如此

使用 delve 在容器中调试 Golang:container_linux.go:380:启动容器进程导致:exec:/dlv:stat /dlv:没有这样的文件或目录

Go:用于 XML 解码的嵌套 struct 中的提升字段

如何在golang中使用ozzo验证进行时间最大验证

在 Go 中将十六进制转换为带符号的 Int

将函数的值作为输入参数返回给另一个