英文:
populate struct fields in Go by looping over data from another function
问题
我在一个网络应用程序(Fiber框架)中有几个处理程序,其中一个处理程序从外部API检索数据,另一个处理程序从这些数据中获取子集并执行一些业务逻辑(例如发送报告等)。
这两个处理程序位于同一个包中。在handler2.go中,我可以解引用来自handler1.go的数据,并且我想使用该数据中的特定值来填充handler2.go中的结构字段。来自handler1.go的解引用数据本身是一个可以循环遍历的结构数组。
在handler2.go中,我有一个结构体:
type Report struct {
contact string
date string
resource string
}
// 从handler1.go函数获取数据并用它填充Report结构体
// 每个"report"都是一个结构体,所以需要创建一个结构体列表
func getReportData() {
reportData := GetReport() // 调用handler1.go中的函数
for _, report := range *reportData {
fmt.Println(report.Date)
}
因此,我想要的不仅仅是打印数据(打印语句只是为了显示我可以访问所需的数据),我还想使用循环和report.<KEY>
语法从数据中获取特定项,并将其填充到Report结构体中。
如何使用Report结构体创建一个填充了通过此for
循环获取的数据的结构体列表?
对于MVP,我可以简单地将这个结构体列表(以JSON格式)进行格式化,并在Web应用程序中显示一个端点。我只是不知道如何正确构造这个数据。
英文:
I have a couple handlers in a web app (Fiber framework) where one handler retrieves data from an external API and the other handler takes a subset of this data and performs some business logic (ie sends a report, etc).
Both handlers are in the same package. In handler2.go I am able to dereference the data from handler1.go and I want to use specific values from that data to populate the struct fields in handler2.go. The dereferenced data from handler1.go is itself an array of structs that I can loop over.
In handler2.go , I have a struct:
type Report struct {
contact string
date string
resource string
}
// get data from handler1.go function and use it to populate the Report struct
// each "report" is a struct, so need to create a list of structs
func getReportData() {
reportData := GetReport() // call function in handler1.go
for _, report := range *reportData {
fmt.Println(report.Date)
}
So instead of simply printing the data (the print statement is just to show that I have access to the data I need) I want to populate the Report struct with specifc items from the data that I can can access using the loop and the report.<KEY>
syntax.
How can I create a list of structs (using the Report struct) populated with the data I can get via this for
loop?
For an MVP , I can simply format this list of structs (in json) and display an endpoint in the web app. I am just struggling with how to construct this data properly.
答案1
得分: 1
为了回答这个直接的问题,如果我们假设GetReport()
返回的值有Date
、Contact
和Resource
字段,那么你可以这样编写代码:
type Report struct {
contact string
date string
resource string
}
// 返回一个报告列表(切片)
func getReportData() (reports []Report) {
reportData := GetReport()
for _, report := range reportData {
myReport := Report{
contact: report.Contact,
date: report.Date,
resource: report.Resource,
}
reports = append(reports, myReport)
}
return
}
这段代码定义了一个名为Report
的结构体,它有contact
、date
和resource
三个字段。getReportData()
函数会调用GetReport()
函数获取报告数据,并将其转换为Report
结构体的切片。然后,将每个报告的字段值赋给对应的结构体字段,并将该结构体添加到报告列表中。最后,返回报告列表。
英文:
To answer the direct question, if we assume that the values returned by GetReport()
have Date
, Contact
, and Resource
fields, then you could write:
type Report struct {
contact string
date string
resource string
}
// Return a list (well, slice) of Reports
func getReportData() (reports []Report) {
reportData := GetReport()
for _, report := range reportData {
myReport := Report{
contact: report.Contact,
date: report.Date,
resource: report.Resource,
}
reports = append(reports, myReport)
}
return
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论