英文:
Returning multiple maps in one variable
问题
我正在处理一个XML解析器,并且我遇到了以下问题:
我有一个函数,用于获取一些标签的值,例如电影标题和发布日期:
func whatever() map[string]interface{} {
}
我希望它返回以下形式的内容:
[map[title:Movie01] map[title:Movie02]]
但是不能改变返回类型。
目前我只有:
map[title:Movie01]
显然,我不能在一个单独的map中有重复的"title"键。
你能帮我解决这个问题吗?这个问题困扰了我几个小时了。
英文:
I am working on a XML parser and I have the following problem :
I have this function which gather some tags value, for example movie title and release date :
func whatever() map[string]interface{} {
}
And I would like it to return something of this form :
[map[title:Movie01] map[title:Movie02]]
Without changing the return type.
All I have for now is :
map[title:Movie01]
And obviously I cannot have duplicate "title" key in one single map.
Can you help me on this ? It's been bothering me for a couple of hours now.
答案1
得分: 1
根据记录,正如我在评论中提到的,你可以尝试返回一个切片的映射,例如:
func whatever() []map[string]interface{} {
}
不过,根据你的数据,你可能会发现为你的领域定义一个合适的结构体并返回它会有更好的解决方案。
英文:
For the record then, as I mentioned in the comments you could try returning a slice of maps such as
func whatever() []map[string]interface{} {
}
Though depending on your data you might find a better solution in defining an appropriate struct for your domain and returning that.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论