第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

在 Go 上將兩個(gè)結(jié)構(gòu)分組為第三個(gè)結(jié)構(gòu)的最佳方法

在 Go 上將兩個(gè)結(jié)構(gòu)分組為第三個(gè)結(jié)構(gòu)的最佳方法

Go
繁星點(diǎn)點(diǎn)滴滴 2023-07-31 15:37:17
我有以下結(jié)構(gòu):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}我需要?jiǎng)?chuàng)建一個(gè) C1 類(lèi)型的 []struct,它保存 B1 的 B1Set,B1 超過(guò) 2K 個(gè)條目,而 A1 只有 10 個(gè)條目,一個(gè)非常緩慢且低效的實(shí)現(xiàn)是循環(huán) A1,并詢(xún)問(wèn)是否B1-key 等于 A1-key 并將結(jié)果存儲(chǔ)在映射中,但是..有沒(méi)有更好的方法來(lái)實(shí)現(xiàn)這一點(diǎn)?提前致謝,添加更多信息:它們是兩個(gè)不同的 JSON 文件:Json1:[  {    "id": "device1",    "name": "dev1",    "pool": "pool1"  },  {    "id": "device2",    "name": "dev2",    "pool": "pool2"  }  ...]還有另一個(gè) 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"  },  ...]
查看完整描述

1 回答

?
MMMHUHU

TA貢獻(xiàn)1834條經(jīng)驗(yàn) 獲得超8個(gè)贊

這是您想要做的嗎? https://play.golang.org/p/AZNzQAwRhN0

其作用是構(gòu)建一個(gè)按池對(duì)所有端口進(jìn)行分組的映射。然后它循環(huán)遍歷我標(biāo)記的內(nèi)容clusters,并通過(guò)按值抓取匹配切片來(lái)將 的切片分配Port給匹配的切片。ClusterPool

package main


import (

    "encoding/json"

    "fmt"

)


type Cluster struct {

    ID    string `json:"id"`

    Name  string `json:"name"`

    Pool  string `json:"pool"`

    Ports []Port `json:"ports"`

}


type Port struct {

    Name   string `json:"name"`

    Size   int    `json:"size"`

    Pool   string `json:"pool"`

    Status string `json:"status"`

}


func main() {

    var resources []Port

    err := json.Unmarshal([]byte(resourceJSON), &resources)

    if err != nil {

        panic(err)

    }

    resourcesByPool := make(map[string][]Port)

    for _, resource := range resources {

        if _, ok := resourcesByPool[resource.Pool]; !ok {

            resourcesByPool[resource.Pool] = []Port{}

        }

        resourcesByPool[resource.Pool] = append(resourcesByPool[resource.Pool], resource)

    }


    var clusters []Cluster

    err = json.Unmarshal([]byte(clusterJSON), &clusters)

    if err != nil {

        panic(err)

    }

    for i := 0; i < len(clusters); i++ {

        clusters[i].Ports = resourcesByPool[clusters[i].Pool]

    }


    out, err := json.MarshalIndent(clusters, "", "    ")

    if err != nil {

        panic(err)

    }


    fmt.Println(string(out))


}


var (

    clusterJSON = `[

  {

    "id": "device1",

    "name": "dev1",

    "pool": "pool1"

  },

  {

    "id": "device2",

    "name": "dev2",

    "pool": "pool2"

  }

]`


    resourceJSON = `[

  {

    "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"

  }]`

)


查看完整回答
反對(duì) 回復(fù) 2023-07-31
  • 1 回答
  • 0 關(guān)注
  • 172 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)