Linked Questions

Popular Questions

Golang group and merge by value in goroutine

Asked by At

I'm new in go and tried to populate slice data by same values in GO. Refer to the following example

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

I would like to group input to a new slice based on the same ID, so the result output would be new slice of new struct with grouped image with same ID.

  • H̶o̶w̶ ̶w̶o̶u̶l̶d̶ ̶I̶ ̶a̶c̶h̶i̶e̶v̶e̶d̶ ̶t̶h̶e̶ ̶̶o̶u̶t̶p̶u̶t̶̶ ̶r̶e̶s̶u̶l̶t̶ ̶u̶s̶i̶n̶g̶ ̶G̶O̶? update: got the answer from Peter Eichelsheim
  • Also, if I had to ge image size in the input with http.get and want to use goroutine, how would I achieve the result? since my last code here playground not achieving the correct output (always get the last input)

note: I don't know why I get null in go playground, but in my laptop the result is: [{"id":11,"name":"product 11","image":[{"name":"https://i.imgur.com/B0D4iRk.jpg","width":1280,"height":960}]}]

In PHP, I would do something below to achieve the intended output.

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;

Thanks

Related Questions