gpt4 book ai didi

go - 条件(动态)结构标签

转载 作者:数据小太阳 更新时间:2023-10-29 03:39:28 24 4
gpt4 key购买 nike

我正在尝试用 Go 解析一些 xml 文档。为此,我需要定义一些结构,并且我的结构标签取决于特定条件。

想象一下下面的代码(尽管我知道它不会工作)

if someCondition {
type MyType struct {
// some common fields
Date []string `xml:"value"`
}
} else {
type MyType struct {
// some common fields
Date []string `xml:"anotherValue"`
}
}

var t MyType
// do the unmarshalling ...

问题在于这两个结构有很多共同的字段。唯一的区别在于其中一个字段,我想防止重复。我该如何解决这个问题?

最佳答案

您使用不同 类型来解码。基本上,您编写了两次解码代码,然后运行第一个版本或第二个版本。对此没有动态解决方案。

关于go - 条件(动态)结构标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56314512/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com