Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $YECBGYFECGEAFWHA as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2

Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $BBWFDDBHHYHDXXAB as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2
在 Go 中使用 DRY 原则处理 XML 结构体标签_创想鸟

在 Go 中使用 DRY 原则处理 XML 结构体标签

在 go 中使用 dry 原则处理 xml 结构体标签

在 G%ignore_a_1% 语言中解析 XML 数据时,经常会遇到需要在多个结构体中定义相同字段和 XML 标签的情况,例如,每个结构体都包含一个 Description 字段,并使用相同的 xml:”description,omitempty” 标签。为了避免重复定义,可以使用嵌入带有结构体标签的公共结构体的方式,实现代码的 DRY (Don’t Repeat Yourself) 原则。

嵌入结构体

我们可以创建一个包含公共字段和标签的结构体,然后将其嵌入到其他结构体中。例如,创建一个名为 describable 的结构体,其中包含 Description 字段和 xml:”description” 标签:

type describable struct{    Description string `xml:"description"`}

然后,可以将 describable 结构体嵌入到其他结构体中,例如 subobjA 和 subobjB:

type subobjA struct {    describable    XMLName     xml.Name `xml:"subobjA"`}type subobjB struct {    describable    XMLName     xml.Name `xml:"subobjB"`}type obj struct {    XMLName     xml.Name `xml:"obj"`    A           subobjA    B           subobjB}

通过这种方式,subobjA 和 subobjB 结构体都继承了 Description 字段和 xml:”description” 标签,避免了重复定义。

字段提升

需要注意的是,嵌入结构体中的字段会被提升到外部结构体,可以直接通过外部结构体访问。例如,可以通过 sampleObj.Description 访问 sampleObj.describable.Description。

Go 语言规范中对字段提升的定义如下:

Shakker Shakker

多功能AI图像生成和编辑平台

Shakker 103 查看详情 Shakker

A field or method f of an anonymous field in a struct x is called promoted if x.f is a legal selector that denotes that field or method f.Promoted fields act like ordinary fields of a struct except that they cannot be used as field names in composite literals of the struct.

这意味着,嵌入结构体中的字段可以像普通字段一样使用,但不能在结构体字面量中使用。

示例代码

以下是一个完整的示例代码,演示了如何使用嵌入结构体来解析 XML 数据:

package mainimport (    "encoding/xml"    "fmt")type describable struct {    Description string `xml:"description"`}type subobjA struct {    describable    XMLName xml.Name `xml:"subobjA"`    Foo     string   `xml:"foo"`}type subobjB struct {    describable    XMLName xml.Name `xml:"subobjB"`    Bar     string   `xml:"bar"`}type obj struct {    XMLName xml.Name `xml:"obj"`    A       subobjA  `xml:"subobjA"`    B       subobjB  `xml:"subobjB"`}func main() {    sampleXml := `    outer object            first kind of subobject        some goop                second kind of subobject        some other goop    `    sampleObj := obj{}    err := xml.Unmarshal([]byte(sampleXml), &sampleObj)    if err != nil {        fmt.Println("Error unmarshalling XML:", err)        return    }    fmt.Println(sampleObj.Description)    fmt.Println(sampleObj.A.Description)    fmt.Println(sampleObj.B.Description)    fmt.Println(sampleObj.A.Foo)    fmt.Println(sampleObj.B.Bar)}

在这个示例中,obj 结构体包含 subobjA 和 subobjB 结构体,而这两个结构体又都嵌入了 describable 结构体。通过这种方式,我们可以避免重复定义 Description 字段和 xml:”description” 标签。

总结

通过嵌入带有结构体标签的公共结构体,可以有效地避免在多个结构体中重复定义相同的字段和标签,提高代码的可维护性和可读性。同时,Go 语言的字段提升机制也简化了对嵌入结构体字段的访问,使得代码更加简洁。在处理 XML 数据时,可以考虑使用这种方式来组织结构体,实现代码的 DRY 原则。

以上就是在 Go 中使用 DRY 原则处理 XML 结构体标签的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1133017.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
PC端两栏布局,一栏固定另一栏滚动:如何避免固定栏在不同分辨率下位置偏移?
上一篇 2025年12月2日 17:22:22
《黑神话悟空》AMD显卡答疑
下一篇 2025年12月2日 17:22:32

相关推荐

发表回复

登录后才能评论
关注微信