goroutine中的Golang群与按值合并

goroutine中的Golang群与按值合并,go,Go,我是go新手,尝试用go中相同的值填充切片数据。 请参考以下示例 input struct { ID string `json:"id"` Name string `json:"name"` Image string `json:"image"` } output struct { ID string `json:"id"` Name string `json:"name"` Image []img `json:"image"` } img struct {

我是go新手,尝试用go中相同的值填充切片数据。 请参考以下示例

input struct {
  ID string `json:"id"`
  Name string `json:"name"`
  Image string `json:"image"`
}

output struct {
  ID    string `json:"id"`
  Name  string `json:"name"`
  Image []img `json:"image"`
}

img struct {
  Name string `json:"name"`
  Width  int  `json:"width"`
  Height int  `json:"height"`
}

input = [{
        "id": 10,
        "name": "product 10",
        "image": {"name": "https://i.imgur.com/eKSk6Fq.jpg"}
    }, {
        "id": 10,
            "name": "product 10",
            "image": {"name": "https://i.imgur.com/np1wmxw.jpg"}
    }, {
        "id": 11,
            "name": "product 11",
            "image": {"name": "https://i.imgur.com/jlFgGpe.jpg"}
    }, {
        "id": 11,
            "name": "product 11",
            "image": {"name": "https://i.imgur.com/B0D4iRk.jpg"}
    }, {
        "id": 11,
            "name": "product 11",
            "image": {"name": "https://i.imgur.com/4AiXzf8.jpg"}
    }]

// expected output
output = [{
    "id": 10,
    "name": "product 10",
    "image": [{
        "name": "https://i.imgur.com/eKSk6Fq.jpg",
        "width": 900,
        "height": 600
    }, {
        "name": "https://i.imgur.com/np1wmxw.jpg",
        "width": 600,
        "height": 600
    }]
}, {
    "id": 11,
    "name": "product 11",
    "image": [{
        "name": "https://i.imgur.com/jlFgGpe.jpg",
        "width": 639,
        "height": 700
    }, {
        "name": "https://i.imgur.com/B0D4iRk.jpg",
        "width": 1280,
        "height": 960
    }, {
        "name": "https://i.imgur.com/4AiXzf8.jpg",
        "width": 540,
        "height": 405
    }]
}]
我想根据相同的
ID
input
分组到一个新的切片, 因此,
output
的结果将是新结构的新切片,其分组的
image
具有相同的
ID

  • 7.0 0 0,7.0 0 0,7.0 0,8.0,0 0 0,0 0 0,0 0 0,7 7 7,0 0 0,7 7 7,0 0 0,0 0 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,w,w,w,w,w,w,w,w,w,w,w,w,w,0,0,0,w,w,0,0,0,w,w,w,w,w,w,w,w,w,w,w,w,w,w,w,w,0,w,w,w,w,0,w,w,w,w,w,w,0,0,0,0,0,0,w,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,resultusInggo?更新:从Peter Eichelsheim那里得到了答案
  • 另外,如果我必须使用http.get在
    输入中设置图像大小,并且想要使用goroutine,我将如何实现结果?由于我在这里的上一个代码没有实现正确的输出(始终获取最后的输入)
注意:我不知道为什么我在围棋游戏中得到空值,但在我的笔记本电脑中结果是:[{“id”:11,“name”:“product 11”,“image”:[{“name”:“,“width”:1280,“height”:960}]}]

在PHP中,我将执行以下操作以实现预期的
输出

foreach ($input as $key => $value) {
            if (!isset($output[$value["id"]])) {
                $output[$value["id"]] = [
                    "id" => $value["id"],
                    "name" => $value["name"],
                    "image" => [],
                ];
            }

            $get = getimagesize($value["image"]["name"]);
            if ($get) {
                $width  = isset($get[0]) ? $get[0] : 0;
                $height = isset($get[1]) ? $get[1] : 0;
            }

            $output[$value["id"]]["image"][$key] = [
                "name" => $value["image"]["name"],
                "width" => $width,
                "height" => $height,
            ];

            $output[$value["id"]]["image"] = array_values($output[$value["id"]]["image"]);
}

$output = array_values($output);
$json = json_encode($output, true);

echo $json;

谢谢这里有一个带有示例json输入的小示例,使用map[int]输出将俱乐部图像转换为相同的产品ID

package main

import (
    "encoding/json"
    "fmt"
    "log"
)

type input struct {
    ID    int `json:"id"`
    Name  string `json:"name"`
    Image img    `json:"image"`
}

type output struct {
    ID    int `json:"id"`
    Name  string `json:"name"`
    Image []img  `json:"image"`
}

type img struct {
    Name string `json:"name"`
}

func main() {

    var jsoninput = []byte(`
    [{
        "id": 10,
        "name": "product 10",
        "image": {"name": "image 10a"}
    }, {
        "id": 10,
            "name": "product 10",
            "image": {"name": "image 10b"}
    }, {
        "id": 11,
            "name": "product 11",
            "image": {"name": "image 11a"}
    }, {
        "id": 11,
            "name": "product 11",
            "image": {"name": "image 11b"}
    }, {
        "id": 11,
            "name": "product 11",
            "image": {"name": "image 11c"}
    }]`)

    var inputs []input

    err := json.Unmarshal(jsoninput, &inputs)
    if err != nil {
        log.Fatalln("could not Unmarshal:", err)
    }

    var outputlist = make(map[int]output)

    for _, inp := range inputs {
        outputlist[inp.ID] = output{inp.ID, inp.Name, append(outputlist[inp.ID].Image, inp.Image)}
    }

    var outputs []output

    for _, outp := range outputlist{
        outputs = append(outputs,outp)
    }

    jsonoutput, err := json.Marshal(outputs)

    fmt.Println(string(jsonoutput))
}

顶部提示:
var outputs[]output
,您知道片将有多大(
len(outputlist)
),因此您可以预先分配所需的内存,而不是依赖于golang重新分配
cap(output)*2
,每次您通过写入超过
2^N
元素时:
outputlist:=make([]output,0,len(outputlist)
谢谢你的回答!它适用于我以前的结果。我已经更新了我的问题,如果你能解决上面第二个问题,我将不胜感激。谢谢第一个循环似乎没有使用
idx
,所以应该是
对于u,输入:=范围输入
。另外:
输出:=map[int]输出{
写起来比较短“快写了"当然是偏好,但我要说的是第一点。editsWell缩短了4个字符,所以客观上缩短了。但是偏好在这种情况下是一个有效的论点:D我很久以前和很久以前就不得不回答一个类似的采访问题,特别是关于使用你添加到问题中的例程获取图像的问题:D用这样的方式去面试…淘气!
package main

import (
    "encoding/json"
    "fmt"
    "log"
)

type input struct {
    ID    int `json:"id"`
    Name  string `json:"name"`
    Image img    `json:"image"`
}

type output struct {
    ID    int `json:"id"`
    Name  string `json:"name"`
    Image []img  `json:"image"`
}

type img struct {
    Name string `json:"name"`
}

func main() {

    var jsoninput = []byte(`
    [{
        "id": 10,
        "name": "product 10",
        "image": {"name": "image 10a"}
    }, {
        "id": 10,
            "name": "product 10",
            "image": {"name": "image 10b"}
    }, {
        "id": 11,
            "name": "product 11",
            "image": {"name": "image 11a"}
    }, {
        "id": 11,
            "name": "product 11",
            "image": {"name": "image 11b"}
    }, {
        "id": 11,
            "name": "product 11",
            "image": {"name": "image 11c"}
    }]`)

    var inputs []input

    err := json.Unmarshal(jsoninput, &inputs)
    if err != nil {
        log.Fatalln("could not Unmarshal:", err)
    }

    var outputlist = make(map[int]output)

    for _, inp := range inputs {
        outputlist[inp.ID] = output{inp.ID, inp.Name, append(outputlist[inp.ID].Image, inp.Image)}
    }

    var outputs []output

    for _, outp := range outputlist{
        outputs = append(outputs,outp)
    }

    jsonoutput, err := json.Marshal(outputs)

    fmt.Println(string(jsonoutput))
}