英文:
XML Marshal not working in this Go example
问题
在这段代码中,返回的元素x没有正文 - 我认为MarshalIndent没有正常工作。
我将无法解析结构体Record。
是否有任何解决方法,以便可以按预期返回值。
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
}
//为什么这个没有正确打印
fmt.Printf("%s\n", x)
}
MarshalIndent没有返回预期的结果。
英文:
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.
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)
}
The MarshalIndent does not return the expected result.
答案1
得分: 1
《Go编程语言规范》
导出标识符
为了允许从另一个包中访问标识符,可以将其导出。如果标识符同时满足以下两个条件,则它是导出的:
- 标识符名称的第一个字符是一个Unicode大写字母(Unicode类别"Lu");
- 标识符在包块中声明,或者它是字段名或方法名。
所有其他标识符都不是导出的。
将你的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>
英文:
> The Go Programming Language Specification
>
> Exported identifiers
>
> An identifier may be exported to permit access to it from another
> package. An identifier is exported if both:
>
> 1. the first character of the identifier's name is a Unicode upper case letter (Unicode class "Lu"); and
> 2. the identifier is declared in the package block or it is a field name or method name.
>
> All other identifiers are not exported.
Export your Record
struct field names (first character upper case) to permit access to them from the xml
package. For example,
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)
}
Output:
1
2
3
<Record>
<a>1</a>
<b>2</b>
<c>3</c>
</Record>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论