douqianmin5367 2016-05-12 11:03
浏览 178
已采纳

生成XML时如何在GO中忽略空字段

I have the following struct:

type CustomAttribute struct {
    Id     string   `xml:"attribute-id,attr,omitempty"`
    Values []string `xml:"value,omitempty"`
}

type Store struct {
    XMLName          xml.Name          `xml:"store"`
    Id               string            `xml:"store-id,attr,omitempty"`
    Name             string            `xml:"name,omitempty"`
    Address1         string            `xml:"address1,omitempty"`
    Address2         string            `xml:"address2,omitempty"`
    City             string            `xml:"city,omitempty"`
    PostalCode       string            `xml:"postal-code,omitempty"`
    StateCode        string            `xml:"state-code,omitempty"`
    CountryCode      string            `xml:"country-code,omitempty"`
    Phone            string            `xml:"phone,omitempty"`
    Lat              float64           `xml:"latitude,omitempty"`
    Lng              float64           `xml:"longitude,omitempty"`
    CustomAttributes []CustomAttribute `xml:"custom-attributes>custom-attribute,omitempty"`
}

and then I initialise the struct as follows:

    store := &Store{
        Id:          storeId,
        Name:        row[4],
        Address1:    row[5],
        Address2:    row[6],
        City:        row[7],
        PostalCode:  row[9],
        StateCode:   row[8],
        CountryCode: row[11],
        Phone:       row[10],
    }

So the CustomAttributes array is always empty, and len(store.CustomAttributes) is 0 so any idea why the generated XML still contains the empty "custom-attributes" tag?

    <custom-attributes></custom-attributes>
  • 写回答

2条回答 默认 最新

  • doumou5109 2016-05-12 13:00
    关注

    One solution is to make the CustomAttributes field a pointer. It will be omitted when the value is nil. Look for "zero values" in Marshal documentation.

    package main
    
    import (
        "encoding/xml"
        "fmt"
        "log"
    )
    
    type Store struct {
        XMLName          xml.Name          `xml:"store"`
        CustomAttributes *[]CustomAttribute `xml:"custom-attributes>custom-attribute,omitempty"`
    }
    
    type CustomAttribute struct {
        Id     string   `xml:"attribute-id,attr,omitempty"`
        Values []string `xml:"value,omitempty"`
    }
    
    func print(store *Store) {
        data, err := xml.Marshal(store)
        if err != nil {
            log.Fatal(err)
        }
        fmt.Println(string(data))
    }
    
    func main() {
        print(&Store{})
        print(&Store{
            CustomAttributes: &[]CustomAttribute{},
        })
        print(&Store{
            CustomAttributes: &[]CustomAttribute{
                {Id: "hello"},
            },
        })
    }
    

    <kbd>Playground</kbd>

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料