XML Marshal 在此 Go 示例中不起作用
XML Marshal not working in this Go example
在此代码中,returned 元素 x 没有正文 - 我认为 MarshalIndent 无法正常工作。
我将无法使用 struct Record。
是否有任何变通方法可以 return 预期的值。
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)
}
MarshalIndent 没有return预期的结果。
The Go Programming Language Specification
An identifier may be exported to permit access to it from another
package. An identifier is exported if both:
- the first character of the identifier's name is a Unicode upper case letter (Unicode class "Lu"); and
- the identifier is declared in the package block or it is a field name or method name.
All other identifiers are not exported.
导出您的 Record
结构字段名称(第一个字符大写)以允许从 xml
包访问它们。例如,
package main
import "fmt"
import "encoding/xml"
type Record struct {
A int64 `xml:"a,omitempty"`
B int64 `xml:"b,omitempty"`
C int64 `xml:"c,omitempty"`
}
func main() {
candiateXML := &Record{1, 2, 3}
fmt.Printf("%v\n", candiateXML.A)
fmt.Printf("%v\n", candiateXML.B)
fmt.Printf("%v\n", candiateXML.C)
x, err := xml.MarshalIndent(candiateXML, "", " ")
if err != nil {
return
}
fmt.Printf("%s\n", x)
}
输出:
1
2
3
<Record>
<a>1</a>
<b>2</b>
<c>3</c>
</Record>
在此代码中,returned 元素 x 没有正文 - 我认为 MarshalIndent 无法正常工作。 我将无法使用 struct Record。 是否有任何变通方法可以 return 预期的值。
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)
}
MarshalIndent 没有return预期的结果。
The Go Programming Language Specification
An identifier may be exported to permit access to it from another package. An identifier is exported if both:
- the first character of the identifier's name is a Unicode upper case letter (Unicode class "Lu"); and
- the identifier is declared in the package block or it is a field name or method name.
All other identifiers are not exported.
导出您的 Record
结构字段名称(第一个字符大写)以允许从 xml
包访问它们。例如,
package main
import "fmt"
import "encoding/xml"
type Record struct {
A int64 `xml:"a,omitempty"`
B int64 `xml:"b,omitempty"`
C int64 `xml:"c,omitempty"`
}
func main() {
candiateXML := &Record{1, 2, 3}
fmt.Printf("%v\n", candiateXML.A)
fmt.Printf("%v\n", candiateXML.B)
fmt.Printf("%v\n", candiateXML.C)
x, err := xml.MarshalIndent(candiateXML, "", " ")
if err != nil {
return
}
fmt.Printf("%s\n", x)
}
输出:
1
2
3
<Record>
<a>1</a>
<b>2</b>
<c>3</c>
</Record>