合并地图和结构体(Map and Struct)的Go语言实现方法。

huangapple go评论85阅读模式
英文:

Merging of map and struct golang

问题

我有两个项目,collectionsaccounts,分别由两个结构体表示,我想将它们合并为一个单一的响应。

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 则被定义为一个 map,如下所示 accounts := make(map[int64][]string),数据看起来像这样 map[1:[19565 21423] 7:[]]

我想要做的是将这两个结构体合并,类似于以下方式:

// 合并为单个结构体
type CollectionWithAccounts struct {
    Collections []*collection.Collection
    AccountIDs  []string
}

// 初始化结构体
collectionsWithAccounts := make([]CollectionWithAccounts, 0)

// 在循环中合并结构体
for _, collection := range collections {
    for _, account := range accounts {
        collectionsWithAccounts.Collections = append(collectionsWithAccounts, collection)
        collectionsWithAccounts.Accounts = append(collectionsWithAccounts, account)
    }
}

如何实现这个合并呢?

英文:

I have 2 items, collections and accounts represented by 2 structs that I would like to merge into a single response.

collections, accounts, err := h.Service.Many(ctx, params)

The collection struct is defined as follows:

type Collection struct {
	ID          int64      `json:"id"`
	Name        *string    `json:"name"`
	Description *string    `json:"description"`
	Total       *int64     `json:"total"`
}

And accounts is defined as a map as such accounts := make(map[int64][]string) and the data looks like this map[1:[19565 21423] 7:[]]

What I would like to do is merge these 2 something like the following:

// 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)
	}
}

How can I accomplish this merge?

答案1

得分: 1

即使没有任何循环,你也可以这样做:

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

// 合并为单个结构体
type CollectionWithAccounts struct {
    Collections []*Collection
    AccountIDs  []AccountID
}

func main() {
    // 获取数据
    // []*Collection, []AccountID, err
    collections, accounts, err := h.Service.Many(ctx, params)

    // 处理错误
    if err != nil {
        fmt.Println(err.Error())
        // 更多逻辑
    }

    collectionWithAccounts := CollectionWithAccounts{
        Collections: collections,
        AccountIDs:  accounts,
    }
}
英文:

You can do this even without any loops:

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,
	}	
}

huangapple
  • 本文由 发表于 2022年4月6日 02:43:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/71756914.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定