草庐IT

XML Marshal 在此 Go 示例中不起作用

coder 2024-07-07 原文

在此代码中,返回的元素 x 没有正文 - 我相信 MarshalIndent 无法正常工作。 我将无法使用 struct 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
    }
    //why this is not printing properly
    fmt.Printf("%s\n", x)
}

MarshalIndent 未返回预期结果。

最佳答案

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.

导出您的 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>

关于XML Marshal 在此 Go 示例中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44250935/

有关XML Marshal 在此 Go 示例中不起作用的更多相关文章

  1. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  2. ruby-on-rails - s3_direct_upload 在生产服务器中不工作 - 2

    在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo

  3. postman——集合——执行集合——测试脚本——pm对象简单示例02 - 2

    //1.验证返回状态码是否是200pm.test("Statuscodeis200",function(){pm.response.to.have.status(200);});//2.验证返回body内是否含有某个值pm.test("Bodymatchesstring",function(){pm.expect(pm.response.text()).to.include("string_you_want_to_search");});//3.验证某个返回值是否是100pm.test("Yourtestname",function(){varjsonData=pm.response.json

  4. ruby-on-rails - "assigns"在 Ruby on Rails 中有什么作用? - 2

    我目前正在尝试学习RubyonRails和测试框架RSpec。assigns在此RSpec测试中做什么?describe"GETindex"doit"assignsallmymodelas@mymodel"domymodel=Factory(:mymodel)get:indexassigns(:mymodels).shouldeq([mymodel])endend 最佳答案 assigns只是检查您在Controller中设置的实例变量的值。这里检查@mymodels。 关于ruby-o

  5. Ruby-vips 图像处理库。有什么好的使用示例吗? - 2

    我对图像处理完全陌生。我对JPEG内部是什么以及它是如何工作一无所知。我想知道,是否可以在某处找到执行以下简单操作的ruby​​代码:打开jpeg文件。遍历每个像素并将其颜色设置为fx绿色。将结果写入另一个文件。我对如何使用ruby​​-vips库实现这一点特别感兴趣https://github.com/ender672/ruby-vips我的目标-学习如何使用ruby​​-vips执行基本的图像处理操作(Gamma校正、亮度、色调……)任何指向比“helloworld”更复杂的工作示例的链接——比如ruby​​-vips的github页面上的链接,我们将不胜感激!如果有ruby​​-

  6. arrays - 如何在下面的示例中将两个值数组分组为 n 个值数组? - 2

    我已经有很多两个值数组,例如下面的例子ary=[[1,2],[2,3],[1,3],[4,5],[5,6],[4,7],[7,8],[4,8]]我想把它们分组到[1,2,3],[4,5],[5,6],[4,7,8]因为意思是1和2有关系,2和3有关系,1和3有关系,所以1,2,3都有关系我如何通过ruby​​库或任何算法来做到这一点? 最佳答案 这是基本Bron–Kerboschalgorithm的Ruby实现:classGraphdefinitialize(edges)@edges=edgesenddeffind_maximum_

  7. ruby - 字符串文字前面的 * 在 ruby​​ 中有什么作用? - 2

    这段代码似乎创建了一个范围从a到z的数组,但我不明白*的作用。有人可以解释一下吗?[*"a".."z"] 最佳答案 它叫做splatoperator.SplattinganLvalueAmaximumofonelvaluemaybesplattedinwhichcaseitisassignedanArrayconsistingoftheremainingrvaluesthatlackcorrespondinglvalues.Iftherightmostlvalueissplattedthenitconsumesallrvaluesw

  8. ruby - Google-api-ruby-client 翻译 API 示例 - 2

    很高兴看到google代码:google-api-ruby-client项目,因为这对我来说意味着Ruby人员可以使用GoogleAPI-s来完善代码。虽然我现在很困惑,因为给出的唯一示例使用Buzz,并且根据我的实验,Google翻译(v2)api的行为必须与google-api-ruby-client中的Buzz完全不同。.我对“Explorer”演示示例很感兴趣——但据我所知,它并不是一个探索器。它所做的只是调用一个Buzz服务,然后浏览它已经知道的关于Buzz服务的事情。对我来说,Explorer应该让您“发现”所公开的服务和方法/功能,而不一定已经知道它们。我很想听听使用这个

  9. ruby - 为什么这个 eval 在 Ruby 中不起作用 - 2

    你能解释一下吗?我想评估来自两个不同来源的值和计算。一个消息来源为我提供了以下信息(以编程方式):'a=2'第二个来源给了我这个表达式来评估:'a+3'这个有效:a=2eval'a+3'这也有效:eval'a=2;a+3'但我真正需要的是这个,但它不起作用:eval'a=2'eval'a+3'我想了解其中的区别,以及如何使最后一个选项起作用。感谢您的帮助。 最佳答案 您可以创建一个Binding,并将相同的绑定(bind)与每个eval相关联调用:1.9.3p194:008>b=binding=>#1.9.3p194:009>eva

  10. ruby-on-rails - Spring 不起作用。 [未初始化常量 Spring::SID::DL] - 2

    我无法运行Spring。这是错误日志。myid-no-MacBook-Pro:myid$spring/Users/myid/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/spring-0.0.10/lib/spring/sid.rb:17:in`fiddle_func':uninitializedconstantSpring::SID::DL(NameError)from/Users/myid/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/spring-0.0.10/li

随机推荐