XML Marshal not working in this example GoLang (Please help)

package main

import "fmt"
import "encoding/xml"
import "time"

type Record struct {
    a int64     `xml:"a,omitempty"`
    b int64     `xml:"b,omitempty"`
    c int64     `xml:"c,omitempty"`
    d int64     `xml:"d,omitempty"`
    e int64     `xml:"e,omitempty"`
    f string    `xml:"f,omitempty"`
    g string    `xml:"g,omitempty"`
    h string    `xml:"h,omitempty"`
    i string    `xml:"i,omitempty"`
    j string    `xml:"j,omitempty"`
    k time.Time `xml:"k,omitempty"`
    l time.Time `xml:"l,omitempty"`
    m string    `xml:"m,omitempty"`
    n string    `xml:"n,omitempty"`
    o string    `xml:"o,omitempty"`
    p int64     `xml:"p,omitempty"`
}

func main() {
    temp, _ := time.Parse(time.RFC3339, "")
    candiateXML := &Record{1, 2, 3, 4, 5, "6", "7", "8", "9", "10", temp, temp, "13", "14", "15", 16}
    fmt.Printf("%v\n", candiateXML.a)
    fmt.Printf("%v\n", candiateXML.b)
    fmt.Printf("%v\n", candiateXML.c)
    fmt.Printf("%v\n", candiateXML.d)
    fmt.Printf("%v\n", candiateXML.e)
    fmt.Printf("%s\n", candiateXML.f)
    fmt.Printf("%s\n", candiateXML.g)
    fmt.Printf("%s\n", candiateXML.h)
    fmt.Printf("%s\n", candiateXML.i)
    fmt.Printf("%s\n", candiateXML.j)
    fmt.Printf("%d\n", candiateXML.k)
    fmt.Printf("%d\n", candiateXML.l)
    fmt.Printf("%s\n", candiateXML.m)
    fmt.Printf("%s\n", candiateXML.n)
    fmt.Printf("%s\n", candiateXML.o)
    fmt.Printf("%v\n", candiateXML.p)

    x, err := xml.MarshalIndent(candiateXML, "", "  ")
    if err != nil {
        return
    }
    //why this is not printing properly
    fmt.Printf("%s\n", x)
}

In this code the returned element x does not have body - I believe the MarshalIndent is not working properly. I will not be able the struct Record. Is there any work around so that this can return the value as expected.

Thanks in advance!

For the XML marshaller to be able to see/access the fields they must be exported, i.e., starting with an uppercase letter.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.