在 Go 上将两个结构分组为第三个结构的最佳方法

我有以下结构:


type A1 struct {

   IdA1 string

   NameA1 string

   key string

}


type B1 struct {

   IdB1 string

   NameB1 string

   Size string

   key string

}


type C1 struct {

   IdA1 string

   Name string

   B1Set B1

}

我需要创建一个 C1 类型的 []struct,它保存 B1 的 B1Set,B1 超过 2K 个条目,而 A1 只有 10 个条目,一个非常缓慢且低效的实现是循环 A1,并询问是否B1-key 等于 A1-key 并将结果存储在映射中,但是..有没有更好的方法来实现这一点?


提前致谢,


添加更多信息:


它们是两个不同的 JSON 文件:


Json1:

[

  {

    "id": "device1",

    "name": "dev1",

    "pool": "pool1"

  },

  {

    "id": "device2",

    "name": "dev2",

    "pool": "pool2"

  }

  ...

]

还有另一个 Json:


[

  {

    "name": "port1",

    "size": 10,

    "pool": "pool1",

    "status": "active"

  },

  {

    "name": "port2",

    "size": 60,

    "pool": "pool1",

    "status": "active"

  },

  {

    "name": "port3",

    "size": 20,

    "pool": "pool2",

    "status": "down"

  },

  {

    "name": "port8",

    "size": 100,

    "pool": "pool2",

    "status": "active"

  },

  {

    "name": "port10",

    "size": 8000,

    "pool": "pool1",

    "status": "active"

  },

  ...

]


繁星点点滴滴
浏览 126回答 1
1回答

MMMHUHU

这是您想要做的吗?&nbsp;https://play.golang.org/p/AZNzQAwRhN0其作用是构建一个按池对所有端口进行分组的映射。然后它循环遍历我标记的内容clusters,并通过按值抓取匹配切片来将 的切片分配Port给匹配的切片。ClusterPoolpackage mainimport (&nbsp; &nbsp; "encoding/json"&nbsp; &nbsp; "fmt")type Cluster struct {&nbsp; &nbsp; ID&nbsp; &nbsp; string `json:"id"`&nbsp; &nbsp; Name&nbsp; string `json:"name"`&nbsp; &nbsp; Pool&nbsp; string `json:"pool"`&nbsp; &nbsp; Ports []Port `json:"ports"`}type Port struct {&nbsp; &nbsp; Name&nbsp; &nbsp;string `json:"name"`&nbsp; &nbsp; Size&nbsp; &nbsp;int&nbsp; &nbsp; `json:"size"`&nbsp; &nbsp; Pool&nbsp; &nbsp;string `json:"pool"`&nbsp; &nbsp; Status string `json:"status"`}func main() {&nbsp; &nbsp; var resources []Port&nbsp; &nbsp; err := json.Unmarshal([]byte(resourceJSON), &resources)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; resourcesByPool := make(map[string][]Port)&nbsp; &nbsp; for _, resource := range resources {&nbsp; &nbsp; &nbsp; &nbsp; if _, ok := resourcesByPool[resource.Pool]; !ok {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resourcesByPool[resource.Pool] = []Port{}&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; resourcesByPool[resource.Pool] = append(resourcesByPool[resource.Pool], resource)&nbsp; &nbsp; }&nbsp; &nbsp; var clusters []Cluster&nbsp; &nbsp; err = json.Unmarshal([]byte(clusterJSON), &clusters)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; for i := 0; i < len(clusters); i++ {&nbsp; &nbsp; &nbsp; &nbsp; clusters[i].Ports = resourcesByPool[clusters[i].Pool]&nbsp; &nbsp; }&nbsp; &nbsp; out, err := json.MarshalIndent(clusters, "", "&nbsp; &nbsp; ")&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Println(string(out))}var (&nbsp; &nbsp; clusterJSON = `[&nbsp; {&nbsp; &nbsp; "id": "device1",&nbsp; &nbsp; "name": "dev1",&nbsp; &nbsp; "pool": "pool1"&nbsp; },&nbsp; {&nbsp; &nbsp; "id": "device2",&nbsp; &nbsp; "name": "dev2",&nbsp; &nbsp; "pool": "pool2"&nbsp; }]`&nbsp; &nbsp; resourceJSON = `[&nbsp; {&nbsp; &nbsp; "name": "port1",&nbsp; &nbsp; "size": 10,&nbsp; &nbsp; "pool": "pool1",&nbsp; &nbsp; "status": "active"&nbsp; },&nbsp; {&nbsp; &nbsp; "name": "port2",&nbsp; &nbsp; "size": 60,&nbsp; &nbsp; "pool": "pool1",&nbsp; &nbsp; "status": "active"&nbsp; },&nbsp; {&nbsp; &nbsp; "name": "port3",&nbsp; &nbsp; "size": 20,&nbsp; &nbsp; "pool": "pool2",&nbsp; &nbsp; "status": "down"&nbsp; },&nbsp; {&nbsp; &nbsp; "name": "port8",&nbsp; &nbsp; "size": 100,&nbsp; &nbsp; "pool": "pool2",&nbsp; &nbsp; "status": "active"&nbsp; },&nbsp; {&nbsp; &nbsp; "name": "port10",&nbsp; &nbsp; "size": 8000,&nbsp; &nbsp; "pool": "pool1",&nbsp; &nbsp; "status": "active"&nbsp; }]`)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go