将XML解组为结构体并转换为切片。

huangapple go评论85阅读模式
英文:

Unmarshal XML to struct and convert to slice

问题

我在Golang中有一个简单的项目,用来学习这门语言。我正在开发的"service"的主要目的是运行一个守护进程,将以XML形式暴露的URL保存起来。这样我就可以"生成"自己的稍后阅读服务。目前为止一切顺利。你可以在这里找到这个项目:https://github.com/rogierlommers/readinglist-golang

我正在使用Gin-Gonic作为提供HTML的框架。我已经成功读取了一个XML文件,并将其解组,但现在我想向这个"thing"中添加一些新数据。换句话说,我认为我需要将其转换为一个切片,但我不知道如何处理。

例如,端点r.GET("/add/:url")应该使用函数util.AddRecord将新的URL插入到切片中。但是怎么做呢?

[编辑]
基本上,我的问题可以在这个Go Playground中查看:http://play.golang.org/p/Vx0s02E12R

英文:

I have a simple project in Golang which I use to learn this language. Main purpose of the "service" which I'm developing is to run a daemon to save URLs which are exposed as XML. This way I can "produce" my own read-later service. So far so good :). You can find the project here: https://github.com/rogierlommers/readinglist-golang

I'm using Gin-Gonic as the framework for serving html. I've already managed to read an xml file, unmarshal it but now I want to add some new data into this "thing". In other words: I think I need to convert it into a slice, but I don't know how to manage this.

F.e. the endpoint r.GET("/add/:url") should use the function util.AddRecord to insert the new url into the slice. But how?

[edit]
Basically my problem can be viewed in this go playground: http://play.golang.org/p/Vx0s02E12R

答案1

得分: 2

在你的问题评论中,你问道:

> 我首先需要创建一个切片,对吗?

答案是肯定的,但是你已经在你的ReadingListRecords结构体中有一个切片:

<!-- language-all: lang-go -->

type ReadingListRecords struct {
    XMLName xml.Name `xml:&quot;records&quot;`
    Records []Record `xml:&quot;record&quot;`
}

所以,你可以简单地在该切片上调用append函数,并传入一个新的记录结构体:

records.Records = append(record.Records, Record{xml.Name{&quot;&quot;, &quot;record&quot;}, 4, &quot;url&quot;, &quot;2015-03-09 00:00:00&quot;})

你还可以扩展ReadingListRecords结构体的API,以包含一个方便的Append函数:

type RecordSet interface {
    Append(record Record) error
}

func (records *ReadingListRecords) Append(record Record) error {
    newRecords := append(records.Records, record)

    if newRecords == nil {
	    return errors.New(&quot;Could not append record&quot;)
    } else {
	    records.Records = newRecords
	    return nil
    }
}

添加一个接口似乎是个好主意,因为你想在多个应用程序中使用它作为服务。

这里查看我对你的playground的fork

英文:

In a comment on your question you asked:

> I first need to create a slice, right?

The answer is yes, but you already have a slice in your ReadingListRecords struct:

<!-- language-all: lang-go -->

type ReadingListRecords struct {
    XMLName xml.Name `xml:&quot;records&quot;`
    Records []Record `xml:&quot;record&quot;`
}

So, you can simply call append on that slice and pass in a new record struct:

records.Records = append(record.Records, Record{xml.Name{&quot;&quot;, &quot;record&quot;}, 4, &quot;url&quot;, &quot;2015-03-09 00:00:00&quot;})

You could also expand the API for your ReadingListRecords structure to include a handy Append function:

type RecordSet interface {
    Append(record Record) error
}

func (records *ReadingListRecords) Append(record Record) error {
    newRecords := append(records.Records, record)

    if newRecords == nil {
	    return errors.New(&quot;Could not append record&quot;)
    } else {
	    records.Records = newRecords
	    return nil
    }
}

Adding an interface seems like a good idea since you would like to use this as a service in multiple applications.

See my fork of your playground here.

huangapple
  • 本文由 发表于 2015年3月10日 04:10:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/28950892.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定