将结构体中的字符数组转换为字符串
来源:stackoverflow
时间:2024-02-09 20:06:24 460浏览 收藏
目前golang学习网上已经有很多关于Golang的文章了,自己在初次阅读这些文章中,也见识到了很多学习思路;那么本文《将结构体中的字符数组转换为字符串》,也希望能帮助到大家,如果阅读完后真的对你学习Golang有帮助,欢迎动动手指,评论留言并分享~
我有一个 struct
如下:
type tourdata struct { artistid int //artist id relationid string //key for relations city string country string tourdates []string } type myrelation struct { id int `json:"id"` dateslocations map[string][]string `json:"dateslocations"` }
其中包含来自 csv 文件的数据:
1,nagoya-japan,nagoya,japan, 1,penrose-new_zealand,penrose,new_zealand, 1,dunedin-new_zealand,dunedin,new_zealand, 2,playa_del_carmen-mexico,playa del carmen,mexico, 2,papeete-french_polynesia,papeete,french_polynesia,
myrelations 由 api 填充,其中包含:
"index": [ { "id": 1, "dateslocations": { "dunedin-new_zealand": [ "10-02-2020" ], "nagoya-japan": [ "30-01-2019" ], "penrose-new_zealand": [ "07-02-2020" ] } }, { "id": 2, "dateslocations": { "papeete-french_polynesia": [ "16-11-2019" ], "playa_del_carmen-mexico": [ "05-12-2019", "06-12-2019", "07-12-2019", "08-12-2019", "09-12-2019" ] } }
日期来自另一个结构。我用来填充此结构的代码如下:
var onerecord tourdata var allrecords []tourdata for _, each := range csvdata { onerecord.artistid, _ = strconv.atoi(each[0]) onerecord.relationid = each[1] onerecord.city = each[2] onerecord.country = each[3] onerecord.tourdates = relations.index[onerecord.artistid-1].dateslocations[each[1]] allrecords = append(allrecords, onerecord) } jsondata, err := json.marshal(allrecords) // convert to json json.unmarshal(jsondata, &tourthings)
我需要将所有 1 组合在一起,然后是 2 等。我想创建另一个结构,并从这个结构中填充,但运气不佳 - 有什么想法吗?
为了澄清,我想说 tourdata.city 等于:
[nagoya,penrose,dunedin] [playa del carmen, papeete]
目前,如果我要打印 tourdata[0].city,我会打印 nagoya。
我尝试创建另一个结构体,并使用以下字段从 tourdata 结构体中填充:
type tourdataarrays struct { artistid int city []string country []string tourdates [][]string }
然后使用以下代码填充结构:
var tourRecord TourDataArrays var tourRecords []TourDataArrays for i := 0; i < len(Relations.Index); i++ { for j := 0; j < len(allRecords); j++ { if allRecords[i].ArtistID == i+1 { tourRecord.City = append(tourRecord.City, allRecords[j].City) } } tourRecords = append(tourRecords, tourRecord) }
但是,这会将所有城市添加到一个数组中,即
[名古屋、彭罗斯、达尼丁、卡门海滩、帕皮提]。
正确答案
如果我正确理解您的要求,您还需要将 city 声明为字符串数组。 (还有相应的国家/地区)。
查看此解决方案:https://go.dev/play/p/osgkbfWV3c5
请注意,我尚未从 Json 中的一个字段中删除国家/地区的重复数据并派生出城市和国家/地区。
终于介绍完啦!小伙伴们,这篇关于《将结构体中的字符数组转换为字符串》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习