map 和 struct golang 的合并
Merging of map and struct golang
我有 2 个项目,collections
和 accounts
由 2 个结构表示,我想将它们合并到一个响应中。
collections, accounts, err := h.Service.Many(ctx, params)
集合结构定义如下:
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)
}
}
我怎样才能完成这个合并?
即使没有任何循环,您也可以这样做:
package main
import "fmt"
type Collection struct {
ID int64 `json:"id"`
Name *string `json:"name"`
Description *string `json:"description"`
Total *int64 `json:"total"`
}
type AccountID map[int64][]string
// merge into single struct
type CollectionWithAccounts struct {
Collections []*Collection
AccountIDs []AccountID
}
func main() {
// get the data
// []*Collections, []AccountID, err
collections, accounts, err := h.Service.Many(ctx, params)
// handle error
if err != nil {
fmt.Println(err.Error())
// more logic
}
collectionsWithAccounts := CollectionWithAccounts{
Collections: collections,
AccountIDs: accounts,
}
}
我有 2 个项目,collections
和 accounts
由 2 个结构表示,我想将它们合并到一个响应中。
collections, accounts, err := h.Service.Many(ctx, params)
集合结构定义如下:
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)
}
}
我怎样才能完成这个合并?
即使没有任何循环,您也可以这样做:
package main
import "fmt"
type Collection struct {
ID int64 `json:"id"`
Name *string `json:"name"`
Description *string `json:"description"`
Total *int64 `json:"total"`
}
type AccountID map[int64][]string
// merge into single struct
type CollectionWithAccounts struct {
Collections []*Collection
AccountIDs []AccountID
}
func main() {
// get the data
// []*Collections, []AccountID, err
collections, accounts, err := h.Service.Many(ctx, params)
// handle error
if err != nil {
fmt.Println(err.Error())
// more logic
}
collectionsWithAccounts := CollectionWithAccounts{
Collections: collections,
AccountIDs: accounts,
}
}