英文:
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:"records"`
Records []Record `xml:"record"`
}
所以,你可以简单地在该切片上调用append
函数,并传入一个新的记录结构体:
records.Records = append(record.Records, Record{xml.Name{"", "record"}, 4, "url", "2015-03-09 00:00:00"})
你还可以扩展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("Could not append record")
} else {
records.Records = newRecords
return nil
}
}
添加一个接口似乎是个好主意,因为你想在多个应用程序中使用它作为服务。
英文:
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:"records"`
Records []Record `xml:"record"`
}
So, you can simply call append on that slice and pass in a new record struct:
records.Records = append(record.Records, Record{xml.Name{"", "record"}, 4, "url", "2015-03-09 00:00:00"})
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("Could not append record")
} 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论