我有两个项目,collections
和accounts
,由两个结构体表示,我希望将它们合并为单个响应。
collections, accounts, err := h.Service.Many(ctx, params)
collection结构体的定义如下:
type Collection struct {
ID int64 `json:"id"`
Name *string `json:"name"`
Description *string `json:"description"`
Total *int64 `json:"total"`
}
账户被定义为一个映射,就像这样 accounts := make(map[int64][]string)
,数据看起来像这样 map[1:[19565 21423] 7:[]]
我想做的是将这两个合并,类似于以下内容:
// merge into single struct
type CollectionWithAccounts struct {
Collections []*collection.Collection
AccountIDs []string
}
// initialize struct
collectionsWithAccounts := make([]CollectionWithAccounts, 0)
// merge strucst in loop
for _, collection := range collections {
for _, account := range accounts {
collectionsWithAccounts.Collections = append(collectionsWithAccounts, collection)
collectionsWithAccounts.Accounts = append(collectionsWithAccounts, account)
}
}
我该如何完成这个合并?